Search in sources :

Example 56 with ReplaceEdit

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

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

the class TextMatchUpdater method addTextUpdates.

private void addTextUpdates(ICompilationUnit cu, Set<TextMatch> matches) {
    for (Iterator<TextMatch> resultIter = matches.iterator(); resultIter.hasNext(); ) {
        TextMatch match = resultIter.next();
        if (!match.isQualified() && fOnlyQualified) {
            continue;
        }
        int matchStart = match.getStartPosition();
        ReplaceEdit edit = new ReplaceEdit(matchStart, fCurrentNameLength, fNewName);
        try {
            TextChangeCompatibility.addTextEdit(fManager.get(cu), TEXT_EDIT_LABEL, edit, TEXTUAL_MATCHES);
        } catch (MalformedTreeException e) {
        // conflicting update -> omit text match
        }
    }
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) TextMatch(org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RefactoringScanner.TextMatch)

Example 58 with ReplaceEdit

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

the class MoveCuUpdateCreator method addReferenceUpdates.

private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
    List<ICompilationUnit> cuList = Arrays.asList(fCus);
    SearchResultGroup[] references = getReferences(movedUnit, pm, status);
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup searchResultGroup = references[i];
        ICompilationUnit referencingCu = searchResultGroup.getCompilationUnit();
        if (referencingCu == null) {
            continue;
        }
        boolean simpleReferencesNeedNewImport = simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
        SearchMatch[] results = searchResultGroup.getSearchResults();
        for (int j = 0; j < results.length; j++) {
            // TODO: should update type references with results from addImport
            TypeReference reference = (TypeReference) results[j];
            if (reference.isImportDeclaration()) {
                ImportRewrite rewrite = getImportRewrite(referencingCu);
                IImportDeclaration importDecl = (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
                if (Flags.isStatic(importDecl.getFlags())) {
                    rewrite.removeStaticImport(importDecl.getElementName());
                    addStaticImport(movedUnit, importDecl, rewrite);
                } else {
                    rewrite.removeImport(importDecl.getElementName());
                    rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
                }
            } else if (reference.isQualified()) {
                TextChange textChange = changeManager.get(referencingCu);
                String changeName = RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
                TextEdit replaceEdit = new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
                TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
            } else if (simpleReferencesNeedNewImport) {
                ImportRewrite importEdit = getImportRewrite(referencingCu);
                String typeName = reference.getSimpleName();
                importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 59 with ReplaceEdit

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

the class CreateCopyOfCompilationUnitChange method createChangeManager.

private static TextChangeManager createChangeManager(IProgressMonitor monitor, ICompilationUnit copy, String newName) throws CoreException {
    TextChangeManager manager = new TextChangeManager();
    SearchResultGroup refs = getReferences(copy, monitor);
    if (refs == null) {
        return manager;
    }
    if (refs.getCompilationUnit() == null) {
        return manager;
    }
    String name = RefactoringCoreMessages.CopyRefactoring_update_ref;
    SearchMatch[] results = refs.getSearchResults();
    for (int j = 0; j < results.length; j++) {
        SearchMatch searchResult = results[j];
        if (searchResult.getAccuracy() == SearchMatch.A_INACCURATE) {
            continue;
        }
        int offset = searchResult.getOffset();
        int length = searchResult.getLength();
        TextChangeCompatibility.addTextEdit(manager.get(copy), name, new ReplaceEdit(offset, length, newName));
    }
    return manager;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 60 with ReplaceEdit

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

the class FormatterHandler method convertEdit.

private static org.eclipse.lsp4j.TextEdit convertEdit(TextEdit edit, IDocument document) {
    org.eclipse.lsp4j.TextEdit textEdit = new org.eclipse.lsp4j.TextEdit();
    if (edit instanceof ReplaceEdit) {
        ReplaceEdit replaceEdit = (ReplaceEdit) edit;
        textEdit.setNewText(replaceEdit.getText());
        int offset = edit.getOffset();
        textEdit.setRange(new Range(createPosition(document, offset), createPosition(document, offset + edit.getLength())));
    }
    return textEdit;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) Range(org.eclipse.lsp4j.Range)

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