Skip to content

Commit

Permalink
eclipse-jdtlsGH-28: Added support for the super- and subtype hierarch…
Browse files Browse the repository at this point in the history
…ies.

Closes: eclipse-jdtls#28.

Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Jan 23, 2019
1 parent 57336a2 commit 5bc2d86
Show file tree
Hide file tree
Showing 10 changed files with 835 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
package org.eclipse.jdt.ls.core.internal;

import static org.eclipse.core.resources.IResource.DEPTH_ONE;
import static org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.ALL_DEFAULT;
import static org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.M_APP_RETURNTYPE;
import static org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.ROOT_VARIABLE;

import java.io.File;
import java.io.IOException;
Expand All @@ -30,13 +33,15 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IBuffer;
Expand All @@ -51,6 +56,7 @@
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
Expand All @@ -72,6 +78,7 @@
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
import org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers;
import org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels;
import org.eclipse.jdt.ls.core.internal.managers.ContentProviderManager;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
Expand All @@ -81,6 +88,7 @@
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolKind;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
Expand Down Expand Up @@ -273,14 +281,99 @@ public static String getPackageName(IJavaProject javaProject, String fileContent
return (pkg == null || pkg.getName() == null)?"":pkg.getName().getFullyQualifiedName();
}

/**
* Returns with the human readable name of the element. For types with type
* arguments, it is {@code Comparable<T>} instead of {@code Comparable}. First,
* this method tries to retrieve the
* {@link JavaElementLabels#getElementLabel(IJavaElement, long) label} of the
* element, then falls back to {@link IJavaElement#getElementName() element
* name}. Returns {@code null} if the argument does not have a name.
*/
public static String getName(IJavaElement element) {
Assert.isNotNull(element, "element");
String name = JavaElementLabels.getElementLabel(element, ALL_DEFAULT);
return name == null ? element.getElementName() : name;
}

/**
* Given the uri returns a {@link IClassFile}.
* May return null if it can not resolve the uri to a
* library.
* Returns with the details of the document symbol. This is usually the type
* information of a member. For methods, this is the type information of the
* return type. Can return with an empty string, but never {@code null}.
*
* @see JDTUtils#getName(IJavaElement)
*/
public static String getDetail(IJavaElement element) {
Assert.isNotNull(element, "element");
String name = getName(element);
String nameWithDetails = JavaElementLabels.getElementLabel(element, ALL_DEFAULT | M_APP_RETURNTYPE | ROOT_VARIABLE);
if (nameWithDetails != null && nameWithDetails.startsWith(name)) {
return nameWithDetails.substring(name.length());
}
return "";
}

/**
* Returns with the document symbol {@code SymbolKind kind} for the Java
* element.
*/
public static SymbolKind getSymbolKind(IJavaElement element) {
switch (element.getElementType()) {
case IJavaElement.ANNOTATION:
return SymbolKind.Property; // TODO: find a better mapping
case IJavaElement.CLASS_FILE:
case IJavaElement.COMPILATION_UNIT:
return SymbolKind.File;
case IJavaElement.FIELD:
return SymbolKind.Field;
case IJavaElement.IMPORT_CONTAINER:
case IJavaElement.IMPORT_DECLARATION:
return SymbolKind.Module;
case IJavaElement.INITIALIZER:
return SymbolKind.Constructor;
case IJavaElement.LOCAL_VARIABLE:
case IJavaElement.TYPE_PARAMETER:
return SymbolKind.Variable;
case IJavaElement.METHOD:
return SymbolKind.Method;
case IJavaElement.PACKAGE_DECLARATION:
return SymbolKind.Package;
case IJavaElement.TYPE:
try {
IType type = (IType) element;
if (type.isEnum()) {
return SymbolKind.Enum;
} else if (type.isInterface()) {
return SymbolKind.Interface;
} else {
return SymbolKind.Class;
}
} catch (JavaModelException e) {
return SymbolKind.Class;
}
}
return SymbolKind.String;
}

/**
* {@code true} if the element is deprecated. Otherwise, {@code false}.
*/
public static boolean isDeprecated(IJavaElement element) throws JavaModelException {
Assert.isNotNull(element, "element");
if (element instanceof ITypeRoot) {
return Flags.isDeprecated(((ITypeRoot) element).findPrimaryType().getFlags());
} else if (element instanceof IMember) {
return Flags.isDeprecated(((IMember) element).getFlags());
}
return false;
}

