Search in sources :

Example 31 with ISourceRange

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 32 with ISourceRange

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 33 with ISourceRange

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();
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 34 with ISourceRange

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;
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IClassFile(org.eclipse.jdt.core.IClassFile) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 35 with ISourceRange

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));
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) CoreException(org.eclipse.core.runtime.CoreException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SourceRange(org.eclipse.jdt.core.SourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ISourceRange (org.eclipse.jdt.core.ISourceRange)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)18 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 JavaModelException (org.eclipse.jdt.core.JavaModelException)13 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)10 ISourceReference (org.eclipse.jdt.core.ISourceReference)9 IType (org.eclipse.jdt.core.IType)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 SourceRange (org.eclipse.jdt.core.SourceRange)6 IBuffer (org.eclipse.jdt.core.IBuffer)5 IMethod (org.eclipse.jdt.core.IMethod)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 ArrayList (java.util.ArrayList)4 IField (org.eclipse.jdt.core.IField)4 IMember (org.eclipse.jdt.core.IMember)4 CoreException (org.eclipse.core.runtime.CoreException)3 IClassFile (org.eclipse.jdt.core.IClassFile)3