Fix #173 : Add support for filtering project symbols
Add new provider which will filter symbols for the current selected resource's project in GoTo Symbol dialog.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Pivotal, 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:
|
||||
* Gayan Perera <gayanper@gmail.com> - In project symbol provider implementation
|
||||
*******************************************************************************/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
@@ -31,6 +31,7 @@ import org.eclipse.ui.handlers.HandlerUtil;
|
||||
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;
|
||||
@@ -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
|
||||
@@ -75,7 +78,7 @@ public class InWorkspaceSymbolsProvider implements SymbolsProvider {
|
||||
.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) {
|
||||
@@ -83,6 +86,14 @@ public class InWorkspaceSymbolsProvider implements SymbolsProvider {
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -96,16 +107,17 @@ public class InWorkspaceSymbolsProvider implements SymbolsProvider {
|
||||
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;
|
||||
}
|
||||
@@ -114,4 +126,13 @@ public class InWorkspaceSymbolsProvider implements SymbolsProvider {
|
||||
public boolean fromFile(SymbolInformation symbol) {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected Predicate<? super Either<SymbolInformation, DocumentSymbol>> symbolFilter() {
|
||||
return Predicates.alwaysTrue();
|
||||
}
|
||||
|
||||
protected IProject getProject() {
|
||||
return project;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.eclipse.ui.texteditor.ITextEditor;
|
||||
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")
|
||||
@@ -64,7 +65,7 @@ public class GotoSymbolHandler extends AbstractHandler {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user