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));
}
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()]);
}
}
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;
}
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;
}
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);
}
Aggregations