Search in sources :

Example 21 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project eclipse.jdt.ls by eclipse.

the class WorkspaceSymbolHandlerTest method testWorkspaceSearch.

@Test
public void testWorkspaceSearch() {
    String query = "Array";
    List<SymbolInformation> results = WorkspaceSymbolHandler.search(query, monitor);
    assertNotNull(results);
    assertEquals("Unexpected results", 11, results.size());
    Range defaultRange = JDTUtils.newRange();
    for (SymbolInformation symbol : results) {
        assertNotNull("Kind is missing", symbol.getKind());
        assertNotNull("ContainerName is missing", symbol.getContainerName());
        assertTrue(symbol.getName().startsWith(query));
        Location location = symbol.getLocation();
        assertEquals(defaultRange, location.getRange());
        // No class in the workspace project starts with Array, so everything comes from the JDK
        assertTrue("Unexpected uri " + location.getUri(), location.getUri().startsWith("jdt://"));
    }
}
Also used : Range(org.eclipse.lsp4j.Range) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Location(org.eclipse.lsp4j.Location) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Example 22 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project eclipse.jdt.ls by eclipse.

the class DocumentSymbolHandlerTest method testDeprecated_property.

@Test
public void testDeprecated_property() throws Exception {
    when(preferenceManager.getClientPreferences().isSymbolTagSupported()).thenReturn(false);
    String className = "org.sample.Bar";
    List<? extends SymbolInformation> symbols = getSymbols(className);
    SymbolInformation deprecated = symbols.stream().filter(symbol -> symbol.getName().equals("MyInterface")).findFirst().orElse(null);
    assertNotNull(deprecated);
    assertEquals(SymbolKind.Interface, deprecated.getKind());
    assertNotNull(deprecated.getDeprecated());
    assertTrue("Should be deprecated", deprecated.getDeprecated());
}
Also used : SymbolInformation(org.eclipse.lsp4j.SymbolInformation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 23 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project eclipse.jdt.ls by eclipse.

the class WorkspaceSymbolHandler method search.

public static List<SymbolInformation> search(String query, int maxResults, String projectName, boolean sourceOnly, IProgressMonitor monitor) {
    ArrayList<SymbolInformation> symbols = new ArrayList<>();
    if (StringUtils.isBlank(query)) {
        return symbols;
    }
    try {
        monitor.beginTask("Searching the types...", 100);
        IJavaSearchScope searchScope = createSearchScope(projectName, sourceOnly);
        int typeMatchRule = SearchPattern.R_CAMELCASE_MATCH;
        if (query.contains("*") || query.contains("?")) {
            typeMatchRule |= SearchPattern.R_PATTERN_MATCH;
        }
        PreferenceManager preferenceManager = JavaLanguageServerPlugin.getPreferencesManager();
        new SearchEngine().searchAllTypeNames(null, SearchPattern.R_PATTERN_MATCH, query.trim().toCharArray(), typeMatchRule, IJavaSearchConstants.TYPE, searchScope, new TypeNameMatchRequestor() {

            @Override
            public void acceptTypeNameMatch(TypeNameMatch match) {
                try {
                    if (maxResults > 0 && symbols.size() >= maxResults) {
                        return;
                    }
                    Location location = null;
                    try {
                        if (!sourceOnly && match.getType().isBinary()) {
                            location = JDTUtils.toLocation(match.getType().getClassFile());
                        } else if (!match.getType().isBinary()) {
                            location = JDTUtils.toLocation(match.getType());
                        }
                    } catch (Exception e) {
                        JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
                        return;
                    }
                    if (location != null && match.getSimpleTypeName() != null && !match.getSimpleTypeName().isEmpty()) {
                        SymbolInformation symbolInformation = new SymbolInformation();
                        symbolInformation.setContainerName(match.getTypeContainerName());
                        symbolInformation.setName(match.getSimpleTypeName());
                        symbolInformation.setKind(mapKind(match));
                        if (Flags.isDeprecated(match.getType().getFlags())) {
                            if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
                                symbolInformation.setTags(List.of(SymbolTag.Deprecated));
                            } else {
                                symbolInformation.setDeprecated(true);
                            }
                        }
                        symbolInformation.setLocation(location);
                        symbols.add(symbolInformation);
                        if (maxResults > 0 && symbols.size() >= maxResults) {
                            monitor.setCanceled(true);
                        }
                    }
                } catch (Exception e) {
                    JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
                    return;
                }
            }

            private SymbolKind mapKind(TypeNameMatch match) {
                int flags = match.getModifiers();
                if (Flags.isInterface(flags)) {
                    return SymbolKind.Interface;
                }
                if (Flags.isAnnotation(flags)) {
                    return SymbolKind.Property;
                }
                if (Flags.isEnum(flags)) {
                    return SymbolKind.Enum;
                }
                return SymbolKind.Class;
            }
        }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
        if (preferenceManager != null && preferenceManager.getPreferences().isIncludeSourceMethodDeclarations()) {
            monitor.beginTask("Searching methods...", 100);
            IJavaSearchScope nonSourceSearchScope = createSearchScope(projectName, true);
            new SearchEngine().searchAllMethodNames(null, SearchPattern.R_PATTERN_MATCH, query.trim().toCharArray(), typeMatchRule, nonSourceSearchScope, new MethodNameMatchRequestor() {

                @Override
                public void acceptMethodNameMatch(MethodNameMatch match) {
                    try {
                        if (maxResults > 0 && symbols.size() >= maxResults) {
                            return;
                        }
                        Location location = null;
                        try {
                            location = JDTUtils.toLocation(match.getMethod());
                        } catch (Exception e) {
                            JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
                            return;
                        }
                        if (location != null && match.getMethod().getElementName() != null && !match.getMethod().getElementName().isEmpty()) {
                            SymbolInformation symbolInformation = new SymbolInformation();
                            symbolInformation.setContainerName(match.getMethod().getDeclaringType().getFullyQualifiedName());
                            symbolInformation.setName(match.getMethod().getElementName());
                            symbolInformation.setKind(SymbolKind.Method);
                            if (Flags.isDeprecated(match.getMethod().getFlags())) {
                                if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
                                    symbolInformation.setTags(List.of(SymbolTag.Deprecated));
                                } else {
                                    symbolInformation.setDeprecated(true);
                                }
                            }
                            symbolInformation.setLocation(location);
                            symbols.add(symbolInformation);
                            if (maxResults > 0 && symbols.size() >= maxResults) {
                                monitor.setCanceled(true);
                            }
                        }
                    } catch (Exception e) {
                        JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
                        return;
                    }
                }
            }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
        }
    } catch (Exception e) {
        if (e instanceof OperationCanceledException) {
        // ignore.
        } else {
            JavaLanguageServerPlugin.logException("Problem getting search for" + query, e);
        }
    } finally {
        monitor.done();
    }
    return symbols;
}
Also used : MethodNameMatch(org.eclipse.jdt.core.search.MethodNameMatch) SymbolKind(org.eclipse.lsp4j.SymbolKind) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) JavaModelException(org.eclipse.jdt.core.JavaModelException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) MethodNameMatchRequestor(org.eclipse.jdt.core.search.MethodNameMatchRequestor) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) Location(org.eclipse.lsp4j.Location)

