use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.
the class HighlightingPresenter method createPresentation.
/**
* Create a text presentation in the background.
* <p>
* NOTE: Called from background thread.
* </p>
*
* @param addedPositions
* the added positions
* @param removedPositions
* the removed positions
* @return the text presentation or <code>null</code>, if reconciliation should be canceled
*/
public TextPresentation createPresentation(List<AttributedPosition> addedPositions, List<AttributedPosition> removedPositions) {
SourceViewer sourceViewer = fSourceViewer;
XtextPresentationReconciler presentationReconciler = fPresentationReconciler;
if (sourceViewer == null || presentationReconciler == null)
return null;
if (isCanceled())
return null;
IDocument document = sourceViewer.getDocument();
if (document == null)
return null;
int minStart = Integer.MAX_VALUE;
int maxEnd = Integer.MIN_VALUE;
for (int i = 0, n = removedPositions.size(); i < n; i++) {
Position position = removedPositions.get(i);
int offset = position.getOffset();
minStart = Math.min(minStart, offset);
maxEnd = Math.max(maxEnd, offset + position.getLength());
}
for (int i = 0, n = addedPositions.size(); i < n; i++) {
Position position = addedPositions.get(i);
int offset = position.getOffset();
minStart = Math.min(minStart, offset);
maxEnd = Math.max(maxEnd, offset + position.getLength());
}
if (minStart < maxEnd)
try {
return presentationReconciler.createRepairDescription(new Region(minStart, maxEnd - minStart), document);
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
}
return null;
}
use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.
the class XtextQuickAssistProcessor method getApplicableAnnotations.
protected Set<Annotation> getApplicableAnnotations(final IXtextDocument document, final IAnnotationModel annotationModel, final int offset) throws BadLocationException {
final int line = document.getLineOfOffset(offset);
final String delim = document.getLineDelimiter(line);
final int delimLength = delim != null ? delim.length() : 0;
final int lineLength = document.getLineLength(line) - delimLength;
final int lineOffset = document.getLineOffset(line);
Iterator<?> iterator;
if (annotationModel instanceof IAnnotationModelExtension2)
iterator = ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(lineOffset, lineLength, true, true);
else
iterator = annotationModel.getAnnotationIterator();
Map<Annotation, Position> possibleAnnotations = Maps.newLinkedHashMap();
boolean hasIncludingAnnotation = false;
int smallestAnnotationDistance = Integer.MAX_VALUE;
int offsetOfFirstAnnotation = Integer.MAX_VALUE;
while (iterator.hasNext()) {
Object key = iterator.next();
if (key instanceof Annotation) {
Annotation annotation = (Annotation) key;
if (isSupported(annotation)) {
Position pos = annotationModel.getPosition(annotation);
// Consider all annotations that are on the same line as the cursor
if (pos != null && pos.overlapsWith(lineOffset, lineLength)) {
possibleAnnotations.put(annotation, pos);
if (pos.includes(offset)) {
hasIncludingAnnotation = true;
} else if (offset >= pos.getOffset()) {
int annotationDistance = offset - (pos.getOffset() + pos.getLength());
if (annotationDistance < smallestAnnotationDistance)
smallestAnnotationDistance = annotationDistance;
} else if (pos.getOffset() < offsetOfFirstAnnotation) {
offsetOfFirstAnnotation = pos.getOffset();
}
}
}
}
}
if (possibleAnnotations.isEmpty()) {
return Collections.emptySet();
}
// There is an annotation that includes the cursor, so accept all including annotations
if (hasIncludingAnnotation) {
Set<Annotation> includingAnnotations = Sets.newLinkedHashSetWithExpectedSize(possibleAnnotations.size());
for (Map.Entry<Annotation, Position> entry : possibleAnnotations.entrySet()) {
if (entry.getValue().includes(offset) && canFix(entry.getKey()))
includingAnnotations.add(entry.getKey());
}
return includingAnnotations;
}
// There is an annotation that is left of the cursor, so accept the nearest annotations on the left
if (smallestAnnotationDistance != Integer.MAX_VALUE) {
Set<Annotation> nearestAnnotations = Sets.newLinkedHashSetWithExpectedSize(possibleAnnotations.size());
for (Map.Entry<Annotation, Position> entry : possibleAnnotations.entrySet()) {
Position pos = entry.getValue();
if (offset >= pos.getOffset() && offset - (pos.getOffset() + pos.getLength()) == smallestAnnotationDistance && canFix(entry.getKey()))
nearestAnnotations.add(entry.getKey());
}
return nearestAnnotations;
}
// Accept the nearest annotations on the right
Set<Annotation> nearestAnnotations = Sets.newLinkedHashSetWithExpectedSize(possibleAnnotations.size());
for (Map.Entry<Annotation, Position> entry : possibleAnnotations.entrySet()) {
if (entry.getValue().getOffset() == offsetOfFirstAnnotation && canFix(entry.getKey()))
nearestAnnotations.add(entry.getKey());
}
return nearestAnnotations;
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class AnnotationModel method getRegionAnnotationIterator.
/**
* Returns an iterator as specified in {@link IAnnotationModelExtension2#getAnnotationIterator(int, int, boolean, boolean)}
*
* @param offset region start
* @param length region length
* @param canStartBefore position can start before region
* @param canEndAfter position can end after region
* @return an iterator to iterate over annotations in region
* @see IAnnotationModelExtension2#getAnnotationIterator(int, int, boolean, boolean)
* @since 3.4
*/
private Iterator<Annotation> getRegionAnnotationIterator(int offset, int length, boolean canStartBefore, boolean canEndAfter) {
if (!(fDocument instanceof AbstractDocument))
return new RegionIterator(getAnnotationIterator(true), this, offset, length, canStartBefore, canEndAfter);
AbstractDocument document = (AbstractDocument) fDocument;
cleanup(true);
try {
Position[] positions = document.getPositions(IDocument.DEFAULT_CATEGORY, offset, length, canStartBefore, canEndAfter);
return new AnnotationsInterator(positions, fPositions);
} catch (BadPositionCategoryException e) {
// can happen if e.g. the document doesn't contain such a category, or when removed in a different thread
return Collections.<Annotation>emptyList().iterator();
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class AnnotationModel method modifyAnnotationPosition.
/**
* Modifies the associated position of the given annotation to the given
* position. If the annotation is not yet managed by this annotation model,
* the annotation is added. When the position is <code>null</code>, the
* annotation is removed from the model.
* <p>
* If requested, all annotation model change listeners will be informed
* about the change.
*
* @param annotation the annotation whose associated position should be
* modified
* @param position the position to whose values the associated position
* should be changed
* @param fireModelChanged indicates whether to notify all model listeners
* @since 3.0
*/
protected void modifyAnnotationPosition(Annotation annotation, Position position, boolean fireModelChanged) {
if (position == null) {
removeAnnotation(annotation, fireModelChanged);
} else {
Position p = fAnnotations.get(annotation);
if (p != null) {
if (position.getOffset() != p.getOffset() || position.getLength() != p.getLength()) {
fDocument.removePosition(p);
p.setOffset(position.getOffset());
p.setLength(position.getLength());
try {
fDocument.addPosition(p);
} catch (BadLocationException e) {
// ignore invalid position
}
}
synchronized (getLockObject()) {
getAnnotationModelEvent().annotationChanged(annotation);
}
if (fireModelChanged)
fireModelChanged();
} else {
try {
addAnnotation(annotation, position, fireModelChanged);
} catch (BadLocationException x) {
// ignore invalid position
}
}
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class AnnotationModel method getPosition.
@Override
public Position getPosition(Annotation annotation) {
Position position = fAnnotations.get(annotation);
if (position != null)
return position;
Iterator<IAnnotationModel> it = fAttachments.values().iterator();
while (position == null && it.hasNext()) position = it.next().getPosition(annotation);
return position;
}
Aggregations