Java Code Examples for org.eclipse.lsp4j.Location#setUri()

The following examples show how to use org.eclipse.lsp4j.Location#setUri() . 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: PsiUtilsImpl.java    From intellij-quarkus with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Location toLocation(PsiMember psiMember) {
    PsiElement sourceElement = psiMember.getNavigationElement();
    if (sourceElement != null) {
        PsiFile file = sourceElement.getContainingFile();
        Location location = new Location();
        location.setUri(VfsUtilCore.convertToURL(file.getVirtualFile().getUrl()).toExternalForm());
        Document document = PsiDocumentManager.getInstance(psiMember.getProject()).getDocument(file);
        TextRange range = sourceElement.getTextRange();
        int startLine = document.getLineNumber(range.getStartOffset());
        int startLineOffset = document.getLineStartOffset(startLine);
        int endLine = document.getLineNumber(range.getEndOffset());
        int endLineOffset = document.getLineStartOffset(endLine);
        location.setRange(new Range(LSPIJUtils.toPosition(range.getStartOffset(), document), LSPIJUtils.toPosition(range.getEndOffset(), document)));
        return location;
    }
    return null;
}
 
Example 2
Source File: NavigateToDefinitionHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static Location fixLocation(IJavaElement element, Location location, IJavaProject javaProject) {
	if (location == null) {
		return null;
	}
	if (!javaProject.equals(element.getJavaProject()) && element.getJavaProject().getProject().getName().equals(ProjectsManager.DEFAULT_PROJECT_NAME)) {
		// see issue at: https://github.com/eclipse/eclipse.jdt.ls/issues/842 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=541573
		// for jdk classes, jdt will reuse the java model by altering project to share the model between projects
		// so that sometimes the project for `element` is default project and the project is different from the project for `unit`
		// this fix is to replace the project name with non-default ones since default project should be transparent to users.
		if (location.getUri().contains(ProjectsManager.DEFAULT_PROJECT_NAME)) {
			String patched = StringUtils.replaceOnce(location.getUri(), ProjectsManager.DEFAULT_PROJECT_NAME, javaProject.getProject().getName());
			try {
				IClassFile cf = (IClassFile) JavaCore.create(JDTUtils.toURI(patched).getQuery());
				if (cf != null && cf.exists()) {
					location.setUri(patched);
				}
			} catch (Exception ex) {

			}
		}
	}
	return location;
}
 
Example 3
Source File: LanguageServerCompilerUtils.java    From vscode-as3mxml with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a compiler source location to a language server location. May
 * return null if the line or column of the source location is -1.
 */
public static Location getLocationFromSourceLocation(ISourceLocation sourceLocation)
{
    Path sourceLocationPath = Paths.get(sourceLocation.getSourcePath());
    Location location = new Location();
    location.setUri(sourceLocationPath.toUri().toString());

    Range range = getRangeFromSourceLocation(sourceLocation);
    if (range == null)
    {
        //this is probably generated by the compiler somehow
        return null;
    }
    location.setRange(range);

    return location;
}
 
Example 4
Source File: DocumentSymbolHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
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 (element.getParent() != null) {
				si.setContainerName(element.getParent().getElementName());
			}
			location.setUri(ResourceUtils.toClientUri(location.getUri()));
			si.setLocation(location);
			if (!symbols.contains(si)) {
				symbols.add(si);
			}
		}
	}
}
 
Example 5
Source File: DefinitionTextUtils.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
public Location toLocation()
{
    Location location = new Location();
    String escapedText = UrlEscapers.urlFragmentEscaper().escape(text);
    URI uri = URI.create("swc://" + path + "?" + escapedText);
    location.setUri(uri.toString());
    location.setRange(toRange());
    return location;
}
 
Example 6
Source File: DocumentSymbolService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.16
 */
protected SymbolInformation createSymbol(String uri, DocumentSymbol symbol,
		Function1<? super DocumentSymbol, ? extends String> containerNameProvider) {
	SymbolInformation symbolInformation = new SymbolInformation();
	symbolInformation.setName(symbol.getName());
	symbolInformation.setKind(symbol.getKind());
	symbolInformation.setDeprecated(symbol.getDeprecated());
	Location location = new Location();
	location.setUri(uri);
	location.setRange(symbol.getSelectionRange());
	symbolInformation.setLocation(location);
	symbolInformation.setContainerName(containerNameProvider.apply(symbol));
	return symbolInformation;
}