Search in sources :

Example 41 with IRegion

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

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

the class ToggleSLCommentAction method isBlockCommented.

/**
 * Determines whether each line is prefixed by one of the prefixes.
 *
 * @param startLine Start line in document
 * @param endLine End line in document
 * @param prefixes Possible comment prefixes
 * @param document The document
 * @return <code>true</code> iff each line from <code>startLine</code>
 *             to and including <code>endLine</code> is prepended by one
 *             of the <code>prefixes</code>, ignoring whitespace at the
 *             begin of line
 * @since 2.1
 */
protected boolean isBlockCommented(int startLine, int endLine, String[] prefixes, IDocument document) {
    try {
        // check for occurrences of prefixes in the given lines
        for (int i = startLine; i <= endLine; i++) {
            IRegion line = document.getLineInformation(i);
            String text = document.get(line.getOffset(), line.getLength());
            int[] found = TextUtilities.indexOf(prefixes, text, 0);
            if (found[0] == -1)
                // found a line which is not commented
                return false;
            String s = document.get(line.getOffset(), found[0]);
            s = s.trim();
            if (s.length() != 0)
                // found a line which is not commented
                return false;
        }
        return true;
    } catch (BadLocationException x) {
        // should not happen
        log.error(x.getMessage(), x);
    }
    return false;
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 43 with IRegion

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

the class ToggleSLCommentAction method isSelectionCommented.

/**
 * Is the given selection single-line commented?
 *
 * @param selection Selection to check
 * @return <code>true</code> iff all selected lines are commented
 * @since 2.1
 */
protected boolean isSelectionCommented(ISelection selection) {
    if (!(selection instanceof ITextSelection))
        return false;
    ITextSelection textSelection = (ITextSelection) selection;
    if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0)
        return false;
    IDocument document = getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
    try {
        IRegion block = getTextBlockFromSelection(textSelection, document);
        ITypedRegion[] regions = TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
        // [startline, endline, startline, endline, ...]
        int[] lines = new int[regions.length * 2];
        for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
            // start line of region
            lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
            // end line of region
            int length = regions[i].getLength();
            int offset = regions[i].getOffset() + length;
            if (length > 0)
                offset--;
            lines[j + 1] = (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
        }
        // Perform the check
        for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
            String[] prefixes = fPrefixesMap.get(regions[i].getType());
            if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
                if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
                    return false;
        }
        return true;
    } catch (BadLocationException x) {
        // should not happen
        log.error(x.getMessage(), x);
    }
    return false;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 44 with IRegion

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

the class XtextTemplateContext method createTemplateTranslator.

protected TemplateTranslator createTemplateTranslator() {
    try {
        int offset = getStart();
        IRegion lineRegion = getDocument().getLineInformationOfOffset(offset);
        String line = getDocument().get(lineRegion.getOffset(), lineRegion.getLength());
        int i = 0;
        while (i < line.length() && Character.isWhitespace(line.charAt(i))) {
            i++;
        }
        if (i != 0)
            return new IndentationAwareTemplateTranslator(line.substring(0, i));
        return new TemplateTranslator();
    } catch (BadLocationException ex) {
        return new TemplateTranslator();
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 45 with IRegion

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

the class DefaultFoldingRegionProviderTest method testSignificantPartOfMlComment_01.

@Test
public void testSignificantPartOfMlComment_01() throws Exception {
    IFile iFile = createFile("foo/bar.foldingtestlanguage", "\n/**\n *\n */\n element foo end");
    IXtextDocument document = openFileAndReturnDocument(iFile);
    DefaultFoldingRegionProvider reg = createFoldingRegionProvider();
    Collection<FoldedPosition> regions = reg.getFoldingRegions(document);
    assertEquals(1, regions.size());
    FoldedPosition position = Iterables.getOnlyElement(regions);
    assertEquals(1, position.getOffset());
    assertEquals("/**\n *\n */\n".length(), position.getLength());
    assertEquals(0, position.computeCaptionOffset(document));
    IRegion[] projectionRegions = position.computeProjectionRegions(document);
    assertEquals(1, projectionRegions.length);
    assertEquals("\n/**\n".length(), projectionRegions[0].getOffset());
    assertEquals(" *\n */\n".length(), projectionRegions[0].getLength());
}
Also used : IFile(org.eclipse.core.resources.IFile) DefaultFoldingRegionProvider(org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider) FoldedPosition(org.eclipse.xtext.ui.editor.folding.FoldedPosition) IRegion(org.eclipse.jface.text.IRegion) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)668 BadLocationException (org.eclipse.jface.text.BadLocationException)291 Region (org.eclipse.jface.text.Region)265 IDocument (org.eclipse.jface.text.IDocument)143 Test (org.junit.Test)108 Point (org.eclipse.swt.graphics.Point)76 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)57 ITypedRegion (org.eclipse.jface.text.ITypedRegion)55 Position (org.eclipse.jface.text.Position)50 ArrayList (java.util.ArrayList)37 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)35 IFile (org.eclipse.core.resources.IFile)33 ITextSelection (org.eclipse.jface.text.ITextSelection)32 IEditorPart (org.eclipse.ui.IEditorPart)29 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)24 StyledText (org.eclipse.swt.custom.StyledText)22 CoreException (org.eclipse.core.runtime.CoreException)19 List (java.util.List)18 Document (org.eclipse.jface.text.Document)17 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)17