]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/SCLAnnotationModel.java
1a0e7bcc7033c2e01444d0ab1ab30c3e6e00f47b
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / SCLAnnotationModel.java
1 package org.simantics.scl.ui.editor2;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.concurrent.ForkJoinPool;
7
8 import org.eclipse.jface.text.IDocument;
9 import org.eclipse.jface.text.Position;
10 import org.eclipse.jface.text.source.Annotation;
11 import org.eclipse.jface.text.source.AnnotationModel;
12 import org.simantics.scl.compiler.errors.CompilationError;
13 import org.simantics.scl.compiler.errors.DoesNotExist;
14 import org.simantics.scl.compiler.errors.ErrorSeverity;
15 import org.simantics.scl.compiler.errors.Failable;
16 import org.simantics.scl.compiler.errors.Failure;
17 import org.simantics.scl.compiler.errors.Locations;
18 import org.simantics.scl.compiler.module.Module;
19 import org.simantics.scl.compiler.module.repository.ModuleRepository;
20 import org.simantics.scl.compiler.module.repository.UpdateListener;
21
22 public class SCLAnnotationModel extends AnnotationModel {
23     private final SCLModuleEditorInput input;
24     private final ModuleRepository repository;
25     private volatile boolean connected = false;
26     
27     public SCLAnnotationModel(SCLModuleEditorInput input,
28             ModuleRepository repository) {
29         this.input = input;
30         this.repository = repository;
31     }
32     
33     UpdateListener updateListener = new UpdateListener() {
34         @Override
35         public void notifyAboutUpdate() {
36             if(connected)
37                 updateAnnotations();
38         }
39     };
40     
41     private void updateAnnotations() {
42         ForkJoinPool.commonPool().submit(() -> {
43             if (!connected)
44                 return;
45             Failable<Module> result = repository.getModule(input.getModuleName(), updateListener);
46             if(result instanceof Failure) {
47                 Failure failure = (Failure)result;
48                 setAnnotations(Arrays.asList(failure.errors));
49             }
50             else if(result == DoesNotExist.INSTANCE)
51                 setAnnotations(Collections.<CompilationError>emptyList());
52             else
53                 setAnnotations(Arrays.asList(result.getResult().getWarnings()));
54         });
55     }
56     
57     protected void setAnnotations(List<CompilationError> errors) {
58         synchronized(getLockObject()) {
59             removeAllAnnotations();
60             for(CompilationError error : errors) {
61                 Annotation annotation = new Annotation(
62                         error.severity == ErrorSeverity.ERROR || error.severity == ErrorSeverity.IMPORT_ERROR ?
63                                 "org.eclipse.ui.workbench.texteditor.error" : //$NON-NLS-1$
64                                     "org.eclipse.ui.workbench.texteditor.warning", //$NON-NLS-1$
65                                     true, error.description);
66                 int begin = Locations.beginOf(error.location);
67                 int end = Locations.endOf(error.location);
68                 if(begin < 0 || end < begin) {
69                     begin = 0;
70                     end = 1;
71                 }
72                 Position position = new Position(begin, end - begin);
73                 addAnnotation(annotation, position);
74             }
75         }
76     }
77
78     @Override
79     public void connect(IDocument document) {
80         super.connect(document);
81         connected = true;
82         updateAnnotations();
83     }
84     
85     @Override
86     public void disconnect(IDocument document) {
87         connected = false;
88         updateListener.stopListening();
89         super.disconnect(document);
90     }
91 }