Skip to content

Commit 4e715ba

Browse files
committed
JythonAutocompletionProvider.debug: a static integer flag to control the level of debugging messages.
1 parent f099d7a commit 4e715ba

4 files changed

Lines changed: 33 additions & 22 deletions

File tree

‎src/main/java/org/scijava/plugins/scripteditor/jython/JythonAutoCompletions.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ public List<String> apply(final Path filepath) {
8282
.collect(Collectors.toList());
8383
return modules;
8484
} catch (Exception e) {
85-
e.printStackTrace();
85+
System.out.println("Failed to load jython module");
86+
if (JythonAutocompletionProvider.debug >= 2) e.printStackTrace();
8687
} finally {
8788
if (null != jar) try { jar.close(); } catch (Exception ee) {}
8889
}
8990
return Collections.emptyList();
9091
}
9192
}).findFirst().orElse(Collections.emptyList());
9293
} catch (IOException e) {
93-
e.printStackTrace();
94+
System.out.println("Cannot find jython-slim-*.jar file");
95+
if (JythonAutocompletionProvider.debug >= 2) e.printStackTrace();
9496
} finally {
9597
jython_jar_modules = ls;
9698
}
@@ -138,7 +140,7 @@ public List<Completion> completionsFor(final CompletionProvider provider, String
138140
JythonScriptParser.print("PYTHONPATH:\n" + String.join("\n", Scope.indexer.getLoadPath()));
139141
} catch (Exception e) {
140142
System.out.println("Failed to add path from sys.path.append expression.");
141-
e.printStackTrace();
143+
if (JythonAutocompletionProvider.debug >= 2) e.printStackTrace();
142144
}
143145

144146
// Situations to autocomplete:
@@ -170,7 +172,8 @@ public List<Completion> completionsFor(final CompletionProvider provider, String
170172
: s.substring(dir.length(), s.length() -3)) // remove ending ".py"
171173
.replace('/', '.'));
172174
} catch (IOException e) {
173-
e.printStackTrace();
175+
System.out.println("Failed to read jython module file.");
176+
if (JythonAutocompletionProvider.debug >= 2) e.printStackTrace();
174177
}
175178
return null;
176179
}).flatMap(Function.identity())

