@@ -118,28 +118,30 @@ public class ErrorCheckerService {
118118 private volatile long nextUiUpdate = 0 ;
119119
120120 private final Object requestLock = new Object ();
121- private final Object serialCallbackLock = new Object ();
122121 private boolean needsCheck = false ;
123122
124123 private CompletableFuture <PreprocessedSketch > preprocessingTask =
125124 new CompletableFuture <PreprocessedSketch >() {{
126125 complete (PreprocessedSketch .empty ()); // initialization block
127126 }};
128127
129- private CompletableFuture <?> lastErrorCheckTask =
130- new CompletableFuture () {{
131- complete (null ); // initalization block
132- }};
133-
134128 private CompletableFuture <?> lastCallback =
135129 new CompletableFuture () {{
136130 complete (null ); // initialization block
137131 }};
138132
133+ private final Consumer <PreprocessedSketch > errorHandlerListener = this ::handleSketchErrors ;
134+
135+ private volatile boolean isEnabled = true ;
136+ private volatile boolean isContinuousCheckEnabled = true ;
137+
139138
140139 public ErrorCheckerService (JavaEditor editor ) {
141140 this .editor = editor ;
142141 astGenerator = new ASTGenerator (editor , this );
142+ isEnabled = !editor .hasJavaTabs ();
143+ isContinuousCheckEnabled = JavaMode .errorCheckEnabled ;
144+ registerDoneListener (errorHandlerListener );
143145 }
144146
145147
@@ -202,24 +204,14 @@ public void cancel() {
202204
203205
204206 public void notifySketchChanged () {
205- if (editor . hasJavaTabs () ) return ;
207+ if (! isEnabled ) return ;
206208 synchronized (requestLock ) {
207209 if (preprocessingTask .isDone ()) {
208210 preprocessingTask = new CompletableFuture <>();
209- lastErrorCheckTask = preprocessingTask
210- // Run error handler after both preprocessing
211- // task and previous error handler completed
212- .thenAcceptBothAsync (lastErrorCheckTask ,
213- (ps , a ) -> handleSketchErrors (ps ))
214- // Make sure exception in error handler won't spoil the chain
215- .handleAsync ((res , e ) -> {
216- if (e != null ) Messages .loge ("problem during error handling" , e );
217- return res ;
218- });
219- // Fire listeners, don't trigger check
220- acceptWhenDone (this ::fireDoneListeners , false );
211+ // Register callback which executes all listeners
212+ registerCallback (this ::fireDoneListeners );
221213 }
222- if (isContinuousCheckEnabled () ) {
214+ if (isContinuousCheckEnabled ) {
223215 // Continuous check enabled, request
224216 nextUiUpdate = System .currentTimeMillis () + errorCheckInterval ;
225217 requestQueue .offer (Boolean .TRUE );
@@ -231,24 +223,9 @@ public void notifySketchChanged() {
231223 }
232224
233225
234- public void acceptWhenDone (Consumer <PreprocessedSketch > callback ) {
235- // Public version always triggers check
236- acceptWhenDone (callback , true );
237- }
238-
239-
240- private void acceptWhenDone (Consumer <PreprocessedSketch > callback , boolean triggerCheck ) {
241- if (editor .hasJavaTabs ()) return ;
242- if (triggerCheck ) {
243- // Continuous check not enabled, request check now
244- synchronized (requestLock ) {
245- if (!isContinuousCheckEnabled () && needsCheck ) {
246- needsCheck = false ;
247- requestQueue .offer (Boolean .TRUE );
248- }
249- }
250- }
251- synchronized (serialCallbackLock ) {
226+ private void registerCallback (Consumer <PreprocessedSketch > callback ) {
227+ if (!isEnabled ) return ;
228+ synchronized (requestLock ) {
252229 lastCallback = preprocessingTask
253230 // Run callback after both preprocessing task and previous callback
254231 .thenAcceptBothAsync (lastCallback , (ps , a ) -> callback .accept (ps ))
@@ -261,6 +238,19 @@ private void acceptWhenDone(Consumer<PreprocessedSketch> callback, boolean trigg
261238 }
262239
263240
241+ public void acceptWhenDone (Consumer <PreprocessedSketch > callback ) {
242+ if (!isEnabled ) return ;
243+ synchronized (requestLock ) {
244+ // Continuous check not enabled, request check now
245+ if (needsCheck && !isContinuousCheckEnabled ) {
246+ needsCheck = false ;
247+ requestQueue .offer (Boolean .TRUE );
248+ }
249+ registerCallback (callback );
250+ }
251+ }
252+
253+
264254
265255 /// LISTENERS ----------------------------------------------------------------
266256
@@ -529,7 +519,7 @@ private void handleSketchErrors(PreprocessedSketch ps) {
529519 }
530520 }
531521
532- final boolean updateErrorToggle = ps .hasSyntaxErrors ||
522+ final boolean hasErrors = ps .hasSyntaxErrors ||
533523 ps .hasCompilationErrors ;
534524
535525 if (scheduledUiUpdate != null ) {
@@ -540,11 +530,8 @@ private void handleSketchErrors(PreprocessedSketch ps) {
540530 Runnable uiUpdater = () -> {
541531 if (nextUiUpdate > 0 && System .currentTimeMillis () >= nextUiUpdate ) {
542532 EventQueue .invokeLater (() -> {
543- if (JavaMode .errorCheckEnabled ) {
544- updateErrorTable (problems );
545- editor .updateErrorBar (problems );
546- editor .getTextArea ().repaint ();
547- editor .updateErrorToggle (updateErrorToggle );
533+ if (isContinuousCheckEnabled ) {
534+ setProblemList (problems , hasErrors );
548535 }
549536 });
550537 }
@@ -554,6 +541,14 @@ private void handleSketchErrors(PreprocessedSketch ps) {
554541 }
555542
556543
544+ protected void setProblemList (List <Problem > problems , boolean hasErrors ) {
545+ updateErrorTable (problems );
546+ editor .updateErrorBar (problems );
547+ editor .getTextArea ().repaint ();
548+ editor .updateErrorToggle (hasErrors );
549+ }
550+
551+
557552 protected String [] buildClassPath (List <ImportStatement > neededImports ) {
558553 JavaMode mode = (JavaMode ) editor .getMode ();
559554 Sketch sketch = editor .getSketch ();
@@ -811,7 +806,7 @@ public void updateEditorStatus() {
811806
812807 // editor.statusNotice("Position: " +
813808 // editor.getTextArea().getCaretLine());
814- if (JavaMode . errorCheckEnabled ) {
809+ if (isContinuousCheckEnabled ) {
815810 LineMarker errorMarker = editor .findError (editor .getTextArea ().getCaretLine ());
816811 if (errorMarker != null ) {
817812 if (errorMarker .getType () == LineMarker .WARNING ) {
@@ -897,22 +892,28 @@ protected static boolean checkIfImportsChanged(List<ImportStatement> prevImports
897892 }
898893
899894
900- private static boolean isContinuousCheckEnabled () {
901- return JavaMode .errorCheckEnabled ;
895+ public void handlePreferencesChange () {
896+ isContinuousCheckEnabled = JavaMode .errorCheckEnabled ;
897+ if (isContinuousCheckEnabled ) {
898+ Messages .log (editor .getSketch ().getName () + " Error Checker enabled." );
899+ notifySketchChanged ();
900+ } else {
901+ Messages .log (editor .getSketch ().getName () + " Error Checker disabled." );
902+ setProblemList (Collections .emptyList (), false );
903+ }
902904 }
903905
904906
905- public void handleErrorCheckingToggle () {
906- if (!JavaMode .errorCheckEnabled ) {
907- Messages .log (editor .getSketch ().getName () + " Error Checker disabled." );
908- editor .getErrorPoints ().clear ();
909- updateErrorTable (Collections .emptyList ());
910- updateEditorStatus ();
911- editor .getTextArea ().repaint ();
912- editor .repaintErrorBar ();
913- } else {
914- Messages .log (editor .getSketch ().getName () + " Error Checker enabled." );
907+ public void handleHasJavaTabsChange (boolean hasJavaTabs ) {
908+ isEnabled = !hasJavaTabs ;
909+ if (isEnabled ) {
915910 notifySketchChanged ();
911+ } else {
912+ preprocessingTask .cancel (false );
913+ setProblemList (Collections .emptyList (), false );
914+ if (astGenerator .getGui ().showUsageBinding != null ) {
915+ astGenerator .getGui ().showUsageWindow .setVisible (false );
916+ }
916917 }
917918 }
918919
0 commit comments