Skip to content

Latest commit

 

History

History

code-lsp

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

SciJava Code LSP

Code intelligence for SciJava scripts (completions, signature help, hover and diagnostics), in the terms of the Language Server Protocol.

lsp4j supplies the protocol: its types, the LanguageServer and LanguageClient interfaces, and JSON-RPC over a pair of streams. This module is the client side that lsp4j leaves to its users (deciding which servers answer for a script, keeping them in sync with it, and starting server processes), plus what is particular to SciJava scripts.

 client (e.g. Script Editor)
   │  ScriptSession: a script's text, and offsets into it
   ▼
 LanguageServerService ──► the LanguageServerPlugin serving the language
   │  a server of the session's own
   ├──► in-process servers (e.g. Groovy, Jython), built on ScriptLanguageServer
   └──► server processes (e.g. jedi-language-server), via LspServerSpec
          seeing the script as plain code (TransformingLanguageServer)

It does four things, each in a package of its own.

1. Brokering between scripts and servers (org.scijava.code.lsp)

The core. LanguageServerPlugins offer lsp4j LanguageServers for script languages; each language is served by the highest-priority plugin supporting it. A server combining several sources (e.g. Jython's own analysis and jedi's) combines them itself.

Clients ask about a script through a ScriptSession from the service, with a server of its own: it keeps the server's copy of the script up to date, names it (its file's URI, or an untitled: one), takes offsets into its text (see Positions), and reports the problems found in it. For code typed into a running interpreter, the server learns the interpreter's state through its Environment. Tools that ask once (e.g. to check a script) can call LanguageServerService.diagnose. For code typed into an interpreter whose language no plugin serves, a session completes the interpreter's variables.

2. Running server processes (org.scijava.code.lsp.launch)

For servers that are not Java in the SciJava process (e.g. jedi-language-server, for Python). An LspServerPlugin offers an LspServerSpec for an LSP language id (e.g. python), saying how to start a server for an environment and returning its streams (an LspConnection). Plugins get such a server via LanguageServerService.launch: started when first needed, one per environment, restarted if it stops, and stopped when idle.

3. Translating SciJava scripts (org.scijava.code.lsp.script)

A SciJava script is not quite plain code of its language: its #@ parameters define variables before it runs. LanguageServerService.scriptParameters parses them; a ScriptDocument is the script as plain code, with them declared with their run-time types, as its language's ScriptDialect says. TransformingLanguageServer shows servers that know the language but not SciJava (e.g. jedi) this text, and maps positions in their answers back. In-process servers use ScriptDocument too, to know the parameters.

4. A toolkit for in-process servers (org.scijava.code.lsp.jvm)

For servers of JVM languages, written in Java (e.g. Groovy's and Jython's): ScriptLanguageServer, a base class keeping the open documents and turning requests into questions about a document and an offset; Members, completions for Java classes' members by reflection; Callable, something to call as a completion snippet; Signatures, rating overloads against the arguments typed; and ClassIndex, the classes on the classpath, with their javadoc.

Who uses what

1 2 3 4
script-editor ✓
scripting-groovy ✓ ✓ ✓
scripting-jython ✓ ✓ ✓ ✓
scripting-appose-python ✓ ✓ ✓

The specialized packages depend only on org.scijava.code.lsp, never on each other. The module exports all four; its implementation (org.scijava.code.lsp.impl) is internal.