浏览代码

Use ResourceService to load libraries that are contributed as xtext (#1349)

* Use ResourceService to load libraries that are contributed as xtext 

If a library is added to the scope that is described via xtext, the
resource should be loaded with the corresponding xtext setup /
ResourceDescriptionStrategy / NameProvider etc.

* added missing copyright

* Added missing parameter
Andreas Mülder 8 年之前
父节点
当前提交
0f1de8dc2a

+ 20 - 10
plugins/org.yakindu.base.expressions/src/org/yakindu/base/expressions/scoping/AbstractLibraryGlobalScopeProvider.java

@@ -22,6 +22,8 @@ import org.eclipse.emf.ecore.resource.ResourceSet;
 import org.eclipse.emf.ecore.resource.URIConverter;
 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
 import org.eclipse.xtext.resource.IEObjectDescription;
+import org.eclipse.xtext.resource.IResourceDescription;
+import org.eclipse.xtext.resource.IResourceServiceProvider;
 import org.eclipse.xtext.resource.impl.EObjectDescriptionLookUp;
 import org.eclipse.xtext.scoping.IGlobalScopeProvider;
 import org.eclipse.xtext.scoping.IScope;
@@ -35,6 +37,7 @@ import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
+import com.google.inject.Inject;
 
 /**
  * 
@@ -44,10 +47,13 @@ import com.google.common.collect.Lists;
 public abstract class AbstractLibraryGlobalScopeProvider extends AbstractGlobalScopeProvider
 		implements IGlobalScopeProvider {
 
-	protected abstract Set<URI> getLibraries();
+	@Inject
+	private IResourceServiceProvider.Registry serviceProviderRegistry;
 
-	protected Iterable<URI> getValidLibraries() {
-		return Iterables.filter(getLibraries(), new Predicate<URI>() {
+	protected abstract Set<URI> getLibraries(Resource context);
+
+	protected Iterable<URI> getValidLibraries(Resource context) {
+		return Iterables.filter(getLibraries(context), new Predicate<URI>() {
 			@Override
 			public boolean apply(URI input) {
 				return URIConverter.INSTANCE.exists(input, Collections.EMPTY_MAP);
@@ -69,7 +75,7 @@ public abstract class AbstractLibraryGlobalScopeProvider extends AbstractGlobalS
 	@Override
 	public IScope getScope(Resource context, EReference reference, Predicate<IEObjectDescription> filter) {
 		List<IEObjectDescription> descriptions = Lists.newArrayList();
-		for (URI uri : getValidLibraries()) {
+		for (URI uri : getValidLibraries(context)) {
 			try {
 				Iterables.addAll(descriptions, libraryCache.get(uri));
 			} catch (ExecutionException e) {
@@ -81,15 +87,19 @@ public abstract class AbstractLibraryGlobalScopeProvider extends AbstractGlobalS
 	}
 
 	protected Iterable<IEObjectDescription> getDescriptions(URI uri) {
-		ResourceSet set = new ResourceSetImpl();
 		List<IEObjectDescription> result = Lists.newArrayList();
+		ResourceSet set = new ResourceSetImpl();
 		Resource resource = set.getResource(uri, true);
-		System.out.println("Loading " + uri);
-		Iterable<IEObjectDescription> iterable = Scopes
-				.scopedElementsFor(Lists.newArrayList(resource.getAllContents()));
-		Iterables.addAll(result, iterable);
+		IResourceServiceProvider resourceServiceProvider = serviceProviderRegistry.getResourceServiceProvider(uri);
+		if (resourceServiceProvider == null) {
+			Iterables.addAll(result, Scopes.scopedElementsFor(Lists.newArrayList(resource.getAllContents())));
+		} else {
+			IResourceDescription resourceDescription = resourceServiceProvider.getResourceDescriptionManager()
+					.getResourceDescription(resource);
+			Iterables.addAll(result, resourceDescription.getExportedObjects());
+		}
 		resource.unload();
 		return result;
 	}
 
-}
+}

+ 2 - 1
plugins/org.yakindu.sct.model.stext/src/org/yakindu/sct/model/stext/scoping/STextLibraryGlobalScopeProvider.java

@@ -3,6 +3,7 @@ package org.yakindu.sct.model.stext.scoping;
 import java.util.Set;
 
 import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
 import org.yakindu.base.expressions.scoping.AbstractLibraryGlobalScopeProvider;
 
 import com.google.common.collect.Sets;
@@ -18,7 +19,7 @@ public class STextLibraryGlobalScopeProvider extends AbstractLibraryGlobalScopeP
 			.createURI("platform:/plugin/org.yakindu.sct.model.stext.lib/lib/STextLib.xmi");
 
 	@Override
-	public Set<URI> getLibraries() {
+	public Set<URI> getLibraries(Resource context) {
 		return Sets.newHashSet(STEXT_LIB);
 	}