Example 24 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method definitionToSymbol.

private SymbolInformation definitionToSymbol(IDefinition definition) {
    SymbolInformation symbol = new SymbolInformation();
    if (definition instanceof IClassDefinition) {
        symbol.setKind(SymbolKind.Class);
    } else if (definition instanceof IInterfaceDefinition) {
        symbol.setKind(SymbolKind.Interface);
    } else if (definition instanceof IFunctionDefinition) {
        IFunctionDefinition functionDefinition = (IFunctionDefinition) definition;
        if (functionDefinition.isConstructor()) {
            symbol.setKind(SymbolKind.Constructor);
        } else {
            symbol.setKind(SymbolKind.Function);
        }
    } else if (definition instanceof IFunctionDefinition) {
        symbol.setKind(SymbolKind.Function);
    } else if (definition instanceof IConstantDefinition) {
        symbol.setKind(SymbolKind.Constant);
    } else {
        symbol.setKind(SymbolKind.Variable);
    }
    symbol.setName(definition.getQualifiedName());
    Location location = new Location();
    String sourcePath = definition.getSourcePath();
    if (sourcePath == null) {
        //I'm not sure why getSourcePath() can sometimes return null, but
        //getContainingFilePath() seems to work as a fallback -JT
        sourcePath = definition.getContainingFilePath();
    }
    Path definitionPath = Paths.get(sourcePath);
    location.setUri(definitionPath.toUri().toString());
    Position start = new Position();
    Position end = new Position();
    //getLine() and getColumn() may include things like metadata, so it
    //makes more sense to jump to where the definition name starts
    int line = definition.getNameLine();
    int column = definition.getNameColumn();
    if (line < 0 || column < 0) {
        //this is not ideal, but MXML variable definitions may not have a
        //node associated with them, so we need to figure this out from the
        //offset instead of a pre-calculated line and column -JT
        String code = sourceByPath.get(Paths.get(sourcePath));
        offsetToLineAndCharacter(new StringReader(code), definition.getNameStart(), start);
        end.setLine(start.getLine());
        end.setCharacter(start.getCharacter());
    } else {
        start.setLine(line);
        start.setCharacter(column);
        end.setLine(line);
        end.setCharacter(column);
    }
    Range range = new Range();
    range.setStart(start);
    range.setEnd(end);
    location.setRange(range);
    symbol.setLocation(location);
    return symbol;
}
Also used : Path(java.nio.file.Path) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IInterfaceDefinition(org.apache.flex.compiler.definitions.IInterfaceDefinition) IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) Position(org.eclipse.lsp4j.Position) StringReader(java.io.StringReader) Range(org.eclipse.lsp4j.Range) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IConstantDefinition(org.apache.flex.compiler.definitions.IConstantDefinition) Location(org.eclipse.lsp4j.Location) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation)

