Search in sources :

Example 1 with DocumentSymbol

use of org.eclipse.lsp4j.DocumentSymbol 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 2 with DocumentSymbol

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

the class DocumentSymbolHandlerTest method internalGetHierarchicalSymbols.

private List<? extends DocumentSymbol> internalGetHierarchicalSymbols(IProject project, IProgressMonitor monitor, String className) throws JavaModelException, UnsupportedEncodingException, InterruptedException, ExecutionException {
    String uri = ClassFileUtil.getURI(project, className);
    TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri);
    DocumentSymbolParams params = new DocumentSymbolParams();
    params.setTextDocument(identifier);
    when(preferenceManager.getClientPreferences().isHierarchicalDocumentSymbolSupported()).thenReturn(true);
    // @formatter:off
    List<DocumentSymbol> symbols = new DocumentSymbolHandler(preferenceManager).documentSymbol(params, monitor).stream().map(Either::getRight).collect(toList());
    // @formatter:on
    assertTrue(symbols.size() > 0);
    return symbols;
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) DocumentSymbolParams(org.eclipse.lsp4j.DocumentSymbolParams) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol)

Example 3 with DocumentSymbol

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

the class DocumentSymbolHandler method toDocumentSymbol.

private DocumentSymbol toDocumentSymbol(IJavaElement unit, IProgressMonitor monitor) {
    int type = unit.getElementType();
    if (type != TYPE && type != FIELD && type != METHOD && type != PACKAGE_DECLARATION && type != COMPILATION_UNIT) {
        return null;
    }
    if (monitor.isCanceled()) {
        throw new OperationCanceledException("User abort");
    }
    DocumentSymbol symbol = new DocumentSymbol();
    try {
        String name = getName(unit);
        symbol.setName(name);
        symbol.setRange(getRange(unit));
        symbol.setSelectionRange(getSelectionRange(unit));
        symbol.setKind(mapKind(unit));
        if (JDTUtils.isDeprecated(unit)) {
            if (preferenceManager.getClientPreferences().isSymbolTagSupported()) {
                symbol.setTags(List.of(SymbolTag.Deprecated));
            } else {
                symbol.setDeprecated(true);
            }
        }
        symbol.setDetail(getDetail(unit, name));
        if (unit instanceof IParent) {
            // @formatter:off
            IJavaElement[] children = filter(((IParent) unit).getChildren());
            symbol.setChildren(Stream.of(children).map(child -> toDocumentSymbol(child, monitor)).filter(Objects::nonNull).collect(Collectors.toList()));
        // @formatter:off
        }
    } catch (JavaModelException e) {
        Exceptions.sneakyThrow(e);
    }
    return symbol;
}
Also used : M_APP_RETURNTYPE(org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.M_APP_RETURNTYPE) METHOD(org.eclipse.jdt.core.IJavaElement.METHOD) Arrays(java.util.Arrays) Exceptions(org.eclipse.xtext.xbase.lib.Exceptions) IField(org.eclipse.jdt.core.IField) JavaModelException(org.eclipse.jdt.core.JavaModelException) SymbolKind(org.eclipse.lsp4j.SymbolKind) DocumentSymbolParams(org.eclipse.lsp4j.DocumentSymbolParams) COMPILATION_UNIT(org.eclipse.jdt.core.IJavaElement.COMPILATION_UNIT) IMember(org.eclipse.jdt.core.IMember) Range(org.eclipse.lsp4j.Range) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) Flags(org.eclipse.jdt.core.Flags) IParent(org.eclipse.jdt.core.IParent) JavaLanguageServerPlugin.logInfo(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logInfo) Location(org.eclipse.lsp4j.Location) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) PACKAGE_DECLARATION(org.eclipse.jdt.core.IJavaElement.PACKAGE_DECLARATION) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) ROOT_VARIABLE(org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.ROOT_VARIABLE) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) FIELD(org.eclipse.jdt.core.IJavaElement.FIELD) Collections.emptyList(java.util.Collections.emptyList) ALL_DEFAULT(org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.ALL_DEFAULT) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Collectors(java.util.stream.Collectors) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Objects(java.util.Objects) IType(org.eclipse.jdt.core.IType) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) SymbolTag(org.eclipse.lsp4j.SymbolTag) Stream(java.util.stream.Stream) IJavaElement(org.eclipse.jdt.core.IJavaElement) FULL_RANGE(org.eclipse.jdt.ls.core.internal.JDTUtils.LocationType.FULL_RANGE) IMethod(org.eclipse.jdt.core.IMethod) TYPE(org.eclipse.jdt.core.IJavaElement.TYPE) JavaElementLabels(org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels) Collections(java.util.Collections) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IParent(org.eclipse.jdt.core.IParent) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) Objects(java.util.Objects) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol)

