Search in sources :

Example 96 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class HighlightStrategy method applyHighlights.

private void applyHighlights() {
    if (sourceViewer == null || !enabled) {
        return;
    }
    removeOccurrenceAnnotations();
    int searchOffset = document.get().indexOf(SEARCH_TERM);
    if (searchOffset == -1) {
        return;
    }
    sourceViewer.getAnnotationModel().addAnnotation(new Annotation(ANNOTATION_TYPE, false, null), new Position(searchOffset, 5));
}
Also used : Position(org.eclipse.jface.text.Position) Annotation(org.eclipse.jface.text.source.Annotation)

Example 97 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class DefaultWordHighlightStrategy method applyHighlights.

private void applyHighlights(int offset) {
    if (sourceViewer == null || !enabled) {
        removeOccurrenceAnnotations();
        return;
    }
    String text = document.get();
    offset = ((ITextViewerExtension5) sourceViewer).widgetOffset2ModelOffset(offset);
    String word = findCurrentWord(text, offset);
    if (word == null) {
        removeOccurrenceAnnotations();
        return;
    }
    Matcher m = WORD_PATTERN.matcher(text);
    Map<Annotation, Position> annotationMap = new HashMap<>();
    while (m.find()) {
        if (m.group().equals(word)) {
            annotationMap.put(new Annotation(ANNOTATION_TYPE, false, NLS.bind(Messages.DefaultWordHighlightStrategy_OccurrencesOf, word)), new Position(m.start(), m.end() - m.start()));
        }
    }
    if (annotationMap.size() < 2) {
        removeOccurrenceAnnotations();
        return;
    }
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    synchronized (getLockObject(annotationModel)) {
        if (annotationModel instanceof IAnnotationModelExtension) {
            ((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap);
        } else {
            removeOccurrenceAnnotations();
            Iterator<Entry<Annotation, Position>> iter = annotationMap.entrySet().iterator();
            while (iter.hasNext()) {
                Entry<Annotation, Position> mapEntry = iter.next();
                annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue());
            }
        }
        fOccurrenceAnnotations = annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
    }
}
Also used : Entry(java.util.Map.Entry) Matcher(java.util.regex.Matcher) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 98 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class AbstractDecoratedTextEditor method findSelectedOverviewRulerAnnotationLabel.

private String findSelectedOverviewRulerAnnotationLabel() {
    IAnnotationModel model = getSourceViewer().getAnnotationModel();
    if (model == null) {
        return null;
    }
    Point selection = getSourceViewer().getSelectedRange();
    Annotation annotation = null;
    Iterator<Annotation> iter = model.getAnnotationIterator();
    while (iter.hasNext()) {
        annotation = iter.next();
        Position p = model.getPosition(annotation);
        if (p.getOffset() == selection.x && p.getLength() == selection.y)
            break;
    }
    if (annotation != null) {
        AnnotationPreference ap = getAnnotationPreferenceLookup().getAnnotationPreference(annotation);
        if (ap != null)
            return ap.getPreferenceLabel();
    }
    return null;
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Point(org.eclipse.swt.graphics.Point) Annotation(org.eclipse.jface.text.source.Annotation)

Example 99 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class ContentFormatter method updateAffectedPositions.

/**
 * Updates all the overlapping positions. Note, all other positions are
 * automatically updated by their document position updaters.
 *
 * @param document the document to has been formatted
 * @param positions the adapted character positions to be used to update the document positions
 * @param offset the offset of the document region that has been formatted
 */
protected void updateAffectedPositions(IDocument document, int[] positions, int offset) {
    if (document != fDocument)
        return;
    if (positions.length == 0)
        return;
    for (int i = 0; i < positions.length; i++) {
        PositionReference r = fOverlappingPositionReferences.get(i);
        if (r.refersToOffset())
            r.setOffset(offset + positions[i]);
        else
            r.setLength((offset + positions[i]) - r.getOffset());
        Position p = r.getPosition();
        String category = r.getCategory();
        if (!document.containsPosition(category, p.offset, p.length)) {
            try {
                if (positionAboutToBeAdded(document, category, p))
                    document.addPosition(r.getCategory(), p);
            } catch (BadPositionCategoryException x) {
            // can not happen
            } catch (BadLocationException x) {
            // should not happen
            }
        }
    }
    fOverlappingPositionReferences = null;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 100 with Position

use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.

the class ProjectionViewer method widgetSelection2ModelSelection.

/**
 * Adapts the behavior of the super class to respect line based folding.
 *
 * @param widgetSelection the widget selection
 * @return the model selection while respecting line based folding
 */
@Override
protected Point widgetSelection2ModelSelection(Point widgetSelection) {
    if (!isProjectionMode())
        return super.widgetSelection2ModelSelection(widgetSelection);
    /*
		 * There is one requirement that governs preservation of logical
		 * positions:
		 *
		 * 1) a selection with widget_length == 0 should never expand to have
		 * model_length > 0.
		 *
		 * There are a number of ambiguities to resolve with projection regions.
		 * A projected region P has a widget-length of zero. Its widget offset
		 * may interact with the selection S in various ways:
		 *
		 * A) P.widget_offset lies at the caret, S.widget_length is zero.
		 * Requirement 1 applies. S is *behind* P (done so by widgetRange2ModelRange).
		 *
		 * B) P.widget_offset lies inside the widget selection. This case is
		 * easy: P is included in S, which is automatically done so by
		 * widgetRange2ModelRange.
		 *
		 * C) P.widget_offset lies at S.widget_end: This is
		 * arguable - our policy is to include P if it belongs to a projection
		 * annotation that overlaps with the widget selection.
		 *
		 * D) P.widget_offset lies at S.widget_offset: Arguable - our policy
		 * is to include P if it belongs to a projection annotation that
		 * overlaps with the widget selection
		 */
    IRegion modelSelection = widgetRange2ModelRange(new Region(widgetSelection.x, widgetSelection.y));
    if (modelSelection == null)
        return null;
    int modelOffset = modelSelection.getOffset();
    int modelEndOffset = modelOffset + modelSelection.getLength();
    /* Case A: never expand a zero-length selection. S is *behind* P. */
    if (widgetSelection.y == 0)
        return new Point(modelEndOffset, 0);
    int widgetSelectionExclusiveEnd = widgetSelection.x + widgetSelection.y;
    Position[] annotationPositions = computeOverlappingAnnotationPositions(modelSelection);
    for (int i = 0; i < annotationPositions.length; i++) {
        IRegion[] regions = computeCollapsedRegions(annotationPositions[i]);
        if (regions == null)
            continue;
        for (int j = 0; j < regions.length; j++) {
            IRegion modelRange = regions[j];
            IRegion widgetRange = modelRange2ClosestWidgetRange(modelRange);
            // only take collapsed ranges, i.e. widget length is 0
            if (widgetRange != null && widgetRange.getLength() == 0) {
                int widgetOffset = widgetRange.getOffset();
                // D) region is collapsed at S.widget_offset
                if (widgetOffset == widgetSelection.x)
                    modelOffset = Math.min(modelOffset, modelRange.getOffset());
                else // C) region is collapsed at S.widget_end
                if (widgetOffset == widgetSelectionExclusiveEnd)
                    modelEndOffset = Math.max(modelEndOffset, modelRange.getOffset() + modelRange.getLength());
            }
        }
    }
    return new Point(modelOffset, modelEndOffset - modelOffset);
}
Also used : Position(org.eclipse.jface.text.Position) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point)

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