Skip to content

Commit

Permalink
eclipse-jdtlsGH-508: Added handler for textDocument/callHierarchy.
Browse files Browse the repository at this point in the history
Closes eclipse-jdtls#508

Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta committed Jan 28, 2019
1 parent 33b0a71 commit 28cbc72
Show file tree
Hide file tree
Showing 14 changed files with 894 additions and 13 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 @@ -47,10 +52,12 @@
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMemberValuePair;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IOpenable;
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 +79,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 +89,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 +282,108 @@ 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:
try {
if (element instanceof IMethod) {
if (((IMethod) element).isConstructor()) {
return SymbolKind.Constructor;
}
}
return SymbolKind.Method;
} catch (JavaModelException e) {
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 +458,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 @@ -62,6 +62,8 @@
import org.osgi.framework.BundleException;
import org.osgi.util.tracker.ServiceTracker;

import com.google.common.base.Throwables;

public class JavaLanguageServerPlugin extends Plugin {

private static final String JDT_UI_PLUGIN = "org.eclipse.jdt.ui";
Expand Down Expand Up @@ -349,6 +351,16 @@ public static void logInfo(String message) {
}
}

public static void logException(Throwable ex) {
if (context != null) {
String message = ex.getMessage();
if (message == null) {
message = Throwables.getStackTraceAsString(ex);
}
logException(message, ex);
}
}

public static void logException(String message, Throwable ex) {
if (context != null) {
log(new Status(IStatus.ERROR, context.getBundle().getSymbolicName(), message, ex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class CallHierarchy {
private static final String PREF_USE_FILTERS = "PREF_USE_FILTERS"; //$NON-NLS-1$
private static final String PREF_FILTERS_LIST = "PREF_FILTERS_LIST"; //$NON-NLS-1$

private static final String DEFAULT_IGNORE_FILTERS = "java.*,javax.*"; //$NON-NLS-1$
private static final String DEFAULT_IGNORE_FILTERS = ""; //$NON-NLS-1$
private static CallHierarchy fgInstance;
private IJavaSearchScope fSearchScope;
private StringMatcher[] fFilters;
Expand Down
Loading

0 comments on commit 28cbc72

Please sign in to comment.