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;
}
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)));
}
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;
}
use of org.eclipse.ltk.core.refactoring.TextEditChangeGroup in project che by eclipse.
the class RenameAnalyzeUtil method getEditChangeOffsetUpdates.
/**
*
* @param change
* @return Map <Integer oldOffset, Integer updatedOffset>
*/
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;
}
Aggregations