Search in sources :

Example 1 with Aadl2LocationInFile

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();
    }
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) XtextResource(org.eclipse.xtext.resource.XtextResource) TargetURIs(org.eclipse.xtext.findReferences.TargetURIs) PropertyType(org.osate.aadl2.PropertyType) URI(org.eclipse.emf.common.util.URI) Aadl2LocationInFile(org.osate.xtext.aadl2.util.Aadl2LocationInFile) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Property(org.osate.aadl2.Property) EReference(org.eclipse.emf.ecore.EReference) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) ComponentType(org.osate.aadl2.ComponentType) BadLocationException(org.eclipse.jface.text.BadLocationException) PropertyConstant(org.osate.aadl2.PropertyConstant) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IReferenceFinder(org.eclipse.xtext.findReferences.IReferenceFinder) ITextRegion(org.eclipse.xtext.util.ITextRegion) ModelUnit(org.osate.aadl2.ModelUnit) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) NamedElement(org.osate.aadl2.NamedElement) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 List (java.util.List)1 Map (java.util.Map)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 EReference (org.eclipse.emf.ecore.EReference)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IReferenceFinder (org.eclipse.xtext.findReferences.IReferenceFinder)1 TargetURIs (org.eclipse.xtext.findReferences.TargetURIs)1 IReferenceDescription (org.eclipse.xtext.resource.IReferenceDescription)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1 CancelIndicator (org.eclipse.xtext.util.CancelIndicator)1 ITextRegion (org.eclipse.xtext.util.ITextRegion)1 ComponentImplementation (org.osate.aadl2.ComponentImplementation)1 ComponentType (org.osate.aadl2.ComponentType)1 ModelUnit (org.osate.aadl2.ModelUnit)1