Skip to content

Commit d67805f

Browse files
committed
Minor fixes for IJ1 Macro Interpreter execution errors
See https://forum.image.sc/t/shiny-new-script-editor/64160/18
1 parent 331c64e commit d67805f

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/main/java/org/scijava/ui/swing/script/ErrorParser.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
package org.scijava.ui.swing.script;
3131

3232
import java.awt.Color;
33+
import java.awt.Dialog;
3334
import java.awt.Rectangle;
35+
import java.awt.Window;
3436
import java.io.PrintWriter;
3537
import java.io.StringWriter;
3638
import java.util.Arrays;
@@ -66,6 +68,7 @@ public class ErrorParser {
6668
private JTextAreaWriter writer;
6769
private int lengthOfJTextAreaWriter;
6870
private ErrorStripNotifyingParser notifyingParser;
71+
private boolean parsingSucceeded;
6972

7073
public ErrorParser(final EditorPane editorPane) {
7174
this.editorPane = editorPane;
@@ -148,8 +151,23 @@ public void parse(final Throwable t) {
148151
parse(sw.toString());
149152
}
150153

154+
private boolean isIJ1Macro() {
155+
final ScriptLanguage lang = editorPane.getCurrentLanguage();
156+
return lang != null && "IJ1 Macro".equals(lang.getLanguageName());
157+
}
158+
159+
public boolean isLogDetailed() {
160+
if (!isIJ1Macro()) return parsingSucceeded;
161+
for (final Window win : Window.getWindows()) {
162+
if (win != null && win instanceof Dialog && "Macro Error".equals(((Dialog)win).getTitle())) {
163+
return true; // hopefully there is something in the console
164+
}
165+
}
166+
return parsingSucceeded;
167+
}
168+
151169
private boolean isCaretMovable() {
152-
if (errorLines == null || errorLines.isEmpty() || !editorPane.isEditable() || !editorPane.isEnabled()) {
170+
if (errorLines == null || errorLines.isEmpty()) {
153171
UIManager.getLookAndFeel().provideErrorFeedback(editorPane);
154172
return false;
155173
}
@@ -172,9 +190,15 @@ private void gotoLine(final int lineNumber) {
172190
private void parse(final String errorLog) {
173191

174192
final ScriptLanguage lang = editorPane.getCurrentLanguage();
175-
if (lang == null)
193+
if (lang == null) {
194+
abort();
176195
return;
177-
196+
}
197+
final boolean isIJM = isIJ1Macro();
198+
if (isIJM) {
199+
abort("Execution errors handled by the Macro Interpreter. Use the Interpreter's Debug option for error tracking", false);
200+
return;
201+
}
178202
// Do nothing if disabled, or if only selected text was evaluated in the
179203
// script but we don't know where in the document such selection occurred
180204
if (!enabled) {
@@ -188,8 +212,10 @@ private void parse(final String errorLog) {
188212

189213
final boolean isJava = "Java".equals(lang.getLanguageName());
190214
final String fileName = editorPane.getFileName();
191-
if (isJava && fileName == null)
215+
if (isJava && fileName == null) {
216+
abort();
192217
return;
218+
}
193219

194220
// HACK scala code seems to always be pre-pended by some 10 lines of code(!?).
195221
if ("Scala".equals(lang.getLanguageName()))
@@ -209,14 +235,13 @@ else if ("R".equals(lang.getLanguageName()))
209235
parseNonJava(tokenizer.nextToken(), errorLines);
210236
}
211237
}
212-
if (errorLines.isEmpty() && "IJ1 Macro".equals(lang.getLanguageName())) {
213-
abort("Execution errors handled by the Macro Interpreter. Use the Interpreter's Debug option for error tracking", false);
214-
} else if (!errorLines.isEmpty()) {
238+
if (!errorLines.isEmpty()) {
215239
notifyingParser = new ErrorStripNotifyingParser();
216240
editorPane.addParser(notifyingParser);
217241
editorPane.forceReparsing(notifyingParser);
218242
gotoLine(errorLines.first());
219243
}
244+
parsingSucceeded = true;
220245
}
221246

222247
private void parseNonJava(final String lineText, final Collection<Integer> errorLines) {
@@ -298,10 +323,16 @@ private void parseJava(final String filename, final String line, final Collectio
298323
}
299324
}
300325

326+
private void abort() {
327+
parsingSucceeded = false;
328+
}
329+
301330
private void abort(final String msg, final boolean offsetNotice) {
331+
abort();
302332
if (writer != null) {
303-
String finalMsg = "[WARNING] " + msg + "\n";
304-
finalMsg += "[WARNING] Reported error line(s) may not match line numbers in the editor\n";
333+
String finalMsg = "[INFO] " + msg + "\n";
334+
if (offsetNotice)
335+
finalMsg += "[INFO] Reported error line(s) may not match line numbers in the editor\n";
305336
writer.textArea.insert(finalMsg, lengthOfJTextAreaWriter);
306337
}
307338
errorLines = null;

src/main/java/org/scijava/ui/swing/script/TextEditor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,15 +3023,17 @@ public void markCompileStart(final boolean with_timestamp) {
30233023
ExceptionHandler.addThread(Thread.currentThread(), this);
30243024
}
30253025

3026-
public void markCompileEnd() {
3026+
public void markCompileEnd() { // this naming is not-intuitive at all!
30273027
if (errorHandler == null) {
30283028
errorHandler =
30293029
new ErrorHandler(getCurrentLanguage(), errorScreen,
30303030
compileStartPosition.getOffset());
30313031
if (errorHandler.getErrorCount() > 0) getTab().showErrors();
30323032
}
3033-
if (compileStartOffset != errorScreen.getDocument().getLength()) getTab()
3034-
.showErrors();
3033+
if (getEditorPane().getErrorHighlighter().isLogDetailed() &&
3034+
compileStartOffset != errorScreen.getDocument().getLength()) {
3035+
getTab().showErrors();
3036+
}
30353037
if (getTab().showingErrors) {
30363038
errorHandler.scrollToVisible(compileStartOffset);
30373039
}
@@ -3388,8 +3390,8 @@ void showHTMLDialog(final String title, final String htmlContents) {
33883390

33893391
public void handleException(final Throwable e) {
33903392
handleException(e, errorScreen);
3391-
getTab().showErrors();
33923393
getEditorPane().getErrorHighlighter().parse(e);
3394+
getTab().showErrors();
33933395
}
33943396

33953397
public static void

0 commit comments

Comments
 (0)