Search in sources :

Example 11 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project che 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 12 with ReplaceEdit

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

the class ReplaceRefactoring method createFileChange.

private TextChange createFileChange(IFile file, Pattern pattern, Set<FileMatch> matches, RefactoringStatus resultingStatus, Collection<MatchGroup> matchGroups) throws PatternSyntaxException, CoreException {
    PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();
    TextFileChange change = new TextFileChange(Messages.format(SearchMessages.ReplaceRefactoring_group_label_change_for_file, file.getName()), file);
    change.setEdit(new MultiTextEdit());
    ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
    manager.connect(file.getFullPath(), LocationKind.IFILE, null);
    try {
        ITextFileBuffer textFileBuffer = manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
        if (textFileBuffer == null) {
            resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_accessing_file_buffer, file.getName()));
            return null;
        }
        IDocument document = textFileBuffer.getDocument();
        String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
        for (FileMatch match : matches) {
            int offset = match.getOffset();
            int length = match.getLength();
            Position currentPosition = tracker.getCurrentPosition(match);
            if (currentPosition != null) {
                offset = currentPosition.offset;
                if (length != currentPosition.length) {
                    resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                    continue;
                }
            }
            String originalText = getOriginalText(document, offset, length);
            if (originalText == null) {
                resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                continue;
            }
            String replacementString = PatternConstructor.interpretReplaceEscapes(fReplaceString, originalText, lineDelimiter);
            replacementString = computeReplacementString(pattern, document, offset, replacementString);
            if (replacementString == null) {
                resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
                continue;
            }
            ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, replacementString);
            change.addEdit(replaceEdit);
            TextEditChangeGroup textEditChangeGroup = new TextEditChangeGroup(change, new TextEditGroup(SearchMessages.ReplaceRefactoring_group_label_match_replace, replaceEdit));
            change.addTextEditChangeGroup(textEditChangeGroup);
            matchGroups.add(new MatchGroup(textEditChangeGroup, match));
        }
    } finally {
        manager.disconnect(file.getFullPath(), LocationKind.IFILE, null);
    }
    return change;
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) Position(org.eclipse.jface.text.Position) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) PositionTracker(org.eclipse.search2.internal.ui.text.PositionTracker) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 13 with ReplaceEdit

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

the class TextEditTests method testCopy1.

@Test
public void testCopy1() throws Exception {
    MultiTextEdit root = new MultiTextEdit();
    TextEdit e1 = new InsertEdit(2, "yy");
    TextEdit e2 = new ReplaceEdit(2, 3, "3456");
    root.addChild(e1);
    root.addChild(e2);
    List<TextEdit> org = flatten(root);
    TextEditCopier copier = new TextEditCopier(root);
    List<TextEdit> copy = flatten(copier.perform());
    compare(org, copy);
}
Also used : TextEditCopier(org.eclipse.text.edits.TextEditCopier) InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Test(org.junit.Test)

Example 14 with ReplaceEdit

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

the class TextEditTests method testSourceTranformationSplit2.

@Test
public void testSourceTranformationSplit2() throws Exception {
    MoveSourceEdit ms = new MoveSourceEdit(2, 4);
    MoveTargetEdit mt = new MoveTargetEdit(9, ms);
    fRoot.addChild(ms);
    fRoot.addChild(mt);
    RangeMarker r1 = new RangeMarker(3, 2);
    ms.addChild(r1);
    ms.setSourceModifier(new ISourceModifier() {

        @Override
        public ISourceModifier copy() {
            return this;
        }

        @Override
        public ReplaceEdit[] getModifications(String source) {
            return new ReplaceEdit[] { new ReplaceEdit(2, 2, "aa") };
        }
    });
    fRoot.apply(fDocument);
    String result = "0167823aa9";
    Assert.assertEquals("Buffer content", result, fDocument.get());
    assertEquals(r1, 6, 3);
}
Also used : MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ISourceModifier(org.eclipse.text.edits.ISourceModifier) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) RangeMarker(org.eclipse.text.edits.RangeMarker) Test(org.junit.Test)

Example 15 with ReplaceEdit

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

the class TextEditTests method testInsert1.

@Test
public void testInsert1() throws Exception {
    // [][  ]
    TextEdit e1 = new InsertEdit(2, "yy");
    TextEdit e2 = new ReplaceEdit(2, 3, "3456");
    fRoot.addChild(e1);
    fRoot.addChild(e2);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(fRoot, 2, 6);
    assertEquals(e1, 2, 2);
    assertEquals(e2, 4, 4);
    Assert.assertEquals("Buffer content", "01yy345656789", fDocument.get());
    doUndoRedo(undo, "01yy345656789");
}
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)

Aggregations

ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)117 TextEdit (org.eclipse.text.edits.TextEdit)48 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)42 Test (org.junit.Test)34 InsertEdit (org.eclipse.text.edits.InsertEdit)24 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 IDocument (org.eclipse.jface.text.IDocument)13 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)13 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)13 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 DeleteEdit (org.eclipse.text.edits.DeleteEdit)12 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Document (org.eclipse.jface.text.Document)7 CoreException (org.eclipse.core.runtime.CoreException)6 Position (org.eclipse.jface.text.Position)6 TextChange (org.eclipse.ltk.core.refactoring.TextChange)6