Search in sources :

Example 6 with ITextRegion

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

the class XtendOutlineWithEditorLinker method findNodesInRange.

protected void findNodesInRange(final IOutlineNode input, final ITextRegion selectedTextRegion, final List<IOutlineNode> nodes) {
    final ITextRegion textRegion = input.getFullTextRegion();
    if (((textRegion == null) || textRegion.contains(selectedTextRegion))) {
        nodes.add(input);
    }
    List<IOutlineNode> _children = input.getChildren();
    for (final IOutlineNode child : _children) {
        this.findNodesInRange(child, selectedTextRegion, nodes);
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode)

Example 7 with ITextRegion

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

the class XtendLocationInFileProvider method getSignificantTextRegion.

@Override
public ITextRegion getSignificantTextRegion(EObject element) {
    if (element instanceof RichStringLiteral) {
        ICompositeNode elementNode = findNodeFor(element);
        if (elementNode == null) {
            return ITextRegion.EMPTY_REGION;
        }
        ITextRegion result = ITextRegion.EMPTY_REGION;
        for (INode node : elementNode.getLeafNodes()) {
            if (isHidden(node)) {
                continue;
            }
            EObject grammarElement = node.getGrammarElement();
            if (!(grammarElement instanceof RuleCall)) {
                continue;
            }
            RuleCall ruleCall = (RuleCall) grammarElement;
            ITextRegionWithLineInformation region = node.getTextRegionWithLineInformation();
            int offset = region.getOffset();
            int length = region.getLength();
            if (grammarAccess.getRICH_TEXTRule() == ruleCall.getRule()) {
                offset += 3;
                length -= 6;
            } else if (grammarAccess.getRICH_TEXT_STARTRule() == ruleCall.getRule()) {
                offset += 3;
                length -= 4;
            } else if (grammarAccess.getRICH_TEXT_ENDRule() == ruleCall.getRule()) {
                offset += 1;
                length -= 4;
            } else if (grammarAccess.getRICH_TEXT_INBETWEENRule() == ruleCall.getRule()) {
                offset += 1;
                length -= 2;
            } else if (grammarAccess.getCOMMENT_RICH_TEXT_ENDRule() == ruleCall.getRule()) {
                offset += 2;
                length -= 5;
            } else if (grammarAccess.getCOMMENT_RICH_TEXT_INBETWEENRule() == ruleCall.getRule()) {
                offset += 2;
                length -= 3;
            } else {
                continue;
            }
            result = result.merge(toZeroBasedRegion(new TextRegionWithLineInformation(offset, length, region.getLineNumber(), region.getEndLineNumber())));
        }
        return result;
    }
    return super.getSignificantTextRegion(element);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) RichStringLiteral(org.eclipse.xtend.core.xtend.RichStringLiteral) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) RuleCall(org.eclipse.xtext.RuleCall) TextRegionWithLineInformation(org.eclipse.xtext.util.TextRegionWithLineInformation) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation)

Example 8 with ITextRegion

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

the class ValidatingRichStringAcceptor method resetCurrentOffset.

protected void resetCurrentOffset(/* @Nullable */
RichStringLiteral origin) {
    if (root == null)
        root = origin;
    if (origin != null && origin != recent) {
        if (wasCurrentOffset == -1)
            wasCurrentOffset = currentOffset;
        int diff = lastOffsetOfLiteral - currentOffset;
        // no actions are involved, we are interested in the real node
        recent = origin;
        List<INode> featureNodes = NodeModelUtils.findNodesForFeature(origin, XbasePackage.Literals.XSTRING_LITERAL__VALUE);
        if (featureNodes.size() == 1) {
            INode node = featureNodes.get(0);
            ITextRegion textRegion = node.getTextRegion();
            currentOffset = textRegion.getOffset();
            String nodeText = node.getText();
            if (nodeText.endsWith("'''")) {
                lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 3;
            } else if (nodeText.endsWith("''")) {
                lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 2;
            } else if (nodeText.endsWith("'") || nodeText.endsWith("\u00AB")) {
                lastOffsetOfLiteral = currentOffset + textRegion.getLength() - 1;
            }
            if (nodeText.charAt(0) == '\'') {
                currentOffset += 3;
            } else {
                currentOffset += 1;
            }
        }
        currentOffset -= diff;
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 9 with ITextRegion

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

the class FlexTokenRegionProviderTest method testTokenRegionContainsRegion.

@Test
public void testTokenRegionContainsRegion() throws Exception {
    String modelAsString = "a1 / /* comment */ b2";
    List<CommonToken> tokens = getTokens(modelAsString);
    for (int length = 0; length < modelAsString.length(); ++length) {
        for (int offset = 0; offset + length < modelAsString.length(); ++offset) {
            ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(modelAsString, new TextRegion(offset, length));
            // System.out.println(offset + ":" + length + " -> " + tokenRegion);
            CommonToken firstToken = findTokenStartingAt(tokenRegion.getOffset(), tokens);
            assertTrue(firstToken.getStartIndex() <= offset);
            if (tokenRegion.getLength() != 0) {
                CommonToken lastToken = findTokenStopingAt(tokenRegion.getOffset() + tokenRegion.getLength() - 1, tokens);
                assertTrue(lastToken.getStopIndex() >= offset + length - 1);
            }
        }
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) CommonToken(org.antlr.runtime.CommonToken) Test(org.junit.Test)

Example 10 with ITextRegion

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

the class FlexTokenRegionProviderTest method testTokenMerge.

@Test
public void testTokenMerge() throws Exception {
    String model = "  ";
    ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(model, new TextRegion(1, 0));
    assertEquals(0, tokenRegion.getOffset());
    assertEquals(2, tokenRegion.getLength());
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Test(org.junit.Test)

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