Search in sources :

Example 61 with AbstractElement

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

the class AbstractFollowElementFactory method createFollowElement.

public FollowElement createFollowElement(AbstractElement current, int lookAhead) {
    if (logger.isDebugEnabled())
        logger.debug("Creating FollowElement for: " + current);
    FollowElement result = doCreateElement();
    result.setLookAhead(lookAhead);
    if (lookAhead != 1) {
        int from = parser.input.index();
        int to = parser.input.size();
        if (parser.marked > 0) {
            from = parser.firstMarker;
        }
        List<LookAheadTerminal> lookAheadTerminals = Lists.newArrayListWithExpectedSize(to - from);
        for (int tokenIndex = from; tokenIndex < to; tokenIndex++) {
            Token token = parser.input.get(tokenIndex);
            if (token != null) {
                LookAheadTerminal lookAheadTerminal = doCreateLookAheadTerminal(token);
                lookAheadTerminals.add(lookAheadTerminal);
            }
        }
        result.setLookAheadTerminals(lookAheadTerminals);
        result.setLookAhead(lookAheadTerminals.size() + 1);
    }
    result.setGrammarElement(current);
    result.setTrace(Lists.newArrayList(Iterators.filter(parser.grammarElements.iterator(), AbstractElement.class)));
    result.setLocalTrace(Lists.newArrayList(Iterators.filter(parser.localTrace.iterator(), AbstractElement.class)));
    result.setParamStack(Lists.newArrayList(parser.paramStack));
    if (current instanceof UnorderedGroup) {
        if (parser.indexToHandledElements != null) {
            int index = parser.grammarElements.lastIndexOf(current);
            List<AbstractElement> alreadyHandled = Lists.newArrayList(Iterators.filter(parser.indexToHandledElements.get(index).iterator(), AbstractElement.class));
            result.setHandledUnorderedGroupElements(alreadyHandled);
        } else {
            result.setHandledUnorderedGroupElements(Collections.<AbstractElement>emptyList());
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("FollowElement is: " + current);
        logger.debug("==================================");
    }
    return result;
}
Also used : ILookAheadTerminal(org.eclipse.xtext.ide.editor.contentassist.antlr.ILookAheadTerminal) AbstractElement(org.eclipse.xtext.AbstractElement) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Token(org.antlr.runtime.Token) BaseFollowElement(org.eclipse.xtext.ide.editor.contentassist.antlr.BaseFollowElement)

Example 62 with AbstractElement

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

the class PartialSerializer method trySerializeMultiValue.

protected List<SerializationStrategy> trySerializeMultiValue(EObject owner, FeatureChange change, IEObjectRegion ownerRegion, IConstraint constraint) {
    EStructuralFeature feature = change.getFeature();
    List<SerializationStrategy> result = Lists.newArrayList();
    IFeatureInfo featureInfo = constraint.getFeatures()[owner.eClass().getFeatureID(feature)];
    List<IConstraintElement> assignments = featureInfo.getAssignments();
    if (assignments.size() != 1) {
        return null;
    }
    IConstraintElement assignment = assignments.get(0);
    if (!assignment.isMany()) {
        return null;
    }
    List<IAstRegion> originals = findRegions(ownerRegion, change);
    EList<ListChange> listChanges = change.getListChanges();
    if (listChanges.isEmpty() && originals.isEmpty()) {
        ISerializationContext ctx = getSerializationContext(owner);
        AbstractElement ins = assignment.getGrammarElement();
        IHiddenRegion insertAt = insertionPointFinder.findInsertionPoint(ctx, ownerRegion, ins);
        if (insertAt == null) {
            return null;
        }
        for (Object value : (List<?>) owner.eGet(feature)) {
            EObject obj = (EObject) value;
            ISerializationContext context = getSerializationContext(obj);
            result.add(new SerializeRecursiveStrategy(insertAt, obj, context));
        }
        return result;
    }
    // ListTransient listTransient = transientValues.isListTransient(owner, feature);
    List<Object> modifying = Lists.newArrayList(((List<?>) owner.eGet(feature)));
    for (ListChange lc : listChanges) {
        ChangeKind kind = lc.getKind();
        if (kind == ADD_LITERAL) {
            IAstRegion region = originals.get(lc.getIndex());
            result.add(new DeleteRegionStrategy(region));
        } else if (kind == MOVE_LITERAL || kind == REMOVE_LITERAL) {
            if (originals.isEmpty()) {
                return null;
            }
            int index = lc.getIndex();
            IHiddenRegion insertAt;
            if (index >= originals.size()) {
                insertAt = ((ISequentialRegion) originals.get(originals.size() - 1)).getNextHiddenRegion();
            } else {
                insertAt = ((ISequentialRegion) originals.get(index)).getPreviousHiddenRegion();
            }
            EObject value = (EObject) modifying.get(index);
            modifying.remove(index);
            if (kind == REMOVE_LITERAL) {
                ISerializationContext context = getSerializationContext(value);
                result.add(new SerializeRecursiveStrategy(insertAt, value, context));
            } else if (kind == ChangeKind.MOVE_LITERAL) {
                int moveToIndex = lc.getMoveToIndex();
                IAstRegion source = originals.get(moveToIndex);
                result.add(new DeleteRegionStrategy(source));
                result.add(new InsertRegionStrategy(insertAt, source));
            }
        }
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) IFeatureInfo(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IFeatureInfo) IConstraintElement(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraintElement) ISequentialRegion(org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) EObject(org.eclipse.emf.ecore.EObject) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) EList(org.eclipse.emf.common.util.EList) ListChange(org.eclipse.emf.ecore.change.ListChange) ChangeKind(org.eclipse.emf.ecore.change.ChangeKind)

Example 63 with AbstractElement

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

the class IdeContentProposalProvider method _createProposals.

protected void _createProposals(final Assignment assignment, final ContentAssistContext context, final IIdeContentProposalAcceptor acceptor) {
    final AbstractElement terminal = assignment.getTerminal();
    if ((terminal instanceof CrossReference)) {
        this.createProposals(terminal, context, acceptor);
    } else {
        if ((terminal instanceof RuleCall)) {
            final AbstractRule rule = ((RuleCall) terminal).getRule();
            if (((rule instanceof TerminalRule) && context.getPrefix().isEmpty())) {
                String _xifexpression = null;
                String _name = rule.getName();
                boolean _equals = Objects.equal(_name, "STRING");
                if (_equals) {
                    String _feature = assignment.getFeature();
                    String _plus = ("\"" + _feature);
                    _xifexpression = (_plus + "\"");
                } else {
                    _xifexpression = assignment.getFeature();
                }
                final String proposal = _xifexpression;
                final Procedure1<ContentAssistEntry> _function = (ContentAssistEntry it) -> {
                    String _name_1 = rule.getName();
                    boolean _equals_1 = Objects.equal(_name_1, "STRING");
                    if (_equals_1) {
                        ArrayList<TextRegion> _editPositions = it.getEditPositions();
                        int _offset = context.getOffset();
                        int _plus_1 = (_offset + 1);
                        int _length = proposal.length();
                        int _minus = (_length - 2);
                        TextRegion _textRegion = new TextRegion(_plus_1, _minus);
                        _editPositions.add(_textRegion);
                        it.setKind(ContentAssistEntry.KIND_TEXT);
                    } else {
                        ArrayList<TextRegion> _editPositions_1 = it.getEditPositions();
                        int _offset_1 = context.getOffset();
                        int _length_1 = proposal.length();
                        TextRegion _textRegion_1 = new TextRegion(_offset_1, _length_1);
                        _editPositions_1.add(_textRegion_1);
                        it.setKind(ContentAssistEntry.KIND_VALUE);
                    }
                    it.setDescription(rule.getName());
                };
                final ContentAssistEntry entry = this.proposalCreator.createProposal(proposal, context, _function);
                acceptor.accept(entry, this.proposalPriorities.getDefaultPriority(entry));
            }
        }
    }
}
Also used : ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) AbstractElement(org.eclipse.xtext.AbstractElement) TextRegion(org.eclipse.xtext.util.TextRegion) ArrayList(java.util.ArrayList) CrossReference(org.eclipse.xtext.CrossReference) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 64 with AbstractElement

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

