15
15
package org .eclipse .lsp .cobol .service ;
16
16
17
17
import com .google .common .collect .ImmutableList ;
18
+ import com .google .inject .Inject ;
18
19
import com .google .inject .Singleton ;
19
20
import lombok .Synchronized ;
20
21
import org .eclipse .lsp .cobol .common .AnalysisResult ;
22
+ import org .eclipse .lsp .cobol .common .model .NodeType ;
23
+ import org .eclipse .lsp .cobol .common .model .tree .CopyNode ;
21
24
import org .eclipse .lsp .cobol .service .utils .BuildOutlineTreeFromSyntaxTree ;
22
25
import org .eclipse .lsp4j .Diagnostic ;
23
26
24
- import java .util .List ;
25
- import java .util .Map ;
26
- import java .util .Optional ;
27
+ import java .util .*;
27
28
import java .util .concurrent .ConcurrentHashMap ;
29
+ import java .util .function .Predicate ;
28
30
import java .util .stream .Collectors ;
29
31
30
32
/**
34
36
class DocumentModelService {
35
37
private final Map <String , CobolDocumentModel > docs = new ConcurrentHashMap <>();
36
38
private final DiagnosticRepo diagnosticRepo = new DiagnosticRepo ();
39
+ private final CopybookReferenceRepo copybookReferenceRepo ;
40
+
41
+ @ Inject
42
+ DocumentModelService (CopybookReferenceRepo copybookReferenceRepo ) {
43
+ this .copybookReferenceRepo = copybookReferenceRepo ;
44
+ }
37
45
38
46
/**
39
47
* Mark the document as opened and stores document text
@@ -47,16 +55,6 @@ public void openDocument(String uri, String text) {
47
55
documentModel .setOpened (true );
48
56
}
49
57
50
- /**
51
- * Returns available diagnostic for the document
52
- * @param uri - document uri
53
- * @return list of diagnostic
54
- */
55
- @ Synchronized
56
- public List <Diagnostic > getDiagnostics (String uri ) {
57
- return diagnosticRepo .get (uri );
58
- }
59
-
60
58
/**
61
59
* Returns document model object
62
60
* @param uri - document uri
@@ -78,6 +76,11 @@ public void processAnalysisResult(String uri, AnalysisResult analysisResult) {
78
76
d .setAnalysisResult (analysisResult );
79
77
diagnosticRepo .put (analysisResult .getDiagnostics ());
80
78
d .setOutlineResult (BuildOutlineTreeFromSyntaxTree .convert (analysisResult .getRootNode (), uri ));
79
+ analysisResult .getRootNode ().getDepthFirstStream ()
80
+ .filter (n -> n .getNodeType () == NodeType .COPY )
81
+ .filter (n -> n instanceof CopyNode )
82
+ .map (CopyNode .class ::cast )
83
+ .forEach (n -> copybookReferenceRepo .storeCopybookUsageReference (n .getNameLocation ().getUri (), n .getUri ()));
81
84
});
82
85
}
83
86
@@ -133,26 +136,17 @@ public boolean isDocumentSynced(String uri) {
133
136
*/
134
137
@ Synchronized
135
138
public Map <String , List <Diagnostic >> getOpenedDiagnostic () {
136
- return docs . values (). stream ()
137
- . collect ( Collectors . toMap ( CobolDocumentModel :: getUri , d -> {
138
- List <Diagnostic > diagnostics = diagnosticRepo .get (d .getUri ());
139
- if (! d .isOpened () || diagnostics == null ) {
140
- return ImmutableList . of ( );
141
- }
142
- return diagnostics ;
143
- }));
144
- }
139
+ Map < String , List < Diagnostic >> result = new HashMap <>();
140
+ docs . forEach (( key , value ) -> {
141
+ List <Diagnostic > diagnostics = diagnosticRepo .get (value .getUri ());
142
+ if ( diagnostics != null && value .isOpened ()) {
143
+ result . put ( key , diagnostics );
144
+ } else {
145
+ result . put ( key , ImmutableList . of ()) ;
146
+ }
147
+ });
145
148
146
- @ Synchronized
147
- public Map <String , List <Diagnostic >> getAllDiagnostic () {
148
- return docs .values ().stream ()
149
- .collect (Collectors .toMap (CobolDocumentModel ::getUri , d -> {
150
- List <Diagnostic > diagnostics = diagnosticRepo .get (d .getUri ());
151
- if (!d .isOpened () || diagnostics == null ) {
152
- return ImmutableList .of ();
153
- }
154
- return diagnostics ;
155
- }));
149
+ return result ;
156
150
}
157
151
158
152
/**
@@ -171,4 +165,43 @@ public void updateDocument(String uri, String text) {
171
165
});
172
166
}
173
167
168
+ /**
169
+ * Collects all documents with given uri list
170
+ * @param programs - the uri list
171
+ * @return a list of documents
172
+ */
173
+ @ Synchronized
174
+ public List <CobolDocumentModel > getAll (Set <String > programs ) {
175
+ return programs .stream ().map (docs ::get )
176
+ .filter (Objects ::nonNull )
177
+ .collect (Collectors .toList ());
178
+ }
179
+
180
+ /**
181
+ * Updates copybook and returns all affected opened programs filtered by predicate
182
+ * @param uri - copybook uri
183
+ * @param predicate - filtering predicate for programs
184
+ * @return set of affected opened programs
185
+ */
186
+ @ Synchronized
187
+ public Set <String > findAffectedDocumentsForCopybook (String uri , Predicate <CobolDocumentModel > predicate ) {
188
+ Set <String > affectedPrograms = new HashSet <>();
189
+ copybookReferenceRepo
190
+ .getCopybookUsageReference (uri )
191
+ .forEach (
192
+ curi -> {
193
+ if (Optional .ofNullable (get (curi )).map (CobolDocumentModel ::isOpened ).orElse (false )) {
194
+ affectedPrograms .add (curi );
195
+ }
196
+ });
197
+
198
+ // Add all not synced programs
199
+ affectedPrograms .addAll (docs .values ().stream ()
200
+ .filter (d -> !d .isDocumentSynced ())
201
+ .filter (predicate )
202
+ .map (CobolDocumentModel ::getUri )
203
+ .collect (Collectors .toList ()));
204
+
205
+ return affectedPrograms ;
206
+ }
174
207
}
0 commit comments