Search in sources :

Example 1 with TextEditChangeGroup

use of org.eclipse.ltk.core.refactoring.TextEditChangeGroup 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 2 with TextEditChangeGroup

use of org.eclipse.ltk.core.refactoring.TextEditChangeGroup in project che by eclipse.

the class TextChangeCompatibility method addTextEdit.

public static void addTextEdit(TextChange change, String name, TextEdit edit, GroupCategorySet groupCategories) throws MalformedTreeException {
    Assert.isNotNull(change);
    Assert.isNotNull(name);
    Assert.isNotNull(edit);
    TextEdit root = change.getEdit();
    if (root == null) {
        root = new MultiTextEdit();
        change.setEdit(root);
    }
    insert(root, edit);
    change.addTextEditChangeGroup(new TextEditChangeGroup(change, new CategorizedTextEditGroup(name, edit, groupCategories)));
}
Also used : TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup)

Example 3 with TextEditChangeGroup

use of org.eclipse.ltk.core.refactoring.TextEditChangeGroup in project che by eclipse.

the class RenameAnalyzeUtil method getCorrespondingEditChangeRange.

private static IRegion getCorrespondingEditChangeRange(SearchMatch searchResult, TextChangeManager manager) {
    TextChange change = getTextChange(searchResult, manager);
    if (change == null)
        return null;
    IRegion oldMatchRange = createTextRange(searchResult);
    TextEditChangeGroup[] editChanges = change.getTextEditChangeGroups();
    for (int i = 0; i < editChanges.length; i++) {
        if (oldMatchRange.equals(editChanges[i].getRegion()))
            return TextEdit.getCoverage(change.getPreviewEdits(editChanges[i].getTextEdits()));
    }
    return null;
}
Also used : TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IRegion(org.eclipse.jface.text.IRegion)

Example 4 with TextEditChangeGroup

use of org.eclipse.ltk.core.refactoring.TextEditChangeGroup in project che by eclipse.

the class RenameAnalyzeUtil method getEditChangeOffsetUpdates.

/**
	 *
	 * @param change
	 * @return Map &lt;Integer oldOffset, Integer updatedOffset&gt;
	 */
private static Map<Integer, Integer> getEditChangeOffsetUpdates(TextChange change) {
    TextEditChangeGroup[] editChanges = change.getTextEditChangeGroups();
    Map<Integer, Integer> offsetUpdates = new HashMap<Integer, Integer>(editChanges.length);
    for (int i = 0; i < editChanges.length; i++) {
        TextEditChangeGroup editChange = editChanges[i];
        IRegion oldRegion = editChange.getRegion();
        if (oldRegion == null)
            continue;
        IRegion updatedRegion = TextEdit.getCoverage(change.getPreviewEdits(editChange.getTextEdits()));
        if (updatedRegion == null)
            continue;
        offsetUpdates.put(new Integer(oldRegion.getOffset()), new Integer(updatedRegion.getOffset()));
    }
    return offsetUpdates;
}
Also used : TextEditChangeGroup(org.eclipse.ltk.core.refactoring.TextEditChangeGroup) HashMap(java.util.HashMap) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

TextEditChangeGroup (org.eclipse.ltk.core.refactoring.TextEditChangeGroup)4 IRegion (org.eclipse.jface.text.IRegion)2 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)2 HashMap (java.util.HashMap)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 IDocument (org.eclipse.jface.text.IDocument)1 Position (org.eclipse.jface.text.Position)1 CategorizedTextEditGroup (org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup)1 TextChange (org.eclipse.ltk.core.refactoring.TextChange)1 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)1 PositionTracker (org.eclipse.search2.internal.ui.text.PositionTracker)1 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1 TextEdit (org.eclipse.text.edits.TextEdit)1 TextEditGroup (org.eclipse.text.edits.TextEditGroup)1