Skip to content

Commit 6da840c

Browse files
committed
WIP
1 parent d52fab4 commit 6da840c

8 files changed

Lines changed: 361 additions & 53 deletions

File tree

‎pom.xml‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@
8484
<properties>
8585
<package-name>org.scijava.plugins.scripting.jython</package-name>
8686

87-
<!-- TEMP: depend on the in-development scijava-common providing the
88-
org.scijava.script.complete code-completion SPI (Completion,
89-
CodeCompleterPlugin, ClassIndex, ...). Remove once a release
90-
containing it is pinned by pom-scijava. -->
87+
<!-- TEMP: in-development versions providing the code intelligence SPI.
88+
scijava-code-api needs ParameterScriptProcessor#setLogger from
89+
scijava-common. Remove once releases are pinned by pom-scijava. -->
9190
<scijava-common.version>2.100.2-SNAPSHOT</scijava-common.version>
91+
<scijava-code-api.version>0.1.0-SNAPSHOT</scijava-code-api.version>
9292

9393
<license.licenseName>bsd_2</license.licenseName>
9494
<license.copyrightOwners>SciJava developers.</license.copyrightOwners>
@@ -104,6 +104,11 @@
104104
<groupId>org.scijava</groupId>
105105
<artifactId>scijava-common</artifactId>
106106
</dependency>
107+
<dependency>
108+
<groupId>org.scijava</groupId>
109+
<artifactId>scijava-code-api</artifactId>
110+
<version>${scijava-code-api.version}</version>
111+
</dependency>
107112

108113
<!-- Third-party dependencies -->
109114
<dependency>

‎src/main/java/org/scijava/plugins/scripting/jython/CompletionText.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
import java.util.Collections;
3737
import java.util.List;
3838

39-
import org.scijava.script.complete.ClassIndex;
39+
import org.scijava.code.api.ClassIndex;
4040

4141
/**
4242
* A mutable suggestion used internally by the Jython completion engine. It holds
4343
* the replacement text plus optional documentation and (for callables) parameter
4444
* and return-type metadata. The {@link JythonCodeCompleter} converts these into
45-
* toolkit-agnostic {@link org.scijava.script.complete.Completion}s at the
45+
* toolkit-agnostic {@link org.scijava.code.api.Completion}s at the
4646
* boundary.
4747
*
4848
* @author Albert Cardona

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

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.file.FileVisitOption;
3434
import java.nio.file.Files;
3535
import java.util.ArrayList;
36+
import java.util.Arrays;
3637
import java.util.Collections;
3738
import java.util.Comparator;
3839
import java.util.List;
@@ -44,10 +45,10 @@
4445
import java.util.stream.Collectors;
4546

4647
import org.python.indexer.types.NModuleType;
47-
import org.scijava.script.complete.ClassIndex;
48-
import org.scijava.script.complete.Completion;
49-
import org.scijava.script.complete.Completion.TextEdit;
50-
import org.scijava.script.complete.ParameterChoices;
48+
import org.scijava.code.api.ClassIndex;
49+
import org.scijava.code.api.Completion;
50+
import org.scijava.code.api.Completion.TextEdit;
51+
import org.scijava.code.api.ParameterChoices;
5152

5253
/**
5354
* The Jython completion engine: given the code before the caret it analyzes the
@@ -62,7 +63,7 @@
6263
public class JythonAutoCompletions {
6364

6465
private static final Pattern
65-
nameToken = Pattern.compile("^(.*?[ \\t,\\[=\\(]+)([a-zA-Z_][a-zA-Z0-9_]+)$"),
66+
nameToken = Pattern.compile("^(|.*?[ \\t,\\[=\\(+\\-*/%<>!:]+)([a-zA-Z_][a-zA-Z0-9_]*)$"),
6667
invocation = Pattern.compile("^(.*?[ \\t]+|)([a-zA-Z_][a-zA-Z0-9_]+\\()$"),
6768
dotNameToken = Pattern.compile("^(.*?[ \\t]+|)([a-zA-Z0-9_\\.\\[\\](){}]+)\\.([a-zA-Z0-9_]*)$"),
6869
assign = Pattern.compile("^([ \\t]*)(([a-zA-Z_][a-zA-Z0-9_ \\t,]*)[ \\t]+=[ \\t]+(.*))$"),
@@ -76,6 +77,13 @@ public class JythonAutoCompletions {
7677
importStatement = Pattern.compile("^((from[ \\t]+([a-zA-Z0-9._]+)[ \\t]+|[ \\t]*)import[ \\t]+)([a-zA-Z0-9_., \\t]*)$"),
7778
simpleClassName = Pattern.compile("^(.*[ \\t]+|)([A-Z_][a-zA-Z0-9_]+)$");
7879

80+
/** Python 2 (Jython) keywords. */
81+
private static final List<String> KEYWORDS = Arrays.asList("and", "as",
82+
"assert", "break", "class", "continue", "def", "del", "elif", "else",
83+
"except", "exec", "finally", "for", "from", "global", "if", "import", "in",
84+
"is", "lambda", "not", "or", "pass", "print", "raise", "return", "try",
85+
"while", "with", "yield", "None", "True", "False");
86+
7987
private final JythonImportFormat formatter = new JythonImportFormat();
8088

8189
/** Python standard library module names, discovered from the jython jar. */
@@ -100,27 +108,42 @@ public static final class Result {
100108
* which follows {@code codeWithoutLastLine} in the script. {@code
101109
* alreadyEnteredText} is the text the editor will replace.
102110
*/
103-
public Result completionsFor(String codeWithoutLastLine, final String lastLine,
104-
final String alreadyEnteredText)
111+
public Result completionsFor(final String codeWithoutLastLine,
112+
final String lastLine, final String alreadyEnteredText)
113+
{
114+
return completionsFor(codeWithoutLastLine, lastLine, alreadyEnteredText,
115+
Collections.emptyMap());
116+
}
117+
118+
/**
119+
* As {@link #completionsFor(String, String, String)}, for a script in which
120+
* the given variables are already defined before the code runs (e.g. script
121+
* parameters, or the bindings of a live interpreter).
122+
*/
123+
public Result completionsFor(final String codeWithoutLastLine,
124+
final String lastLine, final String alreadyEnteredText,
125+
final Map<String, DotAutocompletions> predefined)
105126
{
106127
final List<Completion> completions = new ArrayList<>();
107128
final int crop = lastLine.length() - alreadyEnteredText.length();
108129

109130
// 1) AST-based completions (names, members, constructors, invocations).
110131
final ParameterChoices choices = astCompletions(codeWithoutLastLine,
111-
lastLine, crop, completions);
132+
lastLine, crop, predefined, completions);
112133

113134
// 2) Import and class-name discovery, always appended.
114135
importCompletions(alreadyEnteredText, codeWithoutLastLine + lastLine,
115136
completions);
116137

117-
return new Result(completions, choices);
138+
return new Result(dedupe(completions), choices);
118139
}
119140

120141
// -- AST-based completions --
121142

122143
private ParameterChoices astCompletions(String codeWithoutLastLine,
123-
final String lastLine, final int crop, final List<Completion> completions)
144+
final String lastLine, final int crop,
145+
final Map<String, DotAutocompletions> predefined,
146+
final List<Completion> completions)
124147
{
125148
// Precondition: can't expand when empty or ending with any of "[]{},; ".
126149
if (lastLine.isEmpty()) return null;
@@ -173,32 +196,43 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
173196
return null;
174197
}
175198

