Search in sources :

Example 1 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project xtext-eclipse by eclipse.

the class ImportsAwareClipboardAction method caretOffset.

private int caretOffset(final XtextEditor xtextEditor) {
    ISourceViewer sourceViewer = xtextEditor.getInternalSourceViewer();
    int caretOffset = sourceViewer.getTextWidget().getCaretOffset();
    if (sourceViewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
        caretOffset = extension.widgetOffset2ModelOffset(caretOffset);
    }
    return caretOffset;
}
Also used : ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Example 2 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project xtext-eclipse by eclipse.

the class HierarchyInformationPresenter method modelRange2WidgetRange.

// copied from org.eclipse.jface.text.information.InformationPresenter due to visibility problems
protected IRegion modelRange2WidgetRange(IRegion region) {
    if (sourceViewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
        return extension.modelRange2WidgetRange(region);
    }
    IRegion visibleRegion = sourceViewer.getVisibleRegion();
    int start = region.getOffset() - visibleRegion.getOffset();
    int end = start + region.getLength();
    if (end > visibleRegion.getLength())
        end = visibleRegion.getLength();
    return new Region(start, end - start);
}
Also used : ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point)

Example 3 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project xtext-eclipse by eclipse.

the class TextViewerMoveLinesAction method runWithEvent.

@Override
public void runWithEvent(Event event) {
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    if (!canModifyViewer())
        return;
    // get involved objects
    IDocument document = viewer.getDocument();
    if (document == null)
        return;
    StyledText widget = viewer.getTextWidget();
    if (widget == null)
        return;
    // get selection
    ITextSelection sel = (ITextSelection) viewer.getSelectionProvider().getSelection();
    if (sel.isEmpty())
        return;
    ITextSelection skippedLine = getSkippedLine(document, sel);
    if (skippedLine == null)
        return;
    try {
        ITextSelection movingArea = getMovingSelection(document, sel, viewer);
        // visible area, bail out
        if (!containedByVisibleRegion(movingArea, viewer) || !containedByVisibleRegion(skippedLine, viewer))
            return;
        // get the content to be moved around: the moving (selected) area and the skipped line
        String moving = movingArea.getText();
        String skipped = skippedLine.getText();
        if (moving == null || skipped == null || document.getLength() == 0)
            return;
        String delim;
        String insertion;
        int offset, deviation;
        if (fUpwards) {
            delim = document.getLineDelimiter(skippedLine.getEndLine());
            if (fCopy) {
                delim = TextUtilities.getDefaultLineDelimiter(document);
                insertion = moving + delim;
                offset = movingArea.getOffset();
                deviation = 0;
            } else {
                Assert.isNotNull(delim);
                insertion = moving + delim + skipped;
                offset = skippedLine.getOffset();
                deviation = -skippedLine.getLength() - delim.length();
            }
        } else {
            delim = document.getLineDelimiter(movingArea.getEndLine());
            if (fCopy) {
                if (delim == null) {
                    delim = TextUtilities.getDefaultLineDelimiter(document);
                    insertion = delim + moving;
                } else {
                    insertion = moving + delim;
                }
                offset = skippedLine.getOffset();
                deviation = movingArea.getLength() + delim.length();
            } else {
                Assert.isNotNull(delim);
                insertion = skipped + delim + moving;
                offset = movingArea.getOffset();
                deviation = skipped.length() + delim.length();
            }
        }
        // modify the document
        beginCompoundEdit();
        if (fCopy) {
            document.replace(offset, 0, insertion);
        } else {
            document.replace(offset, insertion.length(), insertion);
        }
        // move the selection along
        int selOffset = movingArea.getOffset() + deviation;
        int selLength = movingArea.getLength() + (fAddDelimiter ? delim.length() : 0);
        if (!(viewer instanceof ITextViewerExtension5))
            selLength = Math.min(selLength, viewer.getVisibleRegion().getOffset() + viewer.getVisibleRegion().getLength() - selOffset);
        else {
        // TODO need to check what is necessary in the projection case
        }
        selectAndReveal(viewer, selOffset, selLength);
    } catch (BadLocationException x) {
        // won't happen without concurrent modification - bail out
        return;
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 4 with ITextViewerExtension5

use of org.eclipse.jface.text.ITextViewerExtension5 in project xtext-eclipse by eclipse.

the class TextViewerMoveLinesAction method containedByVisibleRegion.

/**
 * Checks if <code>selection</code> is contained by the visible region of <code>viewer</code>.
 * As a special case, a selection is considered contained even if it extends over the visible
 * region, but the extension stays on a partially contained line and contains only white space.
 *
 * @param selection the selection to be checked
 * @param viewer the viewer displaying a visible region of <code>selection</code>'s document.
 * @return <code>true</code>, if <code>selection</code> is contained, <code>false</code> otherwise.
 */
private boolean containedByVisibleRegion(ITextSelection selection, ITextViewer viewer) {
    int min = selection.getOffset();
    int max = min + selection.getLength();
    IDocument document = viewer.getDocument();
    IRegion visible;
    if (viewer instanceof ITextViewerExtension5)
        visible = ((ITextViewerExtension5) viewer).getModelCoverage();
    else
        visible = viewer.getVisibleRegion();
    int visOffset = visible.getOffset();
    try {
        if (visOffset > min) {
            if (document.getLineOfOffset(visOffset) != selection.getStartLine())
                return false;
            if (!isWhitespace(document.get(min, visOffset - min))) {
                return false;
            }
        }
        int visEnd = visOffset + visible.getLength();
        if (visEnd < max) {
            if (document.getLineOfOffset(visEnd) != selection.getEndLine())
                return false;
            if (!isWhitespace(document.get(visEnd, max - visEnd))) {
                return false;
            }
        }
        return true;
    } catch (BadLocationException e) {
    }
    return false;
}
Also used : ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 5 with ITextViewerExtension5

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

the class OverviewRuler method doPaint.

/**
 * Draws this overview ruler.
 *
 * @param gc the GC to draw into
 */
private void doPaint(GC gc) {
    Rectangle r = new Rectangle(0, 0, 0, 0);
    int yy, hh = ANNOTATION_HEIGHT;
    IDocument document = fTextViewer.getDocument();
    StyledText textWidget = fTextViewer.getTextWidget();
    ITextViewerExtension5 extension = null;
    IRegion visible = null;
    if (fTextViewer instanceof ITextViewerExtension5)
        extension = (ITextViewerExtension5) fTextViewer;
    else
        // legacy support
        visible = fTextViewer.getVisibleRegion();
    WidgetInfos infos = null;
    for (Object annotationType : fAnnotationsSortedByLayer) {
        if (skip(annotationType))
            continue;
        int[] style = new int[] { FilterIterator.PERSISTENT, FilterIterator.TEMPORARY };
        for (int element : style) {
            boolean areColorsComputed = false;
            Color fill = null;
            Color stroke = null;
            Iterator<Annotation> e = new FilterIterator(annotationType, element, fCachedAnnotations.iterator());
            while (e.hasNext()) {
                Annotation a = e.next();
                Position p = fModel.getPosition(a);
                if (p == null)
                    continue;
                if (visible != null && !p.overlapsWith(visible.getOffset(), visible.getLength()))
                    continue;
                int annotationOffset = p.getOffset();
                int annotationLength = p.getLength();
                IRegion widgetRegion = null;
                if (visible != null) {
                    annotationOffset = Math.max(p.getOffset(), visible.getOffset());
                    int annotationEnd = Math.min(p.getOffset() + p.getLength(), visible.getOffset() + visible.getLength());
                    annotationLength = annotationEnd - annotationOffset;
                } else {
                    widgetRegion = extension.modelRange2WidgetRange(new Region(annotationOffset, annotationLength));
                    if (widgetRegion == null)
                        continue;
                }
                if (infos == null) {
                    infos = new WidgetInfos(textWidget, fCanvas);
                    r.x = INSET;
                    r.width = infos.bounds.width - (2 * INSET);
                }
                try {
                    int startOffset = visible != null ? annotationOffset - visible.getOffset() : widgetRegion.getOffset();
                    int startLine = textWidget.getLineAtOffset(startOffset);
                    yy = computeY(startLine, infos);
                    if (ANNOTATION_HEIGHT_SCALABLE) {
                        int numberOfLines = document.getNumberOfLines(annotationOffset, annotationLength);
                        // don't count empty trailing line
                        IRegion lastLine = document.getLineInformationOfOffset(annotationOffset + annotationLength);
                        if (lastLine.getOffset() == annotationOffset + annotationLength) {
                            numberOfLines--;
                        }
                        if (numberOfLines > 1) {
                            int yy2 = computeY(startLine + numberOfLines - 1, infos);
                            hh = Math.max(yy2 - yy, ANNOTATION_HEIGHT);
                        } else {
                            hh = ANNOTATION_HEIGHT;
                        }
                    }
                    fAnnotationHeight = hh;
                    if (!areColorsComputed) {
                        stroke = getStrokeColor(annotationType, element == FilterIterator.TEMPORARY);
                        fill = fUseSaturatedColors ? stroke : getFillColor(annotationType, element == FilterIterator.TEMPORARY);
                        areColorsComputed = true;
                    }
                    if (fill != null) {
                        gc.setBackground(fill);
                        gc.fillRectangle(INSET, yy, infos.bounds.width - (2 * INSET), hh);
                    }
                    if (stroke != null) {
                        gc.setForeground(stroke);
                        r.y = yy;
                        if (yy + hh == infos.bounds.height)
                            r.y--;
                        r.height = hh;
                        // NOTE: 0 means width is 1 but with optimized performance
                        gc.setLineWidth(0);
                        gc.drawRectangle(r);
                    }
                } catch (BadLocationException x) {
                }
            }
        }
    }
    if (DEBUG_DRAW) {
        // draw debugging guides (boundaries):
        if (infos == null)
            infos = new WidgetInfos(textWidget, fCanvas);
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_MAGENTA));
        yy = infos.thumbHeight / 2;
        gc.drawLine(0, yy, infos.bounds.x / 2, yy);
        yy = infos.bounds.height - infos.thumbHeight / 2;
        gc.drawLine(0, yy, infos.bounds.x / 2, yy);
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLUE));
        yy = 0;
        gc.drawLine(0, yy, infos.bounds.x / 2, yy);
        yy = infos.bounds.height - 1;
        gc.drawLine(0, yy, infos.bounds.x / 2, yy);
    }
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) Position(org.eclipse.jface.text.Position) ITextViewerExtension5(org.eclipse.jface.text.ITextViewerExtension5) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) 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

ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)58 Point (org.eclipse.swt.graphics.Point)39 IRegion (org.eclipse.jface.text.IRegion)38 StyledText (org.eclipse.swt.custom.StyledText)23 Region (org.eclipse.jface.text.Region)17 BadLocationException (org.eclipse.jface.text.BadLocationException)14 IDocument (org.eclipse.jface.text.IDocument)14 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)7 ITextViewer (org.eclipse.jface.text.ITextViewer)6 Rectangle (org.eclipse.swt.graphics.Rectangle)6 Position (org.eclipse.jface.text.Position)5 GC (org.eclipse.swt.graphics.GC)5 StyleRange (org.eclipse.swt.custom.StyleRange)4 ITextViewerExtension2 (org.eclipse.jface.text.ITextViewerExtension2)3 ITypedRegion (org.eclipse.jface.text.ITypedRegion)3 ITextSelection (org.eclipse.jface.text.ITextSelection)2 Color (org.eclipse.swt.graphics.Color)2 Image (org.eclipse.swt.graphics.Image)2 Display (org.eclipse.swt.widgets.Display)2 Entry (java.util.Map.Entry)1