use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class PositionTracker method dirtyStateChanged.
@Override
public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {
if (isDirty)
return;
final int[] trackCount = new int[1];
doForExistingMatchesIn(buffer, new IFileBufferMatchOperation() {
@Override
public void run(ITextFileBuffer textBuffer, Match match) {
trackCount[0]++;
Position pos = fMatchesToPositions.get(match);
if (pos != null) {
if (pos.isDeleted()) {
AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
// might be that the containing element has been removed.
if (result != null) {
result.removeMatch(match);
}
untrackPosition(textBuffer, match);
} else {
if (match.getBaseUnit() == Match.UNIT_LINE) {
try {
pos = convertToLinePosition(pos, textBuffer.getDocument());
} catch (BadLocationException e) {
SearchPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, e.getLocalizedMessage(), e));
}
}
match.setOffset(pos.getOffset());
match.setLength(pos.getLength());
}
}
}
});
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class PositionTracker method trackPosition.
private void trackPosition(AbstractTextSearchResult result, ITextFileBuffer fb, Match match) {
int offset = match.getOffset();
int length = match.getLength();
if (offset < 0 || length < 0)
return;
try {
IDocument doc = fb.getDocument();
Position position = new Position(offset, length);
if (match.getBaseUnit() == Match.UNIT_LINE) {
position = convertToCharacterPosition(position, doc);
}
doc.addPosition(position);
fMatchesToSearchResults.put(match, result);
fMatchesToPositions.put(match, position);
addFileBufferMapping(fb, match);
} catch (BadLocationException e) {
// the match is outside the document
result.removeMatch(match);
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class PositionTracker method getCurrentPosition.
public Position getCurrentPosition(Match match) {
Position pos = fMatchesToPositions.get(match);
if (pos == null)
return pos;
AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
if (match.getBaseUnit() == Match.UNIT_LINE && result != null) {
ITextFileBuffer fb = getTrackedFileBuffer(result, match.getElement());
if (fb != null) {
IDocument doc = fb.getDocument();
try {
pos = convertToLinePosition(pos, doc);
} catch (BadLocationException e) {
}
}
}
return pos;
}
use of org.eclipse.jface.text.Position 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.jface.text.Position in project eclipse.platform.text by eclipse.
the class AnnotationManagerTest method testAddAnnotation.
@Test
public void testAddAnnotation() throws Exception {
NewSearchUI.runQueryInForeground(null, fQuery1);
AbstractTextSearchResult result = (AbstractTextSearchResult) fQuery1.getSearchResult();
Object[] files = result.getElements();
try {
for (int i = 0; i < files.length; i++) {
IFile file = (IFile) files[i];
ITextEditor editor = (ITextEditor) SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
annotationModel.getAnnotationIterator();
HashSet<Position> positions = new HashSet<>();
for (Iterator<Annotation> iter = annotationModel.getAnnotationIterator(); iter.hasNext(); ) {
Annotation annotation = iter.next();
if (annotation.getType().equals(fAnnotationTypeLookup.getAnnotationType(NewSearchUI.SEARCH_MARKER, IMarker.SEVERITY_INFO))) {
positions.add(annotationModel.getPosition(annotation));
}
}
Match[] matches = result.getMatches(file);
for (int j = 0; j < matches.length; j++) {
Position position = new Position(matches[j].getOffset(), matches[j].getLength());
// $NON-NLS-1$
assertTrue("position not found at: " + j, positions.remove(position));
}
assertEquals(0, positions.size());
}
} finally {
SearchPlugin.getActivePage().closeAllEditors(false);
}
}
Aggregations