Search in sources :

Example 46 with CrossReference

use of org.eclipse.xtext.CrossReference in project xtext-eclipse by eclipse.

the class XtextProposalProvider method completeTerminalRuleCall_Rule.

/**
 * Do not propose enum and parser rules inside of terminal rules.
 */
@Override
public void completeTerminalRuleCall_Rule(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    CrossReference crossReference = (CrossReference) assignment.getTerminal();
    lookupCrossReference(crossReference, context, acceptor, new Predicate<IEObjectDescription>() {

        @Override
        public boolean apply(IEObjectDescription input) {
            return input.getEClass() == XtextPackage.Literals.TERMINAL_RULE;
        }
    });
}
Also used : CrossReference(org.eclipse.xtext.CrossReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 47 with CrossReference

use of org.eclipse.xtext.CrossReference in project xtext-eclipse by eclipse.

the class DefaultReferenceUpdater method createReferenceUpdate.

protected void createReferenceUpdate(EObject referringElement, URI referringResourceURI, EReference reference, int indexInList, EObject newTargetElement, IRefactoringUpdateAcceptor updateAcceptor) {
    if (!transientValueService.isValueInListTransient(referringElement, indexInList, reference)) {
        ITextRegion referenceTextRegion = locationInFileProvider.getFullTextRegion(referringElement, reference, indexInList);
        if (referenceTextRegion != null) {
            CrossReference crossReference = getCrossReference(referringElement, referenceTextRegion.getOffset());
            if (crossReference != null) {
                RefTextEvaluator refTextComparator = getRefTextEvaluator(referringElement, referringResourceURI, reference, indexInList, newTargetElement);
                String newReferenceText = crossReferenceSerializer.getCrossRefText(referringElement, crossReference, newTargetElement, refTextComparator, referenceTextRegion, updateAcceptor.getRefactoringStatus());
                if (newReferenceText == null) {
                    newReferenceText = resolveNameConflict(referringElement, reference, newTargetElement, updateAcceptor);
                }
                if (newReferenceText == null) {
                    updateAcceptor.getRefactoringStatus().add(RefactoringStatus.ERROR, "Refactoring introduces a name conflict.", referringElement, referenceTextRegion);
                }
                createTextChange(referenceTextRegion, newReferenceText, referringElement, newTargetElement, reference, referringResourceURI, updateAcceptor);
            }
        }
    }
}
Also used : RefTextEvaluator(org.eclipse.xtext.ui.refactoring.impl.RefactoringCrossReferenceSerializer.RefTextEvaluator) ITextRegion(org.eclipse.xtext.util.ITextRegion) CrossReference(org.eclipse.xtext.CrossReference)

Example 48 with CrossReference

use of org.eclipse.xtext.CrossReference in project n4js by eclipse.

the class ContentAssistTokenTypeMapper method getInternalTokenType.

/**
 * Converts a grammar element to an Antlr token type (int).
 */
public int getInternalTokenType(EObject grammarElement) {
    Integer type = grammarElements.get(grammarElement);
    if (type == null) {
        if (grammarElement instanceof Keyword) {
            String keyword = ((Keyword) grammarElement).getValue();
            type = tokenTypes.get("'" + keyword + "'");
        } else if (grammarElement instanceof RuleCall) {
            AbstractRule rule = ((RuleCall) grammarElement).getRule();
            type = tokenTypes.get("RULE_" + rule.getName().toUpperCase());
        } else if (grammarElement instanceof AbstractRule) {
            AbstractRule rule = (AbstractRule) grammarElement;
            type = tokenTypes.get("RULE_" + rule.getName().toUpperCase());
        } else if (grammarElement instanceof EnumLiteralDeclaration) {
            String keyword = ((EnumLiteralDeclaration) grammarElement).getLiteral().getValue();
            type = tokenTypes.get("'" + keyword + "'");
        } else if (grammarElement instanceof CrossReference) {
            type = getInternalTokenType(((CrossReference) grammarElement).getTerminal());
        } else {
            throw new IllegalArgumentException(String.valueOf(grammarElement));
        }
        grammarElements.put(grammarElement, type);
    }
    return type;
}
Also used : Keyword(org.eclipse.xtext.Keyword) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) CrossReference(org.eclipse.xtext.CrossReference) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 49 with CrossReference

use of org.eclipse.xtext.CrossReference in project n4js by eclipse.

the class TokenTypeRewriter method rewriteIdentifiers.

