Skip to content

Commit 193a06d

Browse files
gselzerclaude
andcommitted
code-lsp: make ScriptDocument package-private
Only ScriptSession uses it. Public docs now describe the translation in ScriptDialect rather than linking to ScriptDocument; and the package docs are slimmed, saying where clients start (ScriptSession). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1e8bd84 commit 193a06d

5 files changed

Lines changed: 36 additions & 65 deletions

File tree

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434

3535
/**
3636
* A {@link SciJavaPlugin} that can create {@link LanguageServer}s.
37-
* <p>
38-
* Servers need not know SciJava scripts: sessions show them each script as
39-
* plain code of their language, as a {@link ScriptDialect} turns it, its
40-
* parameters declared (see {@link ScriptDocument}).
41-
* </p>
4237
*
4338
* @author Gabriel Selzer
4439
*/

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

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,21 @@
4242

4343
/**
4444
* A {@link SciJavaPlugin} that knows how to turn the scripts of a SciJava
45-
* language into plain code of an LSP language (a {@link ScriptDocument}), for
46-
* language servers of that language to analyze (see
47-
* {@link ScriptSession}). E.g. Appose-Python scripts become
48-
* plain Python, their parameters declared as NumPy arrays and the like.
45+
* language into plain code for language servers to analyze.
4946
* <p>
50-
* So script languages need not offer language servers themselves: servers
51-
* know their language, not SciJava scripts (see {@link LanguageServerPlugin}).
52-
* </p>
53-
* <p>
54-
* A dialect supplies the declarations, rather than translating scripts: a
55-
* {@link ScriptDocument} translates every script the same way, prepending the
56-
* dialect's {@link #header}, and blanking in place the lines SciJava removes
57-
* before a script runs (e.g. its {@code #@} lines). So, for every language,
58-
* positions in a script and in its document differ by just the header's
59-
* length, which is how a session maps servers' answers back to the script
60-
* (and leaves out those about the header); and that mapping is written, and
61-
* tested, once. A language whose scripts need more (e.g. wrapping in a class)
62-
* would extend that rule in {@link ScriptDocument}, for all languages.
47+
* A dialect supplies the declarations, rather than translating scripts:
48+
* every script is translated the same way, prepending the dialect's
49+
* {@link #header} (and a line break), and blanking in place, with spaces, the
50+
* lines SciJava removes before a script runs (e.g. its {@code #@} lines).
51+
* Nothing else changes. So, for every language, positions in a script and in
52+
* the code it becomes differ by just the header's length, which is how a
53+
* session maps servers' answers back to the script (and leaves out those
54+
* about the header); and that mapping is written, and tested, once. A
55+
* language whose scripts need more (e.g. wrapping in a class) would extend
56+
* that one rule, for all languages.
6357
* </p>
6458
*
6559
* @author Gabriel Selzer
66-
* @see ScriptDocument
6760
*/
6861
public interface ScriptDialect extends SciJavaPlugin {
6962

@@ -99,15 +92,15 @@ default Environment environment(final String text,
9992

10093
/**
10194
* Gets the code declaring the variables a script has before it runs, with
102-
* the types they have then, for a {@link ScriptDocument} to prepend to the
103-
* script: e.g. {@code import numpy; image: numpy.ndarray} for an image in
104-
* CPython. It may also import what the declarations need, and declare other
105-
* variables scripts can use (e.g. Appose's {@code task}); variables without
106-
* a useful type may be left out.
95+
* the types they have then, to prepend to the script: e.g.
96+
* {@code import numpy; image: numpy.ndarray} for an image in CPython. It may
97+
* also import what the declarations need, and declare other variables
98+
* scripts can use (e.g. Appose's {@code task}); variables without a useful
99+
* type may be left out.
107100
*
108101
* @param variables The script's variables, by name, in order: its
109-
* parameters (see {@link ScriptDocument}), and, if typed into a
110-
* live interpreter, the interpreter's (see {@link ScriptSession}).
102+
* parameters ({@code #@}), and, if typed into a live interpreter,
103+
* the interpreter's (see {@link ScriptSession}).
111104
*/
112105
Header header(Map<String, Variable> variables);
113106

@@ -160,7 +153,7 @@ final class Header {
160153

161154
/**
162155
* @param code The code: one line, without a line break (the
163-
* {@link ScriptDocument} adds one after it), or empty if none.
156+
* translation adds one after it), or empty if none.
164157
* @param hiddenNames The names to leave out of completion: those the
165158
* code binds only to declare the parameters, e.g.
166159
* {@code numpy} for {@code import numpy}.
@@ -184,8 +177,7 @@ public String code() {
184177
/**
185178
* The names to leave out of completion: those the code binds only to
186179
* declare the parameters, e.g. {@code numpy} for {@code import numpy}
187-
* (unless the script itself mentions them, see
188-
* {@link ScriptDocument#hiddenNames()}).
180+
* (unless the script itself mentions them).
189181
*/
190182
public Set<String> hiddenNames() {
191183
return hiddenNames;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
* @author Gabriel Selzer
6161
* @see ScriptDialect
6262
*/
63-
public final class ScriptDocument {
63+
final class ScriptDocument {
6464

6565
private final String original;
6666
private final String text;
@@ -88,7 +88,7 @@ private ScriptDocument(final String original, final String text,
8888
* A script as it is, as plain text ({@code plaintext}): nothing declared,
8989
* nothing blanked.
9090
*/
91-
public static ScriptDocument of(final String script) {
91+
static ScriptDocument of(final String script) {
9292
return new ScriptDocument(script, script, "plaintext", (t,
9393
f) -> new Environment(), 0, Collections.emptySet());
9494
}
@@ -103,7 +103,7 @@ public static ScriptDocument of(final String script) {
103103
* before running the script (e.g. its {@code #@} lines).
104104
* @param dialect How the script's language declares them.
105105
*/
106-
public static ScriptDocument of(final String script,
106+
static ScriptDocument of(final String script,
107107
final Map<String, ScriptDialect.Variable> variables,
108108
final Set<Integer> removedLines, final ScriptDialect dialect)
109109
{
@@ -128,7 +128,7 @@ Set<String> hiddenNames() {
128128
}
129129

130130
/** The script as plain code of its language. */
131-
public String text() {
131+
String text() {
132132
return text;
133133
}
134134

@@ -160,7 +160,7 @@ int toTransformed(final int offset) {
160160
* Maps an offset in {@link #text()} to one in the script; or
161161
* returns -1 if it lies in the header prepended.
162162
*/
163-
public int toOriginal(final int offset) {
163+
int toOriginal(final int offset) {
164164
return offset < headerLength ? -1 : offset - headerLength;
165165
}
166166

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@
9090
* A session controls its own {@link LanguageServer}: it a server of its own, which it shuts down when closed; it
9191
* keeps the server's copy of the script up to date when mutated by the
9292
* client. The server sees the script as plain code of its language (see
93-
* {@link ScriptDocument}); the session maps positions in its answers back,
93+
* {@link ScriptDialect}); the session maps positions in its answers back,
9494
* leaving out what concerns only the lines it added.
9595
* </p>
9696
* <p>
97-
* The server analyzes the script where it runs, as its document says (see
98-
* {@link ScriptDocument#environment}). As that changes (e.g. as the script's
97+
* The server analyzes the script where it runs, as its dialect says (see
98+
* {@link ScriptDialect#environment}). As that changes (e.g. as the script's
9999
* declared environment is edited), the session moves to a new server, of the
100100
* new environment. It talks to its server off the caller's thread, telling
101101
* and asking it everything in order.

‎scijava-code-lsp/src/main/java/org/scijava/code/lsp/package-info.java‎

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,16 @@
3131
* Code intelligence for SciJava scripts (completions, signature help, hover
3232
* and diagnostics), in the terms of the Language Server Protocol, via lsp4j.
3333
* <p>
34-
* This package brokers between scripts and the language servers that know
35-
* about them. {@link org.scijava.code.lsp.ScriptDialect}s turn scripts of
36-
* SciJava script languages into plain code of LSP languages (e.g.
37-
* {@code python}); {@link org.scijava.code.lsp.LanguageServerPlugin}s offer
38-
* lsp4j {@link org.eclipse.lsp4j.services.LanguageServer}s for LSP languages.
39-
* Clients (e.g. an editor) ask about a script through a
40-
* {@link org.scijava.code.lsp.ScriptSession}, made with the
41-
* {@link org.scijava.code.lsp.LanguageServerService}, in terms of the
42-
* script's text and offsets into it (see
43-
* {@link org.scijava.code.lsp.Positions}); behind it is a server of its own,
44-
* from the highest-priority plugin serving its dialect's language, for where
45-
* the script runs (moving to another as that changes).
34+
* SciJava scripts are a major use case for this library; however, the
35+
* {@link org.scijava.code.lsp.LanguageServerService} is written to offer
36+
* {@link org.eclipse.lsp4j.services.LanguageServer}s more generally.
37+
* {@link org.scijava.code.lsp.ScriptDialect}s convert SciJava scripts to plain
38+
* code that language servers can analyze, and clients ask about a script
39+
* through a {@link org.scijava.code.lsp.ScriptSession}.
4640
* </p>
4741
* <p>
48-
* Servers need know nothing of SciJava scripts. A SciJava script's {@code #@}
49-
* parameters define variables before it runs, which plain code of its
50-
* language would declare. A {@link org.scijava.code.lsp.ScriptDocument} is
51-
* the script as such code, with its parameters declared with their run-time
52-
* types, as its {@link org.scijava.code.lsp.ScriptDialect} says. A session
53-
* shows its servers this text, and maps positions in their answers back to
54-
* the script.
55-
* </p>
56-
* <p>
57-
* The subpackages are for those writing language servers:
58-
* {@link org.scijava.code.lsp.server} (servers that run as processes, and
59-
* servers routing documents by where they run) and
42+
* Some subpackages are useful for writing language servers:
43+
* {@link org.scijava.code.lsp.server} (servers that run as processes) and
6044
* {@link org.scijava.code.lsp.jvm} (servers written in Java).
6145
* </p>
6246
*/

0 commit comments

Comments
 (0)