Skip to content

Commit fcf1a28

Browse files
committed
Update trace and error printing to use JythonDev methods.
1 parent de470b8 commit fcf1a28

5 files changed

Lines changed: 20 additions & 32 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public List<CompletionText> get() {
6060
if (fm.isEmpty()) {
6161
// Maybe it's a python class
6262
// get them all via the org.python.indexer.Indexer and Builtins classes, see StaticDotAutocompletions
63-
if( JythonAutocompletionProvider.debug >= 1) System.out.println("Don't know yet how to handle class " + className);
63+
JythonDev.print("Don't know yet how to handle class " + className);
6464
}
6565
ac.addAll(fm);
6666
}

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ public class JythonAutocompletionProvider extends DefaultCompletionProvider {
5353
private final RSyntaxTextArea text_area;
5454
private final ImportFormat formatter;
5555

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;
64-
6556
public JythonAutocompletionProvider(final RSyntaxTextArea text_area, final ImportFormat formatter) {
6657
this.text_area = text_area;
6758
this.formatter = formatter;
@@ -119,8 +110,8 @@ public List<Completion> getCompletionsImpl(final JTextComponent comp) {
119110
if (cs != null) completions.addAll(cs);
120111
}
121112
catch (Exception e) {
122-
if (debug >= 1) System.out.println("Failed to get autocompletions from " + autoCompletions);
123-
if (debug >= 2) e.printStackTrace();
113+
JythonDev.print("Failed to get autocompletions from " + autoCompletions);
114+
JythonDev.printError(e);
124115
}
125116
// Java class discovery for completions with auto-imports
126117
completions.addAll(getCompletions(alreadyEnteredText));
@@ -147,7 +138,7 @@ public List<Completion> getCompletions(final String text) {
147138
final String packageName = m2.group(3);
148139
String className = m2.group(4); // incomplete or empty, or multiple separated by commas with the last one incomplete or empty
149140

150-
if (debug >= 2) System.out.println("m2 matches className: " + className);
141+
JythonDev.print("m2 matches className: " + className);
151142
final String[] bycomma = className.split(",");
152143
String precomma = "";
153144
if (bycomma.length > 1) {
@@ -277,7 +268,7 @@ private String[] getVariableAnSeedAtCaretLocation() {
277268
final String[] varAndSeed = words[words.length - 1].split("\\.");
278269
return (varAndSeed.length == 2) ? varAndSeed : new String[] { varAndSeed[varAndSeed.length - 1], "" };
279270
} catch (final BadLocationException e) {
280-
if (debug >= 2) e.printStackTrace();
271+
JythonDev.printError(e);
281272
}
282273
return null;
283274
}

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static public Scope parseAST(final String code) {
8686
final mod m = ParserFacade.parse(code, CompileMode.exec, "<none>", new CompilerFlags());
8787
return parseNode(m.getChildren(), null, null);
8888
} catch (Throwable t) {
89-
if (JythonAutocompletionProvider.debug >= 2) t.printStackTrace();
89+
JythonDev.printError(t);
9090
return new Scope(null);
9191
}
9292
}
@@ -110,7 +110,7 @@ static public Scope parseNode(final List<PythonTree> children, final Scope paren
110110
static public void parseNode(final Scope scope, final List<PythonTree> children, final String className) {
111111

112112
for (final PythonTree child : children) {
113-
print(child.getClass());
113+
JythonDev.printTrace(child.getClass());
114114

115115
if (child instanceof ImportFrom)
116116
scope.imports.putAll(parseImportFromStatement( (ImportFrom)child ));
@@ -129,15 +129,15 @@ else if (child instanceof If || child instanceof For || child instanceof While
129129
// no new scope in if/for/while/with/try/ statements in python
130130
parseNode(scope, child.getChildren(), null);
131131
else
132-
print("IGNORING child: " + child + " -- " + (null != child.getChildren() ?
132+
JythonDev.printTrace("IGNORING child: " + child + " -- " + (null != child.getChildren() ?
133133
String.join("::", child.getChildren().stream().map(c -> c.toString()).collect(Collectors.toList()))
134134
: ""));
135135
}
136136
}
137137

138138
static public void parseExpr(final Expr child, final Scope scope) {
139139
// child.getText() shows child is the base
140-
print("Expr: " + child.getText() + ", " + child.getInternalValue() + ", " + child.getValue() + ", children: " + String.join(", ", child.getChildren().stream().map(PythonTree::toString).collect(Collectors.toList())));
140+
JythonDev.printTrace("Expr: " + child.getText() + ", " + child.getInternalValue() + ", " + child.getValue() + ", children: " + String.join(", ", child.getChildren().stream().map(PythonTree::toString).collect(Collectors.toList())));
141141
}
142142

143143
/**
@@ -169,7 +169,7 @@ static public Map<String, DotAutocompletions> parseImportStatement(final Import
169169
}
170170

171171
static private DotAutocompletions maybeStaticToDot(final PythonTree node, final DotAutocompletions da) {
172-
print("children count:" + node.getChildCount() + ", children: " + (null != node.getChildren() ? node.getChildren().stream().map(c -> c.toString()).collect(Collectors.toList()) : ""));
172+
JythonDev.printTrace("children count:" + node.getChildCount() + ", children: " + (null != node.getChildren() ? node.getChildren().stream().map(c -> c.toString()).collect(Collectors.toList()) : ""));
173173
if (node.getChildCount() > 0 && da instanceof StaticDotAutocompletions) {
174174
// It's a right expression (a constructor invocation assigned to a variable on the left) so the left is an instance of the class
175175
return new VarDotAutocompletions(da.getClassname());
@@ -196,7 +196,7 @@ static public Map<String, DotAutocompletions> parseAssignStatement(final Assign
196196
for (int i=0; i<right.getChildren().size(); ++i) {
197197
final CommonTree ct = left.getChildren().get(i).getNode();
198198
if (null == ct) {
199-
print("null for left: '" + left + "'" + " at child node " + i);
199+
JythonDev.printTrace("null for left: '" + left + "'" + " at child node " + i);
200200
continue;
201201
}
202202
final String name = ct.toString();
@@ -303,7 +303,7 @@ static public void parseClassDef(final ClassDef c, final Scope parent) {
303303
for (final expr e: c.getInternalBases()) {
304304
final DotAutocompletions da = parent.find(e.getText(), null);
305305
if (null == da || null == da.getClassname())
306-
print("Could not find completions and className for " + e.getText());
306+
JythonDev.print("Could not find completions and className for " + e.getText());
307307
else
308308
superclassNames.add(da.getClassname());
309309
}
@@ -376,15 +376,15 @@ static public DotAutocompletions parseRight(final PyObject right, final Scope sc
376376
return new VarDotAutocompletions(m.getReturnType().getName());
377377
return new VarDotAutocompletions(c.getField(name).getType().getName());
378378
} catch (Exception e) {
379-
print("Could not find method or field " + name + " in class " + className);
379+
JythonDev.print("Could not find method or field " + name + " in class " + className, e);
380380
}
381381
// Could also be a python module, e.g. attempting to autocomplete "os.path."
382382
try {
383383
final NModuleType module = Scope.indexer.loadModule(className + "." + name); // overly expensive: any way to check it exists without loading it?
384384
if (null != module)
385385
return new StaticDotAutocompletions(className + "." + name);
386386
} catch (Exception e) {
387-
print("Not a python module: " + className + "." + name);
387+
JythonDev.printTrace("Not a python module: " + className + "." + name);
388388
}
389389
}
390390
if (right instanceof Call) {
@@ -398,10 +398,8 @@ static public DotAutocompletions parseRight(final PyObject right, final Scope sc
398398
return parseRight(yield.getValue(), scope);
399399
}
400400

401+
JythonDev.printTrace("Unsupported 'right' is: " + right + " " + (right != null ? right.getClass() : ""));
402+
401403
return DotAutocompletions.EMPTY;
402404
}
403-
404-
static public final void print(Object s) {
405-
if (JythonAutocompletionProvider.debug >= 2) System.out.println(s);
406-
}
407405
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public List<CompletionText> get() {
5454
try {
5555
// Check first if it's a python module
5656
final NModuleType module = Scope.loadPythonModule(this.className); // Scope.indexer.getBuiltinModule(this.className);
57-
System.out.println("module is: " + module);
57+
JythonDev.printTrace("module is: " + module);
5858
if (null != module) {
5959
module.getTable().keySet().forEach( m -> ac.add(new CompletionText(m)));
6060
// Not need to remove: a file system watcher will do so when the module file is updated or deleted.
@@ -89,11 +89,10 @@ public List<CompletionText> get() {
8989
}
9090

9191
if (null != msg) {
92-
if (JythonAutocompletionProvider.debug >= 1) System.out.println(msg);
92+
JythonDev.print(msg);
9393
}
9494
} catch (Exception e) {
95-
if (null != msg && JythonAutocompletionProvider.debug >= 1) System.out.println(msg);
96-
if (JythonAutocompletionProvider.debug >= 2) e.printStackTrace();
95+
JythonDev.print(msg, e);
9796
}
9897
}
9998
return ac;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public List<CompletionText> get() {
5252
final Class<?> c = Class.forName(className);
5353
DotAutocompletions.fieldsAndMethodsInto(c, ac);
5454
} catch (final Exception e) {
55-
if (JythonAutocompletionProvider.debug >= 2) System.out.println("Could not load class " + className + " :: " + e.getMessage());
55+
JythonDev.print("Could not load class " + className, e);
5656
}
5757
}
5858
return ac;

0 commit comments

Comments
 (0)