Example 4 with DocumentSymbol

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

the class DocumentSymbolMapper method toDocumentSymbol.

/**
 * Converts the {@code EObject} argument into a {@link DocumentSymbol document symbol} without the
 * {@link DocumentSymbol#children children} information filled in.
 */
public DocumentSymbol toDocumentSymbol(EObject object) {
    DocumentSymbol documentSymbol = new DocumentSymbol();
    String objectName = nameProvider.getName(object);
    if (objectName != null) {
        documentSymbol.setName(objectName);
    }
    SymbolKind objectKind = kindProvider.getSymbolKind(object);
    if (objectKind != null) {
        documentSymbol.setKind(objectKind);
    }
    Range objectRange = rangeProvider.getRange(object);
    if (objectRange != null) {
        documentSymbol.setRange(objectRange);
    }
    Range objectSelectionRange = rangeProvider.getSelectionRange(object);
    if (objectSelectionRange != null) {
        documentSymbol.setSelectionRange(objectSelectionRange);
    }
    documentSymbol.setDetail(detailsProvider.getDetails(object));
    documentSymbol.setDeprecated(deprecationInfoProvider.isDeprecated(object));
    documentSymbol.setChildren(new ArrayList<>());
    return documentSymbol;
}
Also used : SymbolKind(org.eclipse.lsp4j.SymbolKind) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) Range(org.eclipse.lsp4j.Range)

Example 5 with DocumentSymbol

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

the class DocumentSymbolService method getSymbols.

public List<Either<SymbolInformation, DocumentSymbol>> getSymbols(XtextResource resource, CancelIndicator cancelIndicator) {
    String uri = uriExtensions.toUriString(resource.getURI());
    ArrayList<SymbolInformation> infos = new ArrayList<>();
    List<DocumentSymbol> rootSymbols = Lists.transform(hierarchicalDocumentSymbolService.getSymbols(resource, cancelIndicator), Either::getRight);
    for (DocumentSymbol rootSymbol : rootSymbols) {
        Iterable<DocumentSymbol> symbols = Traverser.forTree(DocumentSymbol::getChildren).depthFirstPreOrder(rootSymbol);
        Function1<? super DocumentSymbol, ? extends String> containerNameProvider = (DocumentSymbol symbol) -> {
            DocumentSymbol firstSymbol = IterableExtensions.findFirst(symbols, (DocumentSymbol it) -> {
                return it != symbol && !IterableExtensions.isNullOrEmpty(it.getChildren()) && it.getChildren().contains(symbol);
            });
            if (firstSymbol != null) {
                return firstSymbol.getName();
            }
            return null;
        };
        for (DocumentSymbol s : symbols) {
            infos.add(createSymbol(uri, s, containerNameProvider));
        }
    }
    return Lists.transform(infos, Either::forLeft);
}
Also used : ArrayList(java.util.ArrayList) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) SymbolInformation(org.eclipse.lsp4j.SymbolInformation)

Aggregations

DocumentSymbol (org.eclipse.lsp4j.DocumentSymbol)10 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)6 DocumentSymbolParams (org.eclipse.lsp4j.DocumentSymbolParams)5 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)5 List (java.util.List)4 ArrayList (java.util.ArrayList)3 Range (org.eclipse.lsp4j.Range)3 Collectors (java.util.stream.Collectors)2 CompletionList (org.eclipse.lsp4j.CompletionList)2 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)2 Exceptions (org.eclipse.xtext.xbase.lib.Exceptions)2 Test (org.junit.Test)2 Beta (com.google.common.annotations.Beta)1 Optional (com.google.common.base.Optional)1 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1