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

The following examples show how to use org.eclipse.jdt.ls.core.internal.JDTUtils#toLocation() . 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: CallHierarchyHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Gets the location of the Java {@code element} based on the desired
 * {@code locationType}.
 */
private Location getLocation(IJavaElement element, LocationType locationType) throws JavaModelException {
	Assert.isNotNull(element, "element");
	Assert.isNotNull(locationType, "locationType");

	Location location = locationType.toLocation(element);
	if (location == null && element instanceof IType) {
		IType type = (IType) element;
		ICompilationUnit unit = (ICompilationUnit) type.getAncestor(COMPILATION_UNIT);
		IClassFile classFile = (IClassFile) type.getAncestor(CLASS_FILE);
		if (unit != null || (classFile != null && classFile.getSourceRange() != null)) {
			location = locationType.toLocation(type);
		}
	}
	if (location == null && element instanceof IMember && ((IMember) element).getClassFile() != null) {
		location = JDTUtils.toLocation(((IMember) element).getClassFile());
	}
	return location;
}
 
Example 2
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 3
Source File: JavaElementLinks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Creates an {@link URI} with the given scheme based on the given element. The
 * additional arguments specify a member referenced from the given element.
 *
 * @param scheme
 *            a scheme
 * @param element
 *            the declaring element
 * @param refTypeName
 *            a (possibly qualified) type or package name, can be
 *            <code>null</code>
 * @param refMemberName
 *            a member name, can be <code>null</code>
 * @param refParameterTypes
 *            a (possibly empty) array of (possibly qualified) parameter type
 *            names, can be <code>null</code>
 * @return an {@link URI}, encoded as {@link URI#toASCIIString() ASCII} string,
 *         ready to be used as <code>href</code> attribute in an
 *         <code>&lt;a&gt;</code> tag
 * @throws URISyntaxException
 *             if the arguments were invalid
 */
public static String createURI(String scheme, IJavaElement element, String refTypeName, String refMemberName, String[] refParameterTypes, int startPosition) throws URISyntaxException {
	/*
	 * We use an opaque URI, not ssp and fragments (to work around Safari bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=212527 (wrongly encodes #)).
	 */

	StringBuffer ssp = new StringBuffer(60);
	ssp.append(LINK_SEPARATOR); // make sure first character is not a / (would be hierarchical URI)

	// replace '[' manually, since URI confuses it for an IPv6 address as per RFC 2732:
	ssp.append(element.getHandleIdentifier().toString().replace('[', LINK_BRACKET_REPLACEMENT)); // segments[1]

	if (refTypeName != null) {
		ssp.append(LINK_SEPARATOR);
		ssp.append(refTypeName); // segments[2]

		if (refMemberName != null) {
			ssp.append(LINK_SEPARATOR);
			ssp.append(refMemberName); // segments[3]

			if (refParameterTypes != null) {
				ssp.append(LINK_SEPARATOR);
				for (int i = 0; i < refParameterTypes.length; i++) {
					ssp.append(refParameterTypes[i]); // segments[4|5|..]
					if (i != refParameterTypes.length - 1) {
						ssp.append(LINK_SEPARATOR);
					}
				}
			}
		}
	}

	URI javadocURI = new URI("eclipse-javadoc", ssp.toString(), null);

	IJavaElement linkTarget = parseURI(javadocURI);
	if (linkTarget == null) {
		return "";
	}

	try {
		Location locationToElement = JDTUtils.toLocation(linkTarget);
		if (locationToElement != null) {
			return locationToElement.getUri() + "#" + (locationToElement.getRange().getStart().getLine() + 1);
		}
	} catch (JavaModelException e) {

	}

	return "";
}
 
Example 4
Source File: DocumentSymbolHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private Range getRange(IJavaElement element) throws JavaModelException {
	Location location = JDTUtils.toLocation(element, FULL_RANGE);
	return location == null ? DEFAULT_RANGE : location.getRange();
}
 
Example 5
Source File: DocumentSymbolHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private Range getSelectionRange(IJavaElement element) throws JavaModelException {
	Location location = JDTUtils.toLocation(element);
	return location == null ? DEFAULT_RANGE : location.getRange();
}
 
Example 6
Source File: NavigateToDefinitionHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private Location computeBreakContinue(ITypeRoot typeRoot, int line, int column) throws CoreException {
	int offset = JsonRpcHelpers.toOffset(typeRoot.getBuffer(), line, column);
	if (offset >= 0) {
		CompilationUnit unit = SharedASTProviderCore.getAST(typeRoot, SharedASTProviderCore.WAIT_YES, null);
		if (unit == null) {
			return null;
		}
		ASTNode selectedNode = NodeFinder.perform(unit, offset, 0);
		ASTNode node = null;
		SimpleName label = null;
		if (selectedNode instanceof BreakStatement) {
			node = selectedNode;
			label = ((BreakStatement) node).getLabel();
		} else if (selectedNode instanceof ContinueStatement) {
			node = selectedNode;
			label = ((ContinueStatement) node).getLabel();
		} else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof BreakStatement) {
			node = selectedNode.getParent();
			label = ((BreakStatement) node).getLabel();
		} else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof ContinueStatement) {
			node = selectedNode.getParent();
			label = ((ContinueStatement) node).getLabel();
		}
		if (node != null) {
			ASTNode parent = node.getParent();
			ASTNode target = null;
			while (parent != null) {
				if (parent instanceof MethodDeclaration || parent instanceof Initializer) {
					break;
				}
				if (label == null) {
					if (parent instanceof ForStatement || parent instanceof EnhancedForStatement || parent instanceof WhileStatement || parent instanceof DoStatement) {
						target = parent;
						break;
					}
					if (node instanceof BreakStatement) {
						if (parent instanceof SwitchStatement || parent instanceof SwitchExpression) {
							target = parent;
							break;
						}
					}
					if (node instanceof LabeledStatement) {
						target = parent;
						break;
					}
				} else if (LabeledStatement.class.isInstance(parent)) {
					LabeledStatement ls = (LabeledStatement) parent;
					if (ls.getLabel().getIdentifier().equals(label.getIdentifier())) {
						target = ls;
						break;
					}
				}
				parent = parent.getParent();
			}
			if (target != null) {
				int start = target.getStartPosition();
				int end = new TokenScanner(unit.getTypeRoot()).getNextEndOffset(node.getStartPosition(), true) - start;
				if (start >= 0 && end >= 0) {
					return JDTUtils.toLocation((ICompilationUnit) typeRoot, start, end);
				}
			}
		}
	}
	return null;
}