Search in sources :

Example 1 with IParent

use of org.eclipse.jdt.core.IParent 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 2 with IParent

use of org.eclipse.jdt.core.IParent in project che by eclipse.

the class JavaModelManager method putInfos.

/*
	 * Puts the infos in the given map (keys are IJavaElements and values are JavaElementInfos)
	 * in the Java model cache in an atomic way if the info is not already present in the cache.
	 * If the info is already present in the cache, it depends upon the forceAdd parameter.
	 * If forceAdd is false it just returns the existing info and if true, this element and it's children are closed and then
	 * this particular info is added to the cache.
	 */
protected synchronized Object putInfos(IJavaElement openedElement, Object newInfo, boolean forceAdd, Map newElements) {
    // remove existing children as the are replaced with the new children contained in newElements
    Object existingInfo = this.cache.peekAtInfo(openedElement);
    if (existingInfo != null && !forceAdd) {
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=372687
        return existingInfo;
    }
    if (openedElement instanceof IParent) {
        closeChildren(existingInfo);
    }
    // (theodora)
    for (Iterator it = newElements.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();
        IJavaElement element = (IJavaElement) entry.getKey();
        if (element instanceof JarPackageFragmentRoot) {
            Object info = entry.getValue();
            it.remove();
            this.cache.putInfo(element, info);
        }
    }
    Iterator iterator = newElements.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry entry = (Map.Entry) iterator.next();
        this.cache.putInfo((IJavaElement) entry.getKey(), entry.getValue());
    }
    return newInfo;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IParent(org.eclipse.jdt.core.IParent) Iterator(java.util.Iterator) HashtableOfArrayToObject(org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject) Map(java.util.Map) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap)

Example 3 with IParent

use of org.eclipse.jdt.core.IParent in project che by eclipse.

the class JavaModelManager method removeInfoAndChildren.

/*
	 * Removes all cached info for the given element (including all children)
	 * from the cache.
	 * Returns the info for the given element, or null if it was closed.
	 */
public synchronized Object removeInfoAndChildren(JavaElement element) throws JavaModelException {
    Object info = this.cache.peekAtInfo(element);
    if (info != null) {
        boolean wasVerbose = false;
        try {
            if (JavaModelCache.VERBOSE) {
                String elementType;
                switch(element.getElementType()) {
                    case IJavaElement.JAVA_PROJECT:
                        //$NON-NLS-1$
                        elementType = "project";
                        break;
                    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                        //$NON-NLS-1$
                        elementType = "root";
                        break;
                    case IJavaElement.PACKAGE_FRAGMENT:
                        //$NON-NLS-1$
                        elementType = "package";
                        break;
                    case IJavaElement.CLASS_FILE:
                        //$NON-NLS-1$
                        elementType = "class file";
                        break;
                    case IJavaElement.COMPILATION_UNIT:
                        //$NON-NLS-1$
                        elementType = "compilation unit";
                        break;
                    default:
                        //$NON-NLS-1$
                        elementType = "element";
                }
                System.out.println(Thread.currentThread() + " CLOSING " + elementType + " " + //$NON-NLS-1$//$NON-NLS-2$
                element.toStringWithAncestors());
                wasVerbose = true;
                JavaModelCache.VERBOSE = false;
            }
            element.closing(info);
            if (element instanceof IParent) {
                closeChildren(info);
            }
            this.cache.removeInfo(element);
            if (wasVerbose) {
                //$NON-NLS-1$
                System.out.println(this.cache.toStringFillingRation("-> "));
            }
        } finally {
            JavaModelCache.VERBOSE = wasVerbose;
        }
        return info;
    }
    return null;
}
Also used : IParent(org.eclipse.jdt.core.IParent) HashtableOfArrayToObject(org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject)

Example 4 with IParent

use of org.eclipse.jdt.core.IParent in project eclipse.jdt.ls by eclipse.

the class DocumentSymbolHandler method collectChildren.

private void collectChildren(ITypeRoot unit, IJavaElement[] elements, ArrayList<SymbolInformation> symbols, IProgressMonitor monitor) throws JavaModelException {
    for (IJavaElement element : elements) {
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        if (element instanceof IParent) {
            collectChildren(unit, filter(((IParent) element).getChildren()), symbols, monitor);
        }
        int type = element.getElementType();
        if (type != IJavaElement.TYPE && type != IJavaElement.FIELD && type != IJavaElement.METHOD) {
            continue;
        }
        Location location = JDTUtils.toLocation(element);
        if (location != null) {
            SymbolInformation si = new SymbolInformation();
            String name = JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT);
            si.setName(name == null ? element.getElementName() : name);
            si.setKind(mapKind(element));
            if (JDTUtils.isDeprecated(element)) {
                if (preferenceManager.getClientPreferences().isSymbolTagSupported()) {
                    si.setTags(List.of(SymbolTag.Deprecated));
                } else {
                    si.setDeprecated(true);
                }
            }
            if (element.getParent() != null) {
                si.setContainerName(element.getParent().getElementName());
            }
            location.setUri(ResourceUtils.toClientUri(location.getUri()));
            si.setLocation(location);
            if (!symbols.contains(si)) {
                symbols.add(si);
            }
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IParent(org.eclipse.jdt.core.IParent) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Location(org.eclipse.lsp4j.Location)

Aggregations

IParent (org.eclipse.jdt.core.IParent)4 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 HashtableOfArrayToObject (org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject)2 Location (org.eclipse.lsp4j.Location)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 WeakHashMap (java.util.WeakHashMap)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Flags (org.eclipse.jdt.core.Flags)1