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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*
*/
package org.eclipse.osbp.xtext.oxtype.imports;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EContentsEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.osbp.xtext.oxtype.linking.JvmTypeAwareLinkingHelper;
import org.eclipse.osbp.xtext.oxtype.linking.JvmTypeAwareLinkingHelper.IJvmLinkCrossRefStringEnhancer;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.TypesPackage;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.ILocationInFileProvider;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.util.ITextRegion;
import com.google.inject.Inject;
public class EObjectUsageCollector {
@Inject
private ILocationInFileProvider locationInFileProvider;
@Inject
private IShouldImportProvider shouldImportProvider;
@Inject
private JvmTypeAwareLinkingHelper jvmTypeLinkingHelper;
@Inject
private IQualifiedNameProvider nameProvider;
private XtextResource resource;
public EObjectUsages collectTypesToImport(XtextResource resource) {
EObjectUsages result = new EObjectUsages();
if (resource != null && !resource.getContents().isEmpty()) {
this.resource = resource;
collectAllReferences(resource.getContents().get(0), result);
}
return result;
}
private void collectAllReferences(EObject root, EObjectUsages result) {
for (TreeIterator<Object> iterator = EcoreUtil.getAllContents(root,
false); iterator.hasNext();) {
Object type = iterator.next();
if (!(type instanceof EObject)) {
continue;
}
EObject eObject = (EObject) type;
for (EContentsEList.FeatureIterator<EObject> featureIterator = (EContentsEList.FeatureIterator<EObject>) eObject
.eCrossReferences().iterator(); featureIterator.hasNext();) {
EObject toImport = (EObject) featureIterator.next();
EReference eReference = (EReference) featureIterator.feature();
if (!shouldImport(toImport, eReference, root)) {
continue;
}
ITextRegion textRegion = locationInFileProvider
.getFullTextRegion(eObject, eReference, 0);
IParseResult parseResult = resource.getParseResult();
if (parseResult != null) {
String completeText = parseResult.getRootNode().getText();
String crossRefText = completeText.substring(
textRegion.getOffset(), textRegion.getOffset()
+ textRegion.getLength());
EObjectUsage usage = null;
if (toImport.eIsProxy()) {
usage = result.addUnresolvedEObject(crossRefText,
eReference.getEType(), eReference, "",
textRegion, eObject);
} else {
usage = result.addResolvedEObject(toImport,
nameProvider.getFullyQualifiedName(toImport),
textRegion, root);
}
if(usage == null) {
continue;
}
// now we need to handle the jvm links for the semantic
// reference
for (EReference jvmRef : jvmTypeLinkingHelper
.getJvmLinkingReferences(eReference)) {
JvmTypeReference jvmTypeRef = (JvmTypeReference) eObject
.eGet(jvmRef);
IJvmLinkCrossRefStringEnhancer enhancer = jvmTypeLinkingHelper
.getEnhancer(jvmRef);
if (enhancer != null) {
crossRefText = enhancer
.enhance(
eObject,
TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE,
crossRefText);
}
result.addUnresolvedJvmLink(usage, crossRefText,
textRegion, jvmTypeRef);
}
}
}
}
}
protected boolean shouldImport(EObject eObject, EReference eRef,
EObject context) {
return shouldImportProvider.shouldImport(eObject, eRef, context);
}
}
|