Skip to content

Commit

Permalink
Fix spring-projects#173 : Add support for filtering project symbols
Browse files Browse the repository at this point in the history
Add new provider which will filter symbols for the current selected
resource's project in GoTo Symbol dialog.
  • Loading branch information
gayanper committed Jan 5, 2019
1 parent a77433d commit b47b63e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.springframework.tooling.ls.eclipse.gotosymbol.dialogs;

import java.util.List;
import java.util.function.Predicate;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageServiceAccessor;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageServer;

@SuppressWarnings("restriction")
public class InProjectSymbolsProvider extends InWorkspaceSymbolsProvider {

private final Predicate<? super Either<SymbolInformation, DocumentSymbol>> FILTER_PREDICATE = e -> {
if(e.isLeft()) {
SymbolInformation symbolInformation = e.getLeft();
Location location = symbolInformation.getLocation();
IResource targetResource = LSPEclipseUtils.findResourceFor(location.getUri());
if (targetResource != null && targetResource.getFullPath() != null) {
return targetResource.getFullPath().toString().startsWith("/" + getProject().getName() + "/");
}
}
return false;
};

public InProjectSymbolsProvider(List<LanguageServer> languageServers, IProject project) {
super(languageServers, project);
}

@Override
public String getName() {
return "Symbols in Project";
}

public static InWorkspaceSymbolsProvider createFor(ExecutionEvent event) {
final IProject project = InWorkspaceSymbolsProvider.projectFor(event);
final List<LanguageServer> languageServers = LanguageServiceAccessor.getLanguageServers(project,
capabilities -> Boolean.TRUE.equals(capabilities.getWorkspaceSymbolProvider()), true);
if (!languageServers.isEmpty()) {
return new InProjectSymbolsProvider(languageServers, project);
}
return null;
}

@Override
protected Predicate<? super Either<SymbolInformation, DocumentSymbol>> symbolFilter() {
return FILTER_PREDICATE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
package org.springframework.tooling.ls.eclipse.gotosymbol.dialogs;

import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.core.commands.ExecutionEvent;
Expand All @@ -31,6 +31,7 @@
import org.springframework.tooling.ls.eclipse.gotosymbol.GotoSymbolPlugin;
import org.springsource.ide.eclipse.commons.livexp.util.ExceptionUtil;

import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;

import reactor.core.publisher.Flux;
Expand All @@ -43,9 +44,11 @@ public class InWorkspaceSymbolsProvider implements SymbolsProvider {
private static final int MAX_RESULTS = 200;

private List<LanguageServer> languageServers;
private IProject project;

public InWorkspaceSymbolsProvider(List<LanguageServer> languageServers) {
public InWorkspaceSymbolsProvider(List<LanguageServer> languageServers, IProject project) {
this.languageServers = languageServers;
this.project = project;
}

@Override
Expand Down Expand Up @@ -75,14 +78,22 @@ public List<Either<SymbolInformation, DocumentSymbol>> fetchFor(String query) th
.map(symbol -> Either.forLeft(symbol))
);
//Consider letting the Flux go out from here instead of blocking and collecting elements.
return symbols.take(MAX_RESULTS).collect(Collectors.toList()).block();
return symbols.filter(symbolFilter()).take(MAX_RESULTS).collect(Collectors.toList()).block();
}

private static void log(Throwable e) {
GotoSymbolPlugin.getInstance().getLog().log(ExceptionUtil.status(e));
}

public static InWorkspaceSymbolsProvider createFor(ExecutionEvent event) {
final IProject project = projectFor(event);
if (project!=null) {
return createFor(project);
}
return null;
}

public static IProject projectFor(ExecutionEvent event) {
IEditorPart part = HandlerUtil.getActiveEditor(event);
IResource resource = null;
if (part != null && part.getEditorInput() != null) {
Expand All @@ -96,16 +107,17 @@ public static InWorkspaceSymbolsProvider createFor(ExecutionEvent event) {
resource = adaptable.getAdapter(IResource.class);
}
if (resource!=null) {
return createFor(resource.getProject());
return resource.getProject();
}
return null;

}

public static InWorkspaceSymbolsProvider createFor(IProject project) {
List<LanguageServer> languageServers = LanguageServiceAccessor.getLanguageServers(project,
capabilities -> Boolean.TRUE.equals(capabilities.getWorkspaceSymbolProvider()), true);
if (!languageServers.isEmpty()) {
return new InWorkspaceSymbolsProvider(languageServers);
return new InWorkspaceSymbolsProvider(languageServers, project);
}
return null;
}
Expand All @@ -114,4 +126,13 @@ public static InWorkspaceSymbolsProvider createFor(IProject project) {
public boolean fromFile(SymbolInformation symbol) {
return false;
}

protected Predicate<? super Either<SymbolInformation, DocumentSymbol>> symbolFilter() {
return Predicates.alwaysTrue();
}

protected IProject getProject() {
return project;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.tooling.ls.eclipse.gotosymbol.dialogs.GotoSymbolDialog;
import org.springframework.tooling.ls.eclipse.gotosymbol.dialogs.GotoSymbolDialogModel;
import org.springframework.tooling.ls.eclipse.gotosymbol.dialogs.InFileSymbolsProvider;
import org.springframework.tooling.ls.eclipse.gotosymbol.dialogs.InProjectSymbolsProvider;
import org.springframework.tooling.ls.eclipse.gotosymbol.dialogs.InWorkspaceSymbolsProvider;

@SuppressWarnings("restriction")
Expand Down Expand Up @@ -64,7 +65,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
final Shell shell = HandlerUtil.getActiveShell(event);
final ITextEditor textEditor = (ITextEditor) part;

GotoSymbolDialogModel model = new GotoSymbolDialogModel(getKeybindings(event), InWorkspaceSymbolsProvider.createFor(event), InFileSymbolsProvider.createFor(textEditor))
GotoSymbolDialogModel model = new GotoSymbolDialogModel(getKeybindings(event), InWorkspaceSymbolsProvider.createFor(event), InProjectSymbolsProvider.createFor(event), InFileSymbolsProvider.createFor(textEditor))
.setOkHandler(symbolInformation -> {
if (symbolInformation!=null) {
Location location = symbolInformation.getLocation();
Expand Down

0 comments on commit b47b63e

Please sign in to comment.