Search in sources :

Example 71 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project che by eclipse.

the class RenameFieldProcessor method addAccessorOccurrences.

private void addAccessorOccurrences(IProgressMonitor pm, IMethod accessor, String editName, String newAccessorName, RefactoringStatus status) throws CoreException {
    Assert.isTrue(accessor.exists());
    IJavaSearchScope scope = RefactoringScopeFactory.create(accessor);
    SearchPattern pattern = SearchPattern.createPattern(accessor, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    SearchResultGroup[] groupedResults = RefactoringSearchEngine.search(pattern, scope, new MethodOccurenceCollector(accessor.getElementName()), pm, status);
    for (int i = 0; i < groupedResults.length; i++) {
        ICompilationUnit cu = groupedResults[i].getCompilationUnit();
        if (cu == null)
            continue;
        SearchMatch[] results = groupedResults[i].getSearchResults();
        for (int j = 0; j < results.length; j++) {
            SearchMatch searchResult = results[j];
            TextEdit edit = new ReplaceEdit(searchResult.getOffset(), searchResult.getLength(), newAccessorName);
            addTextEdit(fChangeManager.get(cu), editName, edit);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) TextEdit(org.eclipse.text.edits.TextEdit) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)

Example 72 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project che by eclipse.

the class RenameMethodProcessor method simpleUpdate.

private void simpleUpdate(SearchMatch element, ICompilationUnit cu, TextChange textChange) {
    String editName = RefactoringCoreMessages.RenameMethodRefactoring_update_occurrence;
    ReplaceEdit replaceEdit = createReplaceEdit(element, cu);
    addTextEdit(textChange, editName, replaceEdit);
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 73 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project che by eclipse.

the class RenameTypeProcessor method addReferenceUpdates.

private void addReferenceUpdates(TextChangeManager manager, IProgressMonitor pm) {
    //$NON-NLS-1$
    pm.beginTask("", fReferences.length);
    for (int i = 0; i < fReferences.length; i++) {
        ICompilationUnit cu = fReferences[i].getCompilationUnit();
        if (cu == null)
            continue;
        String name = RefactoringCoreMessages.RenameTypeRefactoring_update_reference;
        SearchMatch[] results = fReferences[i].getSearchResults();
        for (int j = 0; j < results.length; j++) {
            SearchMatch match = results[j];
            ReplaceEdit replaceEdit = new ReplaceEdit(match.getOffset(), match.getLength(), getNewElementName());
            TextChangeCompatibility.addTextEdit(manager.get(cu), name, replaceEdit, CATEGORY_TYPE_RENAME);
        }
        pm.worked(1);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 74 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method replaceLineCommentBeforeJavaElement.

private void replaceLineCommentBeforeJavaElement(List<TextEdit> commentEdits, LineComment lineComment, List<LineComment> lineComments, int i, String source, TreeSet<Integer> lineStarts) {
    final int replaceLength = "//".length();
    final boolean isFirst = i == 0;
    String replacementText;
    final SourceLocation indentLoc = getIndentForJavadoc(lineComment, source, lineStarts);
    if (isFirst) {
        // TODO JNR how to obey configured indentation?
        replacementText = "/**" + lineSeparator + indentLoc.substring(source) + " *";
    } else {
        replacementText = " *";
    }
    final boolean commentStartsWithSlash = source.charAt(lineComment.getStartPosition() + replaceLength) == '/';
    if (commentStartsWithSlash) {
        replacementText += " ";
    }
    commentEdits.add(new ReplaceEdit(lineComment.getStartPosition(), replaceLength, replacementText));
    replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
    final boolean isLast = i == lineComments.size() - 1;
    if (isLast) {
        // TODO JNR how to obey configured indentation?
        final int position = getEndPosition(lineComment);
        commentEdits.add(new InsertEdit(position, lineSeparator + indentLoc.substring(source) + " */"));
    }
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 75 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method addSingleLineCommentToJavadocEdits.

private void addSingleLineCommentToJavadocEdits(List<TextEdit> commentEdits, ASTNode nextNode, List<LineComment> lineComments, String source, TreeSet<Integer> lineStarts) {
    final int nodeStart = nextNode.getStartPosition();
    final LineComment lineComment = lineComments.get(0);
    // TODO JNR how to obey configured indentation?
    // TODO JNR how to obey configured line length?
    final int commentStart = lineComment.getStartPosition();
    if (commentStart < nodeStart) {
        // assume comment is situated exactly before target node for javadoc
        final String spaceAtStart = getSpaceAtStart(source, lineComment);
        commentEdits.add(new ReplaceEdit(commentStart, "//".length(), "/**" + spaceAtStart));
        commentEdits.add(new InsertEdit(getEndPosition(lineComment), getSpaceAtEnd(source, lineComment) + "*/"));
        replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
    } else {
        // assume comment is situated exactly after target node for javadoc
        final StringBuilder newJavadoc = new StringBuilder().append("/**").append(getSpaceAtStart(source, lineComment));
        appendCommentTextReplaceEndsOfBlockComment(newJavadoc, lineComment, source);
        SourceLocation indent = getIndent(nextNode, lineStarts);
        newJavadoc.append(getSpaceAtEnd(source, lineComment)).append("*/").append(lineSeparator).append(source, indent.getStartPosition(), indent.getEndPosition());
        commentEdits.add(new InsertEdit(nodeStart, newJavadoc.toString()));
        deleteLineCommentAfterNode(commentEdits, source, lineComment);
    }
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) LineComment(org.eclipse.jdt.core.dom.LineComment)

Aggregations

ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)131 TextEdit (org.eclipse.text.edits.TextEdit)53 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)43 Test (org.junit.Test)34 InsertEdit (org.eclipse.text.edits.InsertEdit)25 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)22 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)15 BadLocationException (org.eclipse.jface.text.BadLocationException)14 IDocument (org.eclipse.jface.text.IDocument)14 DeleteEdit (org.eclipse.text.edits.DeleteEdit)13 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)13 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)11 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)10 ArrayList (java.util.ArrayList)9 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)8 TextChange (org.eclipse.ltk.core.refactoring.TextChange)8 List (java.util.List)7 Document (org.eclipse.jface.text.Document)7