Search in sources :

Example 6 with AbstractElement

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

the class InsertionPointFinder method findInsertionPoint.

public IHiddenRegion findInsertionPoint(ISerializationContext ctx, IEObjectRegion obj, AbstractElement ins) {
    ISerState insertionState = findState(ctx, ins);
    Set<AbstractElement> followers = collectAdjacent(insertionState, s -> s.getFollowers());
    Set<AbstractElement> precendents = collectAdjacent(insertionState, s -> s.getPrecedents());
    List<IAstRegion> regions = Lists.newArrayList(obj.getAstRegions());
    if (regions.isEmpty()) {
        return obj.getPreviousHiddenRegion();
    }
    if (followers.contains(regions.get(0).getGrammarElement())) {
        return obj.getPreviousHiddenRegion();
    }
    if (precendents.contains(regions.get(regions.size() - 1).getGrammarElement())) {
        return obj.getNextHiddenRegion();
    }
    for (int i = 0; i < regions.size() - 1; i++) {
        IAstRegion leading = regions.get(i);
        IAstRegion trailing = regions.get(i + 1);
        if (precendents.contains(leading.getGrammarElement()) && followers.contains(trailing.getGrammarElement())) {
            return leading.getNextHiddenRegion();
        }
    }
    return null;
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) AbstractElement(org.eclipse.xtext.AbstractElement) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion)

Example 7 with AbstractElement

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

the class InsertionPointFinder method collectAdjacent.

protected Set<AbstractElement> collectAdjacent(ISerState start, Function<ISerState, List<? extends ISerState>> followers) {
    LinkedList<ISerState> todo = new LinkedList<>();
    Set<ISerState> seen = Sets.newHashSet();
    todo.addAll(followers.apply(start));
    Set<AbstractElement> result = Sets.newLinkedHashSet();
    while (!todo.isEmpty()) {
        ISerState next = todo.pop();
        if (!seen.add(next)) {
            continue;
        }
        if (next.getType() == SerStateType.ELEMENT && !GrammarUtil.isUnassignedAction(next.getGrammarElement())) {
            result.add(next.getGrammarElement());
        } else {
            todo.addAll(followers.apply(next));
        }
    }
    return result;
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) AbstractElement(org.eclipse.xtext.AbstractElement) LinkedList(java.util.LinkedList)

Example 8 with AbstractElement

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

the class IndentationAwareCompletionPrefixProvider method getLastCompleteNodeByOffset.

protected INode getLastCompleteNodeByOffset(INode node, int offset) {
    BidiTreeIterator<INode> iterator = node.getRootNode().getAsTreeIterable().iterator();
    INode result = node;
    ITextRegion candidateTextRegion = node.getTextRegion();
    while (iterator.hasNext()) {
        INode candidate = iterator.next();
        ITextRegion textRegion = candidate.getTextRegion();
        if (textRegion.getOffset() >= offset && !(textRegion.getOffset() == offset && textRegion.getLength() == 0)) {
            if (!candidateTextRegion.equals(textRegion) && candidate instanceof ILeafNode && textRegion.getLength() + textRegion.getOffset() >= offset) {
                break;
            }
        }
        if ((candidate instanceof ILeafNode) && (candidate.getGrammarElement() == null || candidate.getGrammarElement() instanceof AbstractElement || candidate.getGrammarElement() instanceof ParserRule)) {
            if (textRegion.getLength() == 0) {
                if (candidateTextRegion.getOffset() + candidateTextRegion.getLength() < offset || candidateTextRegion.getLength() == 0 && candidateTextRegion.getOffset() <= offset) {
                    result = candidate;
                    candidateTextRegion = candidate.getTextRegion();
                }
            } else {
                result = candidate;
                candidateTextRegion = candidate.getTextRegion();
            }
        }
    }
    return result;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) AbstractElement(org.eclipse.xtext.AbstractElement) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 9 with AbstractElement

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

the class BaseContentAssistParser method getInitializedUnorderedGroupHelper.

/**
 * @since 2.14
 */
protected IUnorderedGroupHelper getInitializedUnorderedGroupHelper(FE element, InternalParser parser, UnorderedGroup group) {
    final IUnorderedGroupHelper helper = parser.getUnorderedGroupHelper();
    helper.enter(group);
    for (AbstractElement consumed : element.getHandledUnorderedGroupElements()) {
        parser.before(consumed);
        helper.select(group, group.getElements().indexOf(consumed));
        helper.returnFromSelection(group);
        parser.after(consumed);
    }
    return helper;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper)

Example 10 with AbstractElement

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

the class BaseContentAssistParser method getFollowElements.

public Collection<FE> getFollowElements(FE element) {
    if (element.getLookAhead() <= 1)
        throw new IllegalArgumentException("lookahead may not be less than or equal to 1");
    Collection<FE> result = new ArrayList<FE>();
    for (AbstractElement elementToParse : getElementsToParse(element)) {
        elementToParse = unwrapSingleElementGroups(elementToParse);
        String ruleName = getRuleName(elementToParse);
        String[][] allRuleNames = getRequiredRuleNames(ruleName, element.getParamStack(), elementToParse);
        for (String[] ruleNames : allRuleNames) {
            for (int i = 0; i < ruleNames.length; i++) {
                InternalParser parser = createParser();
                parser.setUnorderedGroupHelper(createUnorderedGroupHelper());
                parser.getUnorderedGroupHelper().initializeWith(parser);
                ObservableXtextTokenStream tokens = setTokensFromFollowElement(parser, element);
                tokens.setListener(parser);
                parser.getGrammarElements().addAll(element.getTrace());
                parser.getGrammarElements().add(elementToParse);
                parser.getLocalTrace().addAll(element.getLocalTrace());
                parser.getLocalTrace().add(elementToParse);
                parser.getParamStack().addAll(element.getParamStack());
                if (elementToParse instanceof UnorderedGroup && element.getGrammarElement() == elementToParse) {
                    UnorderedGroup group = (UnorderedGroup) elementToParse;
                    IUnorderedGroupHelper helper = getInitializedUnorderedGroupHelper(element, parser, group);
                    parser.setUnorderedGroupHelper(ignoreFirstEntrance(helper));
                }
                Collection<FE> elements = getFollowElements(parser, elementToParse, ruleNames, i);
                result.addAll(elements);
            }
        }
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ArrayList(java.util.ArrayList) IUnorderedGroupHelper(org.eclipse.xtext.parser.antlr.IUnorderedGroupHelper) UnorderedGroup(org.eclipse.xtext.UnorderedGroup)

Aggregations

AbstractElement (org.eclipse.xtext.AbstractElement)126 EObject (org.eclipse.emf.ecore.EObject)39 AbstractRule (org.eclipse.xtext.AbstractRule)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)31 RuleCall (org.eclipse.xtext.RuleCall)26 ParserRule (org.eclipse.xtext.ParserRule)18 Grammar (org.eclipse.xtext.Grammar)17 EList (org.eclipse.emf.common.util.EList)15 Assignment (org.eclipse.xtext.Assignment)15 Test (org.junit.Test)14 Group (org.eclipse.xtext.Group)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 XtextResource (org.eclipse.xtext.resource.XtextResource)11 Action (org.eclipse.xtext.Action)10 TerminalRule (org.eclipse.xtext.TerminalRule)10 Alternatives (org.eclipse.xtext.Alternatives)9 EClassifier (org.eclipse.emf.ecore.EClassifier)8 InternalEObject (org.eclipse.emf.ecore.InternalEObject)8