use of org.osate.xtext.aadl2.util.Aadl2LocationInFile in project osate2 by osate.
the class Aadl2OccurrenceComputer method createAnnotationMap.
@Override
public Map<Annotation, Position> createAnnotationMap(XtextEditor editor, final ITextSelection selection, final SubMonitor monitor) {
final IXtextDocument document = editor.getDocument();
if (document != null) {
return document.tryReadOnly(new CancelableUnitOfWork<Map<Annotation, Position>, XtextResource>() {
@Override
public Map<Annotation, Position> exec(XtextResource resource, final CancelIndicator cancelIndicator) throws Exception {
EObject target = eObjectAtOffsetHelper.resolveElementAt(resource, selection.getOffset());
if (target != null && !target.eIsProxy()) {
final List<EObjectReferenceAndIndex> references = newArrayList();
IReferenceFinder.Acceptor acceptor = new IReferenceFinder.Acceptor() {
@Override
public void accept(IReferenceDescription reference) {
throw new UnsupportedOperationException("Local references are announced per object");
}
@Override
public void accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
EObjectReferenceAndIndex acceptMe = new EObjectReferenceAndIndex();
acceptMe.source = source;
acceptMe.reference = eReference;
acceptMe.idx = index;
references.add(acceptMe);
}
};
Iterable<URI> targetURIs = getTargetURIs(target);
if (!(targetURIs instanceof TargetURIs)) {
TargetURIs result = targetURIsProvider.get();
result.addAllURIs(targetURIs);
targetURIs = result;
}
IProgressMonitor localMonitor = new NullProgressMonitor() {
@Override
public boolean isCanceled() {
return monitor.isCanceled() || cancelIndicator.isCanceled();
}
};
referenceFinder.findReferences((TargetURIs) targetURIs, resource, acceptor, localMonitor);
operationCanceledManager.checkCanceled(cancelIndicator);
Map<Annotation, Position> result = newHashMapWithExpectedSize(references.size() + 1);
if (target.eResource() == resource) {
if (!references.isEmpty() || canBeReferencedLocally(target)) {
ITextRegion declarationRegion = locationInFileProvider.getSignificantTextRegion(target);
addOccurrenceAnnotation(DECLARATION_ANNOTATION_TYPE, document, declarationRegion, result);
declarationRegion = ((Aadl2LocationInFile) locationInFileProvider).getSecondaryTextRegion(target, !(target instanceof ComponentImplementation));
if (declarationRegion != null) {
addOccurrenceAnnotation(OCCURRENCE_ANNOTATION_TYPE, document, declarationRegion, result);
if (target instanceof ComponentType) {
ModelUnit pkg = EcoreUtil2.getContainerOfType(target, ModelUnit.class);
for (ComponentImplementation impl : EcoreUtil2.getAllContentsOfType(pkg, ComponentImplementation.class)) {
if (impl.getType() == target) {
declarationRegion = ((Aadl2LocationInFile) locationInFileProvider).getSecondaryTextRegion(impl, true);
if (declarationRegion != null) {
addOccurrenceAnnotation(OCCURRENCE_ANNOTATION_TYPE, document, declarationRegion, result);
}
}
}
}
}
}
}
for (EObjectReferenceAndIndex highlightMe : references) {
try {
if (localMonitor.isCanceled()) {
return emptyMap();
}
ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(highlightMe.source, highlightMe.reference, highlightMe.idx);
if (target instanceof ComponentImplementation) {
ComponentImplementation impl = (ComponentImplementation) target;
textRegion = getAdjustedRegion(document, textRegion, impl.getImplementationName(), textRegion);
} else if (target instanceof ComponentType || target instanceof Property || target instanceof PropertyType || target instanceof PropertyConstant) {
NamedElement named = (NamedElement) target;
textRegion = getAdjustedRegion(document, textRegion, named.getName(), textRegion);
} else if (target instanceof ModelUnit) {
NamedElement named = (NamedElement) target;
textRegion = getAdjustedRegion(document, textRegion, named.getName(), null);
}
if (textRegion != null) {
addOccurrenceAnnotation(OCCURRENCE_ANNOTATION_TYPE, document, textRegion, result);
}
} catch (Exception exc) {
// outdated index information. Ignore
}
}
return result;
}
return emptyMap();
}
}, () -> emptyMap());
} else {
return emptyMap();
}
}
Aggregations