blob: 2c0a66697dc6ae909170dac6404924c2d75c563a [file] [log] [blame]
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);
}
}