|
| 1 | +package csep.example.cake.scoping; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.eclipse.emf.ecore.EClass; |
| 7 | +import org.eclipse.emf.ecore.EObject; |
| 8 | +import org.eclipse.emf.ecore.EReference; |
| 9 | +import org.eclipse.xtext.linking.impl.DefaultLinkingService; |
| 10 | +import org.eclipse.xtext.linking.impl.IllegalNodeException; |
| 11 | +import org.eclipse.xtext.naming.IQualifiedNameConverter; |
| 12 | +import org.eclipse.xtext.naming.QualifiedName; |
| 13 | +import org.eclipse.xtext.nodemodel.INode; |
| 14 | +import org.eclipse.xtext.resource.IEObjectDescription; |
| 15 | +import org.eclipse.xtext.scoping.IScope; |
| 16 | +import org.eclipse.xtext.scoping.IScopeProvider; |
| 17 | + |
| 18 | +import com.google.inject.Inject; |
| 19 | + |
| 20 | +/** |
| 21 | + * When {@link LinkingServiceWithNode.getLinkedObjects} is called, an {@link INode} instance is provided so we can calculate the name of the referred variable. |
| 22 | + * The problem is that the call to getScope doesn't pass that node, |
| 23 | + * so the underlying scopingprovider has no way to get that name. |
| 24 | + * XXX: This is a hack to solve this problem. |
| 25 | + * @author adam |
| 26 | + * |
| 27 | + */ |
| 28 | +public class LinkingServiceWithNode extends DefaultLinkingService { |
| 29 | + @Inject |
| 30 | + private IQualifiedNameConverter qualifiedNameConverter; |
| 31 | + |
| 32 | + private INode linkNode; |
| 33 | + |
| 34 | + /** |
| 35 | + * Basically a copy of super, only linkNode is remembered before calling getScope |
| 36 | + */ |
| 37 | + public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) |
| 38 | + throws IllegalNodeException { |
| 39 | + final EClass requiredType = ref.getEReferenceType(); |
| 40 | + if (requiredType == null) |
| 41 | + return Collections.<EObject> emptyList(); |
| 42 | + |
| 43 | + final String crossRefString = getCrossRefNodeAsString(node); |
| 44 | + if (crossRefString != null && !crossRefString.equals("")) { |
| 45 | + linkNode = node; |
| 46 | + final IScope scope = getScope(context, ref); |
| 47 | + linkNode = null; |
| 48 | + QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString); |
| 49 | + IEObjectDescription eObjectDescription = scope.getSingleElement(qualifiedLinkName); |
| 50 | + if (eObjectDescription != null) |
| 51 | + return Collections.singletonList(eObjectDescription.getEObjectOrProxy()); |
| 52 | + } |
| 53 | + return Collections.emptyList(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Mostly a copy of super |
| 58 | + */ |
| 59 | + protected IScope getScope(EObject context, EReference reference) { |
| 60 | + IScopeProvider scopeProvider = getScopeProvider(); |
| 61 | + if (scopeProvider == null) |
| 62 | + throw new IllegalStateException("scopeProvider must not be null."); |
| 63 | + try { |
| 64 | + registerImportedNamesAdapter(context); |
| 65 | + if (scopeProvider instanceof IScopeProviderWithNode) { |
| 66 | + ((IScopeProviderWithNode)scopeProvider).setNode(linkNode); |
| 67 | + } |
| 68 | + return scopeProvider.getScope(context, reference); |
| 69 | + } finally { |
| 70 | + unRegisterImportedNamesAdapter(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments