22
22
23
23
import org .eclipse .core .resources .IFile ;
24
24
import org .eclipse .core .resources .IResource ;
25
+ import org .eclipse .core .resources .IResourceRuleFactory ;
26
+ import org .eclipse .core .resources .IWorkspace ;
25
27
import org .eclipse .core .resources .IWorkspaceRunnable ;
26
28
import org .eclipse .core .resources .ResourcesPlugin ;
27
29
import org .eclipse .core .resources .WorkspaceJob ;
32
34
import org .eclipse .core .runtime .NullProgressMonitor ;
33
35
import org .eclipse .core .runtime .Status ;
34
36
import org .eclipse .core .runtime .SubMonitor ;
37
+ import org .eclipse .core .runtime .jobs .ISchedulingRule ;
38
+ import org .eclipse .core .runtime .jobs .MultiRule ;
35
39
import org .eclipse .jdt .core .IBuffer ;
36
40
import org .eclipse .jdt .core .ICompilationUnit ;
37
41
import org .eclipse .jdt .core .IJavaElement ;
@@ -90,7 +94,7 @@ public boolean belongsTo(Object family) {
90
94
return DOCUMENT_LIFE_CYCLE_JOBS .equals (family );
91
95
}
92
96
};
93
- this .validationTimer .setRule (ResourcesPlugin .getWorkspace ().getRoot ());
97
+ // this.validationTimer.setRule(ResourcesPlugin.getWorkspace().getRoot());
94
98
this .publishDiagnosticsJob = new WorkspaceJob ("Publish Diagnostics" ) {
95
99
@ Override
96
100
public IStatus runInWorkspace (IProgressMonitor monitor ) throws CoreException {
@@ -105,7 +109,7 @@ public boolean belongsTo(Object family) {
105
109
return PUBLISH_DIAGNOSTICS_JOBS .equals (family );
106
110
}
107
111
};
108
- this .validationTimer .setRule (ResourcesPlugin .getWorkspace ().getRoot ());
112
+ // this.validationTimer.setRule(ResourcesPlugin.getWorkspace().getRoot());
109
113
}
110
114
}
111
115
@@ -129,30 +133,53 @@ protected void triggerValidation(ICompilationUnit cu, long delay) throws JavaMod
129
133
}
130
134
if (validationTimer != null ) {
131
135
validationTimer .cancel ();
136
+ ISchedulingRule rule = getRule (toReconcile );
132
137
if (publishDiagnosticsJob != null ) {
133
138
publishDiagnosticsJob .cancel ();
139
+ publishDiagnosticsJob .setRule (rule );
134
140
}
141
+ validationTimer .setRule (rule );
135
142
validationTimer .schedule (delay );
136
143
} else {
137
144
performValidation (new NullProgressMonitor ());
138
145
}
139
146
}
140
147
148
+ private ISchedulingRule getRule (Set <ICompilationUnit > units ) {
149
+ ISchedulingRule result = null ;
150
+ IResourceRuleFactory ruleFactory = ResourcesPlugin .getWorkspace ().getRuleFactory ();
151
+ for (ICompilationUnit unit : units ) {
152
+ if (unit .getResource () != null ) {
153
+ ISchedulingRule rule = ruleFactory .createRule (unit .getResource ());
154
+ result = MultiRule .combine (rule , result );
155
+ }
156
+ }
157
+ return result ;
158
+ }
159
+
141
160
private IStatus performValidation (IProgressMonitor monitor ) throws JavaModelException {
142
161
long start = System .currentTimeMillis ();
143
162
144
- List <ICompilationUnit > cusToReconcile = new ArrayList <>() ;
163
+ List <ICompilationUnit > cusToReconcile ;
145
164
synchronized (toReconcile ) {
165
+ if (toReconcile .isEmpty ()) {
166
+ return Status .OK_STATUS ;
167
+ }
168
+ cusToReconcile = new ArrayList <>();
146
169
cusToReconcile .addAll (toReconcile );
147
170
toReconcile .clear ();
148
171
}
149
- if (cusToReconcile . isEmpty ()) {
150
- return Status .OK_STATUS ;
172
+ if (monitor . isCanceled ()) {
173
+ return Status .CANCEL_STATUS ;
151
174
}
152
175
// first reconcile all units with content changes
153
176
SubMonitor progress = SubMonitor .convert (monitor , cusToReconcile .size () + 1 );
154
177
for (ICompilationUnit cu : cusToReconcile ) {
155
- cu .reconcile (ICompilationUnit .NO_AST , true , null , progress .newChild (1 ));
178
+ if (monitor .isCanceled ()) {
179
+ return Status .CANCEL_STATUS ;
180
+ }
181
+ cu .makeConsistent (progress );
182
+ //cu.reconcile(ICompilationUnit.NO_AST, false, null, progress.newChild(1));
156
183
}
157
184
JavaLanguageServerPlugin .logInfo ("Reconciled " + toReconcile .size () + ". Took " + (System .currentTimeMillis () - start ) + " ms" );
158
185
if (monitor .isCanceled ()) {
@@ -165,6 +192,9 @@ private IStatus performValidation(IProgressMonitor monitor) throws JavaModelExce
165
192
} catch (InterruptedException e ) {
166
193
// ignore
167
194
}
195
+ if (monitor .isCanceled ()) {
196
+ return Status .CANCEL_STATUS ;
197
+ }
168
198
publishDiagnosticsJob .schedule (400 );
169
199
} else {
170
200
return publishDiagnostics (new NullProgressMonitor ());
@@ -179,6 +209,9 @@ private IStatus publishDiagnostics(IProgressMonitor monitor) throws JavaModelExc
179
209
}
180
210
this .sharedASTProvider .disposeAST ();
181
211
List <ICompilationUnit > toValidate = Arrays .asList (JavaCore .getWorkingCopies (null ));
212
+ if (toValidate .isEmpty ()) {
213
+ return Status .OK_STATUS ;
214
+ }
182
215
SubMonitor progress = SubMonitor .convert (monitor , toValidate .size () + 1 );
183
216
if (monitor .isCanceled ()) {
184
217
return Status .CANCEL_STATUS ;
@@ -188,6 +221,9 @@ private IStatus publishDiagnostics(IProgressMonitor monitor) throws JavaModelExc
188
221
return Status .CANCEL_STATUS ;
189
222
}
190
223
CompilationUnit astRoot = this .sharedASTProvider .getAST (rootToValidate , CoreASTProvider .WAIT_YES , monitor );
224
+ if (monitor .isCanceled ()) {
225
+ return Status .CANCEL_STATUS ;
226
+ }
191
227
if (astRoot != null ) {
192
228
// report errors, even if there are no problems in the file: The client need to know that they got fixed.
193
229
ICompilationUnit unit = (ICompilationUnit ) astRoot .getTypeRoot ();
@@ -207,10 +243,12 @@ private void publishDiagnostics(ICompilationUnit unit, IProgressMonitor monitor)
207
243
*/
208
244
@ Override
209
245
public IBuffer createBuffer (ICompilationUnit workingCopy ) {
210
- ICompilationUnit original = workingCopy .getPrimary ();
211
- IResource resource = original .getResource ();
212
- if (resource instanceof IFile ) {
213
- return new DocumentAdapter (workingCopy , (IFile ) resource );
246
+ if (!monitor .isCanceled ()) {
247
+ ICompilationUnit original = workingCopy .getPrimary ();
248
+ IResource resource = original .getResource ();
249
+ if (resource instanceof IFile ) {
250
+ return new DocumentAdapter (workingCopy , (IFile ) resource );
251
+ }
214
252
}
215
253
return DocumentAdapter .Null ;
216
254
}
@@ -229,53 +267,57 @@ public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
229
267
}
230
268
231
269
public void didClose (DidCloseTextDocumentParams params ) {
270
+ ISchedulingRule rule = JDTUtils .getRule (params .getTextDocument ().getUri ());
232
271
try {
233
272
ResourcesPlugin .getWorkspace ().run (new IWorkspaceRunnable () {
234
273
@ Override
235
274
public void run (IProgressMonitor monitor ) throws CoreException {
236
275
handleClosed (params );
237
276
}
238
- }, new NullProgressMonitor ());
277
+ }, rule , IWorkspace . AVOID_UPDATE , new NullProgressMonitor ());
239
278
} catch (CoreException e ) {
240
279
JavaLanguageServerPlugin .logException ("Handle document close " , e );
241
280
}
242
281
}
243
282
244
283
public void didOpen (DidOpenTextDocumentParams params ) {
284
+ ISchedulingRule rule = JDTUtils .getRule (params .getTextDocument ().getUri ());
245
285
try {
246
286
ResourcesPlugin .getWorkspace ().run (new IWorkspaceRunnable () {
247
287
@ Override
248
288
public void run (IProgressMonitor monitor ) throws CoreException {
249
289
handleOpen (params );
250
290
}
251
- }, new NullProgressMonitor ());
291
+ }, rule , IWorkspace . AVOID_UPDATE , new NullProgressMonitor ());
252
292
} catch (CoreException e ) {
253
293
JavaLanguageServerPlugin .logException ("Handle document open " , e );
254
294
}
255
295
}
256
296
257
297
public void didChange (DidChangeTextDocumentParams params ) {
298
+ ISchedulingRule rule = JDTUtils .getRule (params .getTextDocument ().getUri ());
258
299
try {
259
300
ResourcesPlugin .getWorkspace ().run (new IWorkspaceRunnable () {
260
301
@ Override
261
302
public void run (IProgressMonitor monitor ) throws CoreException {
262
303
handleChanged (params );
263
304
}
264
- }, new NullProgressMonitor ());
305
+ }, rule , IWorkspace . AVOID_UPDATE , new NullProgressMonitor ());
265
306
} catch (CoreException e ) {
266
307
JavaLanguageServerPlugin .logException ("Handle document change " , e );
267
308
}
268
309
}
269
310
270
311
public void didSave (DidSaveTextDocumentParams params ) {
312
+ ISchedulingRule rule = JDTUtils .getRule (params .getTextDocument ().getUri ());
271
313
try {
272
314
JobHelpers .waitForJobs (DocumentLifeCycleHandler .DOCUMENT_LIFE_CYCLE_JOBS , new NullProgressMonitor ());
273
315
ResourcesPlugin .getWorkspace ().run (new IWorkspaceRunnable () {
274
316
@ Override
275
317
public void run (IProgressMonitor monitor ) throws CoreException {
276
318
handleSaved (params );
277
319
}
278
- }, new NullProgressMonitor ());
320
+ }, rule , IWorkspace . AVOID_UPDATE , new NullProgressMonitor ());
279
321
} catch (CoreException e ) {
280
322
JavaLanguageServerPlugin .logException ("Handle document save " , e );
281
323
}
0 commit comments