use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class LineAnnotationManagerTest method testLineBasedQuery.
@Test
public void testLineBasedQuery() throws Exception {
NewSearchUI.runQueryInForeground(null, fLineQuery);
AbstractTextSearchResult result = (AbstractTextSearchResult) fLineQuery.getSearchResult();
Object[] files = result.getElements();
try {
for (int i = 0; i < files.length; i++) {
IFile file = (IFile) files[0];
ITextEditor editor = (ITextEditor) SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
annotationModel.getAnnotationIterator();
ArrayList<Position> positions = new ArrayList<>();
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 = computeDocumentPositionFromLineMatch(document, matches[j]);
assertTrue("position not found at: " + j, positions.remove(position));
}
assertEquals(0, positions.size());
}
} finally {
SearchPlugin.getActivePage().closeAllEditors(false);
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class PositionTrackerTest method checkInsertAtZero.
private void checkInsertAtZero(AbstractTextSearchResult result, IFile file) throws PartInitException, BadLocationException {
Match[] matches = result.getMatches(file);
int[] originalStarts = new int[matches.length];
for (int i = 0; i < originalStarts.length; i++) {
originalStarts[i] = matches[i].getOffset();
}
try {
SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
ITextFileBuffer fb = FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
IDocument doc = fb.getDocument();
doc.replace(0, 0, "Test");
for (int i = 0; i < originalStarts.length; i++) {
Position currentPosition = InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(matches[i]);
assertNotNull(currentPosition);
assertEquals(originalStarts[i] + "Test".length(), currentPosition.getOffset());
}
} finally {
SearchPlugin.getActivePage().closeAllEditors(false);
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class OverviewRuler method doPaint.
/**
* Draws this overview ruler.
*
* @param gc the GC to draw into
*/
private void doPaint(GC gc) {
Rectangle r = new Rectangle(0, 0, 0, 0);
int yy, hh = ANNOTATION_HEIGHT;
IDocument document = fTextViewer.getDocument();
StyledText textWidget = fTextViewer.getTextWidget();
ITextViewerExtension5 extension = null;
IRegion visible = null;
if (fTextViewer instanceof ITextViewerExtension5)
extension = (ITextViewerExtension5) fTextViewer;
else
// legacy support
visible = fTextViewer.getVisibleRegion();
WidgetInfos infos = null;
for (Object annotationType : fAnnotationsSortedByLayer) {
if (skip(annotationType))
continue;
int[] style = new int[] { FilterIterator.PERSISTENT, FilterIterator.TEMPORARY };
for (int element : style) {
boolean areColorsComputed = false;
Color fill = null;
Color stroke = null;
Iterator<Annotation> e = new FilterIterator(annotationType, element, fCachedAnnotations.iterator());
while (e.hasNext()) {
Annotation a = e.next();
Position p = fModel.getPosition(a);
if (p == null)
continue;
if (visible != null && !p.overlapsWith(visible.getOffset(), visible.getLength()))
continue;
int annotationOffset = p.getOffset();
int annotationLength = p.getLength();
IRegion widgetRegion = null;
if (visible != null) {
annotationOffset = Math.max(p.getOffset(), visible.getOffset());
int annotationEnd = Math.min(p.getOffset() + p.getLength(), visible.getOffset() + visible.getLength());
annotationLength = annotationEnd - annotationOffset;
} else {
widgetRegion = extension.modelRange2WidgetRange(new Region(annotationOffset, annotationLength));
if (widgetRegion == null)
continue;
}
if (infos == null) {
infos = new WidgetInfos(textWidget, fCanvas);
r.x = INSET;
r.width = infos.bounds.width - (2 * INSET);
}
try {
int startOffset = visible != null ? annotationOffset - visible.getOffset() : widgetRegion.getOffset();
int startLine = textWidget.getLineAtOffset(startOffset);
yy = computeY(startLine, infos);
if (ANNOTATION_HEIGHT_SCALABLE) {
int numberOfLines = document.getNumberOfLines(annotationOffset, annotationLength);
// don't count empty trailing line
IRegion lastLine = document.getLineInformationOfOffset(annotationOffset + annotationLength);
if (lastLine.getOffset() == annotationOffset + annotationLength) {
numberOfLines--;
}
if (numberOfLines > 1) {
int yy2 = computeY(startLine + numberOfLines - 1, infos);
hh = Math.max(yy2 - yy, ANNOTATION_HEIGHT);
} else {
hh = ANNOTATION_HEIGHT;
}
}
fAnnotationHeight = hh;
if (!areColorsComputed) {
stroke = getStrokeColor(annotationType, element == FilterIterator.TEMPORARY);
fill = fUseSaturatedColors ? stroke : getFillColor(annotationType, element == FilterIterator.TEMPORARY);
areColorsComputed = true;
}
if (fill != null) {
gc.setBackground(fill);
gc.fillRectangle(INSET, yy, infos.bounds.width - (2 * INSET), hh);
}
if (stroke != null) {
gc.setForeground(stroke);
r.y = yy;
if (yy + hh == infos.bounds.height)
r.y--;
r.height = hh;
// NOTE: 0 means width is 1 but with optimized performance
gc.setLineWidth(0);
gc.drawRectangle(r);
}
} catch (BadLocationException x) {
}
}
}
}
if (DEBUG_DRAW) {
// draw debugging guides (boundaries):
if (infos == null)
infos = new WidgetInfos(textWidget, fCanvas);
gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_MAGENTA));
yy = infos.thumbHeight / 2;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
yy = infos.bounds.height - infos.thumbHeight / 2;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLUE));
yy = 0;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
yy = infos.bounds.height - 1;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class OverviewRuler method getAnnotationPosition.
/**
* Returns the position of the first annotation found in the given line range.
*
* @param lineNumbers the line range
* @return the position of the first found annotation
*/
private Position getAnnotationPosition(int[] lineNumbers) {
if (lineNumbers[0] == -1)
return null;
Position found = null;
try {
IDocument d = fTextViewer.getDocument();
IRegion line = d.getLineInformation(lineNumbers[0]);
int start = line.getOffset();
line = d.getLineInformation(lineNumbers[lineNumbers.length - 1]);
int end = line.getOffset() + line.getLength();
for (int i = fAnnotationsSortedByLayer.size() - 1; i >= 0; i--) {
Object annotationType = fAnnotationsSortedByLayer.get(i);
Iterator<Annotation> e = new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY);
while (e.hasNext() && found == null) {
Annotation a = e.next();
if (a.isMarkedDeleted())
continue;
if (skip(a.getType()))
continue;
Position p = fModel.getPosition(a);
if (p == null)
continue;
int posOffset = p.getOffset();
int posEnd = posOffset + p.getLength();
IRegion region = d.getLineInformationOfOffset(posEnd);
// trailing empty lines don't count
if (posEnd > posOffset && region.getOffset() == posEnd) {
posEnd--;
region = d.getLineInformationOfOffset(posEnd);
}
if (posOffset <= end && posEnd >= start)
found = p;
}
}
} catch (BadLocationException x) {
}
return found;
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class SourceViewer method setRangeIndication.
@Override
public void setRangeIndication(int start, int length, boolean moveCursor) {
if (moveCursor) {
setSelectedRange(start, 0);
revealRange(start, length);
}
if (fRangeIndicator != null && fVisualAnnotationModel instanceof IAnnotationModelExtension) {
IAnnotationModelExtension extension = (IAnnotationModelExtension) fVisualAnnotationModel;
extension.modifyAnnotationPosition(fRangeIndicator, new Position(start, length));
}
}
Aggregations