use of org.eclipse.lsp4j.TypeHierarchyItem in project eclipse.jdt.ls by eclipse.
the class TypeHierarchyCommandTest method testSubTypeHierarchy.
@Test
public void testSubTypeHierarchy() throws Exception {
IProgressMonitor monitor = new NullProgressMonitor();
TypeHierarchyParams params = new TypeHierarchyParams();
String uriString = fJProject.getFile("src/main/java/org/sample/CallHierarchy.java").getLocationURI().toString();
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uriString);
Position position = new Position(2, 43);
params.setTextDocument(identifier);
params.setResolve(2);
params.setDirection(TypeHierarchyDirection.Children);
params.setPosition(position);
TypeHierarchyItem item = fCommand.typeHierarchy(params, monitor);
assertNotNull(item);
assertEquals(item.getName(), "Builder");
assertNull(item.getParents());
assertEquals(item.getChildren().size(), 9);
for (TypeHierarchyItem child : item.getChildren()) {
List<TypeHierarchyItem> subChild = child.getChildren();
assertNotNull(subChild);
if (subChild.size() == 1) {
assertEquals(subChild.get(0).getName(), "ReflectionToStringBuilder");
}
}
}
use of org.eclipse.lsp4j.TypeHierarchyItem in project eclipse.jdt.ls by eclipse.
the class TypeHierarchyCommand method getTypeHierarchy.
private TypeHierarchyItem getTypeHierarchy(String uri, Position position, TypeHierarchyDirection direction, int resolve, TypeHierarchyItem itemInput, IProgressMonitor monitor) {
if (uri == null || position == null || direction == null) {
return null;
}
try {
IType type = null;
if (itemInput == null) {
type = getType(uri, position, monitor);
} else {
String handleIdentifier = JSONUtility.toModel(itemInput.getData(), String.class);
IJavaElement element = JavaCore.create(handleIdentifier);
if (element instanceof IType) {
type = ((IType) element);
} else if (element instanceof IOrdinaryClassFile) {
type = ((IOrdinaryClassFile) element).getType();
} else {
return null;
}
}
TypeHierarchyItem item = TypeHierarchyCommand.toTypeHierarchyItem(type);
if (item == null) {
return null;
}
resolve(item, type, direction, resolve, monitor);
return item;
} catch (JavaModelException e) {
return null;
}
}
use of org.eclipse.lsp4j.TypeHierarchyItem in project eclipse.jdt.ls by eclipse.
the class TypeHierarchyCommand method resolve.
private void resolve(TypeHierarchyItem item, IType type, TypeHierarchyDirection direction, int resolve, IProgressMonitor monitor) throws JavaModelException {
if (monitor.isCanceled() || resolve <= 0) {
return;
}
ITypeHierarchy typeHierarchy = (direction == TypeHierarchyDirection.Parents) ? type.newSupertypeHierarchy(DefaultWorkingCopyOwner.PRIMARY, monitor) : type.newTypeHierarchy(type.getJavaProject(), DefaultWorkingCopyOwner.PRIMARY, monitor);
if (direction == TypeHierarchyDirection.Children || direction == TypeHierarchyDirection.Both) {
List<TypeHierarchyItem> childrenItems = new ArrayList<TypeHierarchyItem>();
IType[] children = typeHierarchy.getSubtypes(type);
for (IType childType : children) {
TypeHierarchyItem childItem = TypeHierarchyCommand.toTypeHierarchyItem(childType);
if (childItem == null) {
continue;
}
resolve(childItem, childType, direction, resolve - 1, monitor);
childrenItems.add(childItem);
}
item.setChildren(childrenItems);
}
if (direction == TypeHierarchyDirection.Parents || direction == TypeHierarchyDirection.Both) {
List<TypeHierarchyItem> parentsItems = new ArrayList<TypeHierarchyItem>();
IType[] parents = typeHierarchy.getSupertypes(type);
for (IType parentType : parents) {
TypeHierarchyItem parentItem = TypeHierarchyCommand.toTypeHierarchyItem(parentType);
if (parentItem == null) {
continue;
}
resolve(parentItem, parentType, direction, resolve - 1, monitor);
parentsItems.add(parentItem);
}
item.setParents(parentsItems);
}
}
use of org.eclipse.lsp4j.TypeHierarchyItem in project eclipse.jdt.ls by eclipse.
the class TypeHierarchyCommand method toTypeHierarchyItem.
private static TypeHierarchyItem toTypeHierarchyItem(IType type) throws JavaModelException {
if (type == null) {
return null;
}
Location location = getLocation(type, LocationType.FULL_RANGE);
Location selectLocation = getLocation(type, LocationType.NAME_RANGE);
if (location == null || selectLocation == null) {
return null;
}
TypeHierarchyItem item = new TypeHierarchyItem();
item.setRange(location.getRange());
item.setUri(location.getUri());
item.setSelectionRange(selectLocation.getRange());
String fullyQualifiedName = type.getFullyQualifiedName();
int index = fullyQualifiedName.lastIndexOf('.');
if (index >= 1 && index < fullyQualifiedName.length() - 1 && !type.isAnonymous()) {
item.setName(fullyQualifiedName.substring(index + 1));
item.setDetail(fullyQualifiedName.substring(0, index));
} else {
item.setName(JDTUtils.getName(type));
IPackageFragment packageFragment = type.getPackageFragment();
if (packageFragment != null) {
item.setDetail(packageFragment.getElementName());
}
}
item.setKind(DocumentSymbolHandler.mapKind(type));
item.setDeprecated(JDTUtils.isDeprecated(type));
item.setData(type.getHandleIdentifier());
return item;
}
use of org.eclipse.lsp4j.TypeHierarchyItem in project eclipse.jdt.ls by eclipse.
the class JDTDelegateCommandHandler method executeCommand.
/* (non-Javadoc)
* @see org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler#executeCommand(java.lang.String, java.util.List, org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception {
if (!StringUtils.isBlank(commandId)) {
switch(commandId) {
case "java.edit.organizeImports":
final OrganizeImportsCommand c = new OrganizeImportsCommand();
final Object result = c.organizeImports(arguments);
final boolean applyNow = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isWorkspaceApplyEditSupported();
if (applyNow) {
JavaLanguageServerPlugin.getInstance().getClientConnection().applyWorkspaceEdit((WorkspaceEdit) result);
// return an empty object to avoid errors on client
return new Object();
} else {
// workspaceEdit on the custom command.
return result;
}
case "java.edit.stringFormatting":
FormatterHandler handler = new FormatterHandler(JavaLanguageServerPlugin.getPreferencesManager());
return handler.stringFormatting((String) arguments.get(0), JSONUtility.toModel(arguments.get(1), Map.class), Integer.parseInt((String) arguments.get(2)), monitor);
case "java.project.resolveSourceAttachment":
return SourceAttachmentCommand.resolveSourceAttachment(arguments, monitor);
case "java.project.updateSourceAttachment":
return SourceAttachmentCommand.updateSourceAttachment(arguments, monitor);
case "java.project.addToSourcePath":
String sourceFolder = (String) arguments.get(0);
return BuildPathCommand.addToSourcePath(sourceFolder);
case "java.project.removeFromSourcePath":
String sourceFolder1 = (String) arguments.get(0);
return BuildPathCommand.removeFromSourcePath(sourceFolder1);
case "java.project.listSourcePaths":
return BuildPathCommand.listSourcePaths();
case "java.project.getSettings":
return ProjectCommand.getProjectSettings((String) arguments.get(0), (ArrayList<String>) arguments.get(1));
case "java.project.getClasspaths":
return ProjectCommand.getClasspaths((String) arguments.get(0), JSONUtility.toModel(arguments.get(1), ClasspathOptions.class));
case "java.project.isTestFile":
return ProjectCommand.isTestFile((String) arguments.get(0));
case "java.project.getAll":
return ProjectCommand.getAllJavaProjects();
case "java.project.refreshDiagnostics":
return DiagnosticsCommand.refreshDiagnostics((String) arguments.get(0), (String) arguments.get(1), (boolean) arguments.get(2));
case "java.project.import":
ProjectCommand.importProject(monitor);
return null;
case "java.project.resolveStackTraceLocation":
List<String> projectNames = null;
if (arguments.size() > 1) {
projectNames = (ArrayList<String>) arguments.get(1);
}
return ResolveSourceMappingHandler.resolveStackTraceLocation((String) arguments.get(0), projectNames);
case "java.navigate.resolveTypeHierarchy":
TypeHierarchyCommand resolveTypeHierarchyCommand = new TypeHierarchyCommand();
TypeHierarchyItem toResolve = JSONUtility.toModel(arguments.get(0), TypeHierarchyItem.class);
TypeHierarchyDirection resolveDirection = TypeHierarchyDirection.forValue(JSONUtility.toModel(arguments.get(1), Integer.class));
int resolveDepth = JSONUtility.toModel(arguments.get(2), Integer.class);
ResolveTypeHierarchyItemParams resolveParams = new ResolveTypeHierarchyItemParams();
resolveParams.setItem(toResolve);
resolveParams.setDirection(resolveDirection);
resolveParams.setResolve(resolveDepth);
TypeHierarchyItem resolvedItem = resolveTypeHierarchyCommand.resolveTypeHierarchy(resolveParams, monitor);
return resolvedItem;
case "java.navigate.openTypeHierarchy":
TypeHierarchyCommand typeHierarchyCommand = new TypeHierarchyCommand();
TypeHierarchyParams params = new TypeHierarchyParams();
TextDocumentPositionParams textParams = JSONUtility.toModel(arguments.get(0), TextDocumentPositionParams.class);
TypeHierarchyDirection direction = TypeHierarchyDirection.forValue(JSONUtility.toModel(arguments.get(1), Integer.class));
int resolve = JSONUtility.toModel(arguments.get(2), Integer.class);
params.setResolve(resolve);
params.setDirection(direction);
params.setTextDocument(textParams.getTextDocument());
params.setPosition(textParams.getPosition());
TypeHierarchyItem typeHierarchyItem = typeHierarchyCommand.typeHierarchy(params, monitor);
return typeHierarchyItem;
case "java.project.upgradeGradle":
String projectUri = (String) arguments.get(0);
return GradleProjectImporter.upgradeGradleVersion(projectUri, monitor);
default:
break;
}
}
throw new UnsupportedOperationException(String.format("Java language server doesn't support the command '%s'.", commandId));
}
Aggregations