the class BaseContentAssistParser method getEntryGrammarElement.

protected AbstractElement getEntryGrammarElement(ICompositeNode entryPoint) {
    EObject grammarElement = entryPoint.getGrammarElement();
    if (grammarElement instanceof RuleCall) {
        AbstractRule rule = ((RuleCall) grammarElement).getRule();
        if (rule instanceof ParserRule) {
            if (!GrammarUtil.isMultipleCardinality(rule.getAlternatives())) {
                grammarElement = rule.getAlternatives();
            }
        }
    } else if (grammarElement instanceof ParserRule) {
        grammarElement = ((ParserRule) grammarElement).getAlternatives();
    } else if (grammarElement instanceof CrossReference) {
        grammarElement = GrammarUtil.containingAssignment(grammarElement);
    }
    AbstractElement result = (AbstractElement) grammarElement;
    if (result instanceof Action) {
        return getEntryGrammarElement((ICompositeNode) entryPoint.getFirstChild());
    }
    return result;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 65 with AbstractElement

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

the class CallHierarchyHelper method caseAlternatives.

public Boolean caseAlternatives(List<AbstractElement> elements) {
    EObject wasGrammarElement = this.grammarElement;
    Set<AbstractRule> visiting = Sets.newHashSet(this.visiting);
    boolean foundSomething = false;
    for (AbstractElement group : elements) {
        this.grammarElement = wasGrammarElement;
        this.visiting = Sets.newHashSet(visiting);
        if (doSwitch(group))
            return true;
        if (wasGrammarElement != this.grammarElement) {
            foundSomething = true;
        }
    }
    if (foundSomething) {
        this.grammarElement = nextGrammarElement;
        this.visiting.clear();
        this.visited.clear();
    }
    return Boolean.FALSE;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) AbstractRule(org.eclipse.xtext.AbstractRule)

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