Search in sources :

Example 36 with InsertEdit

use of org.eclipse.text.edits.InsertEdit 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 37 with InsertEdit

use of org.eclipse.text.edits.InsertEdit 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)

Example 38 with InsertEdit

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

the class ASTCommentRewriter method addBlockCommentToJavadocEdits.

private void addBlockCommentToJavadocEdits(List<TextEdit> commentEdits) {
    for (BlockComment blockComment : this.blockCommentToJavadoc) {
        final int offset = blockComment.getStartPosition() + "/*".length();
        commentEdits.add(new InsertEdit(offset, "*"));
    }
}
Also used : BlockComment(org.eclipse.jdt.core.dom.BlockComment) InsertEdit(org.eclipse.text.edits.InsertEdit)

Example 39 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project eclipse.platform.text by eclipse.

the class TextEditTests method testInsertReplace1.

@Test
public void testInsertReplace1() throws Exception {
    TextEdit e1 = new ReplaceEdit(2, 1, "y");
    TextEdit e2 = new InsertEdit(2, "xx");
    fRoot.addChild(e1);
    fRoot.addChild(e2);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(fRoot, 2, 3);
    assertEquals(e1, 4, 1);
    assertEquals(e2, 2, 2);
    Assert.assertEquals("Buffer content", "01xxy3456789", fDocument.get());
    doUndoRedo(undo, "01xxy3456789");
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 40 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project eclipse.platform.text by eclipse.

the class TextEditTests method testNestedCopySourceWithInsert.

@Test
public void testNestedCopySourceWithInsert() throws Exception {
    CopySourceEdit s1 = new CopySourceEdit(1, 5);
    CopySourceEdit s2 = new CopySourceEdit(2, 3);
    CopySourceEdit s3 = new CopySourceEdit(3, 1);
    InsertEdit i1 = new InsertEdit(4, "x");
    s1.addChild(s2);
    s2.addChild(s3);
    s3.addChild(i1);
    CopyTargetEdit t1 = new CopyTargetEdit(9, s1);
    CopyTargetEdit t2 = new CopyTargetEdit(8, s2);
    CopyTargetEdit t3 = new CopyTargetEdit(7, s3);
    fRoot.addChild(s1);
    fRoot.addChild(t1);
    fRoot.addChild(t2);
    fRoot.addChild(t3);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(s1, 1, 6);
    assertEquals(s2, 2, 4);
    assertEquals(s3, 3, 2);
    assertEquals(i1, 4, 1);
    assertEquals(t1, 16, 6);
    assertEquals(t2, 11, 4);
    assertEquals(t3, 8, 2);
    String result = "0123x4563x723x48123x459";
    Assert.assertEquals("Buffer content", result, fDocument.get());
    doUndoRedo(undo, result);
}
Also used : CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) InsertEdit(org.eclipse.text.edits.InsertEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Aggregations

InsertEdit (org.eclipse.text.edits.InsertEdit)73 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)33 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)25 TextEdit (org.eclipse.text.edits.TextEdit)24 DeleteEdit (org.eclipse.text.edits.DeleteEdit)22 Test (org.junit.Test)22 BadLocationException (org.eclipse.jface.text.BadLocationException)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 ArrayList (java.util.ArrayList)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IDocument (org.eclipse.jface.text.IDocument)8 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)8 Location (org.eclipse.titan.designer.AST.Location)8 Module (org.eclipse.titan.designer.AST.Module)7 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)5 CoreException (org.eclipse.core.runtime.CoreException)5 IFile (org.eclipse.core.resources.IFile)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4