‎src/main/java/org/scijava/plugins/scripteditor/jython/JythonAutocompletionProvider.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public class JythonAutocompletionProvider extends DefaultCompletionProvider {
5252
private final JythonAutoCompletions autoCompletions = new JythonAutoCompletions();
5353
private final RSyntaxTextArea text_area;
5454
private final ImportFormat formatter;
55+
56+
/** Adjust value to see debgging information, including:
57+
* 0: nothing at all;
58+
* 1: plus information messages;
59+
* 2: plus {@code Exception} stack traces.
60+
*
61+
* Defaults to 1.
62+
*/
63+
static public int debug = 1;
5564

5665
public JythonAutocompletionProvider(final RSyntaxTextArea text_area, final ImportFormat formatter) {
5766
this.text_area = text_area;
@@ -109,8 +118,8 @@ public List<Completion> getCompletionsImpl(final JTextComponent comp) {
109118
if (cs != null) completions.addAll(cs);
110119
}
111120
catch (Exception e) {
112-
System.out.println("Failed to get autocompletions from " + autoCompletions);
113-
e.printStackTrace();
121+
if (debug >= 1) System.out.println("Failed to get autocompletions from " + autoCompletions);
122+
if (debug >= 2) e.printStackTrace();
114123
}
115124
// Java class discovery for completions with auto-imports
116125
completions.addAll(getCompletions(alreadyEnteredText));
@@ -137,7 +146,7 @@ public List<Completion> getCompletions(final String text) {
137146
final String packageName = m2.group(3);
138147
String className = m2.group(4); // incomplete or empty, or multiple separated by commas with the last one incomplete or empty
139148

140-
System.out.println("m2 matches className: " + className);
149+
if (debug >= 2) System.out.println("m2 matches className: " + className);
141150
final String[] bycomma = className.split(",");
142151
String precomma = "";
143152
if (bycomma.length > 1) {
@@ -267,7 +276,7 @@ private String[] getVariableAnSeedAtCaretLocation() {
267276
final String[] varAndSeed = words[words.length - 1].split("\\.");
268277
return (varAndSeed.length == 2) ? varAndSeed : new String[] { varAndSeed[varAndSeed.length - 1], "" };
269278
} catch (final BadLocationException e) {
270-
e.printStackTrace();
279+
if (debug >= 2) e.printStackTrace();
271280
}
272281
return null;
273282
}

‎src/main/java/org/scijava/plugins/scripteditor/jython/JythonScriptParser.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,8 @@
6969
import org.python.indexer.types.NModuleType;
7070
import org.scijava.ui.swing.script.autocompletion.CompletionText;
7171

72-
public class JythonScriptParser {
73-
74-
/** Controls whether debugging statements print to stdout. */
75-
static public boolean DEBUG = false;
76-
72+
public class JythonScriptParser
73+
{
7774
/**
7875
* Parse valid jython code.
7976
*
@@ -88,7 +85,7 @@ static public Scope parseAST(final String code) {
8885
final mod m = ParserFacade.parse(code, CompileMode.exec, "<none>", new CompilerFlags());
8986
return parseNode(m.getChildren(), null, null);
9087
} catch (Throwable t) {
91-
t.printStackTrace();
88+
if (JythonAutocompletionProvider.debug >= 2) t.printStackTrace();
9289
return new Scope(null);
9390
}
9491
}
@@ -171,7 +168,7 @@ static public Map<String, DotAutocompletions> parseImportStatement(final Import
171168
}
172169

173170
static private DotAutocompletions maybeStaticToDot(final PythonTree node, final DotAutocompletions da) {
174-
System.out.println("children count:" + node.getChildCount() + ", children: " + (null != node.getChildren() ? node.getChildren().stream().map(c -> c.toString()).collect(Collectors.toList()) : ""));
171+
print("children count:" + node.getChildCount() + ", children: " + (null != node.getChildren() ? node.getChildren().stream().map(c -> c.toString()).collect(Collectors.toList()) : ""));
175172
if (node.getChildCount() > 0 && da instanceof StaticDotAutocompletions) {
176173
// It's a right expression (a constructor invocation assigned to a variable on the left) so the left is an instance of the class
177174
return new VarDotAutocompletions(da.getClassname());
@@ -198,7 +195,7 @@ static public Map<String, DotAutocompletions> parseAssignStatement(final Assign
198195
for (int i=0; i<right.getChildren().size(); ++i) {
199196
final CommonTree ct = left.getChildren().get(i).getNode();
200197
if (null == ct) {
201-
System.out.println("null for left: '" + left + "'" + " at child node " + i);
198+
print("null for left: '" + left + "'" + " at child node " + i);
202199
continue;
203200
}
204201
final String name = ct.toString();
@@ -400,6 +397,6 @@ static public DotAutocompletions parseRight(final PyObject right, final Scope sc
400397
}
401398

402399
static public final void print(Object s) {
403-
if (DEBUG) System.out.println(s);
400+
if (JythonAutocompletionProvider.debug >= 2) System.out.println(s);
404401
}
405402
}

‎src/main/java/org/scijava/plugins/scripteditor/jython/Scope.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ static NModuleType loadPythonModule(final String qname) {
112112
mod = indexer.loadModule(qname);
113113
if (null == mod) return null;
114114
} catch (Exception e) {
115-
System.out.println("Could not load python module named " + qname);
116-
e.printStackTrace();
115+
if (JythonAutocompletionProvider.debug >= 1) System.out.println("Could not load python module named " + qname);
116+
if (JythonAutocompletionProvider.debug >= 2) e.printStackTrace();
117117
return null;
118118
}
119119
try {
@@ -131,9 +131,11 @@ static NModuleType loadPythonModule(final String qname) {
131131
System.out.println("Python module " + qname + " doesn't have an associated file path.");
132132
}
133133
} catch (Exception e) {
134-
System.out.println("Could not load python module named " + qname);
135-
System.out.println(e.getMessage());
136-
e.printStackTrace();
134+
if (JythonAutocompletionProvider.debug >= 1) {
135+
System.out.println("Could not load python module named " + qname);
136+
System.out.println(e.getMessage());
137+
}
138+
if (JythonAutocompletionProvider.debug >= 2) e.printStackTrace();
137139
}
138140
return mod;
139141
}

0 commit comments

Comments
 (0)