/**
* Given the uri returns a {@link IClassFile}. May return null if it can not
* resolve the uri to a library.
*
* @see #toLocation(IClassFile, int, int)
* @param uri with 'jdt' scheme
* @param uri
* with 'jdt' scheme
* @return class file
*/
public static IClassFile resolveClassFile(String uriString){
Expand Down Expand Up @@ -355,6 +448,13 @@ ISourceRange getRange(IJavaElement element) throws JavaModelException {
};

/* default */ abstract ISourceRange getRange(IJavaElement element) throws JavaModelException;

/**
* Sugar for {@link JDTUtils#toLocation(IJavaElement, LocationType)}.
*/
public Location toLocation(IJavaElement element) throws JavaModelException {
return JDTUtils.toLocation(element, this);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import static org.eclipse.jdt.core.IJavaElement.PACKAGE_DECLARATION;
import static org.eclipse.jdt.core.IJavaElement.TYPE;
import static org.eclipse.jdt.ls.core.internal.JDTUtils.LocationType.FULL_RANGE;
import static org.eclipse.jdt.ls.core.internal.JDTUtils.LocationType.NAME_RANGE;
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logInfo;
import static org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.ALL_DEFAULT;
import static org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.M_APP_RETURNTYPE;
import static org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels.ROOT_VARIABLE;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -31,13 +29,13 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IParent;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
Expand All @@ -50,11 +48,11 @@
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.xtext.xbase.lib.Exceptions;

public class DocumentSymbolHandler {

private static Range DEFAULT_RANGE = new Range(new Position(0, 0), new Position(0, 0));

private boolean hierarchicalDocumentSymbolSupported;
Expand Down Expand Up @@ -111,7 +109,7 @@ private void collectChildren(ITypeRoot unit, IJavaElement[] elements, ArrayList<
SymbolInformation si = new SymbolInformation();
String name = JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT);
si.setName(name == null ? element.getElementName() : name);
si.setKind(mapKind(element));
si.setKind(JDTUtils.getSymbolKind(element));
if (element.getParent() != null) {
si.setContainerName(element.getParent().getElementName());
}
Expand Down Expand Up @@ -145,13 +143,13 @@ private DocumentSymbol toDocumentSymbol(IJavaElement unit, IProgressMonitor moni
}
DocumentSymbol symbol = new DocumentSymbol();
try {
String name = getName(unit);
String name = JDTUtils.getName(unit);
symbol.setName(name);
symbol.setRange(getRange(unit));
symbol.setSelectionRange(getSelectionRange(unit));
symbol.setKind(mapKind(unit));
symbol.setDeprecated(isDeprecated(unit));
symbol.setDetail(getDetail(unit, name));
symbol.setKind(JDTUtils.getSymbolKind(unit));
symbol.setDeprecated(JDTUtils.isDeprecated(unit));
symbol.setDetail(JDTUtils.getDetail(unit));
if (unit instanceof IParent) {
//@formatter:off
IJavaElement[] children = filter(((IParent) unit).getChildren());
Expand All @@ -167,36 +165,6 @@ private DocumentSymbol toDocumentSymbol(IJavaElement unit, IProgressMonitor moni
return symbol;
}

private String getName(IJavaElement element) {
String name = JavaElementLabels.getElementLabel(element, ALL_DEFAULT);
return name == null ? element.getElementName() : name;
}

private Range getRange(IJavaElement element) throws JavaModelException {
Location location = JDTUtils.toLocation(element, FULL_RANGE);
return location == null ? DEFAULT_RANGE : location.getRange();
}

private Range getSelectionRange(IJavaElement element) throws JavaModelException {
Location location = JDTUtils.toLocation(element);
return location == null ? DEFAULT_RANGE : location.getRange();
}

private boolean isDeprecated(IJavaElement element) throws JavaModelException {
if (element instanceof ITypeRoot) {
return Flags.isDeprecated(((ITypeRoot) element).findPrimaryType().getFlags());
}
return false;
}

private String getDetail(IJavaElement element, String name) {
String nameWithDetails = JavaElementLabels.getElementLabel(element, ALL_DEFAULT | M_APP_RETURNTYPE | ROOT_VARIABLE);
if (nameWithDetails != null && nameWithDetails.startsWith(name)) {
return nameWithDetails.substring(name.length());
}
return "";
}

private IJavaElement[] filter(IJavaElement[] elements) {
return Stream.of(elements)
.filter(e -> (!isInitializer(e) && !isSyntheticElement(e)))
Expand Down Expand Up @@ -228,35 +196,16 @@ private boolean isSyntheticElement(IJavaElement element) {
}
}

public static SymbolKind mapKind(IJavaElement element) {
switch (element.getElementType()) {
case IJavaElement.ANNOTATION:
return SymbolKind.Property; // TODO: find a better mapping
case IJavaElement.CLASS_FILE:
case IJavaElement.COMPILATION_UNIT:
return SymbolKind.File;
case IJavaElement.FIELD:
return SymbolKind.Field;
case IJavaElement.IMPORT_CONTAINER:
case IJavaElement.IMPORT_DECLARATION:
return SymbolKind.Module;
case IJavaElement.INITIALIZER:
return SymbolKind.Constructor;
case IJavaElement.LOCAL_VARIABLE:
case IJavaElement.TYPE_PARAMETER:
return SymbolKind.Variable;
case IJavaElement.METHOD:
return SymbolKind.Method;
case IJavaElement.PACKAGE_DECLARATION:
return SymbolKind.Package;
case IJavaElement.TYPE:
try {
return (((IType)element).isInterface() ? SymbolKind.Interface : SymbolKind.Class);
} catch (JavaModelException e) {
return SymbolKind.Class;
}
}
return SymbolKind.String;
private Range getRange(IJavaElement element) throws JavaModelException {
Assert.isNotNull(element, "element");
Location location = FULL_RANGE.toLocation(element);
return location == null ? DEFAULT_RANGE : location.getRange();
}

private Range getSelectionRange(IJavaElement element) throws JavaModelException {
Assert.isNotNull(element, "element");
Location location = NAME_RANGE.toLocation(element);
return location == null ? DEFAULT_RANGE : location.getRange();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ InitializeResult initialize(InitializeParams param) {
wsCapabilities.setWorkspaceFolders(wsFoldersOptions);
capabilities.setWorkspace(wsCapabilities);

capabilities.setTypeHierarchy(true);

result.setCapabilities(capabilities);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@
import org.eclipse.lsp4j.Registration;
import org.eclipse.lsp4j.RegistrationParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.ResolveTypeHierarchyItemParams;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.TypeHierarchyItem;
import org.eclipse.lsp4j.TypeHierarchyParams;
import org.eclipse.lsp4j.Unregistration;
import org.eclipse.lsp4j.UnregistrationParams;
import org.eclipse.lsp4j.WillSaveTextDocumentParams;
Expand Down Expand Up @@ -759,6 +762,28 @@ public CompletableFuture<List<? extends Location>> implementation(TextDocumentPo
return computeAsyncWithClientProgress((monitor) -> new ImplementationsHandler(preferenceManager).findImplementations(position, monitor));
}

/*
* (non-Javadoc)
* @see org.eclipse.lsp4j.services.TextDocumentService#typeHierarchy(org.eclipse.lsp4j.TypeHierarchyParams)
*/
@Override
public CompletableFuture<TypeHierarchyItem> typeHierarchy(TypeHierarchyParams params) {
logInfo(">> textDocument/typeHierarchy");
TypeHierarchyHandler handler = new TypeHierarchyHandler(preferenceManager);
return computeAsyncWithClientProgress(monitor -> handler.typeHierarchy(params, monitor));
}

/*
* (non-Javadoc)
* @see org.eclipse.lsp4j.services.TextDocumentService#resolveTypeHierarchy(org.eclipse.lsp4j.ResolveTypeHierarchyItemParams)
*/
@Override
public CompletableFuture<TypeHierarchyItem> resolveTypeHierarchy(ResolveTypeHierarchyItemParams params) {
logInfo(">> typeHierarchy/resolve");
TypeHierarchyResolveHandler handler = new TypeHierarchyResolveHandler(preferenceManager);
return computeAsyncWithClientProgress(monitor -> handler.resolve(params, monitor));
}

public void sendStatus(ServiceStatus serverStatus, String status) {
if (client != null) {
client.sendStatus(serverStatus, status);
Expand Down
Loading

0 comments on commit 5bc2d86

Please sign in to comment.