Search in sources :

Example 41 with Position

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);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) Annotation(org.eclipse.jface.text.source.Annotation) Match(org.eclipse.search.ui.text.Match) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 42 with Position

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);
    }
}
Also used : Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) Match(org.eclipse.search.ui.text.Match)

Example 43 with Position

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);
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) Position(org.eclipse.jface.text.Position) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 44 with Position

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;
}
Also used : Position(org.eclipse.jface.text.Position) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 45 with Position

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));
    }
}
Also used : Position(org.eclipse.jface.text.Position)

Aggregations

Position (org.eclipse.jface.text.Position)421 BadLocationException (org.eclipse.jface.text.BadLocationException)145 Annotation (org.eclipse.jface.text.source.Annotation)110 IDocument (org.eclipse.jface.text.IDocument)80 ArrayList (java.util.ArrayList)75 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)57 IRegion (org.eclipse.jface.text.IRegion)55 Test (org.junit.Test)54 HashMap (java.util.HashMap)49 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)46 Point (org.eclipse.swt.graphics.Point)40 List (java.util.List)39 Iterator (java.util.Iterator)31 TypedPosition (org.eclipse.jface.text.TypedPosition)31 Region (org.eclipse.jface.text.Region)27 IFile (org.eclipse.core.resources.IFile)22 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)21 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)21 Map (java.util.Map)15 CoreException (org.eclipse.core.runtime.CoreException)15