use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.
the class StatementAnalyzer method checkSelectedNodes.
protected void checkSelectedNodes() {
ASTNode[] nodes = getSelectedNodes();
if (nodes.length == 0) {
return;
}
ASTNode node = nodes[0];
int selectionOffset = getSelection().getOffset();
try {
int start = fScanner.getNextStartOffset(selectionOffset, true);
if (start == node.getStartPosition()) {
int lastNodeEnd = ASTNodes.getExclusiveEnd(nodes[nodes.length - 1]);
int pos = fScanner.getNextStartOffset(lastNodeEnd, true);
int selectionEnd = getSelection().getInclusiveEnd();
if (pos <= selectionEnd) {
IScanner scanner = fScanner.getScanner();
// see https://bugs.eclipse.org/324237
char[] token = scanner.getCurrentTokenSource();
if (start < lastNodeEnd && token.length == 1 && (token[0] == ';' || token[0] == ',')) {
setSelection(Selection.createFromStartEnd(start, lastNodeEnd - 1));
} else {
ISourceRange range = new SourceRange(lastNodeEnd, pos - lastNodeEnd);
invalidSelection(RefactoringCoreMessages.StatementAnalyzer_end_of_selection, JavaStatusContext.create(fCUnit, range));
}
}
// success
return;
}
} catch (CoreException e) {
// fall through
}
ISourceRange range = new SourceRange(selectionOffset, node.getStartPosition() - selectionOffset + 1);
invalidSelection(RefactoringCoreMessages.StatementAnalyzer_beginning_of_selection, JavaStatusContext.create(fCUnit, range));
}
use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.
the class JDTUtils method toLocation.
/**
* 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 {
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 = getNameRange(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;
}
use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.
the class JDTUtils method getNameRange.
private static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
ISourceRange nameRange = null;
if (element instanceof IMember) {
IMember member = (IMember) element;
nameRange = member.getNameRange();
if ((!SourceRange.isAvailable(nameRange))) {
nameRange = member.getSourceRange();
}
} else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
nameRange = ((ISourceReference) element).getNameRange();
} else if (element instanceof ISourceReference) {
nameRange = ((ISourceReference) element).getSourceRange();
}
if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
nameRange = getNameRange(element.getParent());
}
return nameRange;
}
use of org.eclipse.jdt.core.ISourceRange in project eclipse.platform.runtime by eclipse.
the class FindUnusedSearchResult method unusedElementFound.
@Override
public void unusedElementFound(IMember member) throws CoreException {
ISourceRange nameRange = member.getNameRange();
addMatch(new Match(member, nameRange.getOffset(), nameRange.getLength()));
}
use of org.eclipse.jdt.core.ISourceRange in project flux by eclipse.
the class NavigationService method computeNavigation.
public JSONObject computeNavigation(String username, String requestorResourcePath, int offset, int length) {
try {
ICompilationUnit liveEditUnit = liveEditUnits.getLiveEditUnit(username, requestorResourcePath);
if (liveEditUnit != null) {
IJavaElement[] elements = liveEditUnit.codeSelect(offset, length);
if (elements != null && elements.length > 0) {
JSONObject result = new JSONObject();
IJavaElement element = elements[0];
IResource resource = element.getResource();
// if the selected element corresponds to a resource in workspace, navigate to it
if (resource != null && resource.getProject() != null) {
String projectName = resource.getProject().getName();
String resourcePath = resource.getProjectRelativePath().toString();
result.put("project", projectName);
result.put("resource", resourcePath);
if (element instanceof ISourceReference) {
ISourceRange nameRange = ((ISourceReference) element).getNameRange();
result.put("offset", nameRange.getOffset());
result.put("length", nameRange.getLength());
}
return result;
}
// walk up the java model until we reach a class file
while (element != null && !(element instanceof IClassFile)) {
element = element.getParent();
}
if (element instanceof IClassFile) {
IClassFile classFile = (IClassFile) element;
ISourceRange sourceRange = classFile.getSourceRange();
if (sourceRange != null) {
String projectName = element.getJavaProject().getProject().getName();
String resourcePath = classFile.getParent().getElementName().replace('.', '/');
resourcePath = "classpath:/" + resourcePath + "/" + classFile.getElementName();
result.put("project", projectName);
result.put("resource", resourcePath);
return result;
}
}
// we don't know how to navigate to this element
}
}
} catch (JavaModelException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
Aggregations