Search in sources :

Example 76 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class FormattingConfigBasedStream method collectLocators.

protected Set<ElementLocator> collectLocators(EObject ele) {
    Set<ElementLocator> result = Sets.newHashSet(activeRangeLocators);
    Collection<ElementLocator> loc = Sets.newHashSet();
    if (ele instanceof AbstractElement)
        for (ElementPattern pattern : matcher.matchNext((AbstractElement) ele)) loc.add(pattern.getLocator());
    if ((last instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) last)) || (ele instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) ele)))
        loc = collectLocatorsForComments(loc, last, ele);
    last = ele;
    for (ElementLocator locator : loc) if (locator.getType() == LocatorType.RANGE && !activeRangeLocators.add(locator))
        activeRangeLocators.remove(locator);
    result.addAll(loc);
    for (ElementLocator locator : result) {
        if (locator instanceof IndentationLocatorStart)
            indentationLevel++;
        else if (locator instanceof IndentationLocatorEnd)
            indentationLevel--;
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ElementLocator(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator) ElementPattern(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementPattern) AbstractRule(org.eclipse.xtext.AbstractRule) IndentationLocatorStart(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorStart) IndentationLocatorEnd(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorEnd)

Example 77 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class AbstractFormatter2 method createCommentReplacer.

public ITextReplacer createCommentReplacer(IComment comment) {
    EObject grammarElement = comment.getGrammarElement();
    if (grammarElement instanceof AbstractRule) {
        String ruleName = ((AbstractRule) grammarElement).getName();
        if (ruleName.startsWith("ML"))
            return new MultilineCommentReplacer(comment, '*');
        if (ruleName.startsWith("SL")) {
            if (comment.getLineRegions().get(0).getIndentation().getLength() > 0)
                return new SinglelineDocCommentReplacer(comment, "//");
            else
                return new SinglelineCodeCommentReplacer(comment, "//");
        }
    }
    String elementName = new GrammarElementTitleSwitch().showQualified().showRule().doSwitch(grammarElement);
    throw new IllegalStateException("No " + ITextReplacer.class.getSimpleName() + " configured for " + elementName);
}
Also used : GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) MultilineCommentReplacer(org.eclipse.xtext.formatting2.internal.MultilineCommentReplacer) EObject(org.eclipse.emf.ecore.EObject) SinglelineCodeCommentReplacer(org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer) SinglelineDocCommentReplacer(org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 78 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class AbstractSemanticRegionsFinder method keywordPairs.

@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(String kw1, String kw2) {
    Preconditions.checkNotNull(kw1);
    Preconditions.checkNotNull(kw2);
    Preconditions.checkArgument(!kw1.equals(kw2));
    Predicate<ISemanticRegion> p1 = new KeywordPredicate(kw1);
    Predicate<ISemanticRegion> p2 = new KeywordPredicate(kw2);
    List<ISemanticRegion> all = findAll(Predicates.or(p1, p2));
    Builder<Pair<ISemanticRegion, ISemanticRegion>> result = ImmutableList.builder();
    LinkedList<ISemanticRegion> stack = new LinkedList<ISemanticRegion>();
    for (ISemanticRegion region : all) {
        if (p1.apply(region))
            stack.push(region);
        else {
            AbstractRule regionRule = GrammarUtil.containingRule(region.getGrammarElement());
            while (!stack.isEmpty()) {
                ISemanticRegion candidate = stack.pop();
                if (region.getSemanticElement() == candidate.getSemanticElement()) {
                    AbstractRule candidateRule = GrammarUtil.containingRule(candidate.getGrammarElement());
                    if (regionRule == candidateRule) {
                        result.add(Pair.of(candidate, region));
                        break;
                    }
                }
            }
        }
    }
    return result.build();
}
Also used : ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) AbstractRule(org.eclipse.xtext.AbstractRule) LinkedList(java.util.LinkedList) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 79 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class StringBasedTextRegionAccessDiffAppender method copyAndAppend.

public IHiddenRegionPart copyAndAppend(IHiddenRegionPart part) {
    StringHiddenRegion region;
    if (this.last instanceof StringHiddenRegion) {
        region = (StringHiddenRegion) this.last;
    } else {
        region = appendHiddenRegion(true);
    }
    String text = part.getText();
    int offset = result.append(text);
    if (part instanceof IComment) {
        IComment comment = ((IComment) part);
        AbstractRule grammarElement = (AbstractRule) comment.getGrammarElement();
        StringComment newComment = new StringComment(region, grammarElement, offset, text.length());
        region.addPart(newComment);
        recordDiff(part, newComment);
        return newComment;
    } else if (part instanceof IWhitespace) {
        IWhitespace ws = (IWhitespace) part;
        AbstractRule grammarElement = (AbstractRule) ws.getGrammarElement();
        StringWhitespace newWs = new StringWhitespace(region, grammarElement, offset, text.length());
        region.addPart(newWs);
        recordDiff(part, newWs);
        return newWs;
    }
    throw new IllegalStateException();
}
Also used : IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 80 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.

the class GrammarUtilTest method testFindCurrentType_01.

@Test
public void testFindCurrentType_01() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate g \'http://1\'");
    _builder.newLine();
    _builder.append("Rule:");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Fragment;");
    _builder.newLine();
    _builder.append("fragment Fragment*: name=ID;");
    _builder.newLine();
    String model = _builder.toString();
    final XtextResource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    final Grammar grammar = ((Grammar) _get);
    final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
    final AbstractElement fragmentCall = rule.getAlternatives();
    final EClassifier currentType = GrammarUtil.findCurrentType(fragmentCall);
    Assert.assertEquals("Rule", currentType.getName());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EClassifier(org.eclipse.emf.ecore.EClassifier) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Aggregations

AbstractRule (org.eclipse.xtext.AbstractRule)222 Test (org.junit.Test)128 Grammar (org.eclipse.xtext.Grammar)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)96 ParserRule (org.eclipse.xtext.ParserRule)94 EObject (org.eclipse.emf.ecore.EObject)59 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 RuleCall (org.eclipse.xtext.RuleCall)36 AbstractElement (org.eclipse.xtext.AbstractElement)34 TerminalRule (org.eclipse.xtext.TerminalRule)20 XtextResource (org.eclipse.xtext.resource.XtextResource)17 EClassifier (org.eclipse.emf.ecore.EClassifier)14 EnumRule (org.eclipse.xtext.EnumRule)14 ArrayList (java.util.ArrayList)12 Assignment (org.eclipse.xtext.Assignment)12 Group (org.eclipse.xtext.Group)12 InternalEObject (org.eclipse.emf.ecore.InternalEObject)11 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)11 EClass (org.eclipse.emf.ecore.EClass)10 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)10