39
39
import org .eclipse .core .runtime .Status ;
40
40
import org .eclipse .core .runtime .SubMonitor ;
41
41
import org .eclipse .core .runtime .jobs .ISchedulingRule ;
42
+ import org .eclipse .core .runtime .jobs .Job ;
42
43
import org .eclipse .core .runtime .jobs .MultiRule ;
43
44
import org .eclipse .jdt .core .IBuffer ;
44
45
import org .eclipse .jdt .core .ICompilationUnit ;
@@ -92,12 +93,23 @@ public abstract class BaseDocumentLifeCycleHandler {
92
93
*/
93
94
private static final long DOCUMENT_LIFECYCLE_MAX_DEBOUNCE = 400 ; /*ms*/
94
95
96
+ /**
97
+ * The min & init value of adaptive debounce time for publish diagnostic job.
98
+ */
99
+ private static final long PUBLISH_DIAGNOSTICS_MIN_DEBOUNCE = 400 ; /*ms*/
100
+
101
+ /**
102
+ * The max value of adaptive debounce time for publish diagnostic job.
103
+ */
104
+ private static final long PUBLISH_DIAGNOSTICS_MAX_DEBOUNCE = 3000 ; /*ms*/
105
+
95
106
private CoreASTProvider sharedASTProvider ;
96
107
private WorkspaceJob validationTimer ;
97
108
private WorkspaceJob publishDiagnosticsJob ;
98
109
private Set <ICompilationUnit > toReconcile = new HashSet <>();
99
110
private Map <String , Integer > documentVersions = new HashMap <>();
100
- private MovingAverage movingAverage = new MovingAverage (DOCUMENT_LIFECYCLE_MAX_DEBOUNCE );
111
+ private MovingAverage movingAverageForValidation = new MovingAverage (DOCUMENT_LIFECYCLE_MAX_DEBOUNCE );
112
+ private MovingAverage movingAverageForDiagnostics = new MovingAverage (PUBLISH_DIAGNOSTICS_MIN_DEBOUNCE );
101
113
102
114
public BaseDocumentLifeCycleHandler (boolean delayValidation ) {
103
115
this .sharedASTProvider = CoreASTProvider .getInstance ();
@@ -107,7 +119,7 @@ public BaseDocumentLifeCycleHandler(boolean delayValidation) {
107
119
public IStatus runInWorkspace (IProgressMonitor monitor ) throws CoreException {
108
120
long start = System .currentTimeMillis ();
109
121
IStatus status = performValidation (monitor );
110
- movingAverage .update (System .currentTimeMillis () - start );
122
+ movingAverageForValidation .update (System .currentTimeMillis () - start );
111
123
return status ;
112
124
}
113
125
@@ -156,7 +168,18 @@ protected void triggerValidation(ICompilationUnit cu, long delay) throws JavaMod
156
168
}
157
169
158
170
private long getDocumentLifecycleDelay () {
159
- return Math .min (DOCUMENT_LIFECYCLE_MAX_DEBOUNCE , Math .round (1.5 * movingAverage .value ));
171
+ return Math .min (DOCUMENT_LIFECYCLE_MAX_DEBOUNCE , Math .round (1.5 * movingAverageForValidation .value ));
172
+ }
173
+
174
+ /**
175
+ * @return the delay time of the publish diagnostics job. The value ranges in
176
+ * ({@link #PUBLISH_DIAGNOSTICS_MIN_DEBOUNCE}, {@link #PUBLISH_DIAGNOSTICS_MAX_DEBOUNCE}) ms.
177
+ */
178
+ private long getPublishDiagnosticsDelay () {
179
+ return Math .min (
180
+ Math .max (PUBLISH_DIAGNOSTICS_MIN_DEBOUNCE , Math .round (1.5 * movingAverageForDiagnostics .value )),
181
+ PUBLISH_DIAGNOSTICS_MAX_DEBOUNCE
182
+ );
160
183
}
161
184
162
185
private ISchedulingRule getRule (Set <ICompilationUnit > units ) {
@@ -209,7 +232,7 @@ private IStatus performValidation(IProgressMonitor monitor) throws JavaModelExce
209
232
if (monitor .isCanceled ()) {
210
233
return Status .CANCEL_STATUS ;
211
234
}
212
- publishDiagnosticsJob . schedule ( 400 );
235
+ schedulePublishDiagnosticJob ( );
213
236
} else {
214
237
return publishDiagnostics (new NullProgressMonitor ());
215
238
}
@@ -280,6 +303,26 @@ public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
280
303
unit .reconcile (ICompilationUnit .NO_AST , flags , wcOwner , monitor );
281
304
}
282
305
306
+ /**
307
+ * Cancel the publish diagnostic job when it's <code>Job.WAITING</code> or <code>Job.SLEEPING</code>.
308
+ * Do nothing for other job states.
309
+ * @return <code>true</code> if the job is cancelled, otherwise return <code>false</code>
310
+ */
311
+ public boolean cancelPublishDiagnosticJob () {
312
+ if (publishDiagnosticsJob .getState () == Job .WAITING || publishDiagnosticsJob .getState () == Job .SLEEPING ) {
313
+ publishDiagnosticsJob .cancel ();
314
+ return true ;
315
+ }
316
+ return false ;
317
+ }
318
+
319
+ /**
320
+ * Schedule the publish diagnostic job.
321
+ */
322
+ public void schedulePublishDiagnosticJob () {
323
+ publishDiagnosticsJob .schedule (getPublishDiagnosticsDelay ());
324
+ }
325
+
283
326
public void didClose (DidCloseTextDocumentParams params ) {
284
327
documentVersions .remove (params .getTextDocument ().getUri ());
285
328
ISchedulingRule rule = JDTUtils .getRule (params .getTextDocument ().getUri ());
@@ -652,7 +695,10 @@ private PublishDiagnosticJob(ISchedulingRule rule) {
652
695
653
696
@ Override
654
697
public IStatus runInWorkspace (IProgressMonitor monitor ) throws CoreException {
655
- return publishDiagnostics (monitor );
698
+ long start = System .currentTimeMillis ();
699
+ IStatus status = publishDiagnostics (monitor );
700
+ movingAverageForDiagnostics .update (System .currentTimeMillis () - start );
701
+ return status ;
656
702
}
657
703
658
704
/* (non-Javadoc)
0 commit comments