21
21
*/
22
22
package com .github ._1c_syntax .bsl .languageserver .context ;
23
23
24
+ import com .github ._1c_syntax .bsl .languageserver .WorkDoneProgressHelper ;
25
+ import com .github ._1c_syntax .bsl .languageserver .configuration .LanguageServerConfiguration ;
24
26
import com .github ._1c_syntax .bsl .languageserver .utils .MdoRefBuilder ;
27
+ import com .github ._1c_syntax .bsl .languageserver .utils .Resources ;
25
28
import com .github ._1c_syntax .bsl .types .ModuleType ;
26
29
import com .github ._1c_syntax .mdclasses .Configuration ;
27
30
import com .github ._1c_syntax .utils .Absolute ;
32
35
import lombok .extern .slf4j .Slf4j ;
33
36
import org .apache .commons .io .FileUtils ;
34
37
import org .eclipse .lsp4j .TextDocumentItem ;
35
- import org .springframework .beans .factory .annotation . Lookup ;
38
+ import org .springframework .beans .factory .ObjectProvider ;
36
39
import org .springframework .stereotype .Component ;
37
40
38
41
import javax .annotation .CheckForNull ;
39
42
import java .io .File ;
40
43
import java .net .URI ;
41
44
import java .nio .charset .StandardCharsets ;
42
45
import java .nio .file .Path ;
43
- import java .util .Collection ;
44
46
import java .util .Collections ;
45
47
import java .util .EnumMap ;
46
48
import java .util .HashMap ;
49
+ import java .util .List ;
47
50
import java .util .Map ;
48
51
import java .util .Optional ;
49
52
import java .util .concurrent .ExecutionException ;
54
57
@ Slf4j
55
58
@ Component
56
59
@ RequiredArgsConstructor
57
- public abstract class ServerContext {
60
+ public class ServerContext {
61
+ private final ObjectProvider <DocumentContext > documentContextProvider ;
62
+ private final WorkDoneProgressHelper workDoneProgressHelper ;
63
+ private final LanguageServerConfiguration languageServerConfiguration ;
64
+
58
65
private final Map <URI , DocumentContext > documents = Collections .synchronizedMap (new HashMap <>());
59
66
private final Lazy <Configuration > configurationMetadata = new Lazy <>(this ::computeConfigurationMetadata );
60
67
@ CheckForNull
@@ -70,29 +77,42 @@ public void populateContext() {
70
77
LOGGER .info ("Can't populate server context. Configuration root is not defined." );
71
78
return ;
72
79
}
80
+
81
+ var workDoneProgressReporter = workDoneProgressHelper .createProgress (0 , "" );
82
+ workDoneProgressReporter .beginProgress (getMessage ("populateFindFiles" ));
83
+
73
84
LOGGER .debug ("Finding files to populate context..." );
74
- Collection < File > files = FileUtils .listFiles (
85
+ var files = ( List < File >) FileUtils .listFiles (
75
86
configurationRoot .toFile (),
76
87
new String []{"bsl" , "os" },
77
88
true
78
89
);
90
+ workDoneProgressReporter .endProgress ("" );
79
91
populateContext (files );
80
92
}
81
93
82
- public void populateContext (Collection <File > uris ) {
94
+ public void populateContext (List <File > files ) {
95
+ var workDoneProgressReporter = workDoneProgressHelper .createProgress (files .size (), getMessage ("populateFilesPostfix" ));
96
+ workDoneProgressReporter .beginProgress (getMessage ("populatePopulatingContext" ));
97
+
83
98
LOGGER .debug ("Populating context..." );
84
99
contextLock .writeLock ().lock ();
85
100
86
- uris .parallelStream ().forEach ((File file ) -> {
101
+ files .parallelStream ().forEach ((File file ) -> {
102
+
103
+ workDoneProgressReporter .tick ();
104
+
87
105
var documentContext = getDocument (file .toURI ());
88
106
if (documentContext == null ) {
89
- documentContext = createDocumentContext (file , 0 );
107
+ documentContext = createDocumentContext (file );
90
108
documentContext .freezeComputedData ();
91
109
documentContext .clearSecondaryData ();
92
110
}
93
111
});
94
112
95
113
contextLock .writeLock ().unlock ();
114
+
115
+ workDoneProgressReporter .endProgress (getMessage ("populateContextPopulated" ));
96
116
LOGGER .debug ("Context populated." );
97
117
}
98
118
@@ -146,7 +166,7 @@ public DocumentContext addDocument(TextDocumentItem textDocumentItem) {
146
166
}
147
167
148
168
public void removeDocument (URI uri ) {
149
- URI absoluteURI = Absolute .uri (uri );
169
+ var absoluteURI = Absolute .uri (uri );
150
170
removeDocumentMdoRefByUri (absoluteURI );
151
171
documents .remove (absoluteURI );
152
172
}
@@ -162,19 +182,16 @@ public Configuration getConfiguration() {
162
182
return configurationMetadata .getOrCompute ();
163
183
}
164
184
165
- @ Lookup
166
- protected abstract DocumentContext lookupDocumentContext (URI absoluteURI );
167
-
168
185
@ SneakyThrows
169
- private DocumentContext createDocumentContext (File file , int version ) {
170
- String content = FileUtils .readFileToString (file , StandardCharsets .UTF_8 );
171
- return createDocumentContext (file .toURI (), content , version );
186
+ private DocumentContext createDocumentContext (File file ) {
187
+ var content = FileUtils .readFileToString (file , StandardCharsets .UTF_8 );
188
+ return createDocumentContext (file .toURI (), content , 0 );
172
189
}
173
190
174
191
private DocumentContext createDocumentContext (URI uri , String content , int version ) {
175
- URI absoluteURI = Absolute .uri (uri );
192
+ var absoluteURI = Absolute .uri (uri );
176
193
177
- var documentContext = lookupDocumentContext (absoluteURI );
194
+ var documentContext = documentContextProvider . getObject (absoluteURI );
178
195
documentContext .rebuild (content , version );
179
196
180
197
documents .put (absoluteURI , documentContext );
@@ -188,6 +205,9 @@ private Configuration computeConfigurationMetadata() {
188
205
return Configuration .create ();
189
206
}
190
207
208
+ var progress = workDoneProgressHelper .createProgress (0 , "" );
209
+ progress .beginProgress (getMessage ("computeConfigurationMetadata" ));
210
+
191
211
Configuration configuration ;
192
212
var executorService = Executors .newCachedThreadPool ();
193
213
try {
@@ -203,6 +223,8 @@ private Configuration computeConfigurationMetadata() {
203
223
executorService .shutdown ();
204
224
}
205
225
226
+ progress .endProgress (getMessage ("computeConfigurationMetadataDone" ));
227
+
206
228
return configuration ;
207
229
}
208
230
@@ -229,4 +251,8 @@ private void removeDocumentMdoRefByUri(URI uri) {
229
251
mdoRefs .remove (uri );
230
252
}
231
253
}
254
+
255
+ private String getMessage (String key ) {
256
+ return Resources .getResourceString (languageServerConfiguration .getLanguage (), getClass (), key );
257
+ }
232
258
}
0 commit comments