Skip to content

Commit 5bb88a3

Browse files
gselzerclaude
andcommitted
code-lsp: let dialects write the header; blank what SciJava removes
A dialect now supplies one method, header, in place of parameterType, declare, imports, imported, extraDeclarations and separator: one line of code to prepend (enforced), and the names it binds only to declare the parameters, which completion hides (Header.hiddenNames, as ScriptDocument.added is now ScriptDocument.hiddenNames). Dialects no longer say which lines to blank (blanks is gone): a ScriptDocument blanks the lines SciJava removes before a script runs, as ScriptProcessing (formerly ScriptParameters) finds them by running SciJava's own ShebangScriptProcessor and ParameterScriptProcessor. Not the full chain: the #@script directive's processor registers the script as a module. #@ lines SciJava rejects, e.g. half-typed ones, are blanked all the same, and their names still declared. Since its servers no longer see #@script lines, a dialect's environment is asked about the script as written. ScriptDocument.parameters() is gone: only the Groovy server used it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 599b2ea commit 5bb88a3

10 files changed

Lines changed: 575 additions & 421 deletions

File tree

‎code-lsp-groovy/src/main/java/org/scijava/code/lsp/groovy/GroovyLanguageServer.java‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public class GroovyLanguageServer extends AbstractLanguageServer {
112112
*/
113113
static final ScriptDialect DIALECT = new Declarations();
114114

115+
/** No lines to blank: the code is plain Groovy. */
116+
private static final Set<Integer> NOTHING = Collections.emptySet();
117+
115118
/** An import statement being typed: "import a.b.C". */
116119
private static final Pattern IMPORT = Pattern.compile(
117120
"^\\s*import\\s+(static\\s+)?([\\w$.]*)$");
@@ -170,16 +173,16 @@ protected CompletionList complete(final Document doc, final int caret) {
170173

171174
final Map<String, Class<?>> predefined = liveVariables();
172175
final ScriptDocument sd = ScriptDocument.of(text.substring(0,
173-
replaceStart), predefined, DIALECT);
176+
replaceStart), predefined, NOTHING, DIALECT);
174177
final TypedAST ast = typedAST(sd, predefined, line.substring(0,
175178
line.length() - prefix.length()));
176179
if (ast == null) return list(doc, caret, caret, none); // string, comment
177180

178181
final List<CompletionItem> items = new ArrayList<>();
179182
if (member) items.addAll(memberCompletions(ast, before, prefix));
180183
else {
181-
items.addAll(variableCompletions(ast.variablesAtCaret(), sd
182-
.parameters(), prefix));
184+
items.addAll(variableCompletions(ast.variablesAtCaret(), predefined,
185+
prefix));
183186
items.addAll(keywordCompletions(prefix));
184187
items.addAll(classCompletions(doc, prefix));
185188
}
@@ -217,7 +220,7 @@ protected SignatureHelp signatureHelp(final Document doc, final int caret) {
217220
protected List<Diagnostic> diagnose(final Document doc) {
218221
final String text = doc.text();
219222
final ScriptDocument sd = ScriptDocument.of(text, liveVariables(),
220-
DIALECT);
223+
NOTHING, DIALECT);
221224
final TypedAST ast = TypedAST.of(sd.text(), loader());
222225
final List<Diagnostic> found = new ArrayList<>();
223226
for (final TypedAST.Problem p : ast.problems()) {
@@ -265,9 +268,9 @@ private CallContext callContext(final Document doc, final int caret,
265268
final String prefix = id.group();
266269
final int start = caret - prefix.length();
267270
final Map<String, Class<?>> predefined = liveVariables();
268-
final TypedAST ast = typedAST(ScriptDocument.of(text.substring(
269-
0, start), predefined, DIALECT), predefined, line.substring(0, line
270-
.length() - prefix.length()));
271+
final TypedAST ast = typedAST(ScriptDocument.of(text.substring(0,
272+
start), predefined, NOTHING, DIALECT), predefined, line.substring(0,
273+
line.length() - prefix.length()));
271274
if (ast == null) return null; // string or comment
272275
final TypedAST.Call call = ast.callAtCaret();
273276
return call == null ? null : new CallContext(ast, call);
@@ -333,7 +336,8 @@ private TypedAST typedAST(final ScriptDocument doc,
333336
final TypedAST ast = TypedAST.of(code, loader());
334337
if (ast.isParsed()) return ast;
335338
final String lineCode = TypedAST.closeBrackets(TypedAST
336-
.markCaret(ScriptDocument.of(lineBefore, predefined, DIALECT)));
339+
.markCaret(ScriptDocument.of(lineBefore, predefined, NOTHING,
340+
DIALECT)));
337341
return lineCode == null ? ast : TypedAST.of(lineCode, loader());
338342
}
339343

@@ -453,8 +457,8 @@ private static List<Callable> gdkMethods(final Class<?> c,
453457
}
454458

455459
private static List<CompletionItem> variableCompletions(
456-
final Map<String, ClassNode> vars, final Map<String, String> predefined,
457-
final String prefix)
460+
final Map<String, ClassNode> vars,
461+
final Map<String, Class<?>> predefined, final String prefix)
458462
{
459463
final Set<String> names = new HashSet<>();
460464
final List<CompletionItem> out = new ArrayList<>();
@@ -467,8 +471,8 @@ private static List<CompletionItem> variableCompletions(
467471
continue;
468472
}
469473
final ClassNode type = vars.get(name);
470-
out.add(variable(name, type != null ? type.toString(false) : predefined
471-
.get(name)));
474+
out.add(variable(name, type != null ? type.toString(false)
475+
: typeName(predefined.get(name))));
472476
}
473477
return out;
474478
}
@@ -756,13 +760,13 @@ public String languageId() {
756760
}
757761

