use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class VerticalRuler method doPaint1.
/**
* Draws the vertical ruler w/o drawing the Canvas background. Uses
* <code>ITextViewerExtension5</code> for its implementation. Will replace
* <code>doPaint(GC)</code>.
*
* @param gc the GC to draw into
*/
protected void doPaint1(GC gc) {
if (fModel == null || fTextViewer == null)
return;
IAnnotationAccessExtension annotationAccessExtension = null;
if (fAnnotationAccess instanceof IAnnotationAccessExtension)
annotationAccessExtension = (IAnnotationAccessExtension) fAnnotationAccess;
ITextViewerExtension5 extension = (ITextViewerExtension5) fTextViewer;
StyledText textWidget = fTextViewer.getTextWidget();
fScrollPos = textWidget.getTopPixel();
Point dimension = fCanvas.getSize();
// draw Annotations
Rectangle r = new Rectangle(0, 0, 0, 0);
// loop at least once through layers.
int maxLayer = 1;
for (int layer = 0; layer < maxLayer; layer++) {
Iterator<Annotation> iter = fModel.getAnnotationIterator();
while (iter.hasNext()) {
IAnnotationPresentation annotationPresentation = null;
Annotation annotation = iter.next();
int lay = IAnnotationAccessExtension.DEFAULT_LAYER;
if (annotationAccessExtension != null)
lay = annotationAccessExtension.getLayer(annotation);
else if (annotation instanceof IAnnotationPresentation) {
annotationPresentation = (IAnnotationPresentation) annotation;
lay = annotationPresentation.getLayer();
}
// dynamically update layer maximum
maxLayer = Math.max(maxLayer, lay + 1);
if (// wrong layer: skip annotation
lay != layer)
continue;
Position position = fModel.getPosition(annotation);
if (position == null)
continue;
IRegion widgetRegion = extension.modelRange2WidgetRange(new Region(position.getOffset(), position.getLength()));
if (widgetRegion == null)
continue;
int startLine = extension.widgetLineOfWidgetOffset(widgetRegion.getOffset());
if (startLine == -1)
continue;
int endLine = extension.widgetLineOfWidgetOffset(widgetRegion.getOffset() + Math.max(widgetRegion.getLength() - 1, 0));
if (endLine == -1)
continue;
r.x = 0;
r.y = JFaceTextUtil.computeLineHeight(textWidget, 0, startLine, startLine) - fScrollPos;
r.width = dimension.x;
int lines = endLine - startLine;
r.height = JFaceTextUtil.computeLineHeight(textWidget, startLine, endLine + 1, lines + 1);
if (// annotation within visible area
r.y < dimension.y && annotationAccessExtension != null)
annotationAccessExtension.paint(annotation, gc, fCanvas, r);
else if (annotationPresentation != null)
annotationPresentation.paint(gc, fCanvas, r);
}
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class AbstractInlinedAnnotation method redraw.
/**
* Redraw the inlined annotation.
*/
public void redraw() {
StyledText text = getTextWidget();
InlinedAnnotationSupport.runInUIThread(text, (t) -> {
Position pos = getPosition();
int offset = pos.getOffset();
ISourceViewer viewer = getViewer();
if (viewer instanceof ITextViewerExtension5) {
// adjust offset according folded content
offset = ((ITextViewerExtension5) viewer).modelOffset2WidgetOffset(offset);
}
InlinedAnnotationDrawingStrategy.draw(this, null, t, offset, pos.getLength(), null);
});
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class InlinedAnnotationDrawingStrategy method updateStyle.
/**
* Returns the style to apply with GlyphMetrics ascent only if needed.
*
* @param annotation the line header annotation
* @param style the current style and null otherwise.
* @return the style to apply with GlyphMetrics ascent only if needed.
*/
static StyleRange updateStyle(LineHeaderAnnotation annotation, StyleRange style) {
int width = annotation.getRedrawnCharacterWidth();
if (width == 0) {
// Update GlyphMetrics only when mining was already drawn
return null;
}
int height = annotation.getHeight();
if (height == 0) {
return null;
}
int fullHeight = height + annotation.getRedrawnCharacterHeight();
if (style == null) {
style = new StyleRange();
Position position = annotation.getPosition();
style.start = position.getOffset();
style.length = 1;
}
GlyphMetrics metrics = style.metrics;
if (!annotation.isMarkedDeleted()) {
if (metrics == null) {
metrics = new GlyphMetrics(fullHeight, 0, width);
} else {
if (metrics.ascent == fullHeight) {
return null;
}
/**
* We must create a new GlyphMetrics instance because comparison with similarTo used
* later in StyledText#setStyleRange will compare the same (modified) and won't
* realize an update happened.
*/
metrics = new GlyphMetrics(fullHeight, 0, width);
}
} else {
metrics = null;
}
style.metrics = metrics;
return style;
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class InclusivePositionUpdater method update.
@Override
public void update(DocumentEvent event) {
int eventOffset = event.getOffset();
int eventOldLength = event.getLength();
int eventNewLength = event.getText() == null ? 0 : event.getText().length();
int deltaLength = eventNewLength - eventOldLength;
try {
Position[] positions = event.getDocument().getPositions(fCategory);
for (int i = 0; i != positions.length; i++) {
Position position = positions[i];
if (position.isDeleted())
continue;
int offset = position.getOffset();
int length = position.getLength();
int end = offset + length;
if (offset > eventOffset + eventOldLength)
// position comes way
// after change - shift
position.setOffset(offset + deltaLength);
else if (end < eventOffset) {
// position comes way before change -
// leave alone
} else if (offset <= eventOffset && end >= eventOffset + eventOldLength) {
// event completely internal to the position - adjust length
position.setLength(length + deltaLength);
} else if (offset < eventOffset) {
// event extends over end of position - adjust length
int newEnd = eventOffset + eventNewLength;
position.setLength(newEnd - offset);
} else if (end > eventOffset + eventOldLength) {
// event extends from before position into it - adjust offset
// and length
// offset becomes end of event, length adjusted accordingly
// we want to recycle the overlapping part
position.setOffset(eventOffset);
int deleted = eventOffset + eventOldLength - offset;
position.setLength(length - deleted + eventNewLength);
} else {
// event consumes the position - delete it
position.delete();
}
}
} catch (BadPositionCategoryException e) {
// ignore and return
}
}
use of org.eclipse.jface.text.Position in project eclipse.platform.text by eclipse.
the class TemplateProposal method apply.
/**
* Inserts the template offered by this proposal into the viewer's document
* and sets up a <code>LinkedModeUI</code> on the viewer to edit any of
* the template's unresolved variables.
*
* @param viewer {@inheritDoc}
* @param trigger {@inheritDoc}
* @param stateMask {@inheritDoc}
* @param offset {@inheritDoc}
*/
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDocument document = viewer.getDocument();
try {
fContext.setReadOnly(false);
int start;
TemplateBuffer templateBuffer;
{
int oldReplaceOffset = getReplaceOffset();
try {
// this may already modify the document (e.g. add imports)
templateBuffer = fContext.evaluate(fTemplate);
} catch (TemplateException e1) {
fSelectedRegion = fRegion;
return;
}
start = getReplaceOffset();
int shift = start - oldReplaceOffset;
int end = Math.max(getReplaceEndOffset(), offset + shift);
// insert template string
String templateString = templateBuffer.getString();
document.replace(start, end - start, templateString);
}
// translate positions
LinkedModeModel model = new LinkedModeModel();
TemplateVariable[] variables = templateBuffer.getVariables();
boolean hasPositions = false;
for (int i = 0; i != variables.length; i++) {
TemplateVariable variable = variables[i];
if (variable.isUnambiguous())
continue;
LinkedPositionGroup group = new LinkedPositionGroup();
int[] offsets = variable.getOffsets();
int length = variable.getLength();
LinkedPosition first;
{
String[] values = variable.getValues();
ICompletionProposal[] proposals = new ICompletionProposal[values.length];
for (int j = 0; j < values.length; j++) {
ensurePositionCategoryInstalled(document, model);
Position pos = new Position(offsets[0] + start, length);
document.addPosition(getCategory(), pos);
proposals[j] = new PositionBasedCompletionProposal(values[j], pos, length);
}
if (proposals.length > 1)
first = new ProposalPosition(document, offsets[0] + start, length, proposals);
else
first = new LinkedPosition(document, offsets[0] + start, length);
}
for (int j = 0; j != offsets.length; j++) if (j == 0)
group.addPosition(first);
else
group.addPosition(new LinkedPosition(document, offsets[j] + start, length));
model.addGroup(group);
hasPositions = true;
}
if (hasPositions) {
model.forceInstall();
LinkedModeUI ui = new LinkedModeUI(model, viewer);
ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
ui.enter();
fSelectedRegion = ui.getSelectedRegion();
} else {
ensurePositionCategoryRemoved(document);
fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
}
} catch (BadLocationException e) {
openErrorDialog(viewer.getTextWidget().getShell(), e);
ensurePositionCategoryRemoved(document);
fSelectedRegion = fRegion;
} catch (BadPositionCategoryException e) {
openErrorDialog(viewer.getTextWidget().getShell(), e);
fSelectedRegion = fRegion;
}
}
Aggregations