Search in sources :

Example 26 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class DefaultDocumentHighlightService method isDocumentHighlightAvailableFor.

/**
 * Returns with {@code true} if the AST element selected from the resource
 * can provide document highlights, otherwise returns with {@code false}.
 *
 * <p>
 * Clients may override this method to change the default behavior.
 *
 * @param selectedElement
 *            the selected element resolved via the offset from the
 *            resource. Can be {@code null}.
 * @param resource
 *            the resource for the document.
 * @param offset
 *            the offset of the selection.
 *
 * @return {@code true} if the document highlight is available for the
 *         selected element, otherwise {@code false}.
 */
protected boolean isDocumentHighlightAvailableFor(final EObject selectedElement, final XtextResource resource, final int offset) {
    if (selectedElement == null || !getSelectedElementFilter().apply(selectedElement)) {
        return false;
    }
    // This code handles the special case where your language has constructs that can refer to
    // themselves. For example "function MyFunction begin ... end MyFunction" defines the function "MyFunction" and
    // terminates its implementation block with an additional repetition of the word "MyFunction". Normally, when
    // you are positioned on a selected element, you only want to highlight that selected element when you are
    // positioned directly on top of the name of the selected element. However, when the selected element can refer
    // to itself then there are references inside the element that must trigger highlighting.  In the example,
    // we also want to highlight "MyFunction" when we are positioned on the "end MyFunction".
    INode crossReferenceNode = offsetHelper.getCrossReferenceNode(resource, new TextRegion(offset, 0));
    if (crossReferenceNode != null) {
        EObject crossReferencedElement = offsetHelper.getCrossReferencedElement(crossReferenceNode);
        if (crossReferencedElement != null && crossReferencedElement == selectedElement) {
            return true;
        }
    }
    final EObject containedElement = offsetHelper.resolveContainedElementAt(resource, offset);
    if (selectedElement == containedElement) {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(containedElement);
        return !isNullOrEmpty(region) && // therefore the end position is exclusive.
        (region.contains(offset) || (region.getOffset() + region.getLength()) == offset);
    }
    return true;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject)

Example 27 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class TextRegions method difference.

public static <T extends ITextRegion> List<T> difference(Iterable<T> r1, Iterable<? extends ITextRegion> r2) {
    ArrayList<T> regions1 = Lists.newArrayList(r1);
    ArrayList<ITextRegion> regions2 = Lists.newArrayList(r2);
    Collections.sort(regions1, Comp.INSTANCE);
    Collections.sort(regions2, Comp.INSTANCE);
    List<T> missing = Lists.newArrayList();
    int i1 = 0, i2 = 0;
    while (i1 < regions1.size() && i2 < regions2.size()) {
        T t1 = regions1.get(i1);
        ITextRegion t2 = regions2.get(i2);
        int compareTo = Comp.INSTANCE.compare(t1, t2);
        if (compareTo == 0) {
            i1++;
            i2++;
        } else if (compareTo < 1) {
            missing.add(t1);
            i1++;
        } else {
            i2++;
        }
    }
    while (i1 < regions1.size()) {
        missing.add(regions1.get(i1));
        i1++;
    }
    return ImmutableList.copyOf(missing);
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 28 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class FormatterTestHelper method assertReplacementsAreInRegion.

protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
    Set<ITextReplacement> invalid = Sets.newHashSet();
    ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
        if (allowed.contains(r))
            continue ALLOWED;
        invalid.add(r);
    }
    if (!invalid.isEmpty()) {
        String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
        fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
    }
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Example 29 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class NodeModelStreamer method feedTokenStream.

@Override
public ITextRegion feedTokenStream(ITokenStream out, ICompositeNode in, int offset, int length) throws IOException {
    List<INode> nodes = getLeafs(in, offset, offset + length);
    if (nodes.isEmpty())
        return new TextRegion(in.getOffset(), 0);
    if (out instanceof ITokenStreamExtension)
        ((ITokenStreamExtension) out).init(findRootRuleForRegion(nodes.get(0)));
    boolean lastIsTokenOrComment = false;
    for (INode node : nodes) {
        boolean currentIsTokenOrComment = tokenUtil.isCommentNode(node) || tokenUtil.isToken(node);
        if (lastIsTokenOrComment && currentIsTokenOrComment)
            writeHiddenEmpty(out);
        lastIsTokenOrComment = currentIsTokenOrComment;
        if (node instanceof ILeafNode) {
            ILeafNode leaf = (ILeafNode) node;
            if (leaf.isHidden())
                writeHidden(out, leaf);
            else
                writeSemantic(out, leaf);
        } else if (node instanceof ICompositeNode)
            writeSemantic(out, (ICompositeNode) node);
    }
    out.flush();
    int rStart = nodes.get(0).getOffset();
    int rLength = nodes.get(nodes.size() - 1).getEndOffset() - rStart;
    return new TextRegion(rStart, rLength);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ITokenStreamExtension(org.eclipse.xtext.parsetree.reconstr.ITokenStreamExtension)

Example 30 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class DefaultNodeModelFormatter method format.

@Override
public IFormattedRegion format(ICompositeNode root, int offset, int length) {
    String indent = getIndentation(root, offset);
    TokenStringBuffer buf = new TokenStringBuffer();
    ITokenStream out = offset == 0 ? buf : new FilterFirstWhitespaceStream(buf);
    ITokenStream fmt;
    if (formatter instanceof IFormatterExtension) {
        EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(root);
        if (semanticElement != null)
            fmt = ((IFormatterExtension) formatter).createFormatterStream(semanticElement, indent, out, false);
        else {
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=380406
            ITextRegion rootRegion = root.getTextRegion();
            return new FormattedRegion(rootRegion.getOffset(), rootRegion.getLength(), root.getText());
        }
    } else
        fmt = formatter.createFormatterStream(indent, out, false);
    try {
        ITextRegion range = nodeModelStreamer.feedTokenStream(fmt, root, offset, length);
        return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString());
    } catch (IOException e) {
        // this should never happen since TokenStringBuffer doesn't throw IOEs.
        throw new RuntimeException(e);
    }
}
Also used : ITokenStream(org.eclipse.xtext.parsetree.reconstr.ITokenStream) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IFormatterExtension(org.eclipse.xtext.formatting.IFormatterExtension) TokenStringBuffer(org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer) IOException(java.io.IOException)

Aggregations

ITextRegion (org.eclipse.xtext.util.ITextRegion)122 TextRegion (org.eclipse.xtext.util.TextRegion)44 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)20 INode (org.eclipse.xtext.nodemodel.INode)19 XtextResource (org.eclipse.xtext.resource.XtextResource)17 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)15 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Region (org.eclipse.jface.text.Region)9 XExpression (org.eclipse.xtext.xbase.XExpression)9 IOException (java.io.IOException)8 CoreException (org.eclipse.core.runtime.CoreException)8 IParseResult (org.eclipse.xtext.parser.IParseResult)7 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)7 IRegion (org.eclipse.jface.text.IRegion)6 ITextSelection (org.eclipse.jface.text.ITextSelection)6 Collection (java.util.Collection)5 ArrayList (java.util.ArrayList)4 TextSelection (org.eclipse.jface.text.TextSelection)4