Search in sources :

Example 46 with Position

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

Example 47 with Position

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);
    });
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) Position(org.eclipse.jface.text.Position) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Example 48 with Position

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

Example 49 with Position

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

Example 50 with Position

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;
    }
}
Also used : LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) Position(org.eclipse.jface.text.Position) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) LinkedModeModel(org.eclipse.jface.text.link.LinkedModeModel) Point(org.eclipse.swt.graphics.Point) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) LinkedModeUI(org.eclipse.jface.text.link.LinkedModeUI) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

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