Skip to content

Commit 3759e50

Browse files
committed
ECS: handle java tabs and preference changes better
1 parent 64b7bf6 commit 3759e50

3 files changed

Lines changed: 70 additions & 67 deletions

File tree

java/src/processing/mode/java/JavaEditor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.beans.*;
66
import java.io.*;
77
import java.util.ArrayList;
8-
import java.util.Collections;
98
import java.util.Iterator;
109
import java.util.List;
1110
import java.util.Map;
@@ -204,9 +203,15 @@ public void rebuild() {
204203
super.rebuild();
205204

206205
// after Rename and New Tab, we may have new .java tabs
207-
hasJavaTabs = checkForJavaTabs();
206+
boolean newHasJavaTabs = checkForJavaTabs();
207+
boolean hasJavaTabsChanged = hasJavaTabs != newHasJavaTabs;
208+
hasJavaTabs = newHasJavaTabs;
208209

209210
if (errorCheckerService != null) {
211+
if (hasJavaTabsChanged) {
212+
errorCheckerService.handleHasJavaTabsChange(hasJavaTabs);
213+
}
214+
210215
int currentTabCount = sketch.getCodeCount();
211216
if (currentTabCount != previousTabCount) {
212217
previousTabCount = currentTabCount;
@@ -2753,7 +2758,7 @@ protected void applyPreferences() {
27532758
jmode.loadPreferences();
27542759
Messages.log("Applying prefs");
27552760
// trigger it once to refresh UI
2756-
errorCheckerService.handleErrorCheckingToggle();
2761+
errorCheckerService.handlePreferencesChange();
27572762
}
27582763
}
27592764

java/src/processing/mode/java/MarkerColumn.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.awt.event.MouseMotionAdapter;
3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.stream.Collectors;
3233

3334
import javax.swing.JPanel;
3435

@@ -114,12 +115,9 @@ public List<LineMarker> getErrorPoints() {
114115

115116

116117
public void updateErrorPoints(final List<Problem> problems) {
117-
errorPoints.clear();
118-
// Each problem.getSourceLine() will have an extra line added because
119-
// of class declaration in the beginning as well as default imports
120-
for (Problem problem : problems) {
121-
errorPoints.add(new LineMarker(problem, problem.isError()));
122-
}
118+
errorPoints = problems.stream()
119+
.map(problem -> new LineMarker(problem, problem.isError()))
120+
.collect(Collectors.toList());
123121
repaint();
124122
editor.getErrorChecker().updateEditorStatus();
125123
}
@@ -166,7 +164,6 @@ private void showMarkerHover(final int y) {
166164

167165

168166
private void recalculateMarkerPositions() {
169-
List<LineMarker> errorPoints = getErrorPoints();
170167
if (errorPoints != null && errorPoints.size() > 0) {
171168
Sketch sketch = editor.getSketch();
172169
SketchCode code = sketch.getCurrentCode();

java/src/processing/mode/java/pdex/ErrorCheckerService.java

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)