Java Code Examples for org.eclipse.jdt.ls.core.internal.JDTUtils#resolveTypeRoot()

The following examples show how to use org.eclipse.jdt.ls.core.internal.JDTUtils#resolveTypeRoot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * public visibility only for test purpose
 */
public static IJavaProject getJavaProjectFromUri(String uri) throws CoreException, URISyntaxException {
	ITypeRoot typeRoot = JDTUtils.resolveTypeRoot(uri);
	if (typeRoot != null) {
		IJavaProject javaProject = typeRoot.getJavaProject();
		if (javaProject == null) {
			throw new CoreException(new Status(IStatus.ERROR, IConstants.PLUGIN_ID, "Given URI does not belong to an existing Java project."));
		}
		return javaProject;
	}

	// check for project root uri
	IContainer[] containers = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(new URI(uri));
	if (containers == null || containers.length == 0) {
		throw new CoreException(new Status(IStatus.ERROR, IConstants.PLUGIN_ID, "Given URI does not belong to any Java project."));
	}

	// For multi-module scenario
	Arrays.sort(containers, (Comparator<IContainer>) (IContainer a, IContainer b) -> {
		return a.getFullPath().toPortableString().length() - b.getFullPath().toPortableString().length();
	});

	IJavaElement targetElement = null;
	for (IContainer container : containers) {
		targetElement = JavaCore.create(container);
		if (targetElement != null) {
			break;
		}
	}

	if (targetElement == null || targetElement.getJavaProject() == null) {
		throw new CoreException(new Status(IStatus.ERROR, IConstants.PLUGIN_ID, "Given URI does not belong to any Java project."));
	}

	return targetElement.getJavaProject();
}
 
Example 2
Source File: DocumentSymbolHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<Either<SymbolInformation, DocumentSymbol>> documentSymbol(DocumentSymbolParams params, IProgressMonitor monitor) {

		ITypeRoot unit = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri());
		if (unit == null) {
			return Collections.emptyList();
		}

		if (hierarchicalDocumentSymbolSupported) {
			List<DocumentSymbol> symbols = this.getHierarchicalOutline(unit, monitor);
			return symbols.stream().map(Either::<SymbolInformation, DocumentSymbol>forRight).collect(toList());
		} else {
			SymbolInformation[] elements = this.getOutline(unit, monitor);
			return Arrays.asList(elements).stream().map(Either::<SymbolInformation, DocumentSymbol>forLeft).collect(toList());
		}
	}
 
Example 3
Source File: FindLinksHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static List<? extends Location> findLinks(String linkType, TextDocumentPositionParams position, IProgressMonitor monitor) {
	if (monitor.isCanceled()) {
		return Collections.emptyList();
	}
	ITypeRoot unit = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());
	if (unit != null && !monitor.isCanceled()) {
		PreferenceManager preferenceManager = JavaLanguageServerPlugin.getInstance().getPreferencesManager();
		try {
			IJavaElement element = JDTUtils.findElementAtSelection(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), preferenceManager, monitor);
			if (!monitor.isCanceled() && Objects.equals(linkType, "superImplementation")) {
				IMethod overriddenMethod = findOverriddenMethod(element, monitor);
				if (!monitor.isCanceled() && overriddenMethod != null) {
					Location location = NavigateToDefinitionHandler.computeDefinitionNavigation(overriddenMethod, element.getJavaProject());
					if (!monitor.isCanceled() && location != null) {
						String declaringTypeName = overriddenMethod.getDeclaringType().getFullyQualifiedName();
						String methodName = overriddenMethod.getElementName();
						String displayName = declaringTypeName + "." + methodName;
						return Collections.singletonList(new LinkLocation(displayName, "method", location));
					}
				}
			}
		} catch (JavaModelException e) {
			// do nothing
		}
	}

	return Collections.emptyList();
}
 
Example 4
Source File: HoverHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public Hover hover(TextDocumentPositionParams position, IProgressMonitor monitor) {
	ITypeRoot unit = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());

	List<Either<String, MarkedString>> content = null;
	if (unit != null && !monitor.isCanceled()) {
		content = computeHover(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), monitor);
	} else {
		content = Collections.singletonList(Either.forLeft(""));
	}
	Hover $ = new Hover();
	$.setContents(content);
	return $;
}
 
Example 5
Source File: NavigateToTypeDefinitionHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<? extends Location> typeDefinition(TextDocumentPositionParams position, IProgressMonitor monitor) {
	ITypeRoot unit = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());
	Location location = null;
	if (unit != null && !monitor.isCanceled()) {
		location = computeTypeDefinitionNavigation(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), monitor);
	}
	return location == null ? null : Arrays.asList(location);
}
 
Example 6
Source File: FoldingRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<FoldingRange> foldingRange(FoldingRangeRequestParams params, IProgressMonitor monitor) {
	List<FoldingRange> $ = new ArrayList<>();
	ITypeRoot unit = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri());
	if (unit == null) {
		return $;
	}
	computeFoldingRanges($, unit, monitor);
	return $;
}
 
Example 7
Source File: NavigateToDefinitionHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public List<? extends Location> definition(TextDocumentPositionParams position, IProgressMonitor monitor) {
	if (monitor.isCanceled()) {
		return Collections.emptyList();
	}
	ITypeRoot unit = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());
	Location location = null;
	if (unit != null && !monitor.isCanceled()) {
		location = computeDefinitionNavigation(unit, position.getPosition().getLine(),
				position.getPosition().getCharacter(), monitor);
	}
	return location == null || monitor.isCanceled() ? Collections.emptyList() : Arrays.asList(location);
}
 
Example 8
Source File: DocumentHighlightHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public List<? extends DocumentHighlight> documentHighlight(TextDocumentPositionParams position, IProgressMonitor monitor) {
	ITypeRoot type = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());
	return computeOccurrences(type, position.getPosition().getLine(),
			position.getPosition().getCharacter(), monitor);
}