Search in sources :

Example 16 with MultiTextEdit

use of org.eclipse.text.edits.MultiTextEdit in project che by eclipse.

the class TextChange method createTextEditProcessor.

private TextEditProcessor createTextEditProcessor(IDocument document, int flags, TextEditBasedChangeGroup[] changes) {
    if (fEdit == null)
        return new TextEditProcessor(document, new MultiTextEdit(0, 0), flags);
    List includes = new ArrayList(0);
    for (int c = 0; c < changes.length; c++) {
        TextEditBasedChangeGroup change = changes[c];
        Assert.isTrue(change.getTextEditChange() == this);
        if (change.isEnabled()) {
            includes.addAll(Arrays.asList(change.getTextEditGroup().getTextEdits()));
        }
    }
    fCopier = new TextEditCopier(fEdit);
    TextEdit copiedEdit = fCopier.perform();
    boolean keep = getKeepPreviewEdits();
    if (keep)
        flags = flags | TextEdit.UPDATE_REGIONS;
    LocalTextEditProcessor result = new LocalTextEditProcessor(document, copiedEdit, flags);
    result.setIncludes(mapEdits((TextEdit[]) includes.toArray(new TextEdit[includes.size()]), fCopier));
    if (!keep)
        fCopier = null;
    return result;
}
Also used : TextEditCopier(org.eclipse.text.edits.TextEditCopier) TextEditProcessor(org.eclipse.text.edits.TextEditProcessor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 17 with MultiTextEdit

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

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

the class DocumentUndoManagerTest method testCompoundTextEdit.

public void testCompoundTextEdit() throws ExecutionException, BadLocationException {
    final int RANDOM_STRING_LENGTH = 50;
    final int RANDOM_REPLACE_COUNT = 100;
    assertTrue(RANDOM_REPLACE_COUNT >= 1);
    assertTrue(RANDOM_REPLACE_COUNT <= MAX_UNDO_LEVEL);
    String original = createRandomString(RANDOM_STRING_LENGTH);
    final IDocument document = new Document(original);
    createUndoManager(document);
    // fUndoManager.beginCompoundChange();
    MultiTextEdit fRoot = new MultiTextEdit();
    TextEdit e1 = new DeleteEdit(3, 1);
    fRoot.addChild(e1);
    fRoot.apply(document);
    fRoot = new MultiTextEdit();
    TextEdit e2 = new DeleteEdit(3, 1);
    fRoot.addChild(e2);
    fRoot.apply(document);
    // fUndoManager.endCompoundChange();
    assertTrue(fUndoManager.undoable());
    // while (fUndoManager.undoable())
    fUndoManager.undo();
    assertTrue(!fUndoManager.undoable());
    final String reverted = document.get();
    assertEquals(original, reverted);
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 19 with MultiTextEdit

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

the class TextEditTests method testUndefinedMultiEdit5.

@Test
public void testUndefinedMultiEdit5() throws Exception {
    MultiTextEdit m2 = new MultiTextEdit();
    m2.addChild(new DeleteEdit(4, 2));
    m2.addChild(new DeleteEdit(1, 3));
    Assert.assertEquals(1, m2.getOffset());
    Assert.assertEquals(5, m2.getLength());
}
Also used : DeleteEdit(org.eclipse.text.edits.DeleteEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Test(org.junit.Test)

Example 20 with MultiTextEdit

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

Aggregations

MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)51 TextEdit (org.eclipse.text.edits.TextEdit)33 Test (org.junit.Test)18 IDocument (org.eclipse.jface.text.IDocument)13 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)13 Document (org.eclipse.jface.text.Document)11 TextEditGroup (org.eclipse.text.edits.TextEditGroup)10 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)9 DeleteEdit (org.eclipse.text.edits.DeleteEdit)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 ArrayList (java.util.ArrayList)5 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)5 HashSet (java.util.HashSet)4 List (java.util.List)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 CopySourceEdit (org.eclipse.text.edits.CopySourceEdit)4 TextEditCopier (org.eclipse.text.edits.TextEditCopier)4