Search in sources :

Example 46 with IRegion

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

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

the class TemplateCompletionProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    // adjust offset to end of normalized selection
    if (selection.getOffset() == offset)
        offset = selection.getOffset() + selection.getLength();
    String prefix = extractPrefix(viewer, offset);
    Region region = new Region(offset - prefix.length(), prefix.length());
    TemplateContext context = createContext(viewer, region);
    if (context == null)
        return new ICompletionProposal[0];
    // name of the selection variables {line, word}_selection //$NON-NLS-1$
    context.setVariable("selection", selection.getText());
    Template[] templates = getTemplates(context.getContextType().getId());
    List<ICompletionProposal> matches = new ArrayList<>();
    for (Template template : templates) {
        try {
            context.getContextType().validate(template.getPattern());
        } catch (TemplateException e) {
            continue;
        }
        if (template.matches(prefix, context.getContextType().getId()))
            matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
    }
    Collections.sort(matches, fgProposalComparator);
    return matches.toArray(new ICompletionProposal[matches.size()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 48 with IRegion

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

the class FindReplaceDocumentAdapterTest method testFind.

@org.junit.Test
public void testFind() {
    FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument);
    try {
        IRegion result = new Region(8, 11);
        // Find case-sensitive
        // $NON-NLS-1$
        IRegion r = findReplaceDocumentAdapter.find(0, "TestPackage", true, true, false, false);
        assertEquals(result, r);
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.find(0, "testpackage", true, true, false, false);
        assertNull(r);
        // Find non-case-sensitive
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.find(0, "TestPackage", true, false, false, false);
        assertEquals(r, result);
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.find(0, "testpackage", true, false, false, false);
        assertEquals(r, result);
    } catch (BadLocationException e) {
        Assert.assertTrue(false);
    }
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 49 with IRegion

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

the class FindReplaceDocumentAdapterTest method testRegexFindLinebreak3.

@Test
public void testRegexFindLinebreak3() throws Exception {
    FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument);
    String contents = "One\r\nTwo\r\n\r\nEnd";
    fDocument.set(contents);
    int two = contents.indexOf("Two");
    int end = contents.indexOf("End");
    IRegion region = adapter.find(0, "[a-zA-Z]+\\R", true, false, false, true);
    assertEquals(new Region(0, two), region);
    region = adapter.find(two, "[a-zA-Z]+\\R", true, false, false, true);
    assertEquals(new Region(two, 3 + 2), region);
    region = adapter.find(0, "[a-zA-Z]+\\R{2}", true, false, false, true);
    assertEquals(new Region(two, end - two), region);
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) Test(org.junit.Test)

Example 50 with IRegion

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

the class FindReplaceDocumentAdapterTest method testBug386751.

/**
 * Test case for: https://bugs.eclipse.org/386751
 */
@Test
public void testBug386751() {
    FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument);
    try {
        IRegion result = adapter.find(0, ".", true, false, true, false);
        assertNull(result);
    } catch (BadLocationException e) {
        Assert.assertTrue(false);
    }
}
Also used : FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)341 BadLocationException (org.eclipse.jface.text.BadLocationException)158 Region (org.eclipse.jface.text.Region)151 Test (org.junit.Test)94 IDocument (org.eclipse.jface.text.IDocument)68 Point (org.eclipse.swt.graphics.Point)49 Position (org.eclipse.jface.text.Position)38 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)20 ITypedRegion (org.eclipse.jface.text.ITypedRegion)20 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)18 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)17 IEditorPart (org.eclipse.ui.IEditorPart)13 ArrayList (java.util.ArrayList)11 Document (org.eclipse.jface.text.Document)11 StyledText (org.eclipse.swt.custom.StyledText)10 ITextSelection (org.eclipse.jface.text.ITextSelection)9 IFile (org.eclipse.core.resources.IFile)8 Annotation (org.eclipse.jface.text.source.Annotation)7 HashMap (java.util.HashMap)6 CoreException (org.eclipse.core.runtime.CoreException)6