aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/main/java/tools/refinery/language/scoping/ProblemLocalScopeProvider.java
blob: 0067bf94d0cfd1fb13592491e96f44789ff5e7fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.scoping;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.IResourceDescriptionsProvider;
import org.eclipse.xtext.resource.ISelectable;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.impl.AbstractGlobalScopeDelegatingScopeProvider;
import org.eclipse.xtext.util.IResourceScopeCache;
import tools.refinery.language.naming.ProblemQualifiedNameProvider;

public class ProblemLocalScopeProvider extends AbstractGlobalScopeDelegatingScopeProvider {
	private static final String CACHE_KEY = "tools.refinery.language.scoping.ProblemLocalScopeProvider.CACHE_KEY";

	@Inject
	@Named(ProblemQualifiedNameProvider.NAMED_DELEGATE)
	private IQualifiedNameProvider delegateQualifiedNameProvider;

	@Inject
	private IResourceDescriptionsProvider resourceDescriptionsProvider;

	@Inject
	private IResourceScopeCache cache;

	@Override
	public IScope getScope(EObject context, EReference reference) {
		var resource = context.eResource();
		if (resource == null) {
			return IScope.NULLSCOPE;
		}
		var globalScope = getGlobalScope(resource, reference);
		var localImports = cache.get(CACHE_KEY, resource, () -> computeLocalImports(resource));
		if (localImports == null) {
			return globalScope;
		}
		var type = reference.getEReferenceType();
		boolean ignoreCase = isIgnoreCase(reference);
		return ShadowingKeyAwareSelectableBasedScope.createScope(globalScope, localImports, type, ignoreCase);
	}

	protected ISelectable computeLocalImports(Resource resource) {
		// Force the use of ProblemResourceDescriptionStrategy to include all QualifiedNames of objects.
		var resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(resource.getResourceSet());
		var resourceDescription = resourceDescriptions.getResourceDescription(resource.getURI());
		if (resourceDescription == null) {
			return null;
		}
		var rootElement = resource.getContents().getFirst();
		if (rootElement == null) {
			return new NoFullyQualifiedNamesSelectable(resourceDescription);
		}
		var rootName = delegateQualifiedNameProvider.getFullyQualifiedName(rootElement);
		if (rootName == null) {
			return new NoFullyQualifiedNamesSelectable(resourceDescription);
		}
		return new NormalizedSelectable(resourceDescription, rootName, QualifiedName.EMPTY);
	}
}