758762
@Override
759-
public String parameterType(final Class<?> type) {
760-
return typeName(type);
761-
}
762-
763-
@Override
764-
public String declare(final String name, final String type) {
765-
return type + " " + name;
763+
public Header header(final Map<String, Class<?>> parameters) {
764+
// NB: Full names need no imports.
765+
final List<String> declarations = new ArrayList<>();
766+
parameters.forEach((name, type) -> declarations.add(typeName(type) +
767+
" " + name));
768+
return new Header(String.join("; ", declarations), Collections
769+
.emptySet());
766770
}
767771
}
768772
}

‎code-lsp/src/main/java/org/scijava/code/lsp/ScriptDialect.java‎

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import java.io.File;
3333
import java.util.Collection;
3434
import java.util.Collections;
35-
import java.util.List;
35+
import java.util.LinkedHashSet;
36+
import java.util.Map;
37+
import java.util.Set;
3638

3739
import org.scijava.plugin.SciJavaPlugin;
3840
import org.scijava.script.ScriptLanguage;
@@ -48,15 +50,15 @@
4850
* know their language, not SciJava scripts (see {@link LanguageServerPlugin}).
4951
* </p>
5052
* <p>
51-
* A dialect describes the translation, rather than doing it: a
52-
* {@link ScriptDocument} translates every script the same way, prepending one
53-
* line of declarations and blanking lines in place, and the dialect supplies
54-
* only the pieces (types, declarations, imports). So, for every language,
55-
* positions in a script and in its document differ by just that line's length,
56-
* which is how a session maps servers' answers back to the script (and leaves
57-
* out those about the line); and the translation is written, and tested, once.
58-
* A language whose scripts need more (e.g. wrapping in a class) would extend
59-
* that rule in {@link ScriptDocument}, for all languages.
53+
* A dialect supplies the declarations, rather than translating scripts: a
54+
* {@link ScriptDocument} translates every script the same way, prepending the
55+
* dialect's {@link #header}, and blanking in place the lines SciJava removes
56+
* before a script runs (e.g. its {@code #@} lines). So, for every language,
57+
* positions in a script and in its document differ by just the header's
58+
* length, which is how a session maps servers' answers back to the script
59+
* (and leaves out those about the header); and that mapping is written, and
60+
* tested, once. A language whose scripts need more (e.g. wrapping in a class)
61+
* would extend that rule in {@link ScriptDocument}, for all languages.
6062
* </p>
6163
*
6264
* @author Gabriel Selzer
@@ -83,8 +85,8 @@ public interface ScriptDialect extends SciJavaPlugin {
8385
* {@link org.scijava.code.lsp.server.RoutingLanguageServer}). The default knows
8486
* nothing: {@link Environment#NONE}.
8587
*
86-
* @param text The script, as its servers see it (see
87-
* {@link ScriptDocument}).
88+
* @param text The script, as written (e.g. with its {@code #@script} line,
89+
* which its servers do not see).
8890
* @param file The script's file, or null if it has none (e.g. unsaved).
8991
*/
9092
// TODO: Consider whether this is necessary: so far only Appose-Python
@@ -94,64 +96,59 @@ default Environment environment(final String text, final File file) {
9496
}
9597

9698
/**
97-
* Gets the type, as written in this language, that a script parameter of
98-
* the given Java type has in a running script, e.g. {@code numpy.ndarray}
99-
* for an image in CPython; or null if it has no useful type (the parameter
100-
* is then not declared).
101-
*/
102-
String parameterType(Class<?> type);
103-
104-
/**
105-
* Declares a variable, e.g. {@code image: numpy.ndarray} (Python) or
106-
* {@code net.imglib2.img.Img image;} (Groovy), without a line break.
99+
* Gets the code declaring a script's parameters with the types they have
100+
* when it runs, for a {@link ScriptDocument} to prepend to the script: e.g.
101+
* {@code import numpy; image: numpy.ndarray} for an image in CPython. It
102+
* may also import what the declarations need, and declare other variables
103+
* scripts can use (e.g. Appose's {@code task}); parameters without a useful
104+
* type may be left out.
107105
*
108-
* @param name The variable's name.
109-
* @param type Its type, as given by {@link #parameterType}.
106+
* @param parameters The script's parameters (see
107+
* {@link LanguageServerService#scriptParameters}), by name, with
108+
* their Java types, in order.
110109
*/
111-
String declare(String name, String type);
110+
Header header(Map<String, Class<?>> parameters);
112111

113112
/**
114-
* Gets statements needed before the declarations, e.g. imports of the types
115-
* they use ({@code import numpy}). The default needs none.
116-
*
117-
* @param types The types declared, as given by {@link #parameterType}.
113+
* One line of code to prepend to a script (see {@link #header}), e.g.
114+
* statements joined by {@code ;}.
118115
*/
119-
default List<String> imports(final Collection<String> types) {
120-
return Collections.emptyList();
121-
}
116+
final class Header {
122117

123-
/**
124-
* Gets the name one of the {@link #imports} binds, e.g. {@code numpy} for
125-
* {@code import numpy}; or null if none. Unless the script itself mentions
126-
* it, the name is the declarations' alone: it is not completed (see
127-
* {@link ScriptDocument#added()}). The default binds none.
128-
*/
129-
default String imported(final String statement) {
130-
return null;
131-
}
118+
private final String code;
119+
private final Set<String> hiddenNames;
132120

133-
/**
134-
* Gets declarations of other variables scripts can use, e.g. Appose's
135-
* {@code task}. The default declares none.
136-
*/
137-
default List<String> extraDeclarations() {
138-
return Collections.emptyList();
139-
}
121+
/**
122+
* @param code The code: one line, without a line break (the
123+
* {@link ScriptDocument} adds one after it), or empty if none.
124+
* @param hiddenNames The names to leave out of completion: those the
125+
* code binds only to declare the parameters, e.g.
126+
* {@code numpy} for {@code import numpy}.
127+
* @throws IllegalArgumentException If the code has a line break.
128+
*/
129+
public Header(final String code, final Collection<String> hiddenNames) {
130+
if (code.indexOf('\n') >= 0 || code.indexOf('\r') >= 0) {
131+
throw new IllegalArgumentException(
132+
"A header is one line, without line breaks: " + code);
133+
}
134+
this.code = code;
135+
this.hiddenNames = Collections.unmodifiableSet(new LinkedHashSet<>(
136+
hiddenNames));
137+
}
140138

141-
/**
142-
* Gets the separator between statements on one line, e.g. {@code "; "}
143-
* (the default).
144-
*/
145-
default String separator() {
146-
return "; ";
147-
}
139+
/** The code: one line, without a line break; or empty if none. */
140+
public String code() {
141+
return code;
142+
}
148143

149-
/**
150-
* Whether a line of the script is not of this language, and must be blanked
151-
* (e.g. {@code #@} lines for languages whose comments start otherwise). The
152-
* default blanks none.
153-
*/
154-
default boolean blanks(final String line) {
155-
return false;
144+
/**
145+
* The names to leave out of completion: those the code binds only to
146+
* declare the parameters, e.g. {@code numpy} for {@code import numpy}
147+
* (unless the script itself mentions them, see
148+
* {@link ScriptDocument#hiddenNames()}).
149+
*/
150+
public Set<String> hiddenNames() {
151+
return hiddenNames;
152+
}
156153
}
157154
}

0 commit comments

Comments
 (0)