Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-6848: Navigation to file improvement: Provide filtering binary files and sorting results by extensions #7983

Merged
merged 1 commit into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.che.ide.filetypes;

import static org.eclipse.che.ide.util.NameUtils.getFileExtension;

import com.google.common.base.Strings;
import com.google.gwt.regexp.shared.RegExp;
import com.google.inject.Inject;
Expand Down Expand Up @@ -84,13 +86,4 @@ public FileType getFileTypeByNamePattern(String name) {

return unknownFileType;
}

private String getFileExtension(String name) {
final int lastDotPosition = name.lastIndexOf('.');
// name has no extension
if (lastDotPosition < 0) {
return "";
}
return name.substring(lastDotPosition + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
package org.eclipse.che.ide.navigation;

import static java.util.Collections.emptyList;
import static java.util.Collections.sort;
import static org.eclipse.che.ide.api.jsonrpc.Constants.WS_AGENT_JSON_RPC_ENDPOINT_ID;
import static org.eclipse.che.ide.util.NameUtils.getFileExtension;

import com.google.common.base.Optional;
import com.google.gwt.http.client.URL;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.List;
import org.eclipse.che.api.core.jsonrpc.commons.RequestTransmitter;
import org.eclipse.che.api.project.shared.dto.ProjectSearchRequestDto;
import org.eclipse.che.api.project.shared.dto.ProjectSearchResponseDto;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.api.project.shared.dto.SearchResultDto;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.ide.resource.Path;
import org.eclipse.che.ide.util.loging.Log;
Expand Down Expand Up @@ -74,12 +74,9 @@ public void onFileSelected(Path path) {
.getWorkspaceRoot()
.getFile(path)
.then(
new Operation<Optional<File>>() {
@Override
public void apply(Optional<File> optFile) throws OperationException {
if (optFile.isPresent()) {
editorAgent.openEditor(optFile.get());
}
optFile -> {
if (optFile.isPresent()) {
editorAgent.openEditor(optFile.get());
}
});
}
Expand All @@ -103,8 +100,20 @@ public void onFileNameChanged(String fileName) {
.methodName("project/search")
.paramsAsDto(requestParams)
.sendAndReceiveResultAsDto(ProjectSearchResponseDto.class, 20_000)
.onSuccess(response -> view.showItems(response.getItemReferences()))
.onSuccess(response -> prepareResults(response))
.onFailure(error -> Log.error(getClass(), error.getMessage()))
.onTimeout(() -> Log.error(getClass(), "Project search request failed due timeout"));
}

private void prepareResults(ProjectSearchResponseDto response) {
List<SearchResultDto> results = response.getItemReferences();
sort(
results,
(o1, o2) -> {
String ext1 = getFileExtension(o1.getItemReference().getName());
String ext2 = getFileExtension(o2.getItemReference().getName());
return ext1.compareToIgnoreCase(ext2);
});
view.showItems(results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ public static boolean checkFolderName(String name) {
public static boolean checkProjectName(String name) {
return PROJECT_NAME.test(name);
}

public static String getFileExtension(String name) {
final int lastDotPosition = name.lastIndexOf('.');
// name has no extension
if (lastDotPosition < 0) {
return "";
}
return name.substring(lastDotPosition + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public void invalidFolderNamesShouldNotGetValidated(String invalidFolderName) {
NameUtils.checkFileName(invalidFolderName));
}

@Test
public void getFileExtension() {
assertEquals("txt", NameUtils.getFileExtension("123.txt"));
assertEquals("", NameUtils.getFileExtension("123"));
assertEquals("zz", NameUtils.getFileExtension("123.txt.zz"));
}

@DataProvider(name = "ValidFilenames")
public Object[][] getValidFilenames() {
return toDataProviderData(VALID_FILENAMES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.equinox.common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.apache.tika.mime.MediaType;
import org.eclipse.che.api.project.server.type.ProjectTypeDef;
import org.eclipse.che.api.search.server.excludes.MediaTypesExcludeMatcher;

/**
* Project type for plain java projects.
Expand All @@ -29,7 +31,9 @@
@Singleton
public class PlainJavaProjectType extends ProjectTypeDef {
@Inject
public PlainJavaProjectType(PlainJavaValueProviderFactory valueProviderFactory) {
public PlainJavaProjectType(
PlainJavaValueProviderFactory valueProviderFactory,
MediaTypesExcludeMatcher mediaTypesExcludeMatcher) {
super(JAVAC, JAVAC_PROJECT_NAME, true, false, true);

setValueProviderFactory(SOURCE_FOLDER, valueProviderFactory);
Expand All @@ -38,5 +42,8 @@ public PlainJavaProjectType(PlainJavaValueProviderFactory valueProviderFactory)
addVariableDefinition(LIBRARY_FOLDER, "java library folder", false);

addParent(JAVA_ID);

mediaTypesExcludeMatcher.addExcludedMediaType(new MediaType("application", "java-vm"));
mediaTypesExcludeMatcher.addExcludedMediaType(new MediaType("application", "java-archive"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.name.Names;
import java.nio.file.PathMatcher;
import java.util.Collections;
import org.eclipse.che.api.languageserver.launcher.LanguageServerLauncher;
import org.eclipse.che.api.languageserver.shared.model.LanguageDescription;
Expand All @@ -32,6 +34,7 @@
import org.eclipse.che.plugin.maven.server.core.MavenTerminalImpl;
import org.eclipse.che.plugin.maven.server.core.project.PomChangeListener;
import org.eclipse.che.plugin.maven.server.projecttype.MavenProjectType;
import org.eclipse.che.plugin.maven.server.projecttype.MavenTargetExcludeMatcher;
import org.eclipse.che.plugin.maven.server.projecttype.MavenValueProviderFactory;
import org.eclipse.che.plugin.maven.server.projecttype.handler.ArchetypeGenerationStrategy;
import org.eclipse.che.plugin.maven.server.projecttype.handler.GeneratorStrategy;
Expand Down Expand Up @@ -65,6 +68,15 @@ protected void configure() {
generatorStrategyMultibinder.addBinding().to(SimpleGeneratorStrategy.class);
generatorStrategyMultibinder.addBinding().to(ArchetypeGenerationStrategy.class);

Multibinder<PathMatcher> excludeMatcher =
newSetBinder(binder(), PathMatcher.class, Names.named("vfs.index_filter_matcher"));
excludeMatcher.addBinding().to(MavenTargetExcludeMatcher.class);

Multibinder<PathMatcher> fileWatcherExcludes =
newSetBinder(
binder(), PathMatcher.class, Names.named("che.user.workspaces.storage.excludes"));
fileWatcherExcludes.addBinding().to(MavenTargetExcludeMatcher.class);

bind(MavenTerminal.class).to(MavenTerminalImpl.class).in(Singleton.class);
bind(MavenProgressNotifier.class).to(MavenServerNotifier.class).in(Singleton.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2012-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.plugin.maven.server.projecttype;

import java.nio.file.Path;
import java.nio.file.PathMatcher;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.che.api.fs.server.PathTransformer;
import org.eclipse.che.api.project.server.ProjectManager;
import org.eclipse.che.api.project.server.impl.RegisteredProject;
import org.eclipse.che.plugin.maven.shared.MavenAttributes;

@Singleton
public class MavenTargetExcludeMatcher implements PathMatcher {

private final ProjectManager projectManager;
private final PathTransformer pathTransformer;

@Inject
public MavenTargetExcludeMatcher(ProjectManager projectManager, PathTransformer pathTransformer) {
this.projectManager = projectManager;
this.pathTransformer = pathTransformer;
}

@Override
public boolean matches(Path fsPath) {
String wsPath = pathTransformer.transform(fsPath);
RegisteredProject project = projectManager.getClosestOrNull(wsPath);
if (project == null) {
return false;
}

if (MavenAttributes.MAVEN_ID.equals(project.getType())) {
String mavenTarget = project.getPath() + "/target";
return wsPath.startsWith(mavenTarget);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public MediaTypesExcludeMatcher(FsManager fsManager, PathTransformer pathTransfo
this.fsManager = fsManager;
}

public void addExcludedMediaType(MediaType mediaType) {
excludedMediaTypes.add(mediaType);
}

public void addExcludedTypes(String type) {
excludedTypes.add(type);
}

@Override
public boolean matches(Path fsPath) {
if (!fsPath.toFile().exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
Expand Down