Search in sources :

Example 46 with ReplaceEdit

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

the class ReplaceCorrectionProposal method addEdits.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jface.text.IDocument)
	 */
@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 47 with ReplaceEdit

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

the class Utils method editsToJsonArray.

public static JSONArray editsToJsonArray(TextEdit edit) {
    final LinkedList<JSONObject> list = new LinkedList<JSONObject>();
    edit.accept(new TextEditVisitor() {

        @Override
        public boolean visit(DeleteEdit delete) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", delete.getOffset());
                json.put("length", delete.getLength());
                json.put("text", "");
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(delete);
        }

        @Override
        public boolean visit(InsertEdit insert) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", insert.getOffset());
                json.put("length", 0);
                json.put("text", insert.getText());
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(insert);
        }

        @Override
        public boolean visit(ReplaceEdit replace) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", replace.getOffset());
                json.put("length", replace.getLength());
                json.put("text", replace.getText());
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(replace);
        }
    });
    return new JSONArray(list);
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) JSONObject(org.json.JSONObject) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DeleteEdit(org.eclipse.text.edits.DeleteEdit) LinkedList(java.util.LinkedList)

Example 48 with ReplaceEdit

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

the class ASTCommentRewriter method addReplacementEdits.

private void addReplacementEdits(final List<TextEdit> commentEdits) {
    if (this.replacements.isEmpty()) {
        return;
    }
    for (Pair<Comment, String> pair : this.replacements) {
        Comment node = pair.getFirst();
        int start = node.getStartPosition();
        int length = node.getLength();
        commentEdits.add(new ReplaceEdit(start, length, pair.getSecond()));
    }
}
Also used : LineComment(org.eclipse.jdt.core.dom.LineComment) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 49 with ReplaceEdit

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

the class ASTCommentRewriter method addSingleLineCommentToJavadocEdits.

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

Example 50 with ReplaceEdit

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

the class DeletePackageFragmentRootChange method doDelete.

@Override
protected Change doDelete(IProgressMonitor pm) throws CoreException {
    if (!confirmDeleteIfReferenced()) {
        return new NullChange();
    }
    int resourceUpdateFlags = IResource.KEEP_HISTORY;
    int jCoreUpdateFlags = IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH;
    // $NON-NLS-1$
    pm.beginTask("", 2);
    IPackageFragmentRoot root = getRoot();
    IResource rootResource = root.getResource();
    CompositeChange result = new CompositeChange(getName());
    IJavaProject[] referencingProjects = JavaElementUtil.getReferencingProjects(root);
    HashMap<IFile, String> classpathFilesContents = new HashMap<>();
    for (int i = 0; i < referencingProjects.length; i++) {
        IJavaProject javaProject = referencingProjects[i];
        // $NON-NLS-1$
        IFile classpathFile = javaProject.getProject().getFile(".classpath");
        if (classpathFile.exists()) {
            classpathFilesContents.put(classpathFile, getFileContents(classpathFile));
        }
    }
    root.delete(resourceUpdateFlags, jCoreUpdateFlags, new SubProgressMonitor(pm, 1));
    // rootDescription.recordStateFromHistory(rootResource, new SubProgressMonitor(pm, 1));
    for (Iterator<Entry<IFile, String>> iterator = classpathFilesContents.entrySet().iterator(); iterator.hasNext(); ) {
        Entry<IFile, String> entry = iterator.next();
        IFile file = entry.getKey();
        String contents = entry.getValue();
        // Restore time stamps? This should probably be some sort of UndoTextFileChange.
        TextFileChange classpathUndo = new TextFileChange(Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_restore_file, BasicElementLabels.getPathLabel(file.getFullPath(), true)), file);
        classpathUndo.setEdit(new ReplaceEdit(0, getFileLength(file), contents));
        result.add(classpathUndo);
    }
    // result.add(new UndoDeleteResourceChange(rootDescription));
    pm.done();
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Entry(java.util.Map.Entry) IJavaProject(org.eclipse.jdt.core.IJavaProject) NullChange(org.eclipse.ltk.core.refactoring.NullChange) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) IResource(org.eclipse.core.resources.IResource)

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