Search in sources :

Example 71 with BadLocationException

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

the class PositionTracker method getCurrentPosition.

public Position getCurrentPosition(Match match) {
    Position pos = fMatchesToPositions.get(match);
    if (pos == null)
        return pos;
    AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
    if (match.getBaseUnit() == Match.UNIT_LINE && result != null) {
        ITextFileBuffer fb = getTrackedFileBuffer(result, match.getElement());
        if (fb != null) {
            IDocument doc = fb.getDocument();
            try {
                pos = convertToLinePosition(pos, doc);
            } catch (BadLocationException e) {
            }
        }
    }
    return pos;
}
Also used : Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 72 with BadLocationException

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

the class RetrieverAction method expandSelection.

private ITextSelection expandSelection(ITextSelection sel, IDocument document, String stopChars) {
    int offset = sel.getOffset();
    int length = sel.getLength();
    // left or right.
    if (length == 0) {
        // try right
        char chr = 0;
        char chl = 0;
        try {
            chr = document.getChar(offset);
        } catch (BadLocationException e2) {
        }
        try {
            chl = document.getChar(offset - 1);
        } catch (BadLocationException e2) {
        }
        if (isPartOfIdentifier(chr)) {
            length = 1;
        } else if (isPartOfIdentifier(chl)) {
            offset--;
            length = 1;
        } else if (stopChars != null && stopChars.indexOf(chr) == -1) {
            length = 1;
        } else if (stopChars != null && stopChars.indexOf(chl) == -1) {
            offset--;
            length = 1;
        } else {
            return sel;
        }
    }
    int a = offset + length - 1;
    int z = a;
    // move z one behind last character.
    try {
        char ch = document.getChar(z);
        while (isValidChar(stopChars, ch)) {
            ch = document.getChar(++z);
        }
    } catch (BadLocationException e2) {
    }
    // move a one before the first character
    try {
        char ch = document.getChar(a);
        while (isValidChar(stopChars, ch)) {
            ch = document.getChar(--a);
        }
    } catch (BadLocationException e2) {
    }
    if (a == z) {
        offset = a;
        length = 0;
    } else {
        offset = a + 1;
        length = z - a - 1;
    }
    return new TextSelection(document, offset, length);
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 73 with BadLocationException

use of org.eclipse.jface.text.BadLocationException 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)

Example 74 with BadLocationException

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

the class OverviewRuler method getAnnotationPosition.

/**
 * Returns the position of the first annotation found in the given line range.
 *
 * @param lineNumbers the line range
 * @return the position of the first found annotation
 */
private Position getAnnotationPosition(int[] lineNumbers) {
    if (lineNumbers[0] == -1)
        return null;
    Position found = null;
    try {
        IDocument d = fTextViewer.getDocument();
        IRegion line = d.getLineInformation(lineNumbers[0]);
        int start = line.getOffset();
        line = d.getLineInformation(lineNumbers[lineNumbers.length - 1]);
        int end = line.getOffset() + line.getLength();
        for (int i = fAnnotationsSortedByLayer.size() - 1; i >= 0; i--) {
            Object annotationType = fAnnotationsSortedByLayer.get(i);
            Iterator<Annotation> e = new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY);
            while (e.hasNext() && found == null) {
                Annotation a = e.next();
                if (a.isMarkedDeleted())
                    continue;
                if (skip(a.getType()))
                    continue;
                Position p = fModel.getPosition(a);
                if (p == null)
                    continue;
                int posOffset = p.getOffset();
                int posEnd = posOffset + p.getLength();
                IRegion region = d.getLineInformationOfOffset(posEnd);
                // trailing empty lines don't count
                if (posEnd > posOffset && region.getOffset() == posEnd) {
                    posEnd--;
                    region = d.getLineInformationOfOffset(posEnd);
                }
                if (posOffset <= end && posEnd >= start)
                    found = p;
            }
        }
    } catch (BadLocationException x) {
    }
    return found;
}
Also used : Position(org.eclipse.jface.text.Position) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 75 with BadLocationException

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

the class TemplateCompletionProcessor method extractPrefix.

/**
 * Heuristically extracts the prefix used for determining template relevance
 * from the viewer's document. The default implementation returns the String from
 * offset backwards that forms a java identifier.
 *
 * @param viewer the viewer
 * @param offset offset into document
 * @return the prefix to consider
 * @see #getRelevance(Template, String)
 */
protected String extractPrefix(ITextViewer viewer, int offset) {
    int i = offset;
    IDocument document = viewer.getDocument();
    if (i > document.getLength())
        // $NON-NLS-1$
        return "";
    try {
        while (i > 0) {
            char ch = document.getChar(i - 1);
            if (!Character.isJavaIdentifierPart(ch))
                break;
            i--;
        }
        return document.get(i, offset - i);
    } catch (BadLocationException e) {
        // $NON-NLS-1$
        return "";
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)455 IDocument (org.eclipse.jface.text.IDocument)196 IRegion (org.eclipse.jface.text.IRegion)161 Test (org.junit.Test)102 Position (org.eclipse.jface.text.Position)101 Region (org.eclipse.jface.text.Region)68 Point (org.eclipse.swt.graphics.Point)61 Document (org.eclipse.jface.text.Document)47 CoreException (org.eclipse.core.runtime.CoreException)34 ArrayList (java.util.ArrayList)27 ITypedRegion (org.eclipse.jface.text.ITypedRegion)27 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)22 DocumentEvent (org.eclipse.jface.text.DocumentEvent)21 ITextSelection (org.eclipse.jface.text.ITextSelection)21 StyledText (org.eclipse.swt.custom.StyledText)18 StyledString (org.eclipse.jface.viewers.StyledString)17 IStatus (org.eclipse.core.runtime.IStatus)16 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)15 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)15 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)15