Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » 2.0->2.1 upgrade scopeprovider no longer called
2.0->2.1 upgrade scopeprovider no longer called [message #764583] Mon, 12 December 2011 13:28 Go to next message
gary s thompson is currently offline gary s thompsonFriend
Messages: 92
Registered: July 2009
Member
Dear all
i have upgraded from xtext 2.0 -> xtext 2.1 after a number of attempts, but, i am having problems as my scopeprovider is longer being called (even though when I generated a new project a stub was created) does anyone have any thoughts?

regards
gary

nb here is he scope provider
package uk.ac.leeds.nsxl2.cork4.scoping;

/*
 * generated by Xtext
 */

import java.util.ArrayList;
import java.util.List;

import memops.ecore_api.Implementation.ImplementationPackage;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.JvmFormalParameter;
import org.eclipse.xtext.common.types.TypesPackage;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.IScopeProvider;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider;
import org.eclipse.xtext.scoping.impl.AbstractScope;
import org.eclipse.xtext.scoping.impl.SimpleLocalScopeProvider;
import org.eclipse.xtext.scoping.impl.SimpleScope;
import org.eclipse.xtext.xbase.XForLoopExpression;
import org.eclipse.xtext.xbase.XbasePackage;
import org.eclipse.xtext.xbase.scoping.LocalVariableScopeContext;
import org.eclipse.xtext.xbase.scoping.XbaseScopeProvider;

import com.google.inject.internal.Lists;

import uk.ac.leeds.nsxl2.cork4.cork4.Axis;
import uk.ac.leeds.nsxl2.cork4.cork4.Cork4Package;
import uk.ac.leeds.nsxl2.cork4.cork4.Loop;
import uk.ac.leeds.nsxl2.cork4.cork4.PathHead;
import uk.ac.leeds.nsxl2.cork4.cork4.Predicate;
import uk.ac.leeds.nsxl2.cork4.cork4.RelativeLocationPathHead;
import uk.ac.leeds.nsxl2.cork4.cork4.RelativeLocationPathTail;
import uk.ac.leeds.nsxl2.cork4.cork4.Step;

/**
 * This class contains custom scoping description.
 * 
 * see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#scoping on
 * how and when to use it
 * 
 */
public class Cork4ScopeProvider extends XbaseScopeProvider {
	private final static Class<?>[] SCOPE_CONTAINER_TYPES = { RelativeLocationPathTail.class,
            RelativeLocationPathHead.class, 
            PathHead.class};

    private final static Class<?>[] PATH_ELEMENT_STEP_CONTAINER_TYPES = { RelativeLocationPathTail.class,
				  RelativeLocationPathHead.class,
				  Predicate.class};


	@Override
	public IScope getScope(EObject context, EReference reference) {
		
		final Cork4Package CORK_EPACKAGE = Cork4Package.eINSTANCE;
		final EClass STEP_ECLASS = CORK_EPACKAGE.getStep();
		final EClass PATH_HEAD_ECLASS = CORK_EPACKAGE.getPathHead();
		final EClass RELATIVE_LOCATION_PATH_HEAD_ECLASS = CORK_EPACKAGE.getRelativeLocationPathHead();
		final EClass RELATIVE_LOCATION_PATH_TAIL_ECLASS = CORK_EPACKAGE.getRelativeLocationPathTail();
		
		final EClass LOOP = CORK_EPACKAGE.getLoop();
		final EClass PATH_DEFINITION = CORK_EPACKAGE.getPathDefinition();
		
		final TypesPackage TYPES_PACKAGE =TypesPackage.eINSTANCE;
		final EClass JVM_DECLARED_TYPE =  TYPES_PACKAGE.getJvmDeclaredType();
		
		
		final XbasePackage  X_BASE_PACKAGE  = XbasePackage.eINSTANCE;
		final EClass X_FEATURE_CALL  =  X_BASE_PACKAGE.getXFeatureCall();

		reportScopeContext(context, reference);

		IScope result = null;
		
		EClass referenceContext = (EClass) reference.eContainer();

		if (referenceContext.equals(STEP_ECLASS)) {
			EObject container = findScopeContainer(context);
			StepAxis stepAxis = getStepAxis(context);
			
			if (sameClass(container, PATH_HEAD_ECLASS)) {
				result = getScopeForPathHead(stepAxis);
		    } else if (sameClass(container, RELATIVE_LOCATION_PATH_TAIL_ECLASS)) {
				RelativeLocationPathTail pathTail = (RelativeLocationPathTail) container;
				result=getScopeForRelativeLocationPathTail(pathTail, stepAxis);
			} else if (sameClass(container, RELATIVE_LOCATION_PATH_HEAD_ECLASS)) {
				RelativeLocationPathHead relativeLocationPathHead  = (RelativeLocationPathHead) container;
				result =  getScopeForRelativeLocationPathHead((relativeLocationPathHead), stepAxis);
			}
		}

		
//		System.err.println(context);
//		if (sameClass(context, LOOP) && reference.getEReferenceType().equals(JVM_DECLARED_TYPE)) {
//			Loop loop = (Loop) context;
//			
//			System.err.println(references(PATH_DEFINITION));
//			result = getGlobalScopeForReference(PATH_DEFINITION);
//			System.err.println("here " + reference.getEReferenceType());
//			System.err.println(loop.getPathSelection());
//		}


		if (result == null) {
			result = super.getScope(context, reference);
		}

		return result;

	}
	
	private StepAxis getStepAxis(EObject context) {
		final Cork4Package CORK_EPACKAGE = Cork4Package.eINSTANCE;
		final EClass STEP_ECLASS = CORK_EPACKAGE.getStep();
		final EClass RELATIVE_LOCATION_PATH_TAIL_ECLASS = CORK_EPACKAGE.getRelativeLocationPathTail();
		Axis axis = null;
		
		if (context.eClass().equals(STEP_ECLASS)) {
			Step  step = (Step) context;
			axis = step.getAxis();
			
		} else if (context.eClass().equals(RELATIVE_LOCATION_PATH_TAIL_ECLASS)) {
			RelativeLocationPathTail relativeLocationPathTail = (RelativeLocationPathTail) context;
			Step step = relativeLocationPathTail.getStep();
			if (step != null) {
				axis = step.getAxis();
			}
		}
		StepAxis stepAxis = StepAxis.REFERENCE;
		if (axis != null){
			String axisValue = axis.getValue();
			if (axisValue.equals("@")){
				stepAxis = StepAxis.ATTRIBUTE;
			}
		}
		
		return stepAxis;
	}


	private boolean sameClass(EObject container, EClass eClass) {
		return container.eClass().equals(eClass);
	}

	private EObject findScopeContainer(EObject context) {
		EObject stepContainer = findParentByTypesOrdered(context, PATH_ELEMENT_STEP_CONTAINER_TYPES);
		EObject scopeContainer = findParentByTypesOrdered(stepContainer.eContainer(), SCOPE_CONTAINER_TYPES);
		if (scopeContainer !=  null) {
			stepContainer =  scopeContainer;
		}
		return stepContainer;
	}

	
	
	private IScope getScopeForRelativeLocationPathTail(
			RelativeLocationPathTail container, StepAxis stepAxis) {
		
		EStructuralFeature tailReference = container.getStep()
				.getNodeTest();
		IScope result = getScopeFromReference(tailReference, stepAxis);
		return result;
	}
	
	private IScope getScopeForRelativeLocationPathHead( RelativeLocationPathHead container, StepAxis stepAxis) {
		EStructuralFeature tailReference = (EStructuralFeature) container.getStep()
				.getNodeTest();
		IScope result = getScopeFromReference(tailReference, stepAxis);
		return result;
	}
	
	enum StepAxis {
		ATTRIBUTE,
		REFERENCE,
		OPERATION
	}
	private IScope getScopeForPathHead(StepAxis stepAxis) {
		EClass root = ImplementationPackage.eINSTANCE.getMemopsRoot();
		IScope result = null;
		List<? extends EObject> structuralFeatures = references(root);
		switch (stepAxis) {
			case REFERENCE:
				structuralFeatures =  references(root);
				result = Scopes.scopeFor(structuralFeatures);
				break;
			case ATTRIBUTE:
				structuralFeatures = attributes(root);
				result = Scopes.scopeFor(structuralFeatures);
				break;				
		}
		
		return result;
	}


	private void reportScopeContext(EObject context, EReference reference) {
		IScopeProvider delegate = (IScopeProvider) getDelegate();
//		IScope parentScope = getScope(context, reference);
		EObject parent = context.eContainer();
		
		System.err.println("---------\n");
		System.err.println("containers" + " " + listContainers(context));

		System.err.println("context: " + context);
		EClass refContainer = (EClass) reference.eContainer();
		System.err.println("ref: " + refContainer.getInstanceClassName() + "->"
				+ reference.getName());
		System.err.println("parent: " + parent);
		System.err.println("delegate: " + delegate);
//		System.err.println("parent scope " + parentScope);
		System.err.println();
	}

	private IScope getScopeFromReference(EStructuralFeature tailReference, StepAxis stepAxis) {
		EClassifier tailType = tailReference.getEType();
		IScope result = null;
		if (tailType instanceof EClass) { 
			EClass referenceType = (EClass) tailType;
			if (stepAxis == StepAxis.REFERENCE) {
				result = getGlobalScopeForReference(referenceType);
			} else if (stepAxis == StepAxis.ATTRIBUTE) {
				List<EAttribute> attributes = attributes(referenceType);
				result = Scopes.scopeFor(attributes);
			}
		}
		return result;
	}

	private IScope getGlobalScopeForReference(EClass referenceType) {
		IScope result;
		List<EReference> references = references(referenceType);
		result = Scopes.scopeFor(references);
		return result;
	}


	@SuppressWarnings("unchecked")
	private <T extends EObject> T findParentByTypesOrdered(EObject context,
			Class<?>[] parentTypeList) {
		T result = null;
		EObject parent = context;

		while (parent.eContainer() != null) {
			for (Class<?> clazz : parentTypeList) {
				if (clazz.isAssignableFrom(parent.getClass())) {
					result = (T) parent;
					break;
				}
			}
			if (result != null) {
				break;
			} else {
				parent = parent.eContainer();
			}
		}
		
		return result;
	}

	private String listContainers(EObject parentPath) {
		String result = "";

		EObject parent = parentPath;
		while (parent.eContainer() != null) {
			result = result + " " + parent.eClass().getName();
			parent = parent.eContainer();
		}

		return result;
	}

	private List<EReference> references(EClass target) {
		return org.eclipse.xtext.EcoreUtil2.typeSelect(
				target.getEAllReferences(), EReference.class);
	}
	private List<EAttribute> attributes(EClass target) {
		return org.eclipse.xtext.EcoreUtil2.typeSelect(
				target.getEAllAttributes(), EAttribute.class);
	}
	
	@SuppressWarnings("restriction")
	@Override
	protected IScope  createLocalVarScope(IScope parentScope, LocalVariableScopeContext scopeContext) {
		IScope result = null;
		EObject context = scopeContext.getContext();
		EReference reference = scopeContext.getReference();
		System.err.println("local var scope");
		System.err.println("context " + context);
		System.err.println("reference " + reference);
		if (context.eContainer() instanceof Loop && context.eContainingFeature() == Cork4Package.Literals.LOOP__EXPRESSION) {
			Loop loop = (Loop) context.eContainer();
			JvmFormalParameter type = loop.getPathSelection().getType();
			parentScope = createLocalScopeForParameter(type, parentScope);
		}
//		final CorkPackage CORK_EPACKAGE = CorkPackage.eINSTANCE;
//				final EClass LOOP = CORK_EPACKAGE.getLoop();
//				List<IEObjectDescription> eObjectDescriptions1 = Lists.newArrayList();
//				if(sameClass(context, LOOP)) {
//					Loop loop = (Loop) context;
//		//			if(loop.getPathSelection() != null) {
//		//				String selectionName = loop.getPathSelection().getName();
//		//				RelativeLocationPathTail path = loop.getPathSelection().getPathDefinition().getHead().getTail();
//		//				System.err.println("start");
//		//				EStructuralFeature type = getTailType(path);
//		//				IEObjectDescription eObjectDescription =  EObjectDescription.create(selectionName, type.getEType());
//		//				eObjectDescriptions.add(eObjectDescription);
//		//				System.err.println(type);
//		//				System.err.println("here");
//		//			}
//				}
		
//		List<IEObjectDescription> eObjectDescriptions = eObjectDescriptions1;
//		
//		if (eObjectDescriptions.size() > 0) {
//			result = new SimpleScope(parent, eObjectDescriptions);
//		}
		
//		if (context.eContainer() instanceof  Loop/*referenceContext.equals(X_FEATURE_CALL)*/) {
//			Loop containerloop = context.eContainer();
//
//		}
//		
//		System.err.println(result);
//		if (result == null) {
//			result = super.createLocalVarScope(parentScope, scopeContext);
//		}

		return parentScope;
	}

	private EStructuralFeature getTailType(RelativeLocationPathTail path) {
		final Cork4Package CORK_EPACKAGE = Cork4Package.eINSTANCE;
		final EClass PREDICATE = CORK_EPACKAGE.getPredicate();		
		final EClass STEP = CORK_EPACKAGE.getStep();
		final EClass RELATIVE_LOCATION_PATH_HEAD_ECLASS = CORK_EPACKAGE.getRelativeLocationPathHead();
		final EClass RELATIVE_LOCATION_PATH_TAIL_ECLASS = CORK_EPACKAGE.getRelativeLocationPathTail();
		
		
		EStructuralFeature type = null;
		for ( TreeIterator<Object> iter = EcoreUtil.getAllContents(path,false); 
			  iter.hasNext(); ) {
			EObject current = (EObject) iter.next();
			if (sameClass(current, PREDICATE)) {
				iter.prune();
			}
			if(sameClass(current, STEP)) {
				Step stepCurrent =  (Step) current;
				EObject parent = current.eContainer();
				boolean hasChildren = false;
				if (sameClass(parent, RELATIVE_LOCATION_PATH_HEAD_ECLASS)) {
					RelativeLocationPathHead parentRelativeLocationPathHead = (RelativeLocationPathHead) parent;
					
					if (parentRelativeLocationPathHead.getTail() != null) {
						hasChildren =  true;
					}
					
				}
				if (sameClass(parent, RELATIVE_LOCATION_PATH_TAIL_ECLASS)) {
					RelativeLocationPathTail parentRelativeLocationPathTail = (RelativeLocationPathTail) parent;
					
					if (parentRelativeLocationPathTail.getTail() != null) {
						hasChildren =  true;
					}
					
				}
				if (!hasChildren) {
					EReference nodeTest = (EReference) stepCurrent.getNodeTest();
					type = nodeTest;
					break;
				}
			}
			
		}
		return type;
		
	}
	
	
	
}

Re: 2.0->2.1 upgrade scopeprovider no longer called [message #764761 is a reply to message #764583] Mon, 12 December 2011 20:02 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

You have to bind it in your runtime module. Most likely you are using Xbase, and Xtext binds just the XbaseScopeProvider. Yours should derive from XbaseScopeProvider then.

public Class<? extends IScopeProvider> bindIScopeProvider () { return MyScopeProvider.class; }


~Karsten


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Previous Topic:Trouble with semantic highlighting
Next Topic:Xtend compiler - temp path issue
Goto Forum:
  


Current Time: Fri Apr 26 18:43:00 GMT 2024

Powered by FUDForum. Page generated in 0.02739 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top