Search in sources :

Example 51 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.

the class RenameNodeCorrectionProposal method addEdits.

@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
    super.addEdits(doc, root);
    // build a full AST
    CompilationUnit unit = CoreASTProvider.getInstance().getAST(getCompilationUnit(), CoreASTProvider.WAIT_YES, null);
    ASTNode name = NodeFinder.perform(unit, fOffset, fLength);
    if (name instanceof SimpleName) {
        SimpleName[] names = LinkedNodeFinder.findByProblems(unit, (SimpleName) name);
        if (names != null) {
            for (int i = 0; i < names.length; i++) {
                SimpleName curr = names[i];
                root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName));
            }
            return;
        }
    }
    root.addChild(new ReplaceEdit(fOffset, fLength, fNewName));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 52 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.

the class ReplaceCorrectionProposal method addEdits.

@Override
protected void addEdits(IDocument doc, TextEdit rootEdit) throws CoreException {
    super.addEdits(doc, rootEdit);
    TextEdit edit = new ReplaceEdit(fOffset, fLength, fReplacementString);
    rootEdit.addChild(edit);
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 53 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.

the class TextEditConverter method applySourceModifier.

private String applySourceModifier(String content, ISourceModifier modifier) {
    if (StringUtils.isBlank(content) || modifier == null) {
        return content;
    }
    SimpleDocument subDocument = new SimpleDocument(content);
    TextEdit newEdit = new MultiTextEdit(0, subDocument.getLength());
    ReplaceEdit[] replaces = modifier.getModifications(content);
    for (ReplaceEdit replace : replaces) {
        newEdit.addChild(replace);
    }
    try {
        newEdit.apply(subDocument, TextEdit.NONE);
    } catch (BadLocationException e) {
        JavaLanguageServerPlugin.logException("Error applying edit to document", e);
    }
    return subDocument.get();
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) SimpleDocument(org.eclipse.jdt.internal.core.util.SimpleDocument) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 54 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls 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 55 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.

the class RenameTypeProcessor method addConstructorRenames.

private void addConstructorRenames(TextChangeManager manager) throws CoreException {
    ICompilationUnit cu = fType.getCompilationUnit();
    IMethod[] methods = fType.getMethods();
    int typeNameLength = fType.getElementName().length();
    for (int i = 0; i < methods.length; i++) {
        if (methods[i].isConstructor()) {
            /*
				 * constructor declarations cannot be fully qualified so we can use simple replace here
				 *
				 * if (methods[i].getNameRange() == null), then it's a binary file so it's wrong anyway
				 * (checked as a precondition)
				 */
            String methodName = methods[i].getElementName();
            ISourceRange range = methods[i].getNameRange();
            String sourceName = cu.getBuffer().getText(range.getOffset(), range.getLength());
            boolean isValid = methodName.equals(sourceName);
            if (isValid) {
                String name = RefactoringCoreMessages.RenameTypeRefactoring_rename_constructor;
                TextChangeCompatibility.addTextEdit(manager.get(cu), name, new ReplaceEdit(methods[i].getNameRange().getOffset(), typeNameLength, getNewElementName()));
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange)

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