176-
// A plain name (variable, function, class) being typed.
199+
// A plain name (variable, function, class, keyword) being typed.
177200
final Matcher m1 = nameToken.matcher(lastLine);
178201
if (m1.find()) {
179-
final Scope scope = JythonScriptParser.parseAST(codeWithoutLastLine)
202+
final Scope scope = JythonScriptParser.parseAST(codeWithoutLastLine,
203+
predefined)
180204
.getLast();
205+
final String seed = m1.group(2);
181206
final String pre = crop > -1 && crop < m1.group(1).length() ? m1.group(1)
182207
.substring(crop) : "";
183-
final Map<String, String> names = scope.findStartsWith2(m1.group(2));
208+
final String head = lastLine.substring(0, lastLine.length() - seed
209+
.length());
210+
final Map<String, String> names = scope.findStartsWith2(seed);
184211
for (final Map.Entry<String, String> e : names.entrySet()) {
212+
completions.add(Completion.builder((head + e.getKey()).substring(crop))
213+
.kind(Completion.Kind.VARIABLE).build());
185214
final String classname = e.getValue();
186215
if (null != classname) {
216+
// A class: also offer its constructors.
187217
try {
188218
final Class<?> c = Class.forName(classname);
189219
for (final java.lang.reflect.Constructor<?> cons : c
190220
.getConstructors())
191221
{
192-
completions.add(makeDotCompletion(pre, m1.group(2),
222+
completions.add(makeDotCompletion(pre, seed,
193223
new CompletionText(e.getKey(), c, cons)));
194224
}
195225
}
196-
catch (final ClassNotFoundException cnfe) {
226+
catch (final ClassNotFoundException | LinkageError exc) {
197227
JythonDev.printTrace("Can't load class: " + classname);
198228
}
199229
}
200-
completions.add(Completion.of((lastLine + e.getKey().substring(m1
201-
.group(2).length())).substring(crop)));
230+
}
231+
for (final String keyword : KEYWORDS) {
232+
if (keyword.startsWith(seed) && !keyword.equals(seed)) {
233+
completions.add(Completion.builder((head + keyword).substring(crop))
234+
.kind(Completion.Kind.KEYWORD).build());
235+
}
202236
}
203237
return scopeChoices(scope);
204238
}
@@ -207,7 +241,8 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
207241
final Matcher m1c = invocation.matcher(lastLine);
208242
if (m1c.find()) {
209243
final String name = m1c.group(2).substring(0, m1c.group(2).length() - 1);
210-
final Scope scope = JythonScriptParser.parseAST(codeWithoutLastLine)
244+
final Scope scope = JythonScriptParser.parseAST(codeWithoutLastLine,
245+
predefined)
211246
.getLast();
212247
final DotAutocompletions da = scope.find(name, DotAutocompletions.EMPTY);
213248
if (da instanceof ConstructorAutocompletions) {
@@ -246,7 +281,7 @@ private ParameterChoices astCompletions(String codeWithoutLastLine,
246281
lastLine.substring(0, start) + varName + " = " + lastLine.substring(
247282
start, lastLine.length() - 1 - seed.length());
248283
}
249-
final Scope scope = JythonScriptParser.parseAST(code);
284+
final Scope scope = JythonScriptParser.parseAST(code, predefined);
250285
final DotAutocompletions da = scope.getLast().find(varName,
251286
DotAutocompletions.EMPTY);
252287
final String fullPre = lastLine.substring(crop);
@@ -368,9 +403,12 @@ private void importCompletions(final String text, final String fullCode,
368403
final Matcher m3 = simpleClassName.matcher(text);
369404
if (m3.find()) {
370405
final String pre = m3.group(1);
371-
for (final String className : ClassIndex.findSimpleClassNamesStartingWith(
372-
m3.group(2)))
373-
{
406+
final List<String> classNames = ClassIndex
407+
.findSimpleClassNamesStartingWith(m3.group(2));
408+
// Closest matches first: shortest simple name, then alphabetical.
409+
classNames.sort(Comparator.comparingInt((String cn) -> cn.length() - cn
410+
.lastIndexOf('.')).thenComparing(Comparator.naturalOrder()));
411+
for (final String className : classNames) {
374412
final String simpleName = className.substring(className.lastIndexOf(
375413
'.') + 1);
376414
final String importStmt = formatter.singleToImportStatement(className);
@@ -387,6 +425,19 @@ private void importCompletions(final String text, final String fullCode,
387425

388426
// -- Helpers --
389427

428+
/**
429+
* Removes duplicate completions (same text and parameters), keeping the first
430+
* occurrence, since the AST and class-name analyses can suggest the same thing.
431+
*/
432+
private static List<Completion> dedupe(final List<Completion> completions) {
433+
final java.util.Set<String> seen = new java.util.HashSet<>();
434+
final List<Completion> result = new ArrayList<>(completions.size());
435+
for (final Completion c : completions) {
436+
if (seen.add(c.insertionText() + "\u0000" + c.parameters())) result.add(c);
437+
}
438+
return result;
439+
}
440+
390441
/** Builds a neutral completion from a {@link CompletionText} and prefix. */
391442
private static Completion makeDotCompletion(final String pre,
392443
final String seed, final CompletionText ct)

‎src/main/java/org/scijava/plugins/scripting/jython/JythonCodeCompleter.java‎

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,27 @@
2828
*/
2929
package org.scijava.plugins.scripting.jython;
3030

31+
import java.util.Collections;
32+
import java.util.LinkedHashMap;
33+
import java.util.Map;
34+
35+
import javax.script.Bindings;
36+
import javax.script.ScriptContext;
37+
38+
import org.python.core.PyJavaType;
39+
import org.python.core.PyModule;
40+
import org.python.core.PyObject;
41+
import org.python.core.PyString;
42+
import org.scijava.Context;
3143
import org.scijava.Priority;
3244
import org.scijava.plugin.Plugin;
3345
import org.scijava.script.ScriptLanguage;
34-
import org.scijava.script.complete.AbstractCodeCompleterPlugin;
35-
import org.scijava.script.complete.ClassIndex;
36-
import org.scijava.script.complete.CodeCompleterPlugin;
37-
import org.scijava.script.complete.CompletionRequest;
38-
import org.scijava.script.complete.CompletionResult;
46+
import org.scijava.code.api.AbstractCodeCompleterPlugin;
47+
import org.scijava.code.api.ClassIndex;
48+
import org.scijava.code.api.CodeCompleterPlugin;
49+
import org.scijava.code.api.CodeCompletionService;
50+
import org.scijava.code.api.CompletionRequest;
51+
import org.scijava.code.api.CompletionResult;
3952

4053
/**
4154
* Toolkit-agnostic code completion for the Jython language, contributed by the
@@ -78,15 +91,82 @@ public CompletionResult complete(final CompletionRequest request) {
7891
final int replaceStart = caret - alreadyEntered.length();
7992

8093
final JythonAutoCompletions.Result result = engine.completionsFor(
81-
codeWithoutLastLine, lastLine, alreadyEntered);
94+
codeWithoutLastLine, lastLine, alreadyEntered, predefinedVariables(
95+
request));
8296
return new CompletionResult(result.completions, replaceStart,
8397
result.parameterChoices);
8498
}
8599

100+
/**
101+
* Gets the variables defined before the script's code runs: script
102+
* parameters declared via {@code #@} lines, and, when completing in a live
103+
* interpreter, the engine's bindings (which reflect actual runtime values, so
104+
* they take precedence).
105+
*/
106+
private Map<String, DotAutocompletions> predefinedVariables(
107+
final CompletionRequest request)
108+
{
109+
final Map<String, DotAutocompletions> vars = new LinkedHashMap<>();
110+
final Context context = getContext();
111+
final CodeCompletionService completionService = context == null ? null
112+
: context.getService(CodeCompletionService.class);
113+
if (completionService != null) {
114+
completionService.scriptParameters(request.text()).forEach((name,
115+
type) -> vars.put(name, new VarDotAutocompletions(boxed(type)
116+
.getName())));
117+
}
118+
final ScriptContext scriptContext = request.context();
119+
final Bindings bindings = scriptContext == null ? null : scriptContext
120+
.getBindings(ScriptContext.ENGINE_SCOPE);
121+
if (bindings != null) {
122+
for (final Map.Entry<String, Object> entry : bindings.entrySet()) {
123+
final String name = entry.getKey();
124+
if (name.startsWith("__") && name.endsWith("__")) continue;
125+
vars.put(name, bindingCompletions(entry.getValue()));
126+
}
127+
}
128+
return vars.isEmpty() ? Collections.emptyMap() : vars;
129+
}
130+
131+
/** Describes a live binding value for completion purposes. */
132+
private static DotAutocompletions bindingCompletions(final Object value) {
133+
if (value instanceof PyJavaType) {
134+
// An imported Java class: offer its constructors and static members.
135+
final Class<?> c = ((PyJavaType) value).getProxyType();
136+
if (c != null) return new StaticDotAutocompletions(c.getName());
137+
}
138+
if (value instanceof PyModule) {
139+
final PyObject name = ((PyModule) value).__findattr__("__name__");
140+
if (name instanceof PyString) {
141+
return new StaticDotAutocompletions(name.toString());
142+
}
143+
}
144+
if (value == null || value instanceof PyObject) {
145+
// A python function, class or instance: known by name only.
146+
return new VarDotAutocompletions(null);
147+
}
148+
return new VarDotAutocompletions(value.getClass().getName());
149+
}
150+
151+
/** Maps primitive types to their wrappers, whose members can be listed. */
152+
private static Class<?> boxed(final Class<?> type) {
153+
if (!type.isPrimitive()) return type;
154+
if (type == int.class) return Integer.class;
155+
if (type == long.class) return Long.class;
156+
if (type == double.class) return Double.class;
157+
if (type == float.class) return Float.class;
158+
if (type == boolean.class) return Boolean.class;
159+
if (type == char.class) return Character.class;
160+
if (type == short.class) return Short.class;
161+
if (type == byte.class) return Byte.class;
162+
return Object.class; // void
163+
}
164+
86165
/**
87166
* The portion of the current line the editor should replace. Mirrors the
88167
* legacy RSTA logic: a maximal suffix of letters, digits, {@code '.'},
89-
* {@code '_'} and spaces (spaces let "from x import Y" complete as a unit).
168+
* {@code '_'} and spaces (spaces let "from x import Y" complete as a unit),
169+
* excluding leading spaces.
90170
*/
91171
private static String alreadyEnteredText(final String lastLine) {
92172
int start = lastLine.length();
@@ -96,6 +176,8 @@ private static String alreadyEnteredText(final String lastLine) {
96176
start--;
97177
else break;
98178
}
179+
// Leading whitespace is not part of what the user is completing.
180+
while (start < lastLine.length() && lastLine.charAt(start) == ' ') start++;
99181
return lastLine.substring(start);
100182
}
101183
}

‎src/main/java/org/scijava/plugins/scripting/jython/JythonImports.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.util.regex.Matcher;
3333
import java.util.regex.Pattern;
3434

35-
import org.scijava.script.complete.Completion.TextEdit;
35+
import org.scijava.code.api.Completion.TextEdit;
3636

3737
/**
3838
* Parses the {@code import} statements in a Jython script, and computes the

0 commit comments

Comments
 (0)