Search in sources :

Example 16 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project xtext-core by eclipse.

the class DocumentSymbolService method createSymbol.

/**
 * @since 2.16
 */
protected SymbolInformation createSymbol(String uri, DocumentSymbol symbol, Function1<? super DocumentSymbol, ? extends String> containerNameProvider) {
    SymbolInformation symbolInformation = new SymbolInformation();
    symbolInformation.setName(symbol.getName());
    symbolInformation.setKind(symbol.getKind());
    symbolInformation.setDeprecated(symbol.getDeprecated());
    Location location = new Location();
    location.setUri(uri);
    location.setRange(symbol.getSelectionRange());
    symbolInformation.setLocation(location);
    symbolInformation.setContainerName(containerNameProvider.apply(symbol));
    return symbolInformation;
}
Also used : SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Location(org.eclipse.lsp4j.Location)

Example 17 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project xtext-core by eclipse.

the class HierarchicalDocumentSymbolService method getSymbols.

public List<Either<SymbolInformation, DocumentSymbol>> getSymbols(XtextResource resource, CancelIndicator cancelIndicator) {
    HashMap<EObject, DocumentSymbol> allSymbols = new HashMap<>();
    ArrayList<DocumentSymbol> rootSymbols = new ArrayList<>();
    Iterator<Object> itr = getAllContents(resource);
    while (itr.hasNext()) {
        operationCanceledManager.checkCanceled(cancelIndicator);
        Optional<EObject> next = toEObject(itr.next());
        if (next.isPresent()) {
            EObject object = next.get();
            DocumentSymbol symbol = symbolMapper.toDocumentSymbol(object);
            if (isValid(symbol)) {
                allSymbols.put(object, symbol);
                EObject parent = object.eContainer();
                if (parent == null) {
                    rootSymbols.add(symbol);
                } else {
                    DocumentSymbol parentSymbol = allSymbols.get(parent);
                    while (parentSymbol == null && parent != null) {
                        parent = parent.eContainer();
                        parentSymbol = allSymbols.get(parent);
                    }
                    if (parentSymbol == null) {
                        rootSymbols.add(symbol);
                    } else {
                        parentSymbol.getChildren().add(symbol);
                    }
                }
            }
        }
    }
    return rootSymbols.stream().map(symbol -> Either.<SymbolInformation, DocumentSymbol>forRight(symbol)).collect(Collectors.toList());
}
Also used : NonNull(org.eclipse.lsp4j.jsonrpc.validation.NonNull) XtextResource(org.eclipse.xtext.resource.XtextResource) Exceptions(org.eclipse.xtext.xbase.lib.Exceptions) Iterator(java.util.Iterator) OperationCanceledManager(org.eclipse.xtext.service.OperationCanceledManager) Inject(com.google.inject.Inject) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) DocumentSymbolParams(org.eclipse.lsp4j.DocumentSymbolParams) HashMap(java.util.HashMap) EObject(org.eclipse.emf.ecore.EObject) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Beta(com.google.common.annotations.Beta) List(java.util.List) Document(org.eclipse.xtext.ide.server.Document) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) Optional(com.google.common.base.Optional) Annotation(java.lang.annotation.Annotation) Resource(org.eclipse.emf.ecore.resource.Resource) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) Singleton(com.google.inject.Singleton) HashMap(java.util.HashMap) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) EObject(org.eclipse.emf.ecore.EObject) SymbolInformation(org.eclipse.lsp4j.SymbolInformation)

Example 18 with SymbolInformation

use of org.eclipse.lsp4j.SymbolInformation in project xtext-core by eclipse.

the class WorkspaceSymbolService method getSymbols.

public List<? extends SymbolInformation> getSymbols(String query, IResourceAccess resourceAccess, IResourceDescriptions indexData, CancelIndicator cancelIndicator) {
    List<SymbolInformation> result = new LinkedList<>();
    for (IResourceDescription resourceDescription : indexData.getAllResourceDescriptions()) {
        operationCanceledManager.checkCanceled(cancelIndicator);
        IResourceServiceProvider resourceServiceProvider = registry.getResourceServiceProvider(resourceDescription.getURI());
        if (resourceServiceProvider != null) {
            DocumentSymbolService documentSymbolService = resourceServiceProvider.get(DocumentSymbolService.class);
            if (documentSymbolService != null) {
                result.addAll(documentSymbolService.getSymbols(resourceDescription, query, resourceAccess, cancelIndicator));
            }
        }
    }
    return result;
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) LinkedList(java.util.LinkedList)

Example 19 with SymbolInformation

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

the class WorkspaceSymbolHandlerTest method testSearchSourceMethodDeclarations.

@Test
public void testSearchSourceMethodDeclarations() {
    preferences.setIncludeSourceMethodDeclarations(true);
    List<SymbolInformation> results = WorkspaceSymbolHandler.search("deleteSomething", "hello", true, monitor);
    assertNotNull(results);
    assertEquals("Found " + results.size() + " result", 1, results.size());
    SymbolInformation res = results.get(0);
    assertEquals(SymbolKind.Method, res.getKind());
    assertEquals(res.getContainerName(), "org.sample.Baz");
    results = WorkspaceSymbolHandler.search("main", "hello", true, monitor);
    assertNotNull(results);
    assertEquals("Found " + results.size() + " result", 11, results.size());
    boolean allMethods = results.stream().allMatch(s -> s.getKind() == SymbolKind.Method);
    assertTrue("Found a non-method symbol", allMethods);
    preferences.setIncludeSourceMethodDeclarations(false);
}
Also used : SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Example 20 with SymbolInformation

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

the class WorkspaceSymbolHandlerTest method testWorkspaceSearchOnFileInWorkspace.

@Test
public void testWorkspaceSearchOnFileInWorkspace() {
    String query = "Baz";
    List<SymbolInformation> results = WorkspaceSymbolHandler.search(query, monitor);
    assertNotNull(results);
    Range defaultRange = JDTUtils.newRange();
    assertEquals("Unexpected results", 2, results.size());
    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();
        assertNotEquals("Range should not equal the default range", defaultRange, location.getRange());
        assertTrue("Unexpected uri " + location.getUri(), location.getUri().startsWith("file://"));
    }
}
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)

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