Java Code Examples for org.eclipse.xtext.GrammarUtil#getReference()
The following examples show how to use
org.eclipse.xtext.GrammarUtil#getReference() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrokenConstructorCallAwareEObjectAtOffsetHelper.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Override protected EObject resolveCrossReferencedElement(INode node) { EObject referenceOwner = NodeModelUtils.findActualSemanticObjectFor(node); if (referenceOwner != null) { EReference crossReference = GrammarUtil.getReference((CrossReference) node.getGrammarElement(), referenceOwner.eClass()); if (!crossReference.isMany()) { EObject resultOrProxy = (EObject) referenceOwner.eGet(crossReference); if (resultOrProxy != null && resultOrProxy.eIsProxy() && crossReference == XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR) { if (referenceOwner instanceof XConstructorCall) { JvmIdentifiableElement linkedType = batchTypeResolver.resolveTypes(referenceOwner).getLinkedFeature((XConstructorCall)referenceOwner); if (linkedType != null) return linkedType; } } return resultOrProxy; } else { return super.resolveCrossReferencedElement(node); } } return null; }
Example 2
Source File: GrammarUtilTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testGetReference() throws Exception { this.with(LangATestLanguageStandaloneSetup.class); XtextResource resource = this.getResourceFromStringAndExpect("type A extends B", 1); Assignment asExtends = this.<LangATestLanguageGrammarAccess>get(LangATestLanguageGrammarAccess.class).getTypeAccess().getExtendsAssignment_2_1(); AbstractElement _terminal = asExtends.getTerminal(); CrossReference xref = ((CrossReference) _terminal); EObject _model = this.getModel(resource); Main model = ((Main) _model); EObject typeA = model.getTypes().get(0); EReference ref = GrammarUtil.getReference(xref, typeA.eClass()); Assert.assertNotNull(ref); Assert.assertEquals("extends", ref.getName()); Assert.assertFalse(ref.isMany()); Assert.assertEquals(typeA.eClass(), ref.getEReferenceType()); }
Example 3
Source File: CrossReferenceSerializer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override public boolean isValid(EObject semanticObject, CrossReference crossref, EObject target, INode node, Acceptor errors) { if ((target == null || target.eIsProxy()) && node != null) { CrossReference crossrefFromNode = GrammarUtil.containingCrossReference(node.getGrammarElement()); return crossref == crossrefFromNode; } final EReference ref = GrammarUtil.getReference(crossref, semanticObject.eClass()); final IScope scope = scopeProvider.getScope(semanticObject, ref); if (scope == null) { if (errors != null) errors.accept(diagnostics.getNoScopeFoundDiagnostic(semanticObject, crossref, target)); return false; } if (target != null && target.eIsProxy()) { target = handleProxy(target, semanticObject, ref); } return getCrossReferenceNameFromScope(semanticObject, crossref, target, scope, errors) != null; }
Example 4
Source File: CrossReferenceSerializer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Override public String serializeCrossRef(EObject context, CrossReference grammarElement, final EObject target, INode node) { final EReference ref = GrammarUtil.getReference(grammarElement, context.eClass()); String text = null; if (node != null) { List<EObject> objects = linkingService.getLinkedObjects(context, ref, node); if (Iterables.any(objects, new Predicate<EObject>() { private final URI targetURI = EcoreUtil.getURI(target); @Override public boolean apply(EObject input) { return input == target || EcoreUtil.getURI(input).equals(targetURI); } })) return ITokenSerializer.KEEP_VALUE_FROM_NODE_MODEL; } text = getUnconvertedLinkText(target, ref, context); if (text != null) return getConvertedValue(text, grammarElement); if (node != null) { return linkingHelper.getCrossRefNodeAsString(node, false); } return null; }
Example 5
Source File: EObjectAtOffsetHelper.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected EObject resolveCrossReferencedElement(INode node) { EObject referenceOwner = NodeModelUtils.findActualSemanticObjectFor(node); if (referenceOwner != null) { EReference crossReference = GrammarUtil.getReference((CrossReference) node.getGrammarElement(), referenceOwner.eClass()); if (!crossReference.isMany()) { return (EObject) referenceOwner.eGet(crossReference); } else { List<?> listValue = (List<?>) referenceOwner.eGet(crossReference); List<INode> nodesForFeature = NodeModelUtils.findNodesForFeature(referenceOwner, crossReference); int currentIndex = 0; for (INode nodeForFeature : nodesForFeature) { if (currentIndex >= listValue.size()) return null; if (nodeForFeature.getTotalOffset() <= node.getTotalOffset() && nodeForFeature.getTotalEndOffset() >= node.getTotalEndOffset()) return (EObject) listValue.get(currentIndex); currentIndex++; } } } return null; }
Example 6
Source File: AbstractJavaBasedContentProposalProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
protected void lookupCrossReference(CrossReference crossReference, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor, Predicate<IEObjectDescription> filter) { ParserRule containingParserRule = GrammarUtil.containingParserRule(crossReference); if (!GrammarUtil.isDatatypeRule(containingParserRule)) { EReference ref; if (containingParserRule.isWildcard()) { // TODO we need better ctrl flow analysis here // The cross reference may come from another parser rule then the current model ref = GrammarUtil.getReference(crossReference, contentAssistContext.getCurrentModel().eClass()); } else { ref = GrammarUtil.getReference(crossReference); } if (ref != null) { lookupCrossReference(crossReference, ref, contentAssistContext, acceptor, filter); } } }
Example 7
Source File: CodetemplatesProposalProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Override public void completeVariable_Parameters(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { if ((mode & NORMAL) != 0) { VariableData data = new VariableData(model); if (data.doCreateProposals()) { String variableType = data.variable.getType(); if ("CrossReference".equals(variableType)) { List<CrossReference> crossReferences = GrammarUtil.containedCrossReferences(data.rule); for(CrossReference crossReference: crossReferences) { EReference reference = GrammarUtil.getReference(crossReference); String fqn = reference.getEContainingClass().getName() + "." + reference.getName(); acceptor.accept(createCompletionProposal(fqn, context)); acceptor.accept(createCompletionProposal("'" + fqn + "'", context)); } } else if ("Enum".equals(variableType)) { } super.completeVariable_Parameters(model, assignment, context, acceptor); } } }
Example 8
Source File: AbstractJavaBasedContentProposalProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected void lookupCrossReference(CrossReference crossReference, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor, Predicate<IEObjectDescription> filter, Function<IEObjectDescription, ICompletionProposal> proposalFactory) { ParserRule containingParserRule = GrammarUtil.containingParserRule(crossReference); if (!GrammarUtil.isDatatypeRule(containingParserRule)) { EReference ref = GrammarUtil.getReference(crossReference); lookupCrossReference(contentAssistContext.getCurrentModel(), ref, acceptor, filter, proposalFactory); } }
Example 9
Source File: DefaultQuickfixProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected EReference getUnresolvedEReference(final Issue issue, EObject target) { final ICompositeNode node = NodeModelUtils.getNode(target); if (node==null) return null; ICompositeNode rootNode = node.getRootNode(); ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, issue.getOffset()); CrossReference crossReference = findCrossReference(target, leaf); if (crossReference != null) { return GrammarUtil.getReference(crossReference, target.eClass()); } return null; }
Example 10
Source File: RefactoringCrossReferenceSerializer.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
public String getCrossRefText(EObject owner, CrossReference crossref, EObject target, RefTextEvaluator refTextEvaluator, ITextRegion linkTextRegion, StatusWrapper status) { try { final EReference ref = GrammarUtil.getReference(crossref, owner.eClass()); final IScope scope = scopeProvider.getScope(owner, ref); if (scope == null) { throw new IllegalStateException("Could not create scope for the given cross reference."); } String ruleName = linkingHelper.getRuleNameFrom(crossref); Iterable<IEObjectDescription> descriptionsForCrossRef = scope.getElements(target); String bestRefText = null; List<String> badNames = new ArrayList<String>(); for (IEObjectDescription desc : descriptionsForCrossRef) { try { String unconvertedRefText = qualifiedNameConverter.toString(desc.getName()); String convertedRefText = valueConverter.toString(unconvertedRefText, ruleName); if (refTextEvaluator.isValid(desc) && (bestRefText == null || refTextEvaluator.isBetterThan(convertedRefText, bestRefText))) bestRefText = convertedRefText; } catch (ValueConverterException e) { // this is a problem only if we don't find any matching value badNames.add(desc.getName().toString()); } } if (bestRefText == null && !badNames.isEmpty()) { status.add(WARNING, "Misconfigured language: New reference text has invalid syntax. Following names are in the scope: " + IterableExtensions.join(badNames, ", "), owner, linkTextRegion); } return bestRefText; } catch (Exception exc) { log.error(exc.getMessage(), exc); status.add(ERROR, exc.getMessage(), owner, linkTextRegion); return null; } }
Example 11
Source File: CodetemplatesProposalProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
public void completeNestedCrossReference(CrossReference crossReference, ContentAssistContext context, ICompletionProposalAcceptor acceptor, TemplateData data) { if (data.doCreateProposals()) { ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language); if (helper != null) { String contextTypeId = helper.getId(data.rule); ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language); TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId); TemplateVariableResolver crossRefResolver = getResolver(contextType, "CrossReference"); if (crossRefResolver != null) { Assignment assignment = (Assignment) crossReference.eContainer(); EReference reference = GrammarUtil.getReference(crossReference); if (reference != null) { String proposalText = "${" + assignment.getFeature() + ":CrossReference(" + reference.getEContainingClass().getName() + "." + reference.getName() + ")}"; StyledString displayText = new StyledString("${", StyledString.DECORATIONS_STYLER) .append(assignment.getFeature()) .append(":CrossReference(", StyledString.DECORATIONS_STYLER) .append(reference.getEContainingClass().getName() + "." + reference.getName(), StyledString.COUNTER_STYLER) .append(")}", StyledString.DECORATIONS_STYLER) .append(" - Create a new template variable", StyledString.QUALIFIER_STYLER); ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context); if (proposal instanceof ConfigurableCompletionProposal) { ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal; configurable.setSelectionStart(configurable.getReplacementOffset() + 2); configurable.setSelectionLength(assignment.getFeature().length()); configurable.setAutoInsertable(false); configurable.setSimpleLinkedMode(context.getViewer(), '\t'); configurable.setPriority(configurable.getPriority() * 2); } acceptor.accept(proposal); } } } } }
Example 12
Source File: CrossReferenceSerializer.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override public boolean equalsOrReplacesNode(EObject context, CrossReference crossref, EObject target, INode node) { if (crossref != node.getGrammarElement()) return false; EReference ref = GrammarUtil.getReference(crossref); if (!ref.isMany()) return true; List<EObject> objects = linkingService.getLinkedObjects(context, ref, node); return objects.contains(target); }
Example 13
Source File: CrossReferenceSerializer.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override public boolean isValid(EObject context, CrossReference crossref, EObject target, IErrorAcceptor errorAcceptor) { try { final EReference ref = GrammarUtil.getReference(crossref, context.eClass()); String text = getUnconvertedLinkText(target, ref, context); if (text == null) return true; // maybe we'll find something useful in the node model later on? getConvertedValue(text, crossref); return true; } catch (ValueConverterException e) { if (errorAcceptor != null) errorAcceptor.error(e.getMessage()); return false; } }
Example 14
Source File: AssignmentFinder.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected Set<AbstractElement> findValidAssignmentsForCrossRef(EObject semanticObj, Iterable<AbstractElement> assignedElements, EObject value, INode node) { Set<AbstractElement> result = Sets.newLinkedHashSet(); for (AbstractElement ass : assignedElements) { CrossReference crossref = GrammarUtil.containingCrossReference(ass); EReference eref = GrammarUtil.getReference(crossref, semanticObj.eClass()); if (EcoreUtil2.isAssignableFrom(eref.getEReferenceType(), value.eClass()) && crossRefSerializer.isValid(semanticObj, crossref, value, node, null)) result.add(ass); } return result; }
Example 15
Source File: ParseTreeConstructorUtil.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
private static List<CrossReference> getCrossReferencesWithSameEReference(CrossReference cr) { Grammar g = GrammarUtil.getGrammar(cr); EReference ref = GrammarUtil.getReference(cr); List<CrossReference> result = Lists.newArrayList(); for (CrossReference c : EcoreUtil2.getAllContentsOfType(g, CrossReference.class)) if (GrammarUtil.getReference(c) == ref) result.add(c); return result; }
Example 16
Source File: CrossReferenceSerializer.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override public String serializeCrossRef(EObject semanticObject, CrossReference crossref, EObject target, INode node, Acceptor errors) { if ((target == null || target.eIsProxy()) && node != null) return tokenUtil.serializeNode(node); final EReference ref = GrammarUtil.getReference(crossref, semanticObject.eClass()); final IScope scope = scopeProvider.getScope(semanticObject, ref); if (scope == null) { if (errors != null) errors.accept(diagnostics.getNoScopeFoundDiagnostic(semanticObject, crossref, target)); return null; } if (target != null && target.eIsProxy()) { target = handleProxy(target, semanticObject, ref); } if (target != null && node != null) { String text = linkingHelper.getCrossRefNodeAsString(node, true); QualifiedName qn = qualifiedNameConverter.toQualifiedName(text); URI targetURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(target); for (IEObjectDescription desc : scope.getElements(qn)) { if (targetURI.equals(desc.getEObjectURI())) return tokenUtil.serializeNode(node); } } return getCrossReferenceNameFromScope(semanticObject, crossref, target, scope, errors); }
Example 17
Source File: TokenDiagnosticProvider.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected String getFullReferenceName(EObject semanticObject, CrossReference reference) { EReference ref = GrammarUtil.getReference(reference); String clazz = semanticObject.eClass().getName(); if (ref.getEContainingClass() != semanticObject.eClass()) clazz = ref.getEContainingClass().getName() + "(" + clazz + ")"; return clazz + "." + ref.getName(); }
Example 18
Source File: LazyLinker.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private void installProxies(EObject obj, IDiagnosticProducer producer, Multimap<EStructuralFeature.Setting, INode> settingsToLink, 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()+"'."); } if (!eRef.isResolveProxies() /*|| eRef.getEOpposite() != null see https://bugs.eclipse.org/bugs/show_bug.cgi?id=282486*/) { final EStructuralFeature.Setting setting = ((InternalEObject) obj).eSetting(eRef); settingsToLink.put(new SettingDelegate(setting), node); } else { createAndSetProxy(obj, node, eRef); 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(obj, producer, settingsToLink, (ICompositeNode) node, true); } } } if (!dontCheckParent && shouldCheckParentNode(parentNode)) { installProxies(obj, producer, settingsToLink, parentNode.getParent(), dontCheckParent); } }
Example 19
Source File: CrossRefTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testGetLinkedObjects() throws Exception { XtextResource r = getResourceFromString("type TypeA extends TypeB type TypeB extends TypeA type AnotherType extends TypeA"); Main model = (Main) r.getParseResult().getRootASTElement(); ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(r.getParseResult().getRootNode(), 6); assertEquals(3, model.getTypes().size()); EObject context = model.getTypes().get(0); Assignment asExtends = get(LangATestLanguageGrammarAccess.class).getTypeAccess().getExtendsAssignment_2_1(); CrossReference xref = (CrossReference) asExtends.getTerminal(); EReference ref = GrammarUtil.getReference(xref, context.eClass()); assertEquals(1, getLinkingService().getLinkedObjects(context, ref, leaf).size()); }
Example 20
Source File: N4JSLinker.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** * 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); } }