use of org.eclipse.search2.internal.ui.text.PositionTracker 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.search2.internal.ui.text.PositionTracker in project eclipse.platform.text by eclipse.
the class AbstractTextSearchViewPage method getCurrentMatchLocation.
/**
* Returns the current location of the match. This takes possible
* modifications of the file into account. Therefore the result may
* differ from the position information that can be obtained directly
* off the match.
* @param match the match to get the position for.
* @return the current position of the match.
*
* @since 3.2
*/
public IRegion getCurrentMatchLocation(Match match) {
PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();
int offset, length;
Position pos = tracker.getCurrentPosition(match);
if (pos == null) {
offset = match.getOffset();
length = match.getLength();
} else {
offset = pos.getOffset();
length = pos.getLength();
}
return new Region(offset, length);
}
Aggregations