private static void rewriteIdentifiers(N4JSGrammarAccess ga, ImmutableMap.Builder<AbstractElement, Integer> builder) {
    ImmutableSet<AbstractRule> identifierRules = ImmutableSet.of(ga.getBindingIdentifierRule(), ga.getIdentifierNameRule(), ga.getIDENTIFIERRule());
    for (ParserRule rule : GrammarUtil.allParserRules(ga.getGrammar())) {
        for (EObject obj : EcoreUtil2.eAllContents(rule.getAlternatives())) {
            if (obj instanceof Assignment) {
                Assignment assignment = (Assignment) obj;
                AbstractElement terminal = assignment.getTerminal();
                int type = InternalN4JSParser.RULE_IDENTIFIER;
                if (terminal instanceof CrossReference) {
                    terminal = ((CrossReference) terminal).getTerminal();
                    type = IDENTIFIER_REF_TOKEN;
                }
                if (terminal instanceof RuleCall) {
                    AbstractRule calledRule = ((RuleCall) terminal).getRule();
                    if (identifierRules.contains(calledRule)) {
                        builder.put(assignment, type);
                    }
                }
            }
        }
    }
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) 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 50 with CrossReference

use of org.eclipse.xtext.CrossReference in project n4js by eclipse.

the class N4JSLinker method installProxies.

/**
 * Installs proxies for all non containment references and only if the node representing the EObject that contains
 * the cross reference has got leaf nodes (as a leaf node represents the cross reference).
 *
 * @param resource
 *            the N4JSResource
 * @param obj
 *            the EObject containing the cross reference
 * @param producer
 *            the error/warning producer
 * @param parentNode
 *            the node representing obj inside the node model
 */
private void installProxies(N4JSResource resource, EObject obj, IDiagnosticProducer producer, ICompositeNode parentNode, boolean dontCheckParent) {
    final EClass eClass = obj.eClass();
    if (eClass.getEAllReferences().size() - eClass.getEAllContainments().size() == 0)
        return;
    for (INode node = parentNode.getFirstChild(); node != null; node = node.getNextSibling()) {
        EObject grammarElement = node.getGrammarElement();
        if (grammarElement instanceof CrossReference && hasLeafNodes(node)) {
            producer.setNode(node);
            CrossReference crossReference = (CrossReference) grammarElement;
            final EReference eRef = GrammarUtil.getReference(crossReference, eClass);
            if (eRef == null) {
                ParserRule parserRule = GrammarUtil.containingParserRule(crossReference);
                final String feature = GrammarUtil.containingAssignment(crossReference).getFeature();
                throw new IllegalStateException("Couldn't find EReference for crossreference '" + eClass.getName() + "::" + feature + "' in parser rule '" + parserRule.getName() + "'.");
            }
            createAndSetProxy(resource, obj, node, eRef, crossReference, producer);
            afterCreateAndSetProxy(obj, node, eRef, crossReference, producer);
        } else if (grammarElement instanceof RuleCall && node instanceof ICompositeNode) {
            RuleCall ruleCall = (RuleCall) grammarElement;
            AbstractRule calledRule = ruleCall.getRule();
            if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
                installProxies(resource, obj, producer, (ICompositeNode) node, true);
            }
        }
    }
    if (!dontCheckParent && shouldCheckParentNode(parentNode)) {
        installProxies(resource, obj, producer, parentNode.getParent(), dontCheckParent);
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EClass(org.eclipse.emf.ecore.EClass) INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) CrossReference(org.eclipse.xtext.CrossReference) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) AbstractRule(org.eclipse.xtext.AbstractRule) EReference(org.eclipse.emf.ecore.EReference) RuleCall(org.eclipse.xtext.RuleCall)

Aggregations

CrossReference (org.eclipse.xtext.CrossReference)53 RuleCall (org.eclipse.xtext.RuleCall)23 EObject (org.eclipse.emf.ecore.EObject)21 AbstractRule (org.eclipse.xtext.AbstractRule)14 AbstractElement (org.eclipse.xtext.AbstractElement)12 Assignment (org.eclipse.xtext.Assignment)11 ParserRule (org.eclipse.xtext.ParserRule)11 INode (org.eclipse.xtext.nodemodel.INode)11 Keyword (org.eclipse.xtext.Keyword)10 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)10 EReference (org.eclipse.emf.ecore.EReference)9 TypeRef (org.eclipse.xtext.TypeRef)7 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)7 Test (org.junit.Test)7 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)6 TerminalRule (org.eclipse.xtext.TerminalRule)6 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)6 Action (org.eclipse.xtext.Action)5 EClass (org.eclipse.emf.ecore.EClass)4 Group (org.eclipse.xtext.Group)4