Example 25 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method scopeToSymbols.

private void scopeToSymbols(IASScope scope, List<SymbolInformation> result) {
    Collection<IDefinition> definitions = scope.getAllLocalDefinitions();
    for (IDefinition definition : definitions) {
        if (definition instanceof IPackageDefinition) {
            IPackageDefinition packageDefinition = (IPackageDefinition) definition;
            IASScope packageScope = packageDefinition.getContainedScope();
            scopeToSymbols(packageScope, result);
        } else if (definition instanceof ITypeDefinition) {
            ITypeDefinition typeDefinition = (ITypeDefinition) definition;
            if (!definition.isImplicit()) {
                SymbolInformation typeSymbol = definitionToSymbol(typeDefinition);
                result.add(typeSymbol);
            }
            IASScope typeScope = typeDefinition.getContainedScope();
            scopeToSymbols(typeScope, result);
        } else if (definition instanceof IFunctionDefinition || definition instanceof IVariableDefinition) {
            if (definition.isImplicit()) {
                continue;
            }
            SymbolInformation localSymbol = definitionToSymbol(definition);
            result.add(localSymbol);
        }
    }
}
Also used : IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) IASScope(org.apache.flex.compiler.scopes.IASScope) IPackageDefinition(org.apache.flex.compiler.definitions.IPackageDefinition) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) IVariableDefinition(org.apache.flex.compiler.definitions.IVariableDefinition) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IDefinition(org.apache.flex.compiler.definitions.IDefinition)

Aggregations

SymbolInformation (org.eclipse.lsp4j.SymbolInformation)54 Location (org.eclipse.lsp4j.Location)24 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 List (java.util.List)10 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)10 SymbolKind (org.eclipse.lsp4j.SymbolKind)9 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)8 ImmutableList (com.google.common.collect.ImmutableList)7 EnhancedSymbolInformation (org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation)7 URI (java.net.URI)6 Path (java.nio.file.Path)6 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)6 Range (org.eclipse.lsp4j.Range)5 Paths (java.nio.file.Paths)4 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Optional (java.util.Optional)4