103 lines
4.4 KiB
Markdown
103 lines
4.4 KiB
Markdown
# sakura::server
|
|||
|
|
|
||
|
|
`sakura::server` is Sakura's language server: what `sakura lsp` serves. It speaks the
|
||
|
|
language server protocol over the standard input and output, checking every `.sakura` file
|
||
|
|
of the editor's workspace folders as `sakura check` would. It publishes each file's notes
|
||
|
|
as diagnostics, and answers hovering, completion, the help shown while a call's arguments
|
||
|
|
are written, colouring, going to a declaration, finding where a name is written, and
|
||
|
|
renaming. The text open in the editor is checked in place of what's on disk.
|
||
|
|
|
||
|
|
The server itself never checks anything. Each job is worked out by a process of its own,
|
||
|
|
so a fault while checking can't stop the server, and a job taking more than 30 seconds is
|
||
|
|
stopped. A base process keeps the workspace parsed and checked, and checks again only what
|
||
|
|
an edit affects.
|
||
|
|
|
||
|
|
## Using it
|
||
|
|
|
||
|
|
Require this repository, which exports each of the compiler's libraries, and name this
|
||
|
|
one among a target's `deps`:
|
||
|
|
|
||
|
|
```yaml
|
||
|
|
requires:
|
||
|
|
- https://git.malunal.studio/sakura/std@v1.0.0
|
||
|
|
- https://git.malunal.studio/sakura/sakurac@v1.0.0
|
||
|
|
|
||
|
|
targets:
|
||
|
|
- name: app
|
||
|
|
type: program
|
||
|
|
deps: [sakura.server]
|
||
|
|
```
|
||
|
|
|
||
|
|
A program serving the protocol as `sakura lsp` does:
|
||
|
|
|
||
|
|
```sakura
|
||
|
|
import system;
|
||
|
|
import sakura::server;
|
||
|
|
|
||
|
|
# Serves the language server protocol until the client exits, checking for
|
||
|
|
# Linux, as `sakura run` would, with each argument a library as `NAME=DIR`.
|
||
|
|
def main(args: span<strview>): void {
|
||
|
|
# Windows can't copy a process, so there the server starts this program
|
||
|
|
# again for its jobs, as `PROGRAM lsp --base` and `PROGRAM lsp --worker`.
|
||
|
|
when os == windows {
|
||
|
|
if args.length == 2 && args[0] == "lsp" && (args[1] == "--base" || args[1] == "--worker") {
|
||
|
|
exit(serve_process(args[1] == "--base"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
# The standard library is found as `sakurac` finds it, unless an argument
|
||
|
|
# names it.
|
||
|
|
val found := found_std(args);
|
||
|
|
var libraries := vector::<strview>::new();
|
||
|
|
for each in args {
|
||
|
|
libraries.append(each);
|
||
|
|
}
|
||
|
|
for &each in found {
|
||
|
|
libraries.append(each.view());
|
||
|
|
}
|
||
|
|
exit(serve(server_options { native = false, system = "linux", libraries = libraries.view() }));
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
Like `sakurac`, the program finds `core`, `system`, and `runtime` as folders beside
|
||
|
|
its executable, or in the nearest folder above it that holds them. `found_std` finds `std`
|
||
|
|
the same way, or else the libraries of std in the `share/sakura` folder above it.
|
||
|
|
|
||
|
|
## What it holds
|
||
|
|
|
||
|
|
- **The server**: `serve`, given its `server_options`:
|
||
|
|
- `native` and `system`: what the workspace is checked for, an executable or the
|
||
|
|
machine, and which system;
|
||
|
|
- `libraries`: the libraries, the standard library's among them, each `NAME=DIR`, as
|
||
|
|
`sakura --lib` takes them.
|
||
|
|
|
||
|
|
It returns 0 where the client shut it down before exiting, and 1 otherwise.
|
||
|
|
- **Programs**: every `.sakura` file of the workspace folders, past hidden folders and
|
||
|
|
the folders of the libraries given, with `core` and the modules of `system`, the
|
||
|
|
standard library, and the libraries that they refer to. Where several files declare
|
||
|
|
`main`, each begins a program of its own, and the one checked is that of the file last
|
||
|
|
opened, edited, or asked about.
|
||
|
|
- **Jobs**: on Linux, the base starts a copy of itself for each job. On Windows, the
|
||
|
|
base and the workers are the program started again, which hands them to
|
||
|
|
`serve_process`.
|
||
|
|
- **Answers**: what's checked evaluates the program's constants on `sakura::machine`, so
|
||
|
|
the diagnostics are what `sakura check` says.
|
||
|
|
- **The protocol's text**: JSON, positions counted in UTF-16 code units, the `file:` URIs
|
||
|
|
of each system's paths, and the Markdown of documentation comments.
|
||
|
|
- **The toolchain**: `toolchain_folder` finds a folder beside the executable or above it,
|
||
|
|
as `core`, `found_std` the standard library, as `--lib` would give it, and
|
||
|
|
`target_checker` sets what a checker checks for. `sakura`'s commands use them too.
|
||
|
|
|
||
|
|
## Testing
|
||
|
|
|
||
|
|
From the repository's root, `blossom test` runs them as `sakura.server.tests`, or, with
|
||
|
|
`std` beside the repository:
|
||
|
|
|
||
|
|
```sh
|
||
|
|
sakurac run --lib std=../std --lib sakura::analyzer=analyzer --lib sakura::compiler=compiler \
|
||
|
|
--lib sakura::machine=machine --lib sakura::server=server \
|
||
|
|
tests/main.sakura tests/server/*.sakura
|
||
|
|
```
|
||
|
|
|
||
|
|
The tests are declared within the module. They check the positions of text whose lines
|
||
|
|
end in CR LF, messages carrying whatever a file holds as JSON, and the paths `file:` URIs
|
||
|
|
name, as Linux and Windows each spell them.
|