use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class RenameAnalyzeUtil method getOldSourceRange.
private static ISourceRange getOldSourceRange(SearchMatch newMatch) {
// cannot transfom offset in preview to offset in original -> just show enclosing method
IJavaElement newMatchElement = (IJavaElement) newMatch.getElement();
IJavaElement primaryElement = newMatchElement.getPrimaryElement();
ISourceRange range = null;
if (primaryElement.exists() && primaryElement instanceof ISourceReference) {
try {
range = ((ISourceReference) primaryElement).getSourceRange();
} catch (JavaModelException e) {
// can live without source range
}
}
return range;
}
use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class RenameFieldProcessor method addDeclarationUpdate.
private void addDeclarationUpdate() throws CoreException {
ISourceRange nameRange = fField.getNameRange();
TextEdit textEdit = new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName());
ICompilationUnit cu = fField.getCompilationUnit();
String groupName = RefactoringCoreMessages.RenameFieldRefactoring_Update_field_declaration;
addTextEdit(fChangeManager.get(cu), groupName, textEdit);
}
use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class RenameLocalVariableProcessor method initAST.
private void initAST() {
if (!fIsComposite)
fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, null);
ISourceRange sourceRange = fLocalVariable.getNameRange();
ASTNode name = NodeFinder.perform(fCompilationUnitNode, sourceRange);
if (name == null)
return;
if (name.getParent() instanceof VariableDeclaration)
fTempDeclarationNode = (VariableDeclaration) name.getParent();
}
use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class SelectionConverter method resolveEnclosingElement.
// public static IJavaElement getElementAtOffset(ITypeRoot input, ITextSelection selection) throws JavaModelException {
// if (input instanceof ICompilationUnit) {
// JavaModelUtil.reconcile((ICompilationUnit)input);
// }
// IJavaElement ref = input.getElementAt(selection.getOffset());
// if (ref == null)
// return input;
// return ref;
// }
//
// public static IJavaElement resolveEnclosingElement(JavaEditor editor, ITextSelection selection) throws JavaModelException {
// ITypeRoot input = getInput(editor);
// if (input != null)
// return resolveEnclosingElement(input, selection);
// return null;
// }
//
public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException {
IJavaElement atOffset = null;
if (input instanceof ICompilationUnit) {
ICompilationUnit cunit = (ICompilationUnit) input;
JavaModelUtil.reconcile(cunit);
atOffset = cunit.getElementAt(selection.getOffset());
} else if (input instanceof IClassFile) {
IClassFile cfile = (IClassFile) input;
atOffset = cfile.getElementAt(selection.getOffset());
} else {
return null;
}
if (atOffset == null) {
return input;
} else {
int selectionEnd = selection.getOffset() + selection.getLength();
IJavaElement result = atOffset;
if (atOffset instanceof ISourceReference) {
ISourceRange range = ((ISourceReference) atOffset).getSourceRange();
while (range.getOffset() + range.getLength() < selectionEnd) {
result = result.getParent();
if (!(result instanceof ISourceReference)) {
result = input;
break;
}
range = ((ISourceReference) result).getSourceRange();
}
}
return result;
}
}
use of org.eclipse.jdt.core.ISourceRange in project che 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));
}
Aggregations