Search in sources :

Example 1 with ASTLocationChainVisitor

use of org.eclipse.titan.designer.AST.ASTLocationChainVisitor in project titan.EclipsePlug-ins by eclipse.

the class OccurencesMarker method doMark.

private void doMark(final IDocument document, final int offset) {
    IAnnotationModel annotationModel = getAnnotationModel();
    if (annotationModel == null) {
        removeOccurences(false);
        error(document, offset, "AnnotationModel is null");
        return;
    }
    IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class);
    if (file == null) {
        removeOccurences(false);
        error(document, offset, "can not determine the file in the editor.");
        return;
    }
    ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(file.getProject());
    if (projectSourceParser == null) {
        removeOccurences(false);
        error(document, offset, "Can not find the projectsourceparser for the project: " + file.getProject());
        return;
    }
    final Module module = projectSourceParser.containedModule(file);
    if (module == null) {
        removeOccurences(false);
        error(document, offset, "The module can not be found in the project.");
        return;
    }
    if (printASTElem.getValue()) {
        ASTLocationChainVisitor locVisitor = new ASTLocationChainVisitor(offset);
        module.accept(locVisitor);
        locVisitor.printChain();
    }
    final Reference reference = getReferenceParser().findReferenceForOpening(file, offset, document);
    if (reference == null) {
        removeOccurences(false);
        // "The reference can not be parsed.");
        return;
    }
    final Location referenceLocaton = reference.getLocation();
    if (referenceLocaton.getOffset() == referenceLocaton.getEndOffset()) {
        removeOccurences(false);
        return;
    }
    // if (reportDebugInformation) {
    // ASTLocationConsistencyVisitor visitor = new
    // ASTLocationConsistencyVisitor(document,
    // module.get_moduletype()==Module.module_type.TTCN3_MODULE);
    // module.accept(visitor);
    // }
    final List<Hit> result = findOccurrences(document, reference, module, offset);
    if (result.isEmpty()) {
        return;
    }
    Map<Annotation, Position> annotationMap = new HashMap<Annotation, Position>();
    for (Hit hit : result) {
        if (hit.identifier != null) {
            int hitOffset = hit.identifier.getLocation().getOffset();
            int hitEndOffset = hit.identifier.getLocation().getEndOffset();
            if (hitOffset >= 0 && hitEndOffset >= 0 && hitEndOffset >= hitOffset) {
                final Annotation annotationToAdd = new Annotation(ANNOTATION_TYPE, false, hit.identifier.getDisplayName());
                final Position position = new Position(hitOffset, hitEndOffset - hitOffset);
                annotationMap.put(annotationToAdd, position);
            }
        }
    }
    synchronized (getLockObject(annotationModel)) {
        if (annotationModel instanceof IAnnotationModelExtension) {
            ((IAnnotationModelExtension) annotationModel).replaceAnnotations(occurrenceAnnotations, annotationMap);
        } else {
            if (occurrenceAnnotations != null) {
                for (Annotation annotationToRemove : occurrenceAnnotations) {
                    annotationModel.removeAnnotation(annotationToRemove);
                }
            }
            for (Map.Entry<Annotation, Position> entry : annotationMap.entrySet()) {
                annotationModel.addAnnotation(entry.getKey(), entry.getValue());
            }
        }
        occurrenceAnnotations = annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) Reference(org.eclipse.titan.designer.AST.Reference) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) Annotation(org.eclipse.jface.text.source.Annotation) Hit(org.eclipse.titan.designer.AST.ReferenceFinder.Hit) ASTLocationChainVisitor(org.eclipse.titan.designer.AST.ASTLocationChainVisitor) Module(org.eclipse.titan.designer.AST.Module) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 IFile (org.eclipse.core.resources.IFile)1 Position (org.eclipse.jface.text.Position)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)1 ASTLocationChainVisitor (org.eclipse.titan.designer.AST.ASTLocationChainVisitor)1 Location (org.eclipse.titan.designer.AST.Location)1 Module (org.eclipse.titan.designer.AST.Module)1 Reference (org.eclipse.titan.designer.AST.Reference)1 Hit (org.eclipse.titan.designer.AST.ReferenceFinder.Hit)1 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)1