Selaa lähdekoodia

Merge pull request #502 from Yakindu/issue_500

Moved filter on EnumerationType to context predicate. Added scoping f…
Thomas Kutz 9 vuotta sitten
vanhempi
commit
212abc8c32

+ 14 - 13
plugins/org.yakindu.sct.model.stext/src/org/yakindu/sct/model/stext/scoping/ContextPredicateProvider.java

@@ -59,21 +59,21 @@ import com.google.common.base.Predicates;
  */
 public class ContextPredicateProvider {
 
-	static class TypePredicate implements Predicate<IEObjectDescription> {
+	public static class TypePredicate implements Predicate<IEObjectDescription> {
 		public boolean apply(IEObjectDescription input) {
 			return TypesPackage.Literals.TYPE.isSuperTypeOf(input.getEClass())
 					&& !TypesPackage.Literals.TYPE_PARAMETER.isSuperTypeOf(input.getEClass());
 		}
 	}
 
-	static class FeaturedTypePredicate implements Predicate<IEObjectDescription> {
+	public static class FeaturedTypePredicate implements Predicate<IEObjectDescription> {
 		public boolean apply(IEObjectDescription input) {
 			return TypesPackage.Literals.TYPE.isSuperTypeOf(input.getEClass())
 					&& TypesPackage.Literals.DECLARATION.isSuperTypeOf(input.getEClass());
 		}
 	}
 
-	static class EventPredicate extends FeaturedTypePredicate {
+	public static class EventPredicate extends FeaturedTypePredicate {
 		@Override
 		public boolean apply(IEObjectDescription input) {
 			if (super.apply(input))
@@ -91,7 +91,7 @@ public class ContextPredicateProvider {
 		}
 	}
 
-	static class VariablePredicate extends FeaturedTypePredicate {
+	public static class VariablePredicate extends FeaturedTypePredicate {
 		@Override
 		public boolean apply(IEObjectDescription input) {
 			if (super.apply(input))
@@ -101,7 +101,7 @@ public class ContextPredicateProvider {
 
 	};
 
-	static class VariableOperationPredicate extends FeaturedTypePredicate {
+	public static class VariableOperationPredicate extends FeaturedTypePredicate {
 		@Override
 		public boolean apply(IEObjectDescription input) {
 			if (super.apply(input))
@@ -111,19 +111,20 @@ public class ContextPredicateProvider {
 		}
 	}
 
-	static class VariableOperationEventEnumeratorPredicate extends FeaturedTypePredicate {
+	public static class VariableOperationEventEnumeratorPredicate extends FeaturedTypePredicate {
 		@Override
 		public boolean apply(IEObjectDescription input) {
 			if (super.apply(input))
 				return true;
-			return (TypesPackage.Literals.PROPERTY.isSuperTypeOf(input.getEClass())
-					|| TypesPackage.Literals.OPERATION.isSuperTypeOf(input.getEClass())
-					|| TypesPackage.Literals.EVENT.isSuperTypeOf(input.getEClass()) || TypesPackage.Literals.ENUMERATOR
-						.isSuperTypeOf(input.getEClass()));
+			return (TypesPackage.Literals.PROPERTY.isSuperTypeOf(input.getEClass()) //
+					|| TypesPackage.Literals.OPERATION.isSuperTypeOf(input.getEClass()) //
+					|| TypesPackage.Literals.EVENT.isSuperTypeOf(input.getEClass()) //
+					|| TypesPackage.Literals.ENUMERATOR.isSuperTypeOf(input.getEClass()) //
+					|| TypesPackage.Literals.ENUMERATION_TYPE.isSuperTypeOf(input.getEClass()));
 		}
 	}
 
-	static class EmptyPredicate implements Predicate<IEObjectDescription> {
+	public static class EmptyPredicate implements Predicate<IEObjectDescription> {
 
 		public boolean apply(IEObjectDescription input) {
 			return true;
@@ -146,11 +147,11 @@ public class ContextPredicateProvider {
 		initMap();
 	}
 
-	private Pair<EClass, EReference> key(EClass eClass) {
+	protected Pair<EClass, EReference> key(EClass eClass) {
 		return Tuples.create(eClass, null);
 	}
 
-	private Pair<EClass, EReference> key(EClass eClass, EReference ref) {
+	protected Pair<EClass, EReference> key(EClass eClass, EReference ref) {
 		return Tuples.create(eClass, ref);
 	}
 

+ 16 - 16
plugins/org.yakindu.sct.model.stext/src/org/yakindu/sct/model/stext/scoping/STextScopeProvider.java

@@ -35,7 +35,6 @@ import org.yakindu.base.types.Declaration;
 import org.yakindu.base.types.EnumerationType;
 import org.yakindu.base.types.Type;
 import org.yakindu.base.types.TypeAlias;
-import org.yakindu.base.types.TypesPackage;
 import org.yakindu.base.xtext.utils.jface.viewers.ContextElementAdapter;
 import org.yakindu.sct.model.sgraph.SGraphPackage;
 import org.yakindu.sct.model.sgraph.Scope;
@@ -66,7 +65,6 @@ public class STextScopeProvider extends AbstractDeclarativeScopeProvider {
 
 		public ErrorHandlerDelegate(ErrorHandler<T> delegate) {
 			this.delegate = delegate;
-
 		}
 
 		public T handle(Object[] params, Throwable throwable) {
@@ -112,17 +110,7 @@ public class STextScopeProvider extends AbstractDeclarativeScopeProvider {
 		IScope unnamedScope = getUnnamedTopLevelScope(context, reference);
 		Predicate<IEObjectDescription> predicate = calculateFilterPredicate(context, reference);
 		unnamedScope = new FilteringScope(unnamedScope, predicate);
-		
-		// TODO: this might be a performance problem -> fix this in context of Add Support for Enumerations #165
-		IScope enumerations = new FilteringScope(getDelegate().getScope(context, reference),
-				new Predicate<IEObjectDescription>() {
-					@Override
-					public boolean apply(IEObjectDescription input) {
-						return input.getEClass() == TypesPackage.Literals.ENUMERATION_TYPE;
-					}
-				});
-		return new SimpleScope(Iterables.concat(namdScope.getAllElements(), unnamedScope.getAllElements(),
-				enumerations.getAllElements()));
+		return new SimpleScope(Iterables.concat(namdScope.getAllElements(), unnamedScope.getAllElements()));
 	}
 
 	public IScope scope_FeatureCall_feature(final FeatureCall context, EReference reference) {
@@ -150,11 +138,17 @@ public class STextScopeProvider extends AbstractDeclarativeScopeProvider {
 			scope = addScopeForComplexType((ComplexType) element, scope, predicate);
 		}
 
+		// enumerators can be either within enum types or an type alias that refers to an enum type
 		if (element instanceof EnumerationType) {
-			return Scopes.scopeFor(((EnumerationType) element).getEnumerator(), scope);
-			// scope = new FilteringScope(scope, predicate);
+			return addScopeForEnumType((EnumerationType) element, scope, predicate);
 		}
-
+		if (element instanceof TypeAlias) {
+			Type originType = ((TypeAlias) element).getOriginType();
+			if (originType instanceof EnumerationType) {
+				return addScopeForEnumType((EnumerationType) originType, scope, predicate);
+			}
+		}
+		
 		if (element instanceof Declaration) {
 			Declaration decl = (Declaration) element;
 			if (decl.getType() instanceof ComplexType) {
@@ -170,6 +164,12 @@ public class STextScopeProvider extends AbstractDeclarativeScopeProvider {
 		return scope;
 	}
 
+	protected IScope addScopeForEnumType(EnumerationType element, IScope scope, final Predicate<IEObjectDescription> predicate) {
+		scope = Scopes.scopeFor((element).getEnumerator(), scope);
+		scope = new FilteringScope(scope, predicate);
+		return scope;
+	}
+
 	protected IScope addScopeForComplexType(final ComplexType type, IScope scope, final Predicate<IEObjectDescription> predicate) {
 		scope = Scopes.scopeFor(type.getAllFeatures(), scope);
 		scope = new FilteringScope(scope, predicate);