/** * Creates a location for a given java element. * Element can be a {@link ICompilationUnit} or {@link IClassFile} * * @param element * @return location or null * @throws JavaModelException */ public static Location toLocation(IJavaElement element) throws JavaModelException { return toLocation(element, LocationType.NAME_RANGE); }
/** * Creates a default location for the class file. * * @param classFile * @return location * @throws JavaModelException */ public static Location toLocation(IClassFile classFile) throws JavaModelException{ return toLocation(classFile, 0, 0); }
private Range getRange(IJavaElement element) throws JavaModelException { Location location = JDTUtils.toLocation(element, FULL_RANGE); return location == null ? DEFAULT_RANGE : location.getRange(); }
private Range getSelectionRange(IJavaElement element) throws JavaModelException { Location location = JDTUtils.toLocation(element); return location == null ? DEFAULT_RANGE : location.getRange(); }
if (useDefaultPosition || offset > 0 && position > 0) { location = toLocation(compilationUnit, offset, position); } else { location = toLocation(element); if (useDefaultPosition || offset > 0 && position > 0) { location = toLocation(cf, offset, position); } else { location = toLocation(element); if (location == null) {//If no source was attached, return default location location = toLocation(cf, 0, 0);
@Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object o = match.getElement(); if (o instanceof IJavaElement) { IJavaElement element = (IJavaElement) o; ICompilationUnit compilationUnit = (ICompilationUnit) element .getAncestor(IJavaElement.COMPILATION_UNIT); if (compilationUnit == null) { return; } Location location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength()); result.add(location); } } }, monitor);
Location locationToElement = JDTUtils.toLocation(linkTarget); if (locationToElement != null) { return locationToElement.getUri() + "#" + (locationToElement.getRange().getStart().getLine() + 1);
@Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object o = match.getElement(); if (o instanceof IJavaElement) { IJavaElement element = (IJavaElement) o; ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); Location location = null; if (compilationUnit != null) { location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength()); } else if (includeClassFiles) { IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (cf != null && cf.getSourceRange() != null) { location = JDTUtils.toLocation(cf, match.getOffset(), match.getLength()); } } if (location != null) { locations.add(location); } } } }, monitor);
/** * Creates a location for a given java element. Unlike {@link #toLocation} this * method can be called to return with a range that contains surrounding * comments (method body), not just the name of the Java element. Element can be * a {@link ICompilationUnit} or {@link IClassFile} * * @param element * @param type the range type. The {@link LocationType#NAME_RANGE name} or {@link LocationType#FULL_RANGE full} range. * @return location or null * @throws JavaModelException */ public static Location toLocation(IJavaElement element, LocationType type) throws JavaModelException { ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (unit == null && cf == null) { return null; } if (element instanceof ISourceReference) { ISourceRange nameRange = type.getRange(element); if (SourceRange.isAvailable(nameRange)) { if (cf == null) { return toLocation(unit, nameRange.getOffset(), nameRange.getLength()); } else { return toLocation(cf, nameRange.getOffset(), nameRange.getLength()); } } } return null; }
private Location computeDefinitionNavigation(ITypeRoot unit, int line, int column, IProgressMonitor monitor) { try { IJavaElement element = JDTUtils.findElementAtSelection(unit, line, column, this.preferenceManager, monitor); if (element == null) { return null; } ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (compilationUnit != null || (cf != null && cf.getSourceRange() != null) ) { return fixLocation(element, JDTUtils.toLocation(element), unit.getJavaProject()); } if (element instanceof IMember && ((IMember) element).getClassFile() != null) { return fixLocation(element, JDTUtils.toLocation(((IMember) element).getClassFile()), unit.getJavaProject()); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem computing definition for" + unit.getElementName(), e); } return null; }
@Override public void acceptTypeNameMatch(TypeNameMatch match) { SymbolInformation symbolInformation = new SymbolInformation(); symbolInformation.setContainerName(match.getTypeContainerName()); symbolInformation.setName(match.getSimpleTypeName()); symbolInformation.setKind(mapKind(match)); Location location; try { if (match.getType().isBinary()) { location = JDTUtils.toLocation(match.getType().getClassFile()); } else { location = JDTUtils.toLocation(match.getType()); } } catch (Exception e) { JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e); return; } symbolInformation.setLocation(location); symbols.add(symbolInformation); }
Location location = JDTUtils.toLocation(element); if (location != null) { SymbolInformation si = new SymbolInformation();
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (compilationUnit != null || (cf != null && cf.getSourceRange() != null)) { return JDTUtils.toLocation(element); return JDTUtils.toLocation(((IMember) element).getClassFile());