Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,103 @@
|
||||
# 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.
|
||||
@@ -0,0 +1,861 @@
|
||||
#! @file answers.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the jobs a process of the server's works out from the files of the
|
||||
#! workspace, and how each is answered: the diagnostics of every file, what the
|
||||
#! name at a position is, the names that may be written there, what is being
|
||||
#! called there, the names of a file as what they are, and where what a name
|
||||
#! refers to is declared and written. Each answer is the text the server shapes
|
||||
#! into what it sends the client.
|
||||
|
||||
import std::net;
|
||||
import std::json;
|
||||
import std::serial;
|
||||
import sakura::analyzer;
|
||||
import sakura::machine;
|
||||
module sakura::server;
|
||||
|
||||
|
||||
#! What a process of its own is asked to work out.
|
||||
pub enum task : u8 {
|
||||
check, #! The diagnostics of every file.
|
||||
hover, #! What the name at a position is.
|
||||
complete, #! The names that may be written at a position.
|
||||
call, #! What is being called at a position, while its arguments are written.
|
||||
tokens, #! The names of a file, as the checker made them.
|
||||
name, #! What the name at a position refers to, and where it is declared.
|
||||
refer, #! Everywhere what the name at a position refers to is written.
|
||||
}
|
||||
|
||||
#! What is asked of a process.
|
||||
pub struct job {
|
||||
pub var task: task; #! What is asked.
|
||||
pub var strict: bool; #! Whether files with mistakes of syntax are left out, as a check without drafts does.
|
||||
pub var path: string; #! The file asked about, or empty.
|
||||
pub var line: u32; #! The line of the position asked about, from 0.
|
||||
pub var character: u32; #! Its UTF-16 code unit within the line, from 0.
|
||||
pub var touched: string; #! The file last opened, edited, or asked about, or empty.
|
||||
pub var entry: string; #! The file declaring `main` the last job was answered from, or empty.
|
||||
}
|
||||
|
||||
#! What the server's processes work with: the program, its checker, and what
|
||||
#! names the files of the editor's documents.
|
||||
pub struct workplace {
|
||||
pub var held: &mut program; #! The program's files, parsed.
|
||||
pub var checker: &mut checker; #! Its checker.
|
||||
pub var documents: &vector<document>; #! The documents open in the editor.
|
||||
}
|
||||
|
||||
#! Written after a `.` being completed, so the value before it parses as one.
|
||||
pub const placeholder: strview = "sakura__completion";
|
||||
|
||||
#! Right after `.`, a member name must follow for the value before it to parse,
|
||||
#! so one is written into the text of the file being completed.
|
||||
#! @param files The files, as gathered.
|
||||
#! @param asked The job.
|
||||
pub def write_placeholder(files: &mut vector<gathered>, asked: &job): void {
|
||||
return if asked.task != task::complete;
|
||||
for i in 0..files.length {
|
||||
continue if files[i].path.view() != asked.path.view();
|
||||
val text := files[i].text.view();
|
||||
val offset := locate(text, asked.line as u64, asked.character as u64);
|
||||
val start := word_start(text, offset);
|
||||
return if start != offset || start == 0 || text[(start - 1) as u64] != '.'
|
||||
|| (start >= 2 && text[(start - 2) as u64] == '.');
|
||||
var written := string::from(text.substr(0, offset as u64), nothing {});
|
||||
written.append(placeholder);
|
||||
written.append(text.substr(offset as u64, text.length - offset as u64));
|
||||
files[i].text = move(&mut written);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#! Where the word a byte of the text ends, or is within, begins.
|
||||
#! @param text The text.
|
||||
#! @param offset The byte.
|
||||
#! @returns The byte the word begins at, which is the byte itself where no word
|
||||
#! ends there.
|
||||
def word_start(text: strview, offset: u32): u32 {
|
||||
var start := offset;
|
||||
while start > 0 && is_word_byte(text[(start - 1) as u64]) {
|
||||
start--;
|
||||
}
|
||||
return start;
|
||||
}
|
||||
|
||||
#! Answers a job from a checked program. The path of the file declaring `main`
|
||||
#! the program begins with comes first, ended by a zero byte, so the server knows
|
||||
#! which program answered.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
pub def answer(place: &mut workplace, asked: &job, out: &mut string): void {
|
||||
if place.held.entry != none {
|
||||
out.append(place.held.paths[place.held.entry].view());
|
||||
}
|
||||
out.push(0);
|
||||
match asked.task {
|
||||
task::check => answer_check(place, out);
|
||||
task::hover => answer_hover(place, asked, out);
|
||||
task::complete => answer_complete(place, asked, out);
|
||||
task::call => answer_call(place, asked, out);
|
||||
task::tokens => answer_tokens(place, asked, out);
|
||||
task::name => answer_name(place, asked, out);
|
||||
task::refer => answer_refer(place, asked, out);
|
||||
}
|
||||
}
|
||||
|
||||
#! The URI a file is named by: the one the editor gave it where it is open,
|
||||
#! which may be spelled other than as its path is, and else its path's.
|
||||
#! @param place What the process works with.
|
||||
#! @param path The file's path.
|
||||
#! @param out Where the URI is written, as a JSON string.
|
||||
def write_file_uri(place: &workplace, path: strview, out: &mut json::writer): void {
|
||||
# The types the syntax makes are shown from a file of their own.
|
||||
if path == builtin_path {
|
||||
val shown := builtin_document();
|
||||
if shown.length != 0 {
|
||||
var uri := string::new(nothing {});
|
||||
append_uri(&mut uri, shown.view());
|
||||
out.text(uri.view());
|
||||
return;
|
||||
}
|
||||
}
|
||||
for &each in place.documents {
|
||||
if each.path.view() == path {
|
||||
out.text(each.uri.view());
|
||||
return;
|
||||
}
|
||||
}
|
||||
var uri := string::new(nothing {});
|
||||
append_uri(&mut uri, path);
|
||||
out.text(uri.view());
|
||||
}
|
||||
|
||||
#! Appends the URI a file's line of a check begins with: the one the editor gave
|
||||
#! it where it is open, which may be spelled other than as its path is, unless
|
||||
#! a space or a line feed in it would end it early, and else its path's.
|
||||
#! @param place What the process works with.
|
||||
#! @param path The file's path.
|
||||
#! @param out Where the URI is written.
|
||||
def append_line_uri(place: &workplace, path: strview, out: &mut string): void {
|
||||
for &each in place.documents {
|
||||
continue if each.path.view() != path;
|
||||
var plain := true;
|
||||
for i in 0..each.uri.length {
|
||||
plain = plain && each.uri[i] != ' ' && each.uri[i] != '\n';
|
||||
}
|
||||
if plain {
|
||||
out.append(each.uri.view());
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
append_uri(out, path);
|
||||
}
|
||||
|
||||
#! Writes a place a name is written as the protocol writes a location.
|
||||
#! @param place What the process works with.
|
||||
#! @param file The file, as the program counts them.
|
||||
#! @param start The byte the name begins at.
|
||||
#! @param end The byte it ends before.
|
||||
#! @param out Where it is written.
|
||||
def write_location(place: &workplace, file: u32, start: u32, end: u32, out: &mut json::writer): void {
|
||||
out.begin_object();
|
||||
out.member("uri");
|
||||
write_file_uri(place, place.held.paths[file].view(), out);
|
||||
out.member("range");
|
||||
write_range(out, place.held.source_text(file), &place.held.sources[file].lines, start, end);
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! Writes a place the checker gave, by the file of its own count.
|
||||
#! @param place What the process works with.
|
||||
#! @param at The place.
|
||||
#! @param out Where it is written.
|
||||
def write_spot(place: &workplace, at: &spot, out: &mut json::writer): void {
|
||||
val file: u32 = place.held.files[at.file];
|
||||
write_location(place, file, at.offset, at.offset + at.length, out);
|
||||
}
|
||||
|
||||
#! The file a job asks about, as the program holds it, and the position asked
|
||||
#! about within it.
|
||||
struct asked_file {
|
||||
var file: u32; #! The file, as the program counts them.
|
||||
var unit: u32; #! The checker's index of it.
|
||||
var text: strview; #! Its text, as its source holds it.
|
||||
var offset: u32; #! The byte of the text the position is at.
|
||||
}
|
||||
|
||||
#! Finds the file a job asks about, and the position within it.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @returns The file, or nothing where the program does not check it.
|
||||
def open_asked(place: &workplace, asked: &job): maybe<asked_file> {
|
||||
val file := place.held.file_of(asked.path.view());
|
||||
return nothing {} if file == none || place.held.units[file] == none;
|
||||
val text := place.held.source_text(file);
|
||||
return asked_file {
|
||||
file = file,
|
||||
unit = place.held.units[file],
|
||||
text = text,
|
||||
offset = locate(text, asked.line as u64, asked.character as u64)
|
||||
};
|
||||
}
|
||||
|
||||
#! Writes a line for each file with notes: its URI, as `append_line_uri` gives
|
||||
#! it, a space, and the JSON array of its diagnostics. A file with mistakes of
|
||||
#! syntax only shows those, since what the checker says of a file it could not
|
||||
#! wholly read would mislead.
|
||||
#! @param place What the process works with.
|
||||
#! @param out Where the answer is written.
|
||||
def answer_check(place: &mut workplace, out: &mut string): void {
|
||||
# The program is checked whole, as `sakura check` would check it, so what is
|
||||
# shown is what it would say. What checking leaves to evaluating is evaluated
|
||||
# once the program checks, which notes what goes wrong as checking does.
|
||||
var notes := vector::<note>::new();
|
||||
var whole := checker::new(&mut place.held.names, &mut notes);
|
||||
place.held.check_whole(&mut whole);
|
||||
if notes.length == 0 {
|
||||
var clean := true;
|
||||
for &file in place.held.files {
|
||||
clean = clean && !place.held.broken(file);
|
||||
}
|
||||
if clean {
|
||||
evaluate(&mut whole);
|
||||
}
|
||||
}
|
||||
|
||||
var diagnostics := message_writer();
|
||||
for i in 0..place.held.paths.length {
|
||||
val file := i as u32;
|
||||
val broken := place.held.broken(file);
|
||||
var found: u32 = 0;
|
||||
diagnostics.clear();
|
||||
diagnostics.begin_array();
|
||||
for &each in place.held.parse_notes[i] {
|
||||
write_diagnostic(place, file, &each, &mut diagnostics);
|
||||
found++;
|
||||
}
|
||||
val unit: u32 = place.held.units[i];
|
||||
if !broken && unit != none {
|
||||
for &each in notes {
|
||||
continue if each.file != unit;
|
||||
write_diagnostic(place, file, &each, &mut diagnostics);
|
||||
found++;
|
||||
}
|
||||
}
|
||||
diagnostics.end_array();
|
||||
continue if found == 0;
|
||||
append_line_uri(place, place.held.paths[i].view(), out);
|
||||
out.push(' ');
|
||||
append_written(out, &mut diagnostics);
|
||||
out.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
#! What a note says: its message, then the instance of a generic it was found
|
||||
#! in, where it was found in one, as `... (box<i64>)`.
|
||||
#! @param each The note.
|
||||
#! @param held The source of its file.
|
||||
#! @returns The text.
|
||||
pub def note_text(each: ¬e, held: &source): string {
|
||||
var text := each.message(held);
|
||||
if each.instance.length != 0 {
|
||||
text.append(" (");
|
||||
text.append(each.instance.view());
|
||||
text.append(")");
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
#! Writes a note as a diagnostic of the protocol: the range of what it is
|
||||
#! about, which marks the character it is at where it is about no text, its
|
||||
#! message, with the instance of a generic it was found in, and the place it
|
||||
#! refers to besides its own.
|
||||
#! @param place What the process works with.
|
||||
#! @param file The file it is of.
|
||||
#! @param each The note.
|
||||
#! @param out Where it is written.
|
||||
def write_diagnostic(place: &workplace, file: u32, each: ¬e, out: &mut json::writer): void {
|
||||
val text := place.held.source_text(file);
|
||||
val start := each.offset;
|
||||
var end := start + each.length;
|
||||
if end == start && (start as u64) < text.length && text[start as u64] != '\n' {
|
||||
val size := serial::utf8_length(text, start as u64);
|
||||
end += size != 0 ? size as u32 : 1;
|
||||
}
|
||||
if end as u64 > text.length {
|
||||
end = text.length as u32;
|
||||
}
|
||||
|
||||
val message := note_text(each, &place.held.sources[file]);
|
||||
val code := $"{each.code}";
|
||||
out.begin_object();
|
||||
out.member("range");
|
||||
write_range(out, text, &place.held.sources[file].lines, start, end);
|
||||
out.member("severity"); out.unsigned(each.warning ? 2 : 1);
|
||||
out.member("source"); out.text("sakura");
|
||||
out.member("code"); out.text(code.view());
|
||||
out.member("message"); out.text(message.view());
|
||||
if each.related.length != 0 && place.held.units[file] != none {
|
||||
val other: u32 = place.held.files[each.related_file];
|
||||
out.member("relatedInformation");
|
||||
out.begin_array();
|
||||
out.begin_object();
|
||||
out.member("location");
|
||||
write_location(place, other, each.related_offset, each.related_offset + each.related_length, out);
|
||||
out.member("message");
|
||||
out.text(each.related.view());
|
||||
out.end_object();
|
||||
out.end_array();
|
||||
}
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! Writes the hover of the name at the position, or nothing where there is none.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
def answer_hover(place: &mut workplace, asked: &job, out: &mut string): void {
|
||||
val opened := open_asked(place, asked) else { return; };
|
||||
var shown := place.checker.hover(opened.unit, opened.offset) else { return; };
|
||||
|
||||
var markdown := string::new(nothing {});
|
||||
append_code(&mut markdown, shown.signature.view());
|
||||
if shown.documentation.length != 0 {
|
||||
markdown.append("\n\n");
|
||||
append_markdown(&mut markdown, shown.documentation.view());
|
||||
}
|
||||
var made := message_writer();
|
||||
made.begin_object();
|
||||
made.member("contents");
|
||||
write_markdown(&mut made, markdown.view());
|
||||
made.member("range");
|
||||
write_range(&mut made, opened.text, &place.held.sources[opened.file].lines, shown.offset, shown.offset + shown.length);
|
||||
made.end_object();
|
||||
append_written(out, &mut made);
|
||||
}
|
||||
|
||||
#! The kind of completion item of the protocol each kind of name is.
|
||||
#! @param kind What the name is.
|
||||
#! @returns The item's kind.
|
||||
def item_kind(kind: symbol): u32 {
|
||||
return match kind {
|
||||
symbol::keyword => 14,
|
||||
symbol::namespace => 9,
|
||||
symbol::function => 3,
|
||||
symbol::method => 2,
|
||||
symbol::variable => 6,
|
||||
symbol::constant => 21,
|
||||
symbol::parameter => 6,
|
||||
symbol::field => 5,
|
||||
symbol::struct_type => 22,
|
||||
symbol::trait_type => 8,
|
||||
symbol::enum_type => 13,
|
||||
symbol::enum_member => 20,
|
||||
symbol::type_name => 7,
|
||||
symbol::type_param => 25
|
||||
};
|
||||
}
|
||||
|
||||
#! Writes what an item shows beside its name in the list, read from the first
|
||||
#! line of its signature, as its member `labelDetails`: the parameters of a
|
||||
#! function, as `(&this, by: int)`, with what it gives, and the type of anything
|
||||
#! else, as `int` of `val count: int`.
|
||||
#! @param item The item.
|
||||
#! @param called Whether the names follow `value.`, where a method's receiver
|
||||
#! is the value, so it is not shown.
|
||||
#! @param out Where it is written, within the item.
|
||||
def write_label_details(item: &completion, called: bool, out: &mut json::writer): void {
|
||||
val function := item.kind == symbol::function || item.kind == symbol::method;
|
||||
val value := item.kind == symbol::variable || item.kind == symbol::constant || item.kind == symbol::parameter
|
||||
|| item.kind == symbol::field;
|
||||
return if !function && !value;
|
||||
|
||||
var signature := item.signature.view();
|
||||
for i in 0..signature.length {
|
||||
if signature[i] == '\n' {
|
||||
signature = signature.substr(0, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
val name := item.name.view();
|
||||
|
||||
# The name as the signature declares it, followed by what describes it.
|
||||
var after := signature.length + 1;
|
||||
var at: u64 = 0;
|
||||
while at + name.length <= signature.length && after > signature.length {
|
||||
val next := at + name.length;
|
||||
if signature.substr(at, name.length) == name && (at == 0 || !is_word_byte(signature[at - 1]))
|
||||
&& next < signature.length && (signature[next] == '(' || signature[next] == '<' || signature[next] == ':') {
|
||||
after = next;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
return if after > signature.length;
|
||||
|
||||
val rest := signature.substr(after, signature.length - after);
|
||||
if rest[0] == ':' && value {
|
||||
val skip: u64 = rest.length > 1 && rest[1] == ' ' ? 2 : 1;
|
||||
out.member("labelDetails");
|
||||
out.begin_object();
|
||||
out.member("description");
|
||||
out.text(rest.substr(skip, rest.length - skip));
|
||||
out.end_object();
|
||||
return;
|
||||
}
|
||||
|
||||
# The parameters end at the `)` closing the first `(`.
|
||||
var depth := 0;
|
||||
var close := rest.length;
|
||||
for i in 0..rest.length {
|
||||
if rest[i] == '(' {
|
||||
depth++;
|
||||
} else if rest[i] == ')' && depth > 0 {
|
||||
depth--;
|
||||
if depth == 0 {
|
||||
close = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return if close == rest.length || !function;
|
||||
|
||||
# Called on a value after `.`, a method is given its receiver, so it is not shown.
|
||||
var shown := string::new(nothing {});
|
||||
var open: u64 = 0;
|
||||
while open < close && rest[open] != '(' {
|
||||
open++;
|
||||
}
|
||||
if called && item.kind == symbol::method && open + 5 <= close && rest.substr(open + 1, 4) == "this" {
|
||||
var past := open + 5;
|
||||
while past < close && rest[past] != ',' && rest[past] != ')' {
|
||||
past++;
|
||||
}
|
||||
if past < close && rest[past] == ',' {
|
||||
past++;
|
||||
}
|
||||
while past < close && rest[past] == ' ' {
|
||||
past++;
|
||||
}
|
||||
shown.append(rest.substr(0, open + 1));
|
||||
shown.append(rest.substr(past, close + 1 - past));
|
||||
} else {
|
||||
shown.append(rest.substr(0, close + 1));
|
||||
}
|
||||
out.member("labelDetails");
|
||||
out.begin_object();
|
||||
out.member("detail");
|
||||
out.text(shown.view());
|
||||
var gives := rest.substr(close + 1, rest.length - close - 1);
|
||||
if gives.length > 2 && gives[0] == ':' && gives[1] == ' ' {
|
||||
gives = gives.substr(2, gives.length - 2);
|
||||
# What a function gives ends where a `where` clause bounding it begins.
|
||||
for i in 0..gives.length {
|
||||
if gives.substr(i, gives.length - i).starts_with(" where ") {
|
||||
gives = gives.substr(0, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
out.member("description");
|
||||
out.text(gives);
|
||||
}
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! Writes the names that may be written at the position, in the order found, so
|
||||
#! locals come before what is declared further away. The answer is the items,
|
||||
#! a zero byte, then the documentation of each item in turn, each ended by a
|
||||
#! zero byte, which the server keeps to give the one item the editor asks to
|
||||
#! resolve rather than sending all of it with the list.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
def answer_complete(place: &mut workplace, asked: &job, out: &mut string): void {
|
||||
val opened := open_asked(place, asked) else { return; };
|
||||
val items := place.checker.complete(opened.unit, opened.offset);
|
||||
|
||||
# Whether the names follow `value.`, where a method's receiver is the value.
|
||||
val start := word_start(opened.text, opened.offset);
|
||||
val called := start > 0 && opened.text[(start - 1) as u64] == '.';
|
||||
|
||||
var documentation := string::new(nothing {});
|
||||
var made := message_writer();
|
||||
made.begin_object();
|
||||
made.member("isIncomplete"); made.boolean(false);
|
||||
made.member("items");
|
||||
made.begin_array();
|
||||
for i in 0..items.length {
|
||||
val item := &items[i];
|
||||
made.begin_object();
|
||||
made.member("label"); made.text(item.name.view());
|
||||
made.member("kind"); made.unsigned(item_kind(item.kind) as u64);
|
||||
val order := $"{i}";
|
||||
var sorted := string::new(nothing {});
|
||||
for _ in order.length..5 {
|
||||
sorted.push('0');
|
||||
}
|
||||
sorted.append(order.view());
|
||||
made.member("sortText"); made.text(sorted.view());
|
||||
if item.insert.length != 0 {
|
||||
made.member("insertText"); made.text(item.insert.view());
|
||||
}
|
||||
write_label_details(item, called, &mut made);
|
||||
|
||||
# The detail is a line, so a struct's signature shows whole in the
|
||||
# documentation.
|
||||
val signature := item.signature.view();
|
||||
var newline := signature.length;
|
||||
for k in 0..signature.length {
|
||||
if signature[k] == '\n' {
|
||||
newline = k;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if signature.length != 0 {
|
||||
made.member("detail"); made.text(signature.substr(0, newline));
|
||||
}
|
||||
|
||||
var markdown := string::new(nothing {});
|
||||
if newline < signature.length {
|
||||
append_code(&mut markdown, signature);
|
||||
}
|
||||
if item.documentation.length != 0 {
|
||||
if newline < signature.length {
|
||||
markdown.append("\n\n");
|
||||
}
|
||||
append_markdown(&mut markdown, item.documentation.view());
|
||||
}
|
||||
if markdown.length != 0 {
|
||||
made.member("data"); made.unsigned(i);
|
||||
}
|
||||
documentation.append(markdown.view());
|
||||
documentation.push(0);
|
||||
made.end_object();
|
||||
}
|
||||
made.end_array();
|
||||
made.end_object();
|
||||
append_written(out, &mut made);
|
||||
out.push(0);
|
||||
out.append(documentation.view());
|
||||
}
|
||||
|
||||
#! Writes what is called at the position: its overloads, each with the
|
||||
#! parameters of its signature marked, and which parameter is being written.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
def answer_call(place: &mut workplace, asked: &job, out: &mut string): void {
|
||||
val opened := open_asked(place, asked) else { return; };
|
||||
var help := place.checker.calls_at(opened.unit, opened.offset) else { return; };
|
||||
|
||||
var made := message_writer();
|
||||
made.begin_object();
|
||||
made.member("signatures");
|
||||
made.begin_array();
|
||||
for &each in help.overloads {
|
||||
made.begin_object();
|
||||
made.member("label"); made.text(each.signature.view());
|
||||
# The contract its body begins with comes before what is written of it.
|
||||
if each.documentation.length != 0 || each.contract.length != 0 {
|
||||
var markdown := string::new(nothing {});
|
||||
if each.contract.length != 0 {
|
||||
append_code(&mut markdown, each.contract.view());
|
||||
}
|
||||
if each.documentation.length != 0 {
|
||||
if markdown.length != 0 {
|
||||
markdown.append("\n\n");
|
||||
}
|
||||
append_markdown(&mut markdown, each.documentation.view());
|
||||
}
|
||||
made.member("documentation");
|
||||
write_markdown(&mut made, markdown.view());
|
||||
}
|
||||
|
||||
# Each parameter is a range of the label, which the editor marks in place.
|
||||
made.member("parameters");
|
||||
made.begin_array();
|
||||
val label := each.signature.view();
|
||||
var begin: u64 = 0;
|
||||
while begin < label.length && label[begin] != '(' {
|
||||
begin++;
|
||||
}
|
||||
var from := begin + 1;
|
||||
var nested := 0;
|
||||
for at in (begin + 1)..label.length {
|
||||
val byte: u8 = label[at];
|
||||
if byte == '(' || byte == '[' || byte == '<' {
|
||||
nested++;
|
||||
} else if (byte == ')' || byte == ']' || byte == '>') && nested > 0 {
|
||||
nested--;
|
||||
continue;
|
||||
}
|
||||
continue if nested > 0 || (byte != ',' && byte != ')');
|
||||
while from < at && label[from] == ' ' {
|
||||
from++;
|
||||
}
|
||||
if at > from {
|
||||
made.begin_object();
|
||||
made.member("label");
|
||||
made.begin_array(); made.unsigned(from); made.unsigned(at); made.end_array();
|
||||
made.end_object();
|
||||
}
|
||||
from = at + 1;
|
||||
break if byte == ')';
|
||||
}
|
||||
made.end_array();
|
||||
# Every argument after a variadic parameter's first is given to it too.
|
||||
val active := each.variadic && each.arity > 0 && help.parameter >= each.arity ? each.arity - 1 : help.parameter;
|
||||
made.member("activeParameter"); made.unsigned(active as u64);
|
||||
made.end_object();
|
||||
}
|
||||
made.end_array();
|
||||
made.member("activeSignature"); made.unsigned(0);
|
||||
made.member("activeParameter"); made.unsigned(help.parameter as u64);
|
||||
made.end_object();
|
||||
append_written(out, &mut made);
|
||||
}
|
||||
|
||||
#! The kind of semantic token each kind of name is, in the order of the legend
|
||||
#! the server declares.
|
||||
#! @param kind What the name is.
|
||||
#! @returns The token's kind.
|
||||
def token_kind(kind: symbol): u32 {
|
||||
return match kind {
|
||||
symbol::namespace => 0,
|
||||
symbol::struct_type => 1,
|
||||
symbol::trait_type => 2,
|
||||
symbol::enum_type => 3,
|
||||
symbol::type_name => 4,
|
||||
symbol::type_param => 5,
|
||||
symbol::parameter => 6,
|
||||
symbol::variable => 7,
|
||||
symbol::constant => 7,
|
||||
symbol::field => 8,
|
||||
symbol::enum_member => 9,
|
||||
symbol::function => 10,
|
||||
symbol::method => 11,
|
||||
symbol::keyword => 12
|
||||
};
|
||||
}
|
||||
|
||||
#! Writes the names of the file as the protocol counts them: five numbers each,
|
||||
#! the line and character of the first being from the beginning of the file and
|
||||
#! of the rest from the name before it.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
def answer_tokens(place: &mut workplace, asked: &job, out: &mut string): void {
|
||||
val opened := open_asked(place, asked) else { return; };
|
||||
val found := place.checker.highlights(opened.unit);
|
||||
val starts := &place.held.sources[opened.file].lines;
|
||||
|
||||
var line: u32 = 0;
|
||||
var character: u32 = 0;
|
||||
var made := message_writer();
|
||||
made.begin_object();
|
||||
made.member("data");
|
||||
made.begin_array();
|
||||
for &each in found {
|
||||
val begins := position_of(opened.text, starts, each.offset);
|
||||
val ends := position_of(opened.text, starts, each.offset + each.length);
|
||||
continue if ends.line != begins.line || ends.character <= begins.character || begins.line < line;
|
||||
made.unsigned((begins.line - line) as u64);
|
||||
made.unsigned((begins.line == line ? begins.character - character : begins.character) as u64);
|
||||
made.unsigned((ends.character - begins.character) as u64);
|
||||
made.unsigned(token_kind(each.kind) as u64);
|
||||
made.unsigned(((each.declared ? 1 : 0) | (each.readonly ? 2 : 0)) as u64);
|
||||
line = begins.line;
|
||||
character = begins.character;
|
||||
}
|
||||
made.end_array();
|
||||
made.end_object();
|
||||
append_written(out, &mut made);
|
||||
}
|
||||
|
||||
#! Writes what the name at the position refers to: the name as it is written,
|
||||
#! where it is, where what it refers to is declared, and why it cannot be
|
||||
#! renamed, which the requests about names shape into their answers.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
def answer_name(place: &mut workplace, asked: &job, out: &mut string): void {
|
||||
val opened := open_asked(place, asked) else { return; };
|
||||
val named := place.checker.name_at(opened.unit, opened.offset) else { return; };
|
||||
|
||||
var made := message_writer();
|
||||
made.begin_object();
|
||||
made.member("name");
|
||||
made.text(opened.text.substr(named.at.offset as u64, named.at.length as u64));
|
||||
made.member("at");
|
||||
write_spot(place, &named.at, &mut made);
|
||||
if named.declared {
|
||||
made.member("declaration");
|
||||
write_spot(place, &named.declaration, &mut made);
|
||||
}
|
||||
if named.refusal.length != 0 {
|
||||
made.member("refusal");
|
||||
made.text(named.refusal);
|
||||
}
|
||||
made.end_object();
|
||||
append_written(out, &mut made);
|
||||
}
|
||||
|
||||
#! A place a name is written, by the file of the program's count, which every
|
||||
#! program the name is sought in shares.
|
||||
struct placed {
|
||||
var file: u32; #! The file, as the program counts them.
|
||||
var start: u32; #! The byte the name begins at.
|
||||
var end: u32; #! The byte it ends before.
|
||||
}
|
||||
|
||||
#! Writes everywhere what the name at the position refers to is written, in
|
||||
#! every program, with where it is declared and why it cannot be renamed, as
|
||||
#! `answer_name` does, whether it is a local or a member, and where its text is
|
||||
#! written but was never resolved.
|
||||
#! @param place What the process works with.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
def answer_refer(place: &mut workplace, asked: &job, out: &mut string): void {
|
||||
val opened := open_asked(place, asked) else { return; };
|
||||
val named := place.checker.name_at(opened.unit, opened.offset) else { return; };
|
||||
val written := opened.text.substr(named.at.offset as u64, named.at.length as u64);
|
||||
|
||||
var places := vector::<placed>::new();
|
||||
var strays := vector::<placed>::new();
|
||||
var seen := vector::<u64>::new();
|
||||
var found := vector::<spot>::new();
|
||||
var stray := vector::<spot>::new();
|
||||
place.checker.refer_all(opened.unit, opened.offset, &mut found, &mut stray);
|
||||
for &each in found {
|
||||
keep_once(place.held.files[each.file], &each, &mut places, &mut seen);
|
||||
}
|
||||
for &each in stray {
|
||||
keep_once(place.held.files[each.file], &each, &mut strays, &mut seen);
|
||||
}
|
||||
if named.declared && !named.local {
|
||||
refer_elsewhere(place, &named.declaration, written, &mut places, &mut strays, &mut seen);
|
||||
}
|
||||
|
||||
var made := message_writer();
|
||||
made.begin_object();
|
||||
made.member("name");
|
||||
made.text(written);
|
||||
made.member("locations");
|
||||
write_places(place, &places, &mut made);
|
||||
if named.declared {
|
||||
made.member("declaration");
|
||||
write_spot(place, &named.declaration, &mut made);
|
||||
}
|
||||
if named.refusal.length != 0 {
|
||||
made.member("refusal");
|
||||
made.text(named.refusal);
|
||||
}
|
||||
made.member("local"); made.boolean(named.local);
|
||||
made.member("member"); made.boolean(named.member);
|
||||
made.member("unresolved");
|
||||
write_places(place, &strays, &mut made);
|
||||
made.end_object();
|
||||
append_written(out, &mut made);
|
||||
}
|
||||
|
||||
#! Writes places a name is written, as an array of locations.
|
||||
#! @param place What the process works with.
|
||||
#! @param places The places.
|
||||
#! @param out Where they are written.
|
||||
def write_places(place: &workplace, places: &vector<placed>, out: &mut json::writer): void {
|
||||
out.begin_array();
|
||||
for &each in places {
|
||||
write_location(place, each.file, each.start, each.end, out);
|
||||
}
|
||||
out.end_array();
|
||||
}
|
||||
|
||||
#! Whether text holds a name as a word of its own.
|
||||
#! @param text The text.
|
||||
#! @param name The name.
|
||||
#! @returns Whether it does.
|
||||
def mentions(text: strview, name: strview): bool {
|
||||
return false if name.length == 0;
|
||||
var at: u64 = 0;
|
||||
while at + name.length <= text.length {
|
||||
if text.substr(at, name.length) == name {
|
||||
val joined := (at > 0 && is_word_byte(text[at - 1])) || (at + name.length < text.length && is_word_byte(text[at + name.length]));
|
||||
return true if !joined;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Keeps where the name is written in the programs the job was not answered
|
||||
#! from. Every file but those beginning a program is in each of them, so only
|
||||
#! those files are looked within, each by checking its program in turn, and
|
||||
#! only where it mentions the name. What is declared in a file beginning a
|
||||
#! program is not written in another program, so it is not sought.
|
||||
#! @param place What the process works with.
|
||||
#! @param declared Where what the name refers to is declared.
|
||||
#! @param name The name as it is written.
|
||||
#! @param places Where the places it is written are kept.
|
||||
#! @param strays Where the places it is written unresolved are kept.
|
||||
#! @param seen The places kept so far.
|
||||
def refer_elsewhere(
|
||||
place: &mut workplace,
|
||||
declared: &spot,
|
||||
name: strview,
|
||||
places: &mut vector<placed>,
|
||||
strays: &mut vector<placed>,
|
||||
seen: &mut vector<u64>
|
||||
): void {
|
||||
val held := &mut place.held;
|
||||
val home: u32 = held.files[declared.file];
|
||||
return if held.mains[home];
|
||||
for i in 0..held.paths.length {
|
||||
val entry := i as u32;
|
||||
continue if !held.mains[i] || entry == held.entry || !mentions(held.texts[i].view(), name);
|
||||
|
||||
# The other program, checked with the same files, by a checker of its own.
|
||||
var notes := vector::<note>::new();
|
||||
var other := checker::new(&mut held.names, &mut notes);
|
||||
target_checker(&mut other, held.system, held.native);
|
||||
var units := vector::<u32>::new();
|
||||
var files := vector::<u32>::new();
|
||||
for j in 0..held.paths.length {
|
||||
val file := j as u32;
|
||||
if (held.mains[j] && file != entry) || (held.strict && held.broken(file)) {
|
||||
units.append(none);
|
||||
continue;
|
||||
}
|
||||
units.append(other.add(&held.sources[j], &held.syntaxes[j], held.roots[j]));
|
||||
files.append(file);
|
||||
}
|
||||
other.check();
|
||||
val there: u32 = units[home];
|
||||
continue if there == none;
|
||||
|
||||
var found := vector::<spot>::new();
|
||||
var stray := vector::<spot>::new();
|
||||
other.refer_all(there, declared.offset, &mut found, &mut stray);
|
||||
for &each in found {
|
||||
keep_once(files[each.file], &each, places, seen);
|
||||
}
|
||||
for &each in stray {
|
||||
keep_once(files[each.file], &each, strays, seen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Keeps a place a checker gave, once.
|
||||
#! @param file The place's file, as the program counts them.
|
||||
#! @param at The place.
|
||||
#! @param into Where it is kept.
|
||||
#! @param seen The places kept so far, by file and offset.
|
||||
def keep_once(file: u32, at: &spot, into: &mut vector<placed>, seen: &mut vector<u64>): void {
|
||||
val key := (file as u64 << 32) | at.offset as u64;
|
||||
return if seen.view().contains(&key);
|
||||
seen.append(key);
|
||||
into.append(placed { file = file, start = at.offset, end = at.offset + at.length });
|
||||
}
|
||||
@@ -0,0 +1,704 @@
|
||||
#! @file jobs.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the server has a job worked out: by asking the base, which
|
||||
#! starts a copy of itself for the job, or where the base cannot, by a process
|
||||
#! of the server's own that works it out from nothing. Either way the job runs
|
||||
#! in a process of its own, which cannot take the server down with it, and is
|
||||
#! stopped where it takes too long.
|
||||
#!
|
||||
#! Windows cannot start a copy of a process, so there the server's processes
|
||||
#! are this program started again, `lsp --base` and `lsp --worker`, which read
|
||||
#! what the server works with from their input. The base answers each job
|
||||
#! itself, bringing the program up to date with the files as they are, so a job
|
||||
#! that faults or never ends takes only the base with it, which the server stops
|
||||
#! and makes again. A worker works out one job from nothing, as the process of
|
||||
#! the server's own does on Linux.
|
||||
|
||||
import std::process;
|
||||
import system;
|
||||
import sakura::analyzer;
|
||||
module sakura::server;
|
||||
|
||||
|
||||
#! What each kind of job is doing, as the client's log says where it fails.
|
||||
#! @param kind The kind of job.
|
||||
#! @returns What it is doing.
|
||||
def doing(kind: task): strview {
|
||||
return match kind {
|
||||
task::check => "Checking the workspace",
|
||||
task::hover => "Describing the name hovered",
|
||||
task::complete => "Completing the name",
|
||||
task::call => "Describing what is called",
|
||||
task::tokens => "Colouring the file",
|
||||
task::name => "Finding what the name refers to",
|
||||
task::refer => "Finding where the name is written"
|
||||
};
|
||||
}
|
||||
|
||||
#! Writes the documents open in the editor to a handle: how many there are, then
|
||||
#! the URI, path, and text of each.
|
||||
#! @param handle The handle.
|
||||
#! @param documents The documents.
|
||||
#! @returns Whether they were written.
|
||||
def put_documents(handle: system::fd, documents: &vector<document>): bool {
|
||||
var sent := put_size(handle, documents.length);
|
||||
for &each in documents {
|
||||
sent = sent && put_text(handle, each.uri.view()) && put_text(handle, each.path.view())
|
||||
&& put_text(handle, each.text.view());
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
#! Reads the documents open in the editor from a handle, as `put_documents`
|
||||
#! writes them, after those already held.
|
||||
#! @param handle The handle.
|
||||
#! @param documents Where the documents are appended.
|
||||
#! @returns Whether they were read.
|
||||
def get_documents(handle: system::fd, documents: &mut vector<document>): bool {
|
||||
var count: u64 = 0;
|
||||
return false if !get_size(handle, &mut count, 0);
|
||||
for _ in 0..count {
|
||||
var each := document { uri = string::new(nothing {}), path = string::new(nothing {}), text = string::new(nothing {}) };
|
||||
return false if !get_text(handle, &mut each.uri, 0) || !get_text(handle, &mut each.path, 0)
|
||||
|| !get_text(handle, &mut each.text, 0);
|
||||
documents.append(move(&mut each));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Writes a job to a handle, with the documents open in the editor.
|
||||
#! @param handle The handle.
|
||||
#! @param asked The job.
|
||||
#! @param documents The documents.
|
||||
#! @returns Whether it was written.
|
||||
def put_job(handle: system::fd, asked: &job, documents: &vector<document>): bool {
|
||||
return put_size(handle, asked.task as u64) && put_size(handle, asked.strict ? 1 : 0)
|
||||
&& put_size(handle, asked.line as u64) && put_size(handle, asked.character as u64)
|
||||
&& put_text(handle, asked.path.view()) && put_text(handle, asked.touched.view())
|
||||
&& put_text(handle, asked.entry.view()) && put_documents(handle, documents);
|
||||
}
|
||||
|
||||
#! Reads a job from a handle, as `put_job` writes it, with the documents open in
|
||||
#! the editor, which replace those known before.
|
||||
#! @param handle The handle.
|
||||
#! @param asked Where the job is written.
|
||||
#! @param documents Where the documents are written.
|
||||
#! @returns Whether it was read.
|
||||
def get_job(handle: system::fd, asked: &mut job, documents: &mut vector<document>): bool {
|
||||
var kind: u64 = 0;
|
||||
var strict: u64 = 0;
|
||||
var line: u64 = 0;
|
||||
var character: u64 = 0;
|
||||
val read := get_size(handle, &mut kind, 0) && get_size(handle, &mut strict, 0)
|
||||
&& get_size(handle, &mut line, 0) && get_size(handle, &mut character, 0)
|
||||
&& get_text(handle, &mut asked.path, 0) && get_text(handle, &mut asked.touched, 0)
|
||||
&& get_text(handle, &mut asked.entry, 0);
|
||||
return false if !read || kind > task::refer as u64;
|
||||
asked.task = kind as u8 as task;
|
||||
asked.strict = strict != 0;
|
||||
asked.line = line as u32;
|
||||
asked.character = character as u32;
|
||||
documents.clear();
|
||||
return get_documents(handle, documents);
|
||||
}
|
||||
|
||||
#! A job with nothing in it, to be read into.
|
||||
#! @returns The job.
|
||||
def empty_job(): job {
|
||||
return job {
|
||||
task = task::check,
|
||||
path = string::new(nothing {}),
|
||||
touched = string::new(nothing {}),
|
||||
entry = string::new(nothing {})
|
||||
};
|
||||
}
|
||||
|
||||
#! Does a job, asking the base and working it out from nothing where the base
|
||||
#! cannot, and gives whether it was done. A job about a file touches it, so one
|
||||
#! about a file declaring `main` is answered from that file's program.
|
||||
#! @param this The server.
|
||||
#! @param asked The job.
|
||||
#! @param output Where the answer is written.
|
||||
#! @returns Whether it was done.
|
||||
def server::run_job(&mut this, asked: &mut job, output: &mut string): bool {
|
||||
this.touch(asked.path.view());
|
||||
asked.touched = string::copy_of(&this.touched);
|
||||
asked.entry = string::copy_of(&this.entry);
|
||||
if !this.ask(asked, output) {
|
||||
output.clear();
|
||||
return false if !this.fork_job(asked, output);
|
||||
}
|
||||
this.answered(asked, output);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Takes the file declaring `main` that a job's program began with off the
|
||||
#! front of its answer, and keeps it, so the next job is answered from the same
|
||||
#! program unless another such file is touched. A job answered from another
|
||||
#! program than the last says so, and has the workspace checked again where it
|
||||
#! was not a check, so the diagnostics shown follow it.
|
||||
#! @param this The server.
|
||||
#! @param asked The job.
|
||||
#! @param output The answer, which the file is taken from.
|
||||
def server::answered(&mut this, asked: &job, output: &mut string): void {
|
||||
val zero: u8 = 0;
|
||||
val end := output.view().index_of(&zero) else { return; };
|
||||
val chosen := output.substr(0, end);
|
||||
if chosen != this.entry.view() {
|
||||
val moved := this.entry.length != 0 && chosen.length != 0;
|
||||
this.entry = string::from(chosen, nothing {});
|
||||
this.dirty = this.dirty || asked.task != task::check;
|
||||
if moved {
|
||||
val shown := this.shown_path(this.entry.view());
|
||||
var text := string::from("Checking the program begun by ", nothing {});
|
||||
text.append(shown);
|
||||
text.append(". The other files declaring `main` are left out until one of them is opened, edited, or asked about.");
|
||||
this.log(log_info, text.view());
|
||||
}
|
||||
}
|
||||
var rest := string::from(output.substr(end + 1, output.length - end - 1), nothing {});
|
||||
output = move(&mut rest);
|
||||
}
|
||||
|
||||
#! Asks the base for a job, giving false where it could not answer, which leaves
|
||||
#! the job to be worked out from nothing. A base whose files moved on is made
|
||||
#! again, and the job asked of the new one. A strict check leaves out files the
|
||||
#! base checked, so it is always worked out from nothing, and
|
||||
#! `SAKURA_LSP_BASE=0` asks for every job to be, which is how the two are
|
||||
#! compared.
|
||||
#! @param this The server.
|
||||
#! @param asked The job.
|
||||
#! @param output Where the answer is written.
|
||||
#! @returns Whether the base answered.
|
||||
def server::ask(&mut this, asked: &job, output: &mut string): bool {
|
||||
return false if asked.strict;
|
||||
if variable("SAKURA_LSP_BASE") is some(wanted) {
|
||||
return false if wanted == "0";
|
||||
}
|
||||
var status := this.ask_once(asked, output);
|
||||
if status == answer_stale {
|
||||
this.stop_base();
|
||||
output.clear();
|
||||
status = this.ask_once(asked, output);
|
||||
}
|
||||
return status == answer_ok;
|
||||
}
|
||||
|
||||
#! Asks the base for a job once.
|
||||
#! @param this The server.
|
||||
#! @param asked The job.
|
||||
#! @param output Where the answer is written.
|
||||
#! @returns What the base said of it, or `answer_fail` where it did not answer.
|
||||
def server::ask_once(&mut this, asked: &job, output: &mut string): u64 {
|
||||
return answer_fail if !this.start_base();
|
||||
val sent := put_job(this.base.jobs, asked, &this.documents);
|
||||
var status: u64 = answer_fail;
|
||||
var answer := string::new(nothing {});
|
||||
val deadline := now_ms() + timeout;
|
||||
val heard := sent && get_size(this.base.answers, &mut status, deadline)
|
||||
&& get_text(this.base.answers, &mut answer, deadline);
|
||||
if !heard {
|
||||
this.stop_base();
|
||||
if sent {
|
||||
this.log_failure(asked.task, " took longer than 30 seconds, or stopped unexpectedly, and was stopped.");
|
||||
}
|
||||
return answer_fail;
|
||||
}
|
||||
if status == answer_ok {
|
||||
output.append(answer.view());
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
#! Logs that a job was not done: what it was doing, then what went wrong.
|
||||
#! @param this The server.
|
||||
#! @param kind The kind of job.
|
||||
#! @param what What went wrong, which follows what it was doing, as
|
||||
#! ` stopped unexpectedly.`.
|
||||
def server::log_failure(&mut this, kind: task, what: strview): void {
|
||||
var text := string::from(doing(kind), nothing {});
|
||||
text.append(what);
|
||||
this.log(log_error, text.view());
|
||||
}
|
||||
|
||||
#! The base itself: it reads and checks the files of the workspace once, then
|
||||
#! answers each job, as `answer_jobs` does on the system it runs on.
|
||||
#! @param this The server, as the base copied or read it.
|
||||
#! @param jobs Where jobs are read.
|
||||
#! @param answers Where answers are written.
|
||||
def server::base_main(&mut this, jobs: system::fd, answers: system::fd): void {
|
||||
# On Linux, the base copies the server, so it leads a group of its own, which
|
||||
# the copies it starts belong to, and writes nothing where the server answers.
|
||||
when os == linux {
|
||||
lead_group();
|
||||
quiet_output();
|
||||
}
|
||||
var files := gather_all(&this.gathering());
|
||||
var held := program::parse(&files, false, this.options.native, this.options.system, &this.gathering());
|
||||
var checker := checker::new(&mut held.names, &mut held.check_notes);
|
||||
held.begin(&mut checker, held.choose(this.touched.view(), this.entry.view()));
|
||||
checker.check();
|
||||
this.answer_jobs(&mut held, &mut checker, jobs, answers);
|
||||
}
|
||||
|
||||
#! Gathers the files a job is worked out from as they are now, with the
|
||||
#! placeholder written where it completes a name after `.`.
|
||||
#! @param this The server.
|
||||
#! @param asked The job.
|
||||
#! @returns The files.
|
||||
def server::gather_for(&this, asked: &job): vector<gathered> {
|
||||
var files := gather_all(&this.gathering());
|
||||
write_placeholder(&mut files, asked);
|
||||
return files;
|
||||
}
|
||||
|
||||
#! Works out one job from nothing, in a process of its own: reads the files,
|
||||
#! parses and checks them, answers, and writes the answer.
|
||||
#! @param this The server, as the process copied or read it.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
def server::work_alone(&mut this, asked: &job, out: system::fd): void {
|
||||
var files := this.gather_for(asked);
|
||||
var held := program::parse(&files, asked.strict, this.options.native, this.options.system, &this.gathering());
|
||||
var checker := checker::new(&mut held.names, &mut held.check_notes);
|
||||
held.begin(&mut checker, held.choose(asked.touched.view(), asked.entry.view()));
|
||||
if asked.task != task::check {
|
||||
checker.check();
|
||||
}
|
||||
var place := workplace { held = &mut held, checker = &mut checker, documents = &this.documents };
|
||||
var made := string::new(nothing {});
|
||||
answer(&mut place, asked, &mut made);
|
||||
put_all(out, made.view());
|
||||
}
|
||||
|
||||
#! The two pipes between the server and a process of its own: one jobs are sent
|
||||
#! to it by, and one its answers come back by.
|
||||
struct pipes {
|
||||
var jobs_read: system::fd; #! Where the process reads jobs.
|
||||
var jobs_write: system::fd; #! Where the server writes them.
|
||||
var answers_read: system::fd; #! Where the server reads answers.
|
||||
var answers_write: system::fd; #! Where the process writes them.
|
||||
}
|
||||
|
||||
#! Makes the two pipes between the server and a process of its own, or neither.
|
||||
#! @returns The pipes, or nothing where they could not be made.
|
||||
def make_pipes(): maybe<pipes> {
|
||||
var made := pipes { jobs_read = -1, jobs_write = -1, answers_read = -1, answers_write = -1 };
|
||||
return nothing {} if system::pipe(&mut made.jobs_read, &mut made.jobs_write) != 0;
|
||||
if system::pipe(&mut made.answers_read, &mut made.answers_write) != 0 {
|
||||
made.jobs_read.close();
|
||||
made.jobs_write.close();
|
||||
return nothing {};
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Closes the ends of the pipes the process keeps, which the server lets go of
|
||||
#! once the process has them.
|
||||
#! @param this The pipes.
|
||||
def pipes::close_process_ends(&this): void {
|
||||
this.jobs_read.close();
|
||||
this.answers_write.close();
|
||||
}
|
||||
|
||||
#! Closes the ends of the pipes the server keeps.
|
||||
#! @param this The pipes.
|
||||
def pipes::close_server_ends(&this): void {
|
||||
this.jobs_write.close();
|
||||
this.answers_read.close();
|
||||
}
|
||||
|
||||
when os == linux {
|
||||
#! Starts the base, giving false where it could not be, so the job is worked out
|
||||
#! from nothing instead.
|
||||
#! @param this The server.
|
||||
#! @returns Whether the base is running.
|
||||
def server::start_base(&mut this): bool {
|
||||
return true if this.base.process > 0;
|
||||
val ends := make_pipes() else { return false; };
|
||||
|
||||
val child := system::fork();
|
||||
if child == 0 {
|
||||
ends.close_server_ends();
|
||||
this.base_main(ends.jobs_read, ends.answers_write);
|
||||
system::exit(0);
|
||||
}
|
||||
ends.close_process_ends();
|
||||
if child < 0 {
|
||||
ends.close_server_ends();
|
||||
return false;
|
||||
}
|
||||
this.base = base_link { process = child, jobs = ends.jobs_write, answers = ends.answers_read };
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Stops the base, and every copy of it working out a job, which is made again
|
||||
#! once it is next needed.
|
||||
#! @param this The server.
|
||||
def server::stop_base(&mut this): void {
|
||||
return if this.base.process <= 0;
|
||||
this.base.jobs.close();
|
||||
this.base.answers.close();
|
||||
system::kill(0 - this.base.process, system::signal::kill);
|
||||
var status := system::process_status {};
|
||||
reap(this.base.process, &mut status);
|
||||
this.base = base_link {};
|
||||
}
|
||||
|
||||
#! Answers each job the base is sent by a copy of itself, which brings the
|
||||
#! program up to date with the files as they are and answers, and once a check
|
||||
#! is answered, brings the base up to date the same way.
|
||||
#! @param this The server, as the base copied it.
|
||||
#! @param held The program, as the base read and checked it.
|
||||
#! @param checker Its checker.
|
||||
#! @param jobs Where jobs are read.
|
||||
#! @param answers Where answers are written.
|
||||
def server::answer_jobs(&mut this, held: &mut program, checker: &mut checker, jobs: system::fd, answers: system::fd): void {
|
||||
var asked := empty_job();
|
||||
while true {
|
||||
if !get_job(jobs, &mut asked, &mut this.documents) {
|
||||
system::exit(0);
|
||||
}
|
||||
|
||||
var reading: system::fd = -1;
|
||||
var writing: system::fd = -1;
|
||||
if system::pipe(&mut reading, &mut writing) != 0 {
|
||||
put_size(answers, answer_fail);
|
||||
put_text(answers, "");
|
||||
continue;
|
||||
}
|
||||
val worker := system::fork();
|
||||
if worker == 0 {
|
||||
reading.close();
|
||||
jobs.close();
|
||||
answers.close();
|
||||
val code := this.base_work(held, checker, &asked, writing);
|
||||
system::exit(code);
|
||||
}
|
||||
writing.close();
|
||||
var output := string::new(nothing {});
|
||||
if worker > 0 {
|
||||
get_rest(reading, &mut output);
|
||||
}
|
||||
reading.close();
|
||||
|
||||
var status := system::process_status {};
|
||||
val waited := worker > 0 && reap(worker, &mut status);
|
||||
val done := waited && status.exited && status.code == 0;
|
||||
val stale := waited && status.exited && status.code == worker_stale;
|
||||
if !put_size(answers, done ? answer_ok : stale ? answer_stale : answer_fail)
|
||||
|| !put_text(answers, done ? output.view() : "") {
|
||||
system::exit(0);
|
||||
}
|
||||
|
||||
# The copy showed the files can be brought up to date without a fault, so
|
||||
# once a check is answered, the base brings itself up to date too.
|
||||
if done && asked.task == task::check {
|
||||
var now := gather_all(&this.gathering());
|
||||
if held.sync(checker, &now, asked.touched.view(), asked.entry.view(), true, &this.gathering()) == synced::stale {
|
||||
system::exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Works out one job, in a copy of the base, which brings the program up to date
|
||||
#! with the files as they are, answers, and writes the answer.
|
||||
#! @param this The server, as the base copied it.
|
||||
#! @param held The program, as the base has it.
|
||||
#! @param checker Its checker.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
#! @returns The status to exit with: 0 where the job is answered, and
|
||||
#! `worker_stale` where the files moved on in a way the program cannot
|
||||
#! follow.
|
||||
def server::base_work(&mut this, held: &mut program, checker: &mut checker, asked: &job, out: system::fd): i32 {
|
||||
var files := this.gather_for(asked);
|
||||
# A check checks the program whole, so what changed is only parsed again.
|
||||
val checks := asked.task != task::check;
|
||||
return worker_stale if held.sync(checker, &files, asked.touched.view(), asked.entry.view(), checks, &this.gathering()) == synced::stale;
|
||||
|
||||
var place := workplace { held = held, checker = checker, documents = &this.documents };
|
||||
var output := string::new(nothing {});
|
||||
answer(&mut place, asked, &mut output);
|
||||
put_all(out, output.view());
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Works out a job from nothing, in a process of its own, and collects what it
|
||||
#! writes. Gives false where it did not finish.
|
||||
#! @param this The server.
|
||||
#! @param asked The job.
|
||||
#! @param output Where the answer is written.
|
||||
#! @returns Whether it finished.
|
||||
def server::fork_job(&mut this, asked: &job, output: &mut string): bool {
|
||||
var reading: system::fd = -1;
|
||||
var writing: system::fd = -1;
|
||||
if system::pipe(&mut reading, &mut writing) != 0 {
|
||||
this.log_failure(asked.task, " failed: no pipe could be made.");
|
||||
return false;
|
||||
}
|
||||
|
||||
val child := system::fork();
|
||||
if child == 0 {
|
||||
reading.close();
|
||||
quiet_output();
|
||||
this.work_alone(asked, writing);
|
||||
system::exit(0);
|
||||
}
|
||||
writing.close();
|
||||
if child < 0 {
|
||||
reading.close();
|
||||
this.log_failure(asked.task, " failed: no process could be started.");
|
||||
return false;
|
||||
}
|
||||
|
||||
val finished := get_until(reading, output, now_ms() + timeout);
|
||||
reading.close();
|
||||
if !finished {
|
||||
system::kill(child, system::signal::kill);
|
||||
}
|
||||
var status := system::process_status {};
|
||||
reap(child, &mut status);
|
||||
|
||||
if !finished {
|
||||
this.log_failure(asked.task, " took longer than 30 seconds and was stopped.");
|
||||
} else if !status.exited {
|
||||
val said := $" stopped unexpectedly with signal {status.signal}.";
|
||||
this.log_failure(asked.task, said.view());
|
||||
} else if status.code != 0 {
|
||||
this.log_failure(asked.task, " stopped unexpectedly.");
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
when os == windows {
|
||||
# The word a process the server starts is sent first, which changes whenever
|
||||
# what it is sent does, so a process of another build, as one put in place of
|
||||
# the executable while the server runs, says it cannot read what it is sent
|
||||
# rather than misreading it.
|
||||
val process_version: u64 = 3;
|
||||
|
||||
# The digits of hexadecimal, as the code of an exception is written with them,
|
||||
# lowest first.
|
||||
val hex_upper: strview = "0123456789ABCDEF";
|
||||
|
||||
#! Writes what a process the server starts works with, which on Windows it
|
||||
#! cannot copy from the server: the libraries, the standard library's among
|
||||
#! them, what the workspace is checked for, the file last touched and the
|
||||
#! program last answered from, the workspace folders, and the documents open
|
||||
#! in the editor.
|
||||
#! @param this The server.
|
||||
#! @param handle The handle.
|
||||
#! @param documents The documents, which are none for a worker, since the
|
||||
#! job it is sent brings them.
|
||||
#! @returns Whether it was written.
|
||||
def server::put_state(&this, handle: system::fd, documents: &vector<document>): bool {
|
||||
var sent := put_size(handle, process_version)
|
||||
&& put_size(handle, this.options.native ? 1 : 0) && put_text(handle, this.options.system)
|
||||
&& put_text(handle, this.touched.view()) && put_text(handle, this.entry.view())
|
||||
&& put_size(handle, this.libraries.length);
|
||||
for &each in this.libraries {
|
||||
sent = sent && put_text(handle, each.view());
|
||||
}
|
||||
sent = sent && put_size(handle, this.roots.length);
|
||||
for &each in this.roots {
|
||||
sent = sent && put_text(handle, each.view());
|
||||
}
|
||||
return sent && put_documents(handle, documents);
|
||||
}
|
||||
|
||||
#! Reads what a process the server starts works with, as `put_state` writes
|
||||
#! it, after the word of its version, which `serve_process` reads.
|
||||
#! @param this The process's server, which it is read into.
|
||||
#! @param handle The handle.
|
||||
#! @param system Where the name of the system the workspace is checked for
|
||||
#! is kept, which the server's options view.
|
||||
#! @returns Whether it was read.
|
||||
def server::get_state(&mut this, handle: system::fd, system: &mut string): bool {
|
||||
var native: u64 = 0;
|
||||
var libraries: u64 = 0;
|
||||
var roots: u64 = 0;
|
||||
val read := get_size(handle, &mut native, 0)
|
||||
&& get_text(handle, system, 0) && get_text(handle, &mut this.touched, 0)
|
||||
&& get_text(handle, &mut this.entry, 0) && get_size(handle, &mut libraries, 0);
|
||||
return false if !read;
|
||||
this.options.native = native != 0;
|
||||
this.options.system = system.view();
|
||||
for _ in 0..libraries {
|
||||
var library := string::new(nothing {});
|
||||
return false if !get_text(handle, &mut library, 0);
|
||||
this.libraries.append(move(&mut library));
|
||||
}
|
||||
return false if !get_size(handle, &mut roots, 0);
|
||||
for _ in 0..roots {
|
||||
var root := string::new(nothing {});
|
||||
return false if !get_text(handle, &mut root, 0);
|
||||
this.roots.append(move(&mut root));
|
||||
}
|
||||
return get_documents(handle, &mut this.documents);
|
||||
}
|
||||
|
||||
#! Works out what the server asks of a process it started, which on Windows is
|
||||
#! this program started again, as `lsp --base` or `lsp --worker`: it reads what
|
||||
#! the server works with from its standard input, then answers, as the base,
|
||||
#! each job it is sent there, or as a worker, the one job, writing the answers
|
||||
#! to its standard output.
|
||||
#! @param base Whether it is the base, rather than a worker.
|
||||
#! @returns The status to exit with: 0 where it read what it was sent, 2 where
|
||||
#! another build of the server sent it, and 1 otherwise.
|
||||
pub def serve_process(base: bool): i32 {
|
||||
val answers := take_output();
|
||||
var version: u64 = 0;
|
||||
return 1 if !get_size(system::fd::stdin, &mut version, 0);
|
||||
if version != process_version {
|
||||
system::fd::stderr.put("sakura: lsp: a process of the language server is only started by the server of the same build\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
var system := string::new(nothing {});
|
||||
var running := make_server(&server_options {});
|
||||
return 1 if !running.get_state(system::fd::stdin, &mut system);
|
||||
if base {
|
||||
running.base_main(system::fd::stdin, answers);
|
||||
return 0;
|
||||
}
|
||||
var asked := empty_job();
|
||||
return 1 if !get_job(system::fd::stdin, &mut asked, &mut running.documents);
|
||||
running.work_alone(&asked, answers);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#! Starts the base, giving false where it could not be, so the job is worked out
|
||||
#! from nothing instead.
|
||||
#! @param this The server.
|
||||
#! @returns Whether the base is running.
|
||||
def server::start_base(&mut this): bool {
|
||||
return true if this.base.process > 0;
|
||||
val ends := make_pipes() else { return false; };
|
||||
|
||||
val child := start_process("--base", ends.jobs_read, ends.answers_write);
|
||||
ends.close_process_ends();
|
||||
if child == 0 {
|
||||
ends.close_server_ends();
|
||||
return false;
|
||||
}
|
||||
this.base = base_link { process = child, jobs = ends.jobs_write, answers = ends.answers_read };
|
||||
|
||||
# The base reads what it works with before anything else, so it is sent now.
|
||||
if !this.put_state(ends.jobs_write, &this.documents) {
|
||||
this.stop_base();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Stops the base, which answers each job itself, so nothing else is working
|
||||
#! one out, and which is made again once it is next needed.
|
||||
#! @param this The server.
|
||||
def server::stop_base(&mut this): void {
|
||||
return if this.base.process <= 0;
|
||||
this.base.jobs.close();
|
||||
this.base.answers.close();
|
||||
var code: u32 = 0;
|
||||
end_process(this.base.process, 0, &mut code);
|
||||
this.base = base_link {};
|
||||
}
|
||||
|
||||
#! Answers each job the base is sent itself, bringing the program up to date
|
||||
#! with the files as they are, since it cannot answer by a copy of itself.
|
||||
#! Files that moved on in a way the program cannot follow end it once it has
|
||||
#! said so, and the server makes another.
|
||||
#! @param this The server, as the base read it.
|
||||
#! @param held The program, as the base read and checked it.
|
||||
#! @param checker Its checker.
|
||||
#! @param jobs Where jobs are read.
|
||||
#! @param answers Where answers are written.
|
||||
def server::answer_jobs(&mut this, held: &mut program, checker: &mut checker, jobs: system::fd, answers: system::fd): void {
|
||||
var asked := empty_job();
|
||||
while true {
|
||||
return if !get_job(jobs, &mut asked, &mut this.documents);
|
||||
var output := string::new(nothing {});
|
||||
val status := this.base_answer(held, checker, &asked, &mut output);
|
||||
return if !put_size(answers, status) || !put_text(answers, output.view()) || status != answer_ok;
|
||||
}
|
||||
}
|
||||
|
||||
#! Works out one job in the base itself, which brings the program up to date
|
||||
#! with the files as they are, checking again what that affects, as the base
|
||||
#! on Linux brings itself up to date once a check is answered, and answers.
|
||||
#! @param this The server, as the base read it.
|
||||
#! @param held The program.
|
||||
#! @param checker Its checker.
|
||||
#! @param asked The job.
|
||||
#! @param out Where the answer is written.
|
||||
#! @returns `answer_ok` where the job is answered, and `answer_stale` where the
|
||||
#! files moved on in a way the program cannot follow, which leaves it
|
||||
#! brought up to date only in part.
|
||||
def server::base_answer(&mut this, held: &mut program, checker: &mut checker, asked: &job, out: &mut string): u64 {
|
||||
var files := this.gather_for(asked);
|
||||
return answer_stale if held.sync(checker, &files, asked.touched.view(), asked.entry.view(), true, &this.gathering()) == synced::stale;
|
||||
|
||||
var place := workplace { held = held, checker = checker, documents = &this.documents };
|
||||
answer(&mut place, asked, out);
|
||||
return answer_ok;
|
||||
}
|
||||
|
||||
#! Works out a job from nothing, in a worker, a process of its own, and
|
||||
#! collects what it writes. Gives false where it did not finish.
|
||||
#! @param this The server.
|
||||
#! @param asked The job.
|
||||
#! @param output Where the answer is written.
|
||||
#! @returns Whether it finished.
|
||||
def server::fork_job(&mut this, asked: &job, output: &mut string): bool {
|
||||
val ends := make_pipes() else {
|
||||
this.log_failure(asked.task, " failed: no pipe could be made.");
|
||||
return false;
|
||||
};
|
||||
|
||||
val child := start_process("--worker", ends.jobs_read, ends.answers_write);
|
||||
ends.close_process_ends();
|
||||
if child == 0 {
|
||||
ends.close_server_ends();
|
||||
this.log_failure(asked.task, " failed: no process could be started.");
|
||||
return false;
|
||||
}
|
||||
|
||||
# The worker reads all it is sent before it writes anything, so it is sent
|
||||
# all at once, and its answer read until it ends. The answer ends as the
|
||||
# worker does, unless it closed its output and went on.
|
||||
val none := vector::<document>::new();
|
||||
if this.put_state(ends.jobs_write, &none) {
|
||||
put_job(ends.jobs_write, asked, &this.documents);
|
||||
}
|
||||
ends.jobs_write.close();
|
||||
val deadline := now_ms() + timeout;
|
||||
val finished := get_until(ends.answers_read, output, deadline);
|
||||
ends.answers_read.close();
|
||||
var code: u32 = 1;
|
||||
val ended := end_process(child, finished ? deadline : 0, &mut code);
|
||||
|
||||
if !ended {
|
||||
this.log_failure(asked.task, " took longer than 30 seconds and was stopped.");
|
||||
} else if code >= 0xC0000000 {
|
||||
var said := string::from(" stopped unexpectedly with exception 0x", nothing {});
|
||||
for i in 0u..8u {
|
||||
said.push(hex_upper[((code >> (28 - 4 * i as u32)) & 0xF) as u64]);
|
||||
}
|
||||
said.append(".");
|
||||
this.log_failure(asked.task, said.view());
|
||||
} else if code != 0 {
|
||||
val said := $" stopped unexpectedly with exit code {code}.";
|
||||
this.log_failure(asked.task, said.view());
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,532 @@
|
||||
#! @file program.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the files a check reads, and the program they make: `core`, every
|
||||
#! `.sakura` file of the workspace folders, and the modules of `system` and the
|
||||
#! standard library those refer to, with the runtime where an executable is
|
||||
#! checked for, read as a check reads them, each with the
|
||||
#! text open in the editor where it is open, and otherwise its text on disk,
|
||||
#! lexed and parsed, and those of one program checked.
|
||||
#!
|
||||
#! Where several files declare `main`, each begins a program holding every
|
||||
#! file that declares none, and one is checked at a time, so their `main`s do
|
||||
#! not clash: that of the file last opened, edited, or asked about where it
|
||||
#! declares `main`, else the one checked last, else the one whose file comes
|
||||
#! first by path. The rest are parsed, so their mistakes of syntax are shown,
|
||||
#! but nothing in them is declared or checked.
|
||||
#!
|
||||
#! Once made, a program is brought up to date with the files as they are by
|
||||
#! parsing again only what changed of each file that changed, and checking
|
||||
#! again only what that affects, as `checker::update` does.
|
||||
|
||||
import std::io;
|
||||
import system;
|
||||
import sakura::analyzer;
|
||||
module sakura::server;
|
||||
|
||||
|
||||
#! A document open in the editor.
|
||||
pub struct document {
|
||||
pub var uri: string; #! The URI the editor names it by.
|
||||
pub var path: string; #! Its path, or empty where it is no file, as an unsaved one.
|
||||
pub var text: string; #! Its text, as the editor has it.
|
||||
}
|
||||
|
||||
#! A file a check reads: its path, and the text it is read with.
|
||||
pub struct gathered {
|
||||
pub var path: string; #! The file's path.
|
||||
pub var text: string; #! Its text.
|
||||
}
|
||||
|
||||
#! What the files of a check are gathered from.
|
||||
pub struct gathering {
|
||||
pub var core: strview; #! `core`'s directory, or empty.
|
||||
pub var system: strview; #! `system`'s directory, or empty.
|
||||
pub var runtime: strview; #! The runtime's directory, or empty.
|
||||
pub var libraries: span<string>; #! The libraries, each as `NAME=DIR`, the standard library's among them.
|
||||
pub var roots: &vector<string>; #! The workspace folders.
|
||||
pub var documents: &vector<document>; #! The documents open in the editor.
|
||||
}
|
||||
|
||||
#! The most directories deep the folders are read.
|
||||
const deepest: u32 = 64;
|
||||
|
||||
#! A name a directory holds, and what it names.
|
||||
struct listed {
|
||||
var name: string;
|
||||
var kind: file_kind;
|
||||
}
|
||||
|
||||
#! Gathers the files a check reads before what they refer to: the types the
|
||||
#! syntax makes and `core`'s, then each of the workspace folders', or the
|
||||
#! documents open in the editor where there is no folder, and any of `system`,
|
||||
#! the runtime, and the libraries, the standard library's among them, open in
|
||||
#! the editor, whose modules are read whole.
|
||||
#! Each is read with the text open in the editor where it is open.
|
||||
#! @param from What they are gathered from.
|
||||
#! @returns The files.
|
||||
pub def gather_all(from: &gathering): vector<gathered> {
|
||||
var made := vector::<gathered>::new();
|
||||
if from.core.length != 0 {
|
||||
made.append(gathered { path = string::from(builtin_path, nothing {}), text = string::from(builtin_text, nothing {}) });
|
||||
gather_directory(from, from.core, &mut made, 0, true);
|
||||
}
|
||||
for &each in from.documents {
|
||||
if is_sakura(each.path.view()) && (within_folder(each.path.view(), from.system) || within_folder(each.path.view(), from.runtime)
|
||||
|| within_libraries(from, each.path.view())) {
|
||||
gather_file(from, each.path.view(), &mut made);
|
||||
}
|
||||
}
|
||||
for &root in from.roots {
|
||||
gather_directory(from, root.view(), &mut made, 0, false);
|
||||
}
|
||||
# Without a workspace folder, the files open in the editor are the program.
|
||||
if from.roots.length == 0 {
|
||||
for &each in from.documents {
|
||||
if each.path.length != 0 && is_sakura(each.path.view()) {
|
||||
gather_file(from, each.path.view(), &mut made);
|
||||
}
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The directory of a library given as `NAME=DIR`.
|
||||
#! @param given The library.
|
||||
#! @returns The directory, or empty where none is given.
|
||||
def library_folder(given: strview): strview {
|
||||
val split := given.index_of(&'=') else { return strview::empty(); };
|
||||
return given.substr(split + 1, given.length - split - 1);
|
||||
}
|
||||
|
||||
#! Whether a path is one of the libraries' directories.
|
||||
#! @param from What the files are gathered from.
|
||||
#! @param at The path.
|
||||
#! @returns Whether it is.
|
||||
def is_library_folder(from: &gathering, at: strview): bool {
|
||||
for &each in from.libraries {
|
||||
return true if library_folder(each.view()) == at;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether a path is within one of the libraries' directories.
|
||||
#! @param from What the files are gathered from.
|
||||
#! @param at The path.
|
||||
#! @returns Whether it is.
|
||||
def within_libraries(from: &gathering, at: strview): bool {
|
||||
for &each in from.libraries {
|
||||
return true if within_folder(at, library_folder(each.view()));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether a path is within a folder.
|
||||
#! @param at The path.
|
||||
#! @param folder The folder, or empty, which holds nothing.
|
||||
#! @returns Whether it is.
|
||||
def within_folder(at: strview, folder: strview): bool {
|
||||
return folder.length != 0 && at.length > folder.length && at.starts_with(folder) && at[folder.length] == '/';
|
||||
}
|
||||
|
||||
#! The resolver of the libraries a check reads: `core`, read whole, and
|
||||
#! `system`, the runtime, and the libraries, the standard library's among them,
|
||||
#! read by what refers to them.
|
||||
#! @param from What the files are gathered from.
|
||||
#! @returns The resolver.
|
||||
pub def library_finder(from: &gathering): resolver {
|
||||
var made := resolver::new();
|
||||
if from.core.length != 0 {
|
||||
made.whole("core", from.core);
|
||||
}
|
||||
if from.system.length != 0 {
|
||||
made.add_library("system", from.system);
|
||||
}
|
||||
if from.runtime.length != 0 {
|
||||
made.add_library("runtime", from.runtime);
|
||||
}
|
||||
for &each in from.libraries {
|
||||
val split := each.view().index_of(&'=') else { continue; };
|
||||
made.add_library(each.view().substr(0, split), library_folder(each.view()));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The text of a library's file, as the editor has it where it is open, and
|
||||
#! otherwise as it is on disk, or empty where it cannot be read.
|
||||
#! @param from What the files are gathered from.
|
||||
#! @param at The file's path.
|
||||
#! @returns The text.
|
||||
def library_text(from: &gathering, at: strview): string {
|
||||
for &each in from.documents {
|
||||
return string::copy_of(&each.text) if each.path.view() == at;
|
||||
}
|
||||
var text := string::new(nothing {});
|
||||
_ = read_text(at, &mut text);
|
||||
return text;
|
||||
}
|
||||
|
||||
#! Adds a file to what is gathered, once, with the text open in the editor where
|
||||
#! it is open, and otherwise its text on disk.
|
||||
#! @param from What the files are gathered from.
|
||||
#! @param at The file's path.
|
||||
#! @param into What is gathered so far.
|
||||
def gather_file(from: &gathering, at: strview, into: &mut vector<gathered>): void {
|
||||
for &each in into {
|
||||
return if each.path.view() == at;
|
||||
}
|
||||
for &each in from.documents {
|
||||
if each.path.view() == at {
|
||||
into.append(gathered { path = string::from(at, nothing {}), text = string::copy_of(&each.text) });
|
||||
return;
|
||||
}
|
||||
}
|
||||
var text := string::new(nothing {});
|
||||
return if !read_text(at, &mut text);
|
||||
into.append(gathered { path = string::from(at, nothing {}), text = move(&mut text) });
|
||||
}
|
||||
|
||||
#! Gathers the `.sakura` files of a directory and those within it, by name,
|
||||
#! passing over hidden directories, `node_modules`, and, within a workspace
|
||||
#! folder, the libraries, which are gathered on their own. A link to a
|
||||
#! directory is not followed, so a link cannot lead the reading in a circle.
|
||||
#! @param from What the files are gathered from.
|
||||
#! @param at The directory's path.
|
||||
#! @param into What is gathered so far.
|
||||
#! @param depth How deep the directory is.
|
||||
#! @param library Whether it is a library's, as `core`'s.
|
||||
def gather_directory(from: &gathering, at: strview, into: &mut vector<gathered>, depth: u32, library: bool): void {
|
||||
return if depth > deepest;
|
||||
var found := path::parse(at) else (_) { return; };
|
||||
var listing := io::directory::open(&found) else (_) { return; };
|
||||
var entries := vector::<listed>::new();
|
||||
while listing.next() is some(each) {
|
||||
continue if each.name.length == 0 || each.name[0] == '.' || each.name == "node_modules";
|
||||
entries.append(listed { name = string::from(each.name, nothing {}), kind = each.kind });
|
||||
}
|
||||
|
||||
# Read in the order of their names, so a check reads the files alike however
|
||||
# the system lists them.
|
||||
entries.sort_by(def(a: &listed, b: &listed): bool { return a.name.compare(b.name.view()) < 0; });
|
||||
|
||||
var inner := vector::<string>::new();
|
||||
for &each in entries {
|
||||
var joined := $"{at}/{each.name}";
|
||||
var kind: file_kind = each.kind;
|
||||
if kind == file_kind::link {
|
||||
var linked := path::parse(joined.view()) else (_) { continue; };
|
||||
val info := io::info(&linked) else (_) { continue; };
|
||||
continue if info.kind == file_kind::directory;
|
||||
kind = info.kind;
|
||||
}
|
||||
if kind == file_kind::directory {
|
||||
# `core`, `system`, the runtime, and the libraries within a workspace
|
||||
# folder are gathered on their own.
|
||||
continue if !library && is_library_folder(from, joined.view());
|
||||
continue if !library && from.core.length != 0 && joined.view() == from.core;
|
||||
continue if !library && from.system.length != 0 && joined.view() == from.system;
|
||||
continue if !library && from.runtime.length != 0 && joined.view() == from.runtime;
|
||||
inner.append(move(&mut joined));
|
||||
} else if kind == file_kind::regular && is_sakura(each.name.view()) {
|
||||
gather_file(from, joined.view(), into);
|
||||
}
|
||||
}
|
||||
for &each in inner {
|
||||
gather_directory(from, each.view(), into, depth + 1, library);
|
||||
}
|
||||
}
|
||||
|
||||
#! What says none, of a file a program leaves out or a program begun by none.
|
||||
pub const none: u32 = 0xFFFFFFFF;
|
||||
|
||||
#! The files of a check, lexed and parsed, and which make the program checked.
|
||||
#! The checker refers to what this holds, so once it is made this stays where
|
||||
#! it is.
|
||||
pub struct program {
|
||||
pub var names: names; #! The names the files were lexed with.
|
||||
pub var parse_notes: vector<vector<note>>; #! What lexing and parsing noted of each file.
|
||||
pub var check_notes: vector<note>; #! What checking noted, each by the checker's index of its file.
|
||||
pub var paths: vector<string>; #! The path of each file.
|
||||
pub var texts: vector<string>; #! The text each was parsed with.
|
||||
pub var sources: vector<source>; #! Each file's source.
|
||||
pub var syntaxes: vector<syntax>; #! What each parsed into.
|
||||
pub var roots: vector<u32>; #! The `document` node of each.
|
||||
pub var mains: vector<bool>; #! Whether each declares `main`.
|
||||
pub var units: vector<u32>; #! The checker's index of each, or `none` where the program leaves it out.
|
||||
pub var files: vector<u32>; #! The file of each of the checker's indexes.
|
||||
pub var entry: u32; #! The file declaring `main` the program begins with, or `none`.
|
||||
pub var strict: bool; #! Whether files with mistakes of syntax are left out, as a check without drafts does.
|
||||
pub var native: bool; #! Whether it is checked for an executable.
|
||||
pub var system: strview; #! The operating system it is checked for, as `linux`.
|
||||
pub var given: u64; #! How many of the files were gathered; the rest are the libraries' files they refer to.
|
||||
var finder: resolver; #! What found the libraries' files, which finds what an edit refers to anew.
|
||||
}
|
||||
|
||||
#! Lexes and parses the files gathered, then the files of the libraries' modules
|
||||
#! they refer to, a round at a time, as a check reads them, ready to be checked
|
||||
#! as a program.
|
||||
#! @param files The files gathered.
|
||||
#! @param strict Whether files with mistakes of syntax are left out of the
|
||||
#! program.
|
||||
#! @param native Whether it is checked for an executable, which reads the
|
||||
#! runtime too.
|
||||
#! @param system The operating system it is checked for, as `linux`.
|
||||
#! @param from What the files were gathered from, which says where the
|
||||
#! libraries are.
|
||||
#! @returns The files, parsed.
|
||||
pub def program::parse(files: &vector<gathered>, strict: bool, native: bool, system: strview, from: &gathering): program {
|
||||
var made := program {
|
||||
names = names::new(),
|
||||
parse_notes = vector::<vector<note>>::new(),
|
||||
check_notes = vector::<note>::new(),
|
||||
paths = vector::<string>::new(),
|
||||
texts = vector::<string>::new(),
|
||||
sources = vector::<source>::new(),
|
||||
syntaxes = vector::<syntax>::new(),
|
||||
roots = vector::<u32>::new(),
|
||||
mains = vector::<bool>::new(),
|
||||
units = vector::<u32>::new(),
|
||||
files = vector::<u32>::new(),
|
||||
entry = none,
|
||||
strict = strict,
|
||||
native = native,
|
||||
system = system,
|
||||
given = files.length,
|
||||
finder = library_finder(from)
|
||||
};
|
||||
|
||||
# Each file's path, source, and syntax stay where they are once the checker
|
||||
# refers to them, so there is room for every file before any is made.
|
||||
val room := files.length + made.finder.files_held();
|
||||
made.paths.reserve(room);
|
||||
made.texts.reserve(room);
|
||||
made.sources.reserve(room);
|
||||
made.syntaxes.reserve(room);
|
||||
made.parse_notes.reserve(room);
|
||||
for &each in files {
|
||||
made.finder.mark(each.path.view());
|
||||
made.add(each.path.view(), &each.text);
|
||||
}
|
||||
|
||||
# What the files refer to is read after them, until a round refers to
|
||||
# nothing new.
|
||||
var done: u64 = 0;
|
||||
while done < made.paths.length {
|
||||
var found := vector::<string>::new();
|
||||
if done == 0 && native {
|
||||
made.finder.want("runtime", &mut found);
|
||||
}
|
||||
val to := made.paths.length;
|
||||
for i in done..to {
|
||||
made.finder.refer(&made.syntaxes[i], &made.names, &mut found);
|
||||
}
|
||||
done = to;
|
||||
for &each in found {
|
||||
val text := library_text(from, each.view());
|
||||
made.add(each.view(), &text);
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Adds a file to the program, lexed and parsed.
|
||||
#! @param this The program.
|
||||
#! @param path The file's path.
|
||||
#! @param text Its text.
|
||||
def program::add(&mut this, path: strview, text: &string): void {
|
||||
val i := this.paths.length;
|
||||
this.paths.append(string::from(path, nothing {}));
|
||||
this.texts.append(string::copy_of(text));
|
||||
this.parse_notes.append(vector::<note>::new());
|
||||
this.sources.append(source::from(this.paths[i].view(), text.view()));
|
||||
this.syntaxes.append(syntax::new());
|
||||
val main := this.names.intern(&"main");
|
||||
var lexing := lexer::new(&mut this.sources[i], &mut this.names, &mut this.parse_notes[i], i as u32);
|
||||
lexing.tokenize_all(&mut this.syntaxes[i]);
|
||||
var parsing := parser::new(&mut this.syntaxes[i], &this.names, &mut this.parse_notes[i], i as u32);
|
||||
this.roots.append(parsing.parse());
|
||||
this.mains.append(this.syntaxes[i].declares_main(this.roots[i], main));
|
||||
}
|
||||
|
||||
#! Whether a file has mistakes of syntax.
|
||||
#! @param this The program.
|
||||
#! @param file The file.
|
||||
#! @returns Whether it has.
|
||||
pub def program::broken(&this, file: u32): bool {
|
||||
return this.parse_notes[file].length != 0;
|
||||
}
|
||||
|
||||
#! The text of a file as its source holds it, which the places the checker
|
||||
#! gives are within: the text it was parsed with, but with each CR LF line
|
||||
#! break the LF alone. A position of the protocol is the same in either, since
|
||||
#! it counts lines, and characters within a line, which the CR ends.
|
||||
#! @param this The program.
|
||||
#! @param file The file.
|
||||
#! @returns The text.
|
||||
pub def program::source_text(&this, file: u32): strview {
|
||||
val held := &this.sources[file];
|
||||
return held.make_lexeme(0, held.length);
|
||||
}
|
||||
|
||||
#! The file declaring `main` a program begins with: the one last opened, edited,
|
||||
#! or asked about where it declares `main`, else the one checked last, else the
|
||||
#! one whose path comes first.
|
||||
#! @param this The program.
|
||||
#! @param touched The path of the file last opened, edited, or asked about, or
|
||||
#! empty.
|
||||
#! @param last The path of the file the last program began with, or empty.
|
||||
#! @returns The file, or `none` where none declares `main`.
|
||||
pub def program::choose(&this, touched: strview, last: strview): u32 {
|
||||
var chosen := none;
|
||||
for i in 0..this.paths.length {
|
||||
if this.mains[i] && this.paths[i].view() == touched && this.usable(i as u32) {
|
||||
chosen = i as u32;
|
||||
}
|
||||
}
|
||||
return chosen if chosen != none;
|
||||
for i in 0..this.paths.length {
|
||||
if this.mains[i] && this.paths[i].view() == last && this.usable(i as u32) {
|
||||
chosen = i as u32;
|
||||
}
|
||||
}
|
||||
return chosen if chosen != none;
|
||||
for i in 0..this.paths.length {
|
||||
continue if !this.mains[i] || !this.usable(i as u32);
|
||||
if chosen == none || this.paths[i].compare(this.paths[chosen].view()) < 0 {
|
||||
chosen = i as u32;
|
||||
}
|
||||
}
|
||||
return chosen;
|
||||
}
|
||||
|
||||
#! Whether a file may be checked: any, unless the program is strict, which
|
||||
#! leaves out files with mistakes of syntax.
|
||||
#! @param this The program.
|
||||
#! @param file The file.
|
||||
#! @returns Whether it may be.
|
||||
def program::usable(&this, file: u32): bool {
|
||||
return !this.strict || !this.broken(file);
|
||||
}
|
||||
|
||||
#! Sets what a checker checks a program for, which `when` decides by: the
|
||||
#! operating system, and the backend of an executable where it is checked for
|
||||
#! one.
|
||||
#! @param checker The checker.
|
||||
#! @param system The operating system, as `linux`.
|
||||
#! @param native Whether it is checked for an executable.
|
||||
pub def target_checker(checker: &mut checker, system: strview, native: bool): void {
|
||||
checker.target.os = checker.names.intern(&system);
|
||||
if native {
|
||||
checker.target.backend = checker.names.intern(&"native");
|
||||
}
|
||||
}
|
||||
|
||||
#! Gives the checker the files of the program the file begins: it, and every
|
||||
#! file declaring no `main`.
|
||||
#! @param this The program.
|
||||
#! @param checker The checker, made of this program's names and notes.
|
||||
#! @param entry The file declaring `main`, or `none`.
|
||||
pub def program::begin(&mut this, checker: &mut checker, entry: u32): void {
|
||||
this.entry = entry;
|
||||
target_checker(checker, this.system, this.native);
|
||||
checker.shelves = this.finder.held();
|
||||
for i in 0..this.paths.length {
|
||||
val file := i as u32;
|
||||
val taken := (!this.mains[i] || file == entry) && this.usable(file);
|
||||
if !taken {
|
||||
this.units.append(none);
|
||||
continue;
|
||||
}
|
||||
this.units.append(checker.add(&this.sources[i], &this.syntaxes[i], this.roots[i]));
|
||||
this.files.append(file);
|
||||
}
|
||||
}
|
||||
|
||||
#! What bringing a program up to date with the files found.
|
||||
pub enum synced : u8 {
|
||||
same, #! It is up to date, having checked again what changed.
|
||||
stale, #! The files moved on in a way it cannot follow: some are new or gone, or another program is to be checked.
|
||||
}
|
||||
|
||||
#! Brings a program up to date with the files as they are now: each file whose
|
||||
#! text changed is parsed again where it changed, and what that affects is
|
||||
#! checked again. Where files are new or gone, a file refers to a module not
|
||||
#! read, or a file's `main` changes which program is checked, the program
|
||||
#! cannot follow, and says so.
|
||||
#! @param this The program.
|
||||
#! @param checker Its checker.
|
||||
#! @param files The files as they are now.
|
||||
#! @param touched The path of the file last opened, edited, or asked about.
|
||||
#! @param last The path of the file the last program began with.
|
||||
#! @param checks Whether what changed is checked again, rather than only
|
||||
#! parsed again, as for a check, which checks the program whole.
|
||||
#! @param from What the files were gathered from, which the libraries'
|
||||
#! files are read again from.
|
||||
#! @returns Whether it is up to date.
|
||||
pub def program::sync(
|
||||
&mut this, checker: &mut checker, files: &vector<gathered>, touched: strview, last: strview, checks: bool, from: &gathering
|
||||
): synced {
|
||||
return synced::stale if files.length != this.given;
|
||||
for i in 0..files.length {
|
||||
return synced::stale if files[i].path.view() != this.paths[i].view();
|
||||
}
|
||||
|
||||
val main := this.names.intern(&"main");
|
||||
for i in 0..this.paths.length {
|
||||
val now := i < this.given ? string::copy_of(&files[i].text) : library_text(from, this.paths[i].view());
|
||||
continue if now.view() == this.texts[i].view();
|
||||
val file := i as u32;
|
||||
val was_broken := this.broken(file);
|
||||
var fresh_source := source::from(this.paths[i].view(), now.view());
|
||||
var fresh := syntax::new();
|
||||
var fresh_notes := vector::<note>::new();
|
||||
var lexing := lexer::new(&mut fresh_source, &mut this.names, &mut fresh_notes, file);
|
||||
lexing.tokenize_all(&mut fresh);
|
||||
this.roots[i] = this.syntaxes[i].reparse(&mut fresh, &this.names, this.roots[i], &mut this.parse_notes[i], &mut fresh_notes, file);
|
||||
this.sources[i] = move(&mut fresh_source);
|
||||
this.texts[i] = string::copy_of(&now);
|
||||
|
||||
# A file referring to a module not read changes what the program holds.
|
||||
var found := vector::<string>::new();
|
||||
this.finder.refer(&this.syntaxes[i], &this.names, &mut found);
|
||||
return synced::stale if found.length != 0;
|
||||
|
||||
# A file changing whether it declares `main`, or a strict program's file
|
||||
# gaining or losing its mistakes, changes what the program holds.
|
||||
val mains := this.syntaxes[i].declares_main(this.roots[i], main);
|
||||
return synced::stale if mains != this.mains[i];
|
||||
return synced::stale if this.strict && this.broken(file) != was_broken;
|
||||
if this.units[i] != none && checks {
|
||||
checker.update(this.units[i], &this.sources[i], this.roots[i]);
|
||||
}
|
||||
}
|
||||
return synced::stale if this.choose(touched, last) != this.entry;
|
||||
return synced::same;
|
||||
}
|
||||
|
||||
#! Checks the program whole with a checker of its own, as `sakura check` would,
|
||||
#! giving the checker the files in the order the program's checker has them, so
|
||||
#! each has the same index in both.
|
||||
#! @param this The program.
|
||||
#! @param checker The checker, made of this program's names and notes of its
|
||||
#! own.
|
||||
pub def program::check_whole(&mut this, checker: &mut checker): void {
|
||||
target_checker(checker, this.system, this.native);
|
||||
for &file in this.files {
|
||||
checker.add(&this.sources[file], &this.syntaxes[file], this.roots[file]);
|
||||
}
|
||||
checker.check();
|
||||
}
|
||||
|
||||
#! The file of a path, or `none`.
|
||||
#! @param this The program.
|
||||
#! @param at The path.
|
||||
#! @returns The file, or `none`.
|
||||
pub def program::file_of(&this, at: strview): u32 {
|
||||
for i in 0..this.paths.length {
|
||||
return i as u32 if this.paths[i].view() == at;
|
||||
}
|
||||
return none;
|
||||
}
|
||||
@@ -0,0 +1,525 @@
|
||||
#! @file requests.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the server answers what the client asks of the program:
|
||||
#! checking the workspace, whose notes it publishes as diagnostics, hovering,
|
||||
#! completing, the help shown while a call's arguments are written, colouring
|
||||
#! a file, going to where a name is declared, finding where it is written, and
|
||||
#! renaming it.
|
||||
|
||||
import std::json;
|
||||
import system;
|
||||
import sakura::analyzer;
|
||||
module sakura::server;
|
||||
|
||||
|
||||
#! Publishes the diagnostics of a file.
|
||||
#! @param this The server.
|
||||
#! @param uri The file's URI.
|
||||
#! @param diagnostics Its diagnostics, as a JSON array.
|
||||
def server::publish(&mut this, uri: strview, diagnostics: strview): void {
|
||||
var body := notification("textDocument/publishDiagnostics");
|
||||
body.begin_object();
|
||||
body.member("uri"); body.text(uri);
|
||||
body.member("diagnostics"); body.raw(diagnostics);
|
||||
body.end_object();
|
||||
this.send_written(&mut body);
|
||||
}
|
||||
|
||||
#! Checks the program, then publishes the diagnostics of each file, clearing
|
||||
#! those of the files that no longer have any. Should checking with the files
|
||||
#! whose syntax has mistakes fail, they are left out, and the rest checked again.
|
||||
#! @param this The server.
|
||||
def server::check(&mut this): void {
|
||||
var asked := empty_job();
|
||||
var output := string::new(nothing {});
|
||||
if !this.run_job(&mut asked, &mut output) {
|
||||
asked.strict = true;
|
||||
output.clear();
|
||||
# A check that did not finish keeps the diagnostics already shown.
|
||||
return if !this.fork_job(&asked, &mut output);
|
||||
this.answered(&asked, &mut output);
|
||||
}
|
||||
|
||||
# Each line is the URI of a file, a space, and its diagnostics. Those of files
|
||||
# no longer reported are cleared first, so a client that takes two URIs for
|
||||
# one file, as the editor's spelling of it while it is open and its path's
|
||||
# once it is closed, keeps the diagnostics published last.
|
||||
var published := vector::<string>::new();
|
||||
var reported := vector::<strview>::new();
|
||||
val space: u8 = ' ';
|
||||
for line in output.view().split('\n') {
|
||||
val at := line.index_of(&space) else { continue; };
|
||||
published.append(string::from(line.substr(0, at), nothing {}));
|
||||
reported.append(line.substr(at + 1, line.length - at - 1));
|
||||
}
|
||||
|
||||
var cleared := move(&mut this.published);
|
||||
this.published = move(&mut published);
|
||||
for &each in cleared {
|
||||
if !this.published.view().contains(&each) {
|
||||
this.publish(each.view(), "[]");
|
||||
}
|
||||
}
|
||||
for i in 0..this.published.length {
|
||||
this.publish(this.published[i].view(), reported[i]);
|
||||
}
|
||||
|
||||
# A base stopped for being out of date is made again now, while nothing waits.
|
||||
this.start_base();
|
||||
}
|
||||
|
||||
#! The job a request about a position asks for: the file of its document, and
|
||||
#! its position.
|
||||
#! @param params What the request is given.
|
||||
#! @param kind The kind of job.
|
||||
#! @param out Where the job is written.
|
||||
#! @returns Whether the request gives a file and a position.
|
||||
def job_at(params: &json::value, kind: task, out: &mut job): bool {
|
||||
val item := params.get("textDocument") else { return false; };
|
||||
val position := params.get("position") else { return false; };
|
||||
val at := position_in(&position) else { return false; };
|
||||
var path := uri_path(text_of(&item, "uri")) else { return false; };
|
||||
out.task = kind;
|
||||
out.path = move(&mut path);
|
||||
out.line = at.line;
|
||||
out.character = at.character;
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Answers a hover, completion, or the help shown while a call's arguments are
|
||||
#! written, from the files as they are now.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
#! @param kind What it asks.
|
||||
def server::query(&mut this, id: strview, params: &json::value, kind: task): void {
|
||||
val none_text: strview = kind == task::complete ? "{\"isIncomplete\":false,\"items\":[]}" : "null";
|
||||
var asked := empty_job();
|
||||
if !job_at(params, kind, &mut asked) {
|
||||
this.respond(id, none_text);
|
||||
return;
|
||||
}
|
||||
|
||||
# Typing `:` only completes a path after `::`, not a type after `name:`.
|
||||
if params.get("context") is some(context) {
|
||||
if text_of(&context, "triggerCharacter") == ":" {
|
||||
val item := params.get("textDocument") else { return; };
|
||||
val at := this.document_of(text_of(&item, "uri"));
|
||||
if at < this.documents.length {
|
||||
val text := this.documents[at].text.view();
|
||||
val offset := locate(text, asked.line as u64, asked.character as u64);
|
||||
if offset < 2 || text[(offset - 2) as u64] != ':' {
|
||||
this.respond(id, none_text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var output := string::new(nothing {});
|
||||
val done := this.run_job(&mut asked, &mut output);
|
||||
|
||||
# The documentation follows the items, after a zero byte, and is kept here so
|
||||
# that resolving an item is answered without asking the program again.
|
||||
var items := output.length;
|
||||
if kind == task::complete {
|
||||
this.documented.clear();
|
||||
val parts := output.view().split(0).to_vector();
|
||||
items = parts[0].length;
|
||||
if done {
|
||||
# Each item's documentation ends in a zero, after which nothing is left.
|
||||
for i in 1..parts.length {
|
||||
continue if i + 1 == parts.length && parts[i].length == 0;
|
||||
this.documented.append(string::from(parts[i], nothing {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.respond(id, done && items != 0 ? output.substr(0, items) : none_text);
|
||||
}
|
||||
|
||||
#! Answers `textDocument/semanticTokens/full` with the names of the file, which
|
||||
#! the editor colours by what the checker made of them rather than by their
|
||||
#! shape.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
def server::colour(&mut this, id: strview, params: &json::value): void {
|
||||
var asked := empty_job();
|
||||
asked.task = task::tokens;
|
||||
var done := false;
|
||||
var output := string::new(nothing {});
|
||||
if params.get("textDocument") is some(item) {
|
||||
if uri_path(text_of(&item, "uri")) is some(&found) {
|
||||
asked.path = string::copy_of(found);
|
||||
done = this.run_job(&mut asked, &mut output);
|
||||
}
|
||||
}
|
||||
this.respond(id, done && output.length != 0 ? output.view() : "{\"data\":[]}");
|
||||
}
|
||||
|
||||
#! Answers `completionItem/resolve` with the item as it was sent, and the
|
||||
#! documentation the server kept for it, which the list itself did not carry.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params The item.
|
||||
def server::resolve(&mut this, id: strview, params: &json::value): void {
|
||||
var which: u64 = 0xFFFFFFFFFFFFFFFF;
|
||||
if params.get("data") is some(data) {
|
||||
if data.as_u64() is value(found) {
|
||||
which = found;
|
||||
}
|
||||
}
|
||||
|
||||
# What the client gave is written again as `json_text` writes it, which writes
|
||||
# a value it cannot write whole as `null`.
|
||||
var body := response(id);
|
||||
body.begin_object();
|
||||
for member in params.members() {
|
||||
continue if member.key == "documentation";
|
||||
var written := json_text(&member.value);
|
||||
body.member(member.key);
|
||||
body.raw(written.view());
|
||||
}
|
||||
if which < this.documented.length && this.documented[which].length != 0 {
|
||||
body.member("documentation");
|
||||
write_markdown(&mut body, this.documented[which].view());
|
||||
}
|
||||
body.end_object();
|
||||
this.send_written(&mut body);
|
||||
}
|
||||
|
||||
#! Asks for a job about the name at the position a request gives, and gives
|
||||
#! what it wrote.
|
||||
#! @param this The server.
|
||||
#! @param params What the request is given.
|
||||
#! @param kind The kind of job.
|
||||
#! @param output Where what the job wrote is written.
|
||||
#! @returns Whether a name the checker resolved is there.
|
||||
def server::ask_name(&mut this, params: &json::value, kind: task, output: &mut string): bool {
|
||||
var asked := empty_job();
|
||||
return false if !job_at(params, kind, &mut asked);
|
||||
return this.run_job(&mut asked, output) && output.length != 0;
|
||||
}
|
||||
|
||||
#! Answers `textDocument/definition` with where what the name at the position
|
||||
#! is declared, or null for what has no declaration to go to, as a built-in
|
||||
#! type.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
def server::define(&mut this, id: strview, params: &json::value): void {
|
||||
var output := string::new(nothing {});
|
||||
if this.ask_name(params, task::name, &mut output) {
|
||||
var parsed := json::parse(output.view()) else (_) {
|
||||
this.respond(id, "null");
|
||||
return;
|
||||
};
|
||||
if parsed.root.get("declaration") is some(found) {
|
||||
var body := response(id);
|
||||
found.encode(&mut body);
|
||||
this.send_written(&mut body);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.respond(id, "null");
|
||||
}
|
||||
|
||||
#! Answers `textDocument/references` with everywhere what the name at the
|
||||
#! position refers to is written, leaving out its declaration where the request
|
||||
#! asks.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
def server::references(&mut this, id: strview, params: &json::value): void {
|
||||
var declared := true;
|
||||
if params.get("context") is some(context) {
|
||||
if context.get("includeDeclaration") is some(include) {
|
||||
if include.as_bool() is value(given) {
|
||||
declared = given;
|
||||
}
|
||||
}
|
||||
}
|
||||
var output := string::new(nothing {});
|
||||
if !this.ask_name(params, task::refer, &mut output) {
|
||||
this.respond(id, "null");
|
||||
return;
|
||||
}
|
||||
var parsed := json::parse(output.view()) else (_) {
|
||||
this.respond(id, "null");
|
||||
return;
|
||||
};
|
||||
val named := parsed.root;
|
||||
var declaration := string::new(nothing {});
|
||||
if named.get("declaration") is some(found) {
|
||||
declaration = json_text(&found);
|
||||
}
|
||||
|
||||
var body := response(id);
|
||||
body.begin_array();
|
||||
if named.get("locations") is some(locations) {
|
||||
for each in locations.elements() {
|
||||
if !declared {
|
||||
var written := json_text(&each);
|
||||
continue if written.view() == declaration.view();
|
||||
}
|
||||
each.encode(&mut body);
|
||||
}
|
||||
}
|
||||
body.end_array();
|
||||
this.send_written(&mut body);
|
||||
}
|
||||
|
||||
#! Whether a rename may change a file: one within a workspace folder, or one open
|
||||
#! in the editor. The standard library is changed only where the workspace holds
|
||||
#! it, so renaming a name a program uses never rewrites the library it came from.
|
||||
#! @param this The server.
|
||||
#! @param at The file's path.
|
||||
#! @returns Whether it may.
|
||||
def server::editable(&this, at: strview): bool {
|
||||
return false if at.length == 0;
|
||||
for &root in this.roots {
|
||||
return true if at.starts_with(root.view()) && (at.length == root.length || at[root.length] == '/');
|
||||
}
|
||||
for &each in this.documents {
|
||||
return true if each.path.view() == at;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Why the name a job found cannot be renamed, or empty text where it can. What
|
||||
#! the checker refuses is said first, then any place it is written that the
|
||||
#! workspace does not hold.
|
||||
#! @param this The server.
|
||||
#! @param named What the job wrote, or nothing where no name was found.
|
||||
#! @returns Why, or empty text.
|
||||
def server::refuse_rename(&this, named: maybe<json::value>): string {
|
||||
var reason := string::new(nothing {});
|
||||
val found := named else {
|
||||
reason.append("Nothing the checker resolved is named here.");
|
||||
return reason;
|
||||
};
|
||||
val name := text_of(&found, "name");
|
||||
if has_text(&found, "refusal") {
|
||||
reason.append("`");
|
||||
reason.append(name);
|
||||
reason.append("` cannot be renamed. ");
|
||||
reason.append(text_of(&found, "refusal"));
|
||||
return reason;
|
||||
}
|
||||
|
||||
# The declaration comes first, then each place it is written.
|
||||
var places := vector::<json::value>::new();
|
||||
if found.get("declaration") is some(declared) {
|
||||
places.append(declared);
|
||||
}
|
||||
if found.get("locations") is some(locations) {
|
||||
for each in locations.elements() {
|
||||
places.append(each);
|
||||
}
|
||||
}
|
||||
for &place in places {
|
||||
var at := string::new(nothing {});
|
||||
var made := uri_path(text_of(&place, "uri"));
|
||||
if made is some(&mut found) {
|
||||
at = move(found);
|
||||
}
|
||||
if !this.editable(at.view()) {
|
||||
reason.append("`");
|
||||
reason.append(name);
|
||||
reason.append("` cannot be renamed. It is written in ");
|
||||
reason.append(at.length != 0 ? at.view() : "a file the server cannot name");
|
||||
reason.append(", which is outside the workspace.");
|
||||
return reason;
|
||||
}
|
||||
}
|
||||
return reason;
|
||||
}
|
||||
|
||||
#! Asks for a job about the name at the position a request about renaming gives,
|
||||
#! and reads what it wrote, answering the request where the name cannot be
|
||||
#! renamed: with an error saying why, or null where what the job wrote is not
|
||||
#! JSON.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
#! @param kind The kind of job.
|
||||
#! @param output Where what the job wrote is kept, which the document read
|
||||
#! views.
|
||||
#! @returns What the job wrote, read, or nothing where the request is answered.
|
||||
def server::ask_renamable(&mut this, id: strview, params: &json::value, kind: task, output: &mut string): maybe<json::document> {
|
||||
val asked := this.ask_name(params, kind, output);
|
||||
var parsed := json::parse(asked ? output.view() : "null") else (_) {
|
||||
this.respond(id, "null");
|
||||
return nothing {};
|
||||
};
|
||||
val named: maybe<json::value> = asked && parsed.root.kind == json::kind::object ? parsed.root : nothing {};
|
||||
var refusal := this.refuse_rename(named);
|
||||
if refusal.length != 0 {
|
||||
this.respond_error(id, request_failed, refusal.view());
|
||||
return nothing {};
|
||||
}
|
||||
return move(parsed);
|
||||
}
|
||||
|
||||
#! Answers `textDocument/prepareRename` with the name at the position, or an
|
||||
#! error saying why it cannot be renamed, so the editor says so before asking
|
||||
#! for a name.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
def server::prepare_rename(&mut this, id: strview, params: &json::value): void {
|
||||
var output := string::new(nothing {});
|
||||
val parsed := this.ask_renamable(id, params, task::name, &mut output) else { return; };
|
||||
val found := parsed.root;
|
||||
var body := response(id);
|
||||
body.begin_object();
|
||||
body.member("range");
|
||||
write_range_of(found.get("at"), &mut body);
|
||||
body.member("placeholder");
|
||||
body.text(text_of(&found, "name"));
|
||||
body.end_object();
|
||||
this.send_written(&mut body);
|
||||
}
|
||||
|
||||
#! Writes the range of a location a job wrote, or `null` where there is none.
|
||||
#! @param location The location, or nothing.
|
||||
#! @param out Where it is written.
|
||||
def write_range_of(location: maybe<json::value>, out: &mut json::writer): void {
|
||||
val found := location else {
|
||||
out.empty();
|
||||
return;
|
||||
};
|
||||
val range := found.get("range") else {
|
||||
out.empty();
|
||||
return;
|
||||
};
|
||||
range.encode(out);
|
||||
}
|
||||
|
||||
#! Whether text is a name a declaration may be given: a letter or `_`, then
|
||||
#! letters, digits, and `_`, which is no keyword, nor a word the language keeps.
|
||||
#! @param text The text.
|
||||
#! @returns Whether it is.
|
||||
def is_name(text: strview): bool {
|
||||
return false if text.length == 0 || (text[0] >= '0' && text[0] <= '9');
|
||||
for i in 0..text.length {
|
||||
return false if !is_word_byte(text[i]);
|
||||
}
|
||||
var names := names::new();
|
||||
val id := names.intern(&text);
|
||||
return !names.is_keyword(id) && !names.is_reserved(id);
|
||||
}
|
||||
|
||||
#! Answers `textDocument/rename` with an edit replacing everywhere the name at
|
||||
#! the position is written, declaration and all, grouped by file.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
def server::rename(&mut this, id: strview, params: &json::value): void {
|
||||
val given := text_of(params, "newName");
|
||||
if !is_name(given) {
|
||||
var reason := string::from("`", nothing {});
|
||||
reason.append(given);
|
||||
reason.append("` is not a name: a name is a letter or `_`, then letters, digits, and `_`, and is not a keyword.");
|
||||
this.respond_error(id, request_failed, reason.view());
|
||||
return;
|
||||
}
|
||||
|
||||
var output := string::new(nothing {});
|
||||
val parsed := this.ask_renamable(id, params, task::refer, &mut output) else { return; };
|
||||
|
||||
# Each file is given once, with every place in it, in the order they were found.
|
||||
val found := parsed.root;
|
||||
var locations := vector::<json::value>::new();
|
||||
if found.get("locations") is some(all) {
|
||||
for each in all.elements() {
|
||||
locations.append(each);
|
||||
}
|
||||
}
|
||||
var body := response(id);
|
||||
body.begin_object();
|
||||
body.member("changes");
|
||||
body.begin_object();
|
||||
for i in 0..locations.length {
|
||||
val uri := text_of(&locations[i], "uri");
|
||||
var seen := false;
|
||||
for j in 0..i {
|
||||
seen = seen || text_of(&locations[j], "uri") == uri;
|
||||
}
|
||||
continue if seen;
|
||||
body.member(uri);
|
||||
body.begin_array();
|
||||
for j in i..locations.length {
|
||||
continue if text_of(&locations[j], "uri") != uri;
|
||||
body.begin_object();
|
||||
body.member("range");
|
||||
write_range_of(locations[j], &mut body);
|
||||
body.member("newText");
|
||||
body.text(given);
|
||||
body.end_object();
|
||||
}
|
||||
body.end_array();
|
||||
}
|
||||
body.end_object();
|
||||
body.end_object();
|
||||
this.send_written(&mut body);
|
||||
this.warn_unresolved(&found, given);
|
||||
}
|
||||
|
||||
#! A path as the editor's user is shown it: within a root of the workspace,
|
||||
#! relative to it.
|
||||
#! @param this The server.
|
||||
#! @param path The path.
|
||||
#! @returns The path shown.
|
||||
def server::shown_path(&this, path: strview): strview {
|
||||
var shown := path;
|
||||
for &root in this.roots {
|
||||
if shown.starts_with(root.view()) && shown.length > root.length && shown[root.length] == '/' {
|
||||
shown = shown.substr(root.length + 1, shown.length - root.length - 1);
|
||||
}
|
||||
}
|
||||
return shown;
|
||||
}
|
||||
|
||||
#! Tells the editor where a name a rename changed is also written but was never
|
||||
#! resolved, as in a branch of `when` for another target, or in the runtime of
|
||||
#! executables where the target is not one, so what the rename left as it is
|
||||
#! can be looked at rather than found broken on that target.
|
||||
#! @param this The server.
|
||||
#! @param named What the job found.
|
||||
#! @param given The name given.
|
||||
def server::warn_unresolved(&mut this, named: &json::value, given: strview): void {
|
||||
val name := text_of(named, "name");
|
||||
var places := string::new(nothing {});
|
||||
var count := 0;
|
||||
if named.get("unresolved") is some(unresolved) {
|
||||
for place in unresolved.elements() {
|
||||
var at := uri_path(text_of(&place, "uri")) else { continue; };
|
||||
val range := place.get("range") else { continue; };
|
||||
val start := range.get("start") else { continue; };
|
||||
val begins := position_in(&start) else { continue; };
|
||||
val shown := this.shown_path(at.view());
|
||||
if places.length != 0 {
|
||||
places.append(", ");
|
||||
}
|
||||
val text := $"{shown}:{begins.line + 1}:{begins.character + 1}";
|
||||
places.append(text.view());
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return if count == 0;
|
||||
|
||||
var message := string::from("Renamed `", nothing {});
|
||||
message.append(name);
|
||||
message.append("` to `");
|
||||
message.append(given);
|
||||
message.append("`, but `");
|
||||
message.append(name);
|
||||
message.append("` is also written in code the checker does not read for this target, where it may be the same name, and there it was left as it is: ");
|
||||
message.append(places.view());
|
||||
message.append(".");
|
||||
this.tell("window/showMessage", log_warning, message.view());
|
||||
}
|
||||
@@ -0,0 +1,654 @@
|
||||
#! @file server.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the language server: it serves the language server protocol over
|
||||
#! the standard input and output until the client exits. Every `.sakura` file
|
||||
#! of the workspace folders is checked together with the standard library,
|
||||
#! using the text of the editor for the files open in it, and each file's notes
|
||||
#! are published as its diagnostics. Where several files declare `main`, each
|
||||
#! begins a program holding every file that does not, and one program is
|
||||
#! checked at a time: that of the file declaring `main` last opened, edited, or
|
||||
#! asked about.
|
||||
#!
|
||||
#! The server itself never checks anything. Each job, as checking the workspace
|
||||
#! or describing the name hovered, is worked out by a process of its own, so a
|
||||
#! fault while checking cannot stop the server, and one taking too long is
|
||||
#! stopped. The server keeps a process, the base, holding the files of the
|
||||
#! workspace parsed and checked. A job is worked out by a process the base
|
||||
#! starts as a copy of itself, which parses again what changed of the files as
|
||||
#! they are, checks again only what that affects, and answers. A check of the
|
||||
#! workspace checks the program whole, so its diagnostics are what
|
||||
#! `sakura check` would say. Once a check is answered, the base brings itself
|
||||
#! up to date with the files, so the next job has less to do. Where the base
|
||||
#! cannot answer, as where files are new or gone, it is made again, and where it
|
||||
#! still cannot, the job is worked out from nothing by a process of its own.
|
||||
#! Windows cannot copy a process, so there the base answers each job itself, as
|
||||
#! a process of its own still, and the server stops it and makes it again where
|
||||
#! a job faults or takes too long.
|
||||
|
||||
import std::io;
|
||||
import std::json;
|
||||
import system;
|
||||
import sakura::analyzer;
|
||||
module sakura::server;
|
||||
|
||||
|
||||
#! How the server is run.
|
||||
pub struct server_options {
|
||||
pub var native: bool; #! Whether the workspace is checked for an executable.
|
||||
pub var system: strview; #! The operating system it is checked for, as `linux`.
|
||||
pub var libraries: span<strview>; #! The libraries, each as `NAME=DIR`, as `--lib` gives them, the standard library's among them.
|
||||
}
|
||||
|
||||
#! The base: the process holding the workspace parsed and checked, which works
|
||||
#! out each job.
|
||||
struct base_link {
|
||||
var process: i64; #! The process, by its number on Linux and its handle on Windows, or 0 where there is none.
|
||||
var jobs: system::fd; #! Where jobs are written to it.
|
||||
var answers: system::fd; #! Where its answers are read.
|
||||
}
|
||||
|
||||
#! The server.
|
||||
struct server {
|
||||
var options: server_options;
|
||||
var libraries: vector<string>; #! The libraries, each as `NAME=DIR`, their directories made absolute.
|
||||
var core: string; #! `core`'s directory, beside the server's executable or above it, or empty.
|
||||
var platform: string; #! `system`'s directory, found as `core`'s is, or empty.
|
||||
var runtime: string; #! The runtime's directory, `runtime`, found as `core`'s is, or empty.
|
||||
var roots: vector<string>; #! The paths of the workspace folders.
|
||||
var documents: vector<document>; #! The documents open in the editor.
|
||||
var published: vector<string>; #! The URIs last published with diagnostics.
|
||||
var documented: vector<string>; #! The documentation of the last completion's items, which resolving one is answered from.
|
||||
var initialized: bool;
|
||||
var shutdown: bool;
|
||||
var dirty: bool; #! Whether something changed since the last check.
|
||||
var touched: string; #! The file last opened, edited, or asked about.
|
||||
var entry: string; #! The file declaring `main` the last job was answered from, or empty.
|
||||
var base: base_link;
|
||||
}
|
||||
|
||||
#! Milliseconds without changes before the workspace is checked.
|
||||
const delay: i64 = 200;
|
||||
|
||||
#! Milliseconds a job may take before it is stopped.
|
||||
const timeout: i64 = 30000;
|
||||
|
||||
# Error codes of JSON-RPC and the protocol.
|
||||
const invalid_request: i64 = -32600;
|
||||
const method_not_found: i64 = -32601;
|
||||
const not_initialized: i64 = -32002;
|
||||
const request_failed: i64 = -32803;
|
||||
|
||||
# Message types of `window/logMessage` and `window/showMessage`.
|
||||
const log_error: u32 = 1;
|
||||
const log_warning: u32 = 2;
|
||||
const log_info: u32 = 3;
|
||||
|
||||
# What a process says of a job: answered, not finished, or that the files moved
|
||||
# on, so the base must be made again. A job says so by its exit status.
|
||||
const answer_ok: u64 = 0;
|
||||
const answer_fail: u64 = 1;
|
||||
const answer_stale: u64 = 2;
|
||||
const worker_stale: i32 = 3;
|
||||
|
||||
#! Serves the language server protocol over the standard input and output
|
||||
#! until the client exits.
|
||||
#! @param given How the server is run.
|
||||
#! @returns The status to exit with: 0 where the client shut the server down
|
||||
#! before it exited, and 1 otherwise.
|
||||
pub def serve(given: server_options): i32 {
|
||||
system::ignore(system::signal::pipe);
|
||||
var running := make_server(&given);
|
||||
for each in given.libraries {
|
||||
val split := each.index_of(&'=') else { continue; };
|
||||
var folder := spelled_folder(each.substr(split + 1, each.length - split - 1)) else { continue; };
|
||||
running.libraries.append($"{each.substr(0, split)}={folder}");
|
||||
}
|
||||
|
||||
var input := string::new(nothing {});
|
||||
var chunk: u8[65536];
|
||||
while true {
|
||||
# Each whole message that has arrived is handled.
|
||||
while true {
|
||||
var body := string::new(nothing {});
|
||||
break if !take_message(&mut input, &mut body);
|
||||
var parsed := json::parse(body.view()) else (_) {
|
||||
running.log(log_warning, "A message from the client was not valid JSON.");
|
||||
continue;
|
||||
};
|
||||
val going := running.handle(parsed.root);
|
||||
if !going {
|
||||
running.stop_base();
|
||||
return running.shutdown ? 0 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
# Checking waits for a pause in the changes, so typing is not held up by it.
|
||||
val waited := wait_readable(system::fd::stdin, running.dirty ? delay : -1);
|
||||
if waited == -4 {
|
||||
continue;
|
||||
}
|
||||
if waited == 0 {
|
||||
running.dirty = false;
|
||||
running.check();
|
||||
continue;
|
||||
}
|
||||
val size := system::fd::stdin.read((&mut chunk) as *mut u8, 65536);
|
||||
if size == -4 {
|
||||
continue;
|
||||
}
|
||||
if size <= 0 {
|
||||
running.stop_base();
|
||||
return running.shutdown ? 0 : 1;
|
||||
}
|
||||
input.append(strview::over((&chunk) as *u8, size as u64));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
#! Makes a server, which knows of no folder or document yet.
|
||||
#! @param given How the server is run, which is copied.
|
||||
#! @returns The server.
|
||||
def make_server(given: &server_options): server {
|
||||
return server {
|
||||
options = server_options { native = given.native, system = given.system, libraries = given.libraries },
|
||||
libraries = vector::<string>::new(),
|
||||
core = toolchain_folder("core"),
|
||||
platform = toolchain_folder("system"),
|
||||
runtime = toolchain_folder("runtime"),
|
||||
roots = vector::<string>::new(),
|
||||
documents = vector::<document>::new(),
|
||||
published = vector::<string>::new(),
|
||||
documented = vector::<string>::new(),
|
||||
touched = string::new(nothing {}),
|
||||
entry = string::new(nothing {})
|
||||
};
|
||||
}
|
||||
|
||||
#! A library's directory, spelled as the paths of the documents are, which its
|
||||
#! files' paths are compared with: made absolute, and on Windows, which spells a
|
||||
#! file many ways, in the one way of its own.
|
||||
#! @param folder The directory, as given.
|
||||
#! @returns The directory, or `nothing` where it cannot be spelled so.
|
||||
def spelled_folder(folder: strview): maybe<string> {
|
||||
var made: maybe<string> = nothing {};
|
||||
when os == linux {
|
||||
if path::parse(folder) is value(&found) {
|
||||
if io::absolute(found) is value(&whole) {
|
||||
made = string::from(whole.view(), nothing {});
|
||||
}
|
||||
}
|
||||
}
|
||||
when os == windows {
|
||||
made = canonical_path(folder);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Takes the body of the next whole message from what has arrived, if one has.
|
||||
#! A header without a length cannot be followed, so it is dropped.
|
||||
#! @param input What has arrived, which the message is taken from.
|
||||
#! @param body Where the body is written.
|
||||
#! @returns Whether a whole message had arrived.
|
||||
def take_message(input: &mut string, body: &mut string): bool {
|
||||
while true {
|
||||
val text := input.view();
|
||||
var end: u64 = 0;
|
||||
var found := false;
|
||||
while end + 4 <= text.length {
|
||||
if text.substr(end, 4) == "\r\n\r\n" {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
end++;
|
||||
}
|
||||
return false if !found;
|
||||
|
||||
val header := end + 4;
|
||||
var known := false;
|
||||
var length: u64 = 0;
|
||||
var line: u64 = 0;
|
||||
while line < end {
|
||||
var stop := line;
|
||||
while stop + 1 < end + 2 && text.substr(stop, 2) != "\r\n" {
|
||||
stop++;
|
||||
}
|
||||
val field := text.substr(line, stop - line);
|
||||
if field.length >= 15 && field.substr(0, 15).lower().view() == "content-length:" {
|
||||
val number := field.substr(15, field.length - 15).trim();
|
||||
length = 0;
|
||||
for i in 0..number.length {
|
||||
break if number[i] < '0' || number[i] > '9';
|
||||
length = length * 10 + (number[i] - '0') as u64;
|
||||
}
|
||||
known = true;
|
||||
}
|
||||
line = stop + 2;
|
||||
}
|
||||
|
||||
if !known {
|
||||
input = string::from(text.substr(header, text.length - header), nothing {});
|
||||
continue;
|
||||
}
|
||||
return false if text.length < header + length;
|
||||
body = string::from(text.substr(header, length), nothing {});
|
||||
input = string::from(text.substr(header + length, text.length - header - length), nothing {});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Sends a message to the client.
|
||||
#! @param this The server.
|
||||
#! @param body The message, as JSON.
|
||||
def server::send(&mut this, body: strview): void {
|
||||
var made := $"Content-Length: {body.length}\r\n\r\n";
|
||||
made.append(body);
|
||||
if !put_all(system::fd::stdout, made.view()) {
|
||||
system::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
#! Begins a message to the client, as JSON-RPC 2.0 writes one, leaving it open
|
||||
#! for its members, which `send_written` closes.
|
||||
#! @returns The writer, within the message.
|
||||
def envelope(): json::writer {
|
||||
var made := message_writer();
|
||||
made.begin_object();
|
||||
made.member("jsonrpc");
|
||||
made.text("2.0");
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Begins the response to a request, whose outcome is written next, as a value.
|
||||
#! @param id The request's ID, as JSON.
|
||||
#! @param outcome The outcome's member: `result`, or `error`.
|
||||
#! @returns The writer, within the response.
|
||||
def response(id: strview, outcome: strview = "result"): json::writer {
|
||||
var made := envelope();
|
||||
made.member("id");
|
||||
made.raw(id);
|
||||
made.member(outcome);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Begins a notification to the client, whose parameters are written next, as a
|
||||
#! value.
|
||||
#! @param method The method it notifies.
|
||||
#! @returns The writer, within the notification.
|
||||
def notification(method: strview): json::writer {
|
||||
var made := envelope();
|
||||
made.member("method");
|
||||
made.text(method);
|
||||
made.member("params");
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Sends a message `response` or `notification` began, once what follows its
|
||||
#! beginning is written.
|
||||
#! @param this The server.
|
||||
#! @param body The message.
|
||||
def server::send_written(&mut this, body: &mut json::writer): void {
|
||||
body.end_object();
|
||||
# A lossy writer given nothing but integers and text never fails.
|
||||
val text := body.finish() else (_) { return; };
|
||||
this.send(text);
|
||||
}
|
||||
|
||||
#! Tells the client something, which it logs, or shows its user.
|
||||
#! @param this The server.
|
||||
#! @param method `window/logMessage`, or `window/showMessage`.
|
||||
#! @param kind How grave it is.
|
||||
#! @param text What it says.
|
||||
def server::tell(&mut this, method: strview, kind: u32, text: strview): void {
|
||||
var body := notification(method);
|
||||
body.begin_object();
|
||||
body.member("type"); body.unsigned(kind as u64);
|
||||
body.member("message"); body.text(text);
|
||||
body.end_object();
|
||||
this.send_written(&mut body);
|
||||
}
|
||||
|
||||
#! Logs a message in the client's log.
|
||||
#! @param this The server.
|
||||
#! @param kind How grave it is.
|
||||
#! @param text What it says.
|
||||
def server::log(&mut this, kind: u32, text: strview): void {
|
||||
this.tell("window/logMessage", kind, text);
|
||||
}
|
||||
|
||||
#! Answers a request.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID, as JSON.
|
||||
#! @param result The answer, as JSON.
|
||||
def server::respond(&mut this, id: strview, result: strview): void {
|
||||
var body := response(id);
|
||||
body.raw(result);
|
||||
this.send_written(&mut body);
|
||||
}
|
||||
|
||||
#! Answers a request with an error.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID, as JSON.
|
||||
#! @param code The error's code.
|
||||
#! @param message What it says.
|
||||
def server::respond_error(&mut this, id: strview, code: i64, message: strview): void {
|
||||
var body := response(id, "error");
|
||||
body.begin_object();
|
||||
body.member("code"); body.signed(code);
|
||||
body.member("message"); body.text(message);
|
||||
body.end_object();
|
||||
this.send_written(&mut body);
|
||||
}
|
||||
|
||||
#! The JSON a value is written as.
|
||||
#! @param value The value.
|
||||
#! @returns Its text.
|
||||
def json_text(value: &json::value): string {
|
||||
var writer := json::writer::new();
|
||||
value.encode(&mut writer);
|
||||
val made := writer.finish() else (_) { return string::from("null", nothing {}); };
|
||||
return string::from(made, nothing {});
|
||||
}
|
||||
|
||||
#! The text of a string member of an object, or empty text where it is none.
|
||||
#! @param value The object.
|
||||
#! @param key The member's name.
|
||||
#! @returns The text.
|
||||
def text_of(value: &json::value, key: strview): strview {
|
||||
val member := value.get(key) else { return ""; };
|
||||
return member.as_text() else "";
|
||||
}
|
||||
|
||||
#! Whether an object has a string member of the name.
|
||||
#! @param value The object.
|
||||
#! @param key The member's name.
|
||||
#! @returns Whether it has.
|
||||
def has_text(value: &json::value, key: strview): bool {
|
||||
val member := value.get(key) else { return false; };
|
||||
return member.kind == json::kind::text;
|
||||
}
|
||||
|
||||
#! Keeps the file last opened, edited, or asked about, whose program the next
|
||||
#! job is answered from where it declares `main`.
|
||||
#! @param this The server.
|
||||
#! @param at The file's path, or empty.
|
||||
def server::touch(&mut this, at: strview): void {
|
||||
return if at.length == 0;
|
||||
this.touched = string::from(at, nothing {});
|
||||
}
|
||||
|
||||
#! The document open of a URI, or the count of documents.
|
||||
#! @param this The server.
|
||||
#! @param uri The URI.
|
||||
#! @returns Its index.
|
||||
def server::document_of(&this, uri: strview): u64 {
|
||||
for i in 0..this.documents.length {
|
||||
return i if this.documents[i].uri.view() == uri;
|
||||
}
|
||||
return this.documents.length;
|
||||
}
|
||||
|
||||
#! Adds a workspace folder, once.
|
||||
#! @param this The server.
|
||||
#! @param uri The folder's URI.
|
||||
def server::add_root(&mut this, uri: strview): void {
|
||||
var found := uri_path(uri) else { return; };
|
||||
return if this.roots.view().contains(&found);
|
||||
this.roots.append(move(&mut found));
|
||||
}
|
||||
|
||||
#! Takes a workspace folder away.
|
||||
#! @param this The server.
|
||||
#! @param uri The folder's URI.
|
||||
def server::remove_root(&mut this, uri: strview): void {
|
||||
val found := uri_path(uri) else { return; };
|
||||
val at := this.roots.view().index_of(&found) else { return; };
|
||||
this.roots.remove_at(at);
|
||||
}
|
||||
|
||||
#! Whether any of the libraries is the standard library's, or one of its
|
||||
#! modules', as `std::io`.
|
||||
#! @param this The server.
|
||||
#! @returns Whether one is.
|
||||
def server::reads_std(&this): bool {
|
||||
for &each in this.libraries {
|
||||
return true if each.view().starts_with("std=") || each.view().starts_with("std::");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! What the files of a check are gathered from now.
|
||||
#! @param this The server.
|
||||
#! @returns What they are gathered from.
|
||||
def server::gathering(&this): gathering {
|
||||
return gathering {
|
||||
core = this.core.view(), system = this.platform.view(), runtime = this.runtime.view(),
|
||||
libraries = this.libraries.view(), roots = &this.roots, documents = &this.documents
|
||||
};
|
||||
}
|
||||
|
||||
#! Handles a message, giving whether the server goes on: it stops once the
|
||||
#! client asks it to exit.
|
||||
#! @param this The server.
|
||||
#! @param message The message.
|
||||
#! @returns Whether it goes on.
|
||||
def server::handle(&mut this, message: json::value): bool {
|
||||
val method := text_of(&message, "method");
|
||||
# A response to the server, which asks nothing.
|
||||
return true if method.length == 0;
|
||||
return false if method == "exit";
|
||||
|
||||
var params := message;
|
||||
if message.get("params") is some(given) {
|
||||
params = given;
|
||||
}
|
||||
if message.get("id") is some(found) {
|
||||
var id := json_text(&found);
|
||||
if method == "initialize" {
|
||||
this.initialize(id.view(), ¶ms);
|
||||
} else if !this.initialized {
|
||||
this.respond_error(id.view(), not_initialized, "The server was not initialized.");
|
||||
} else if this.shutdown {
|
||||
this.respond_error(id.view(), invalid_request, "The server is shutting down.");
|
||||
} else if method == "shutdown" {
|
||||
this.shutdown = true;
|
||||
this.respond(id.view(), "null");
|
||||
} else if method == "textDocument/hover" {
|
||||
this.query(id.view(), ¶ms, task::hover);
|
||||
} else if method == "textDocument/completion" {
|
||||
this.query(id.view(), ¶ms, task::complete);
|
||||
} else if method == "completionItem/resolve" {
|
||||
this.resolve(id.view(), ¶ms);
|
||||
} else if method == "textDocument/definition" {
|
||||
this.define(id.view(), ¶ms);
|
||||
} else if method == "textDocument/references" {
|
||||
this.references(id.view(), ¶ms);
|
||||
} else if method == "textDocument/prepareRename" {
|
||||
this.prepare_rename(id.view(), ¶ms);
|
||||
} else if method == "textDocument/rename" {
|
||||
this.rename(id.view(), ¶ms);
|
||||
} else if method == "textDocument/signatureHelp" {
|
||||
this.query(id.view(), ¶ms, task::call);
|
||||
} else if method == "textDocument/semanticTokens/full" {
|
||||
this.colour(id.view(), ¶ms);
|
||||
} else {
|
||||
this.respond_error(id.view(), method_not_found, "The server does not support this request.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return true if !this.initialized || this.shutdown;
|
||||
if method == "initialized" {
|
||||
# Making the base takes as long as a check, so it is made before it is
|
||||
# asked for, leaving the first check of a session to pay for nothing but
|
||||
# checking.
|
||||
this.start_base();
|
||||
if this.core.length == 0 {
|
||||
this.log(log_warning, "No `core` folder is beside the server's executable or above it, so checks know nothing of the language's own types.");
|
||||
}
|
||||
if !this.reads_std() {
|
||||
this.log(log_warning, "The standard library was not found, so it is left out of checks. Put it beside the server's executable or above it, install it with `blossom install`, or give `--lib std=DIR` to `sakura lsp`.");
|
||||
}
|
||||
}
|
||||
if method == "initialized" || method == "textDocument/didSave" || method == "workspace/didChangeWatchedFiles"
|
||||
|| method == "workspace/didChangeConfiguration" {
|
||||
this.dirty = true;
|
||||
} else if method == "textDocument/didOpen" {
|
||||
this.open(¶ms);
|
||||
} else if method == "textDocument/didChange" {
|
||||
this.change(¶ms);
|
||||
} else if method == "textDocument/didClose" {
|
||||
this.close(¶ms);
|
||||
} else if method == "workspace/didChangeWorkspaceFolders" {
|
||||
this.folders(¶ms);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Answers `initialize` with what the server can do, taking the workspace
|
||||
#! folders it is given.
|
||||
#! @param this The server.
|
||||
#! @param id The request's ID.
|
||||
#! @param params What it is given.
|
||||
def server::initialize(&mut this, id: strview, params: &json::value): void {
|
||||
if params.get("workspaceFolders") is some(folders) {
|
||||
for folder in folders.elements() {
|
||||
this.add_root(text_of(&folder, "uri"));
|
||||
}
|
||||
} else if has_text(params, "rootUri") {
|
||||
this.add_root(text_of(params, "rootUri"));
|
||||
}
|
||||
this.initialized = true;
|
||||
this.respond(id, """
|
||||
{"capabilities":{
|
||||
"positionEncoding":"utf-16",
|
||||
"textDocumentSync":{"openClose":true,"change":2,"save":{"includeText":false}},
|
||||
"hoverProvider":true,
|
||||
"completionProvider":{"triggerCharacters":[".",":"],"resolveProvider":true},
|
||||
"definitionProvider":true,
|
||||
"referencesProvider":true,
|
||||
"renameProvider":{"prepareProvider":true},
|
||||
"signatureHelpProvider":{"triggerCharacters":["(",","],"retriggerCharacters":[",",")"]},
|
||||
"semanticTokensProvider":{"legend":{"tokenTypes":[
|
||||
"namespace","struct","interface","enum","type","typeParameter",
|
||||
"parameter","variable","property","enumMember","function","method",
|
||||
"keyword"],
|
||||
"tokenModifiers":["declaration","readonly"]},"full":true},
|
||||
"workspace":{"workspaceFolders":{"supported":true,"changeNotifications":true}}
|
||||
},
|
||||
"serverInfo":{"name":"sakura"}}
|
||||
""");
|
||||
}
|
||||
|
||||
#! Takes a document the editor opened.
|
||||
#! @param this The server.
|
||||
#! @param params What `textDocument/didOpen` is given.
|
||||
def server::open(&mut this, params: &json::value): void {
|
||||
val item := params.get("textDocument") else { return; };
|
||||
return if !has_text(&item, "uri") || !has_text(&item, "text");
|
||||
val uri := text_of(&item, "uri");
|
||||
var at := this.document_of(uri);
|
||||
if at == this.documents.length {
|
||||
var path := string::new(nothing {});
|
||||
var made := uri_path(uri);
|
||||
if made is some(&mut found) {
|
||||
path = move(found);
|
||||
}
|
||||
this.documents.append(document { uri = string::from(uri, nothing {}), path = move(&mut path), text = string::new(nothing {}) });
|
||||
}
|
||||
this.documents[at].text = string::from(text_of(&item, "text"), nothing {});
|
||||
this.touch(this.documents[at].path.view());
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
#! The byte of a document's text a position of the protocol is at.
|
||||
#! @param text The text.
|
||||
#! @param position The position.
|
||||
#! @param out Where the byte is written.
|
||||
#! @returns Whether the value is a position.
|
||||
def position_at(text: strview, position: &json::value, out: &mut u32): bool {
|
||||
val at := position_in(position) else { return false; };
|
||||
out = locate(text, at.line as u64, at.character as u64);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The position of the protocol a value is.
|
||||
#! @param value The value, as `{"line":0,"character":4}`.
|
||||
#! @returns The position, or nothing where the value is none.
|
||||
def position_in(value: &json::value): maybe<position> {
|
||||
val line := value.get("line") else { return nothing {}; };
|
||||
val character := value.get("character") else { return nothing {}; };
|
||||
val row := line.as_u64() else (_) { return nothing {}; };
|
||||
val column := character.as_u64() else (_) { return nothing {}; };
|
||||
return position { line = row as u32, character = column as u32 };
|
||||
}
|
||||
|
||||
#! Takes the changes the editor made to a document. A change is the text a range
|
||||
#! becomes, and they are made in turn, since each is given against the text the
|
||||
#! one before it left. A change without a range is the whole document.
|
||||
#! @param this The server.
|
||||
#! @param params What `textDocument/didChange` is given.
|
||||
def server::change(&mut this, params: &json::value): void {
|
||||
val item := params.get("textDocument") else { return; };
|
||||
val at := this.document_of(text_of(&item, "uri"));
|
||||
return if at == this.documents.length;
|
||||
val changes := params.get("contentChanges") else { return; };
|
||||
for each in changes.elements() {
|
||||
continue if !has_text(&each, "text");
|
||||
val text := text_of(&each, "text");
|
||||
if each.get("range") is some(range) {
|
||||
var from: u32 = 0;
|
||||
var to: u32 = 0;
|
||||
val start := range.get("start") else { continue; };
|
||||
val end := range.get("end") else { continue; };
|
||||
val old := this.documents[at].text.view();
|
||||
continue if !position_at(old, &start, &mut from) || !position_at(old, &end, &mut to);
|
||||
if to < from {
|
||||
to = from;
|
||||
}
|
||||
var edited := string::from(old.substr(0, from as u64), nothing {});
|
||||
edited.append(text);
|
||||
edited.append(old.substr(to as u64, old.length - to as u64));
|
||||
this.documents[at].text = move(&mut edited);
|
||||
} else {
|
||||
this.documents[at].text = string::from(text, nothing {});
|
||||
}
|
||||
}
|
||||
this.touch(this.documents[at].path.view());
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
#! Lets go of a document the editor closed, which is read from disk again.
|
||||
#! @param this The server.
|
||||
#! @param params What `textDocument/didClose` is given.
|
||||
def server::close(&mut this, params: &json::value): void {
|
||||
val item := params.get("textDocument") else { return; };
|
||||
val at := this.document_of(text_of(&item, "uri"));
|
||||
return if at == this.documents.length;
|
||||
this.documents.remove_at(at);
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
#! Takes the workspace folders the editor added and removed.
|
||||
#! @param this The server.
|
||||
#! @param params What `workspace/didChangeWorkspaceFolders` is given.
|
||||
def server::folders(&mut this, params: &json::value): void {
|
||||
val event := params.get("event") else { return; };
|
||||
if event.get("removed") is some(removed) {
|
||||
for folder in removed.elements() {
|
||||
this.remove_root(text_of(&folder, "uri"));
|
||||
}
|
||||
}
|
||||
if event.get("added") is some(added) {
|
||||
for folder in added.elements() {
|
||||
this.add_root(text_of(&folder, "uri"));
|
||||
}
|
||||
}
|
||||
# The base gathers the files of the folders it was made with, so it is made
|
||||
# again with these.
|
||||
this.stop_base();
|
||||
this.dirty = true;
|
||||
}
|
||||
@@ -0,0 +1,715 @@
|
||||
#! @file system.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains what the server asks of the system beyond what `system` gives:
|
||||
#! making a process the leader of a group of its own, sending what it writes
|
||||
#! nowhere, and the path of the executable running, which `sakura` also finds
|
||||
#! the standard library by, with waiting on a handle for a while, the time, and
|
||||
#! reading and writing whole messages over the pipes between its processes. The
|
||||
#! calls are those of Linux on x86-64, and on Windows the functions of the
|
||||
#! system, where the server also starts its processes itself, and spells each
|
||||
#! path one way, since Windows spells a file many.
|
||||
|
||||
import std::io;
|
||||
import std::process;
|
||||
import system;
|
||||
import system::windows;
|
||||
import sakura::analyzer;
|
||||
|
||||
module sakura::server;
|
||||
|
||||
|
||||
when os == linux {
|
||||
# The numbers of the calls made here, as Linux gives them on x86-64.
|
||||
val call_dup2: i64 = 33;
|
||||
val call_readlink: i64 = 89;
|
||||
val call_setpgid: i64 = 109;
|
||||
|
||||
#! Sends what the process writes to its standard output nowhere, so that only
|
||||
#! the server, which answers the client, ever writes to it.
|
||||
pub def quiet_output(): void {
|
||||
val nowhere := system::open(system::null_device, open_flags::write);
|
||||
return if nowhere < 0;
|
||||
system::linux::syscall(call_dup2, nowhere as u64, 1, 0, 0, 0, 0);
|
||||
nowhere.close();
|
||||
}
|
||||
|
||||
#! Makes the process the leader of a group of its own, which the processes it
|
||||
#! starts belong to, so they are all stopped at once.
|
||||
pub def lead_group(): void {
|
||||
system::linux::syscall(call_setpgid, 0, 0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
#! The path of the executable running.
|
||||
#! @returns The path, or empty text where the system does not say.
|
||||
pub def executable_path(): string {
|
||||
val link: strview = "/proc/self/exe";
|
||||
var name: u8[16];
|
||||
for i in 0..link.length {
|
||||
name[i] = link[i];
|
||||
}
|
||||
name[link.length] = 0;
|
||||
|
||||
var text: u8[4096];
|
||||
val from := (&mut name) as *mut u8;
|
||||
val into := (&mut text) as *mut u8;
|
||||
val length := system::linux::syscall(call_readlink, from as u64, into as u64, 4095, 0, 0, 0);
|
||||
return string::new(nothing {}) if length <= 0;
|
||||
return string::from(span::<u8>::over(into as *u8, length as u64), nothing {});
|
||||
}
|
||||
}
|
||||
|
||||
when os == windows {
|
||||
# What `GetStdHandle` and `SetStdHandle` take for the standard output.
|
||||
val standard_output: u32 = 0xFFFFFFF5;
|
||||
|
||||
# What they take for the standard error.
|
||||
val standard_error: u32 = 0xFFFFFFF4;
|
||||
|
||||
# What the other functions of the system are given, and give.
|
||||
val invalid_handle: u64 = 0xFFFFFFFFFFFFFFFF;
|
||||
val infinite: u32 = 0xFFFFFFFF;
|
||||
val utf8_page: u32 = 65001;
|
||||
val same_access: u32 = 2;
|
||||
val use_std_handles: u32 = 0x100;
|
||||
val handle_list: u64 = 0x20002;
|
||||
val suspended: u32 = 0x4;
|
||||
val extended_startup: u32 = 0x80000;
|
||||
val no_window: u32 = 0x08000000;
|
||||
val extended_limits: u32 = 9; # The limits of a job object, JOBOBJECT_EXTENDED_LIMIT_INFORMATION.
|
||||
val kill_on_close: u64 = 0x2000; # Its processes are stopped once the last handle of it is closed.
|
||||
val basic_find: u32 = 1; # Finding a file without its short name.
|
||||
|
||||
# The most units of UTF-16 a path of the system holds, with the zero it ends
|
||||
# in.
|
||||
val wide_limit: u64 = 32768;
|
||||
|
||||
# The job object every process the server starts is put in, which stops them
|
||||
# all once the server ends, however it ends, or 0 before the first starts.
|
||||
var processes: u64;
|
||||
|
||||
# The system's handle a handle of `system` stands for, which gives the
|
||||
# standard streams as 0, 1, and 2.
|
||||
def system_handle(handle: system::fd): u64 {
|
||||
return handle >= 0 && handle <= 2 ? GetStdHandle(0xFFFFFFF6 - handle as u32) : (handle as i64) as u64;
|
||||
}
|
||||
|
||||
#! Sends what the process writes to its standard output nowhere, so that only
|
||||
#! the server, which answers the client, ever writes to it. The handle of
|
||||
#! nowhere is the standard output from then on, so it stays open.
|
||||
pub def quiet_output(): void {
|
||||
val nowhere := system::open(system::null_device, open_flags::write);
|
||||
return if nowhere < 0;
|
||||
SetStdHandle(standard_output, system_handle(nowhere));
|
||||
}
|
||||
|
||||
#! The path of the executable running, in UTF-8.
|
||||
#! @returns The path, or empty text where the system does not say.
|
||||
pub def executable_path(): string {
|
||||
var wide: u16[4096];
|
||||
var text: u8[16384];
|
||||
val units := (&mut wide) as *mut u16;
|
||||
val into := (&mut text) as *mut u8;
|
||||
val count := GetModuleFileNameW(0, units, 4096);
|
||||
return string::new(nothing {}) if count == 0 || count >= 4096;
|
||||
val length := WideCharToMultiByte(utf8_page, 0, units as *u16, count as i32, into, 16384, null, null);
|
||||
return string::new(nothing {}) if length <= 0;
|
||||
return string::from(span::<u8>::over(into as *u8, length as u64), nothing {});
|
||||
}
|
||||
|
||||
#! Takes the standard output the process was given, which the server reads
|
||||
#! its answers from, and sends anything else written there nowhere, as
|
||||
#! `quiet_output` does.
|
||||
#! @returns The handle of the output it was given.
|
||||
pub def take_output(): system::fd {
|
||||
val given := GetStdHandle(standard_output);
|
||||
quiet_output();
|
||||
return (given as i64) as system::fd;
|
||||
}
|
||||
|
||||
#! Starts a process of the server's, which on Windows is this program started
|
||||
#! again, as `PROGRAM lsp ROLE`, since Windows cannot start a copy of the
|
||||
#! server. It is given the handles as its standard input and output, and the
|
||||
#! server's standard error as its own, which reaches the editor's log, and it
|
||||
#! inherits nothing else, so it never holds the connection to the client open.
|
||||
#! No console is made for it, as the server has none under an editor, and it
|
||||
#! is put in a job object before it runs, which stops it once the server
|
||||
#! ends, however that is.
|
||||
#! @param role What it is to be, as `--worker`.
|
||||
#! @param input The handle it reads.
|
||||
#! @param output The handle it writes.
|
||||
#! @returns The process, as the system's handle of it, or 0 where it could not
|
||||
#! be started.
|
||||
pub def start_process(role: strview, input: system::fd, output: system::fd): i64 {
|
||||
if processes == 0 {
|
||||
processes = CreateJobObjectW(null, null);
|
||||
if processes != 0 {
|
||||
# JOBOBJECT_EXTENDED_LIMIT_INFORMATION, whose flags are 16 bytes in.
|
||||
var limits: u64[18];
|
||||
limits[2] = kill_on_close;
|
||||
SetInformationJobObject(processes, extended_limits, (&mut limits) as *mut u8, 144);
|
||||
}
|
||||
}
|
||||
|
||||
# `"PROGRAM" lsp ROLE`, which Windows may write into while it starts it.
|
||||
var program: u16[32768];
|
||||
val length := GetModuleFileNameW(0, (&mut program) as *mut u16, wide_limit as u32) as u64;
|
||||
return 0 if length == 0 || length >= wide_limit;
|
||||
var command: u16[32800];
|
||||
var at: u64 = 0;
|
||||
command[at] = '"' as u16;
|
||||
at++;
|
||||
for i in 0..length {
|
||||
command[at] = program[i];
|
||||
at++;
|
||||
}
|
||||
val rest := $"\" lsp {role}";
|
||||
for i in 0..rest.length {
|
||||
command[at] = rest[i] as u16;
|
||||
at++;
|
||||
}
|
||||
command[at] = 0;
|
||||
|
||||
# Each handle it is given is a copy made for it to inherit, and it is told to
|
||||
# inherit only those.
|
||||
val ours := GetCurrentProcess();
|
||||
var given: u64[3];
|
||||
given[0] = system_handle(input);
|
||||
given[1] = system_handle(output);
|
||||
given[2] = GetStdHandle(standard_error);
|
||||
var copies: u64[3];
|
||||
var count: u64 = 0;
|
||||
for i in 0u..3u {
|
||||
var copy: u64 = 0;
|
||||
if given[i] != 0 && given[i] != invalid_handle && DuplicateHandle(ours, given[i], ours, &mut copy, 0, 1, same_access) != 0 {
|
||||
copies[count] = copy;
|
||||
count++;
|
||||
}
|
||||
given[i] = copy;
|
||||
}
|
||||
|
||||
var size: u64 = 0;
|
||||
var list: u64[32];
|
||||
InitializeProcThreadAttributeList(null, 1, 0, &mut size);
|
||||
val listed := size <= 256 && InitializeProcThreadAttributeList((&mut list) as *mut u8, 1, 0, &mut size) != 0;
|
||||
var startup := startup_information { size = 112, flags = use_std_handles };
|
||||
startup.standard_input = given[0];
|
||||
startup.standard_output = given[1];
|
||||
startup.standard_error = given[2];
|
||||
startup.attributes = (&mut list) as *mut u8;
|
||||
var made: process_information;
|
||||
val started := listed && given[0] != 0 && given[1] != 0
|
||||
&& UpdateProcThreadAttribute((&mut list) as *mut u8, 0, handle_list, (&mut copies) as *mut u8, count * 8, null, null) != 0
|
||||
&& CreateProcessW(
|
||||
(&program) as *u16, (&mut command) as *mut u16, null, null, 1, no_window | suspended | extended_startup,
|
||||
null, null, &mut startup, &mut made
|
||||
) != 0;
|
||||
if listed {
|
||||
DeleteProcThreadAttributeList((&mut list) as *mut u8);
|
||||
}
|
||||
for i in 0..count {
|
||||
CloseHandle(copies[i]);
|
||||
}
|
||||
return 0 if !started;
|
||||
|
||||
# It starts suspended, so it is in the job object before it can do anything.
|
||||
if processes != 0 {
|
||||
AssignProcessToJobObject(processes, made.process);
|
||||
}
|
||||
ResumeThread(made.thread);
|
||||
CloseHandle(made.thread);
|
||||
return made.process as i64;
|
||||
}
|
||||
|
||||
#! Waits for a process the server started to end, until a moment passes, when
|
||||
#! it is stopped, and lets go of it.
|
||||
#! @param process The process, as `start_process` gives it.
|
||||
#! @param deadline The moment it is stopped, as `now_ms` gives it, or 0 to
|
||||
#! stop it at once.
|
||||
#! @param code Where the status it exited with is written, which is the
|
||||
#! code of the exception where one ended it.
|
||||
#! @returns Whether it ended before it was stopped.
|
||||
pub def end_process(process: i64, deadline: i64, code: &mut u32): bool {
|
||||
val handle := process as u64;
|
||||
val left := deadline - now_ms();
|
||||
val ended := WaitForSingleObject(handle, left > 0 ? left as u32 : 0) == 0;
|
||||
if !ended {
|
||||
TerminateProcess(handle, 1);
|
||||
WaitForSingleObject(handle, infinite);
|
||||
}
|
||||
code = 1;
|
||||
GetExitCodeProcess(handle, code);
|
||||
CloseHandle(handle);
|
||||
return ended;
|
||||
}
|
||||
|
||||
#! The one spelling of a path that paths are compared by, since Windows spells
|
||||
#! a file many ways: absolute, without `.` and `..`, with `/` between names,
|
||||
#! the letter of its drive upper case, and each name that exists spelled as
|
||||
#! its directory spells it, which turns a short name, as `PROGRA~1`, into the
|
||||
#! long one, and gives each letter its case on disk. A share is
|
||||
#! `//server/share/...`, the server's name lower case, as a URI spells it. It
|
||||
#! ends in `/` only where it is the root of a drive. Links are not followed,
|
||||
#! so a path stays within the folder the editor named it by.
|
||||
#! @param given The path.
|
||||
#! @returns The path, or nothing where the system takes it for none.
|
||||
pub def canonical_path(given: strview): maybe<string> {
|
||||
return nothing {} if given.length == 0 || given.length >= wide_limit;
|
||||
var wide: u16[32768];
|
||||
var whole: u16[32768];
|
||||
var spelled: u16[32768];
|
||||
val named := (&mut wide) as *mut u16;
|
||||
val full := (&mut whole) as *mut u16;
|
||||
val out := (&mut spelled) as *mut u16;
|
||||
val units := MultiByteToWideChar(utf8_page, 0, given.data, given.length as i32, named, 32767);
|
||||
return nothing {} if units <= 0;
|
||||
named[units as u64] = 0;
|
||||
val length := GetFullPathNameW(named as *u16, wide_limit as u32, full, null) as u64;
|
||||
return nothing {} if length == 0 || length >= wide_limit;
|
||||
|
||||
# `\\?\D:\x` and `\\?\UNC\server\share` are taken as `D:\x` and
|
||||
# `\\server\share`.
|
||||
var start: u64 = 0;
|
||||
if begins_with(full as *u16, length, 0, "\\\\?\\UNC\\") {
|
||||
start = 6;
|
||||
full[6] = '\\' as u16;
|
||||
} else if begins_with(full as *u16, length, 0, "\\\\?\\") && is_wide_drive(full as *u16, length, 4) {
|
||||
start = 4;
|
||||
}
|
||||
|
||||
# The root, `D:\` or `\\server\share\`, is not looked up.
|
||||
var root := start;
|
||||
val shared := begins_with(full as *u16, length, start, "\\\\");
|
||||
if is_wide_drive(full as *u16, length, start) {
|
||||
root = start + (start + 2 < length && full[start + 2] == '\\' as u16 ? 3 : 2);
|
||||
} else if shared {
|
||||
var names := 0;
|
||||
root = start + 2;
|
||||
while root < length && names < 2 {
|
||||
if full[root] == '\\' as u16 {
|
||||
names++;
|
||||
}
|
||||
root++;
|
||||
}
|
||||
}
|
||||
|
||||
var made: u64 = 0;
|
||||
for i in start..root {
|
||||
out[made] = full[i];
|
||||
made++;
|
||||
}
|
||||
# The server's name is lower case, as a URI spells it.
|
||||
if shared {
|
||||
var i: u64 = 2;
|
||||
while i < made && out[i] != '\\' as u16 {
|
||||
if out[i] >= 'A' as u16 && out[i] <= 'Z' as u16 {
|
||||
out[i] = out[i] + 32;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
# Past a name that does not exist, the rest is kept as it is given. A name
|
||||
# with a wildcard would be looked up as a pattern, so it is not looked up.
|
||||
var found := true;
|
||||
var entry: u64[74];
|
||||
var at := root;
|
||||
while at < length {
|
||||
var end := at;
|
||||
while end < length && full[end] != '\\' as u16 {
|
||||
end++;
|
||||
}
|
||||
if end > at {
|
||||
found = found && !has_wildcard(full as *u16, at, end) && find_entry(full as *u16, end, (&mut entry) as *mut u8);
|
||||
if made > 0 && out[made - 1] != '\\' as u16 {
|
||||
out[made] = '\\' as u16;
|
||||
made++;
|
||||
}
|
||||
# The name found is 44 bytes into what `FindFirstFileExW` writes.
|
||||
val name := ((&entry) as *u8 + 44) as *u16;
|
||||
var count := end - at;
|
||||
if found {
|
||||
count = 0;
|
||||
while count < 260 && name[count] != 0 {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return nothing {} if made + count >= wide_limit;
|
||||
for i in 0..count {
|
||||
out[made] = found ? name[i] : full[at + i];
|
||||
made++;
|
||||
}
|
||||
}
|
||||
at = end + 1;
|
||||
}
|
||||
|
||||
for i in 0..made {
|
||||
if out[i] == '\\' as u16 {
|
||||
out[i] = '/' as u16;
|
||||
}
|
||||
}
|
||||
if is_wide_drive(out as *u16, made, 0) && out[0] >= 'a' as u16 {
|
||||
out[0] = out[0] - 32;
|
||||
}
|
||||
while made > 1 && out[made - 1] == '/' as u16 && !(made == 3 && is_wide_drive(out as *u16, made, 0)) {
|
||||
made--;
|
||||
}
|
||||
|
||||
var bytes: u8[98304];
|
||||
val written := WideCharToMultiByte(utf8_page, 0, out as *u16, made as i32, (&mut bytes) as *mut u8, 98304, null, null);
|
||||
return nothing {} if written <= 0;
|
||||
return string::from(strview::over((&bytes) as *u8, written as u64), nothing {});
|
||||
}
|
||||
|
||||
# Whether the units of UTF-16 from an index on begin with ASCII text.
|
||||
def begins_with(units: *u16, length: u64, at: u64, text: strview): bool {
|
||||
return false if at + text.length > length;
|
||||
for i in 0..text.length {
|
||||
return false if units[at + i] != text[i] as u16;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# Whether the units of UTF-16 from an index on begin with the letter of a
|
||||
# drive and its colon, as `C:`.
|
||||
def is_wide_drive(units: *u16, length: u64, at: u64): bool {
|
||||
return false if at + 2 > length;
|
||||
val letter := units[at];
|
||||
val is_letter := (letter >= 'a' as u16 && letter <= 'z' as u16) || (letter >= 'A' as u16 && letter <= 'Z' as u16);
|
||||
return is_letter && units[at + 1] == ':' as u16;
|
||||
}
|
||||
|
||||
# Whether a name, between two indexes of a path, holds what `FindFirstFileExW`
|
||||
# takes as a wildcard.
|
||||
def has_wildcard(units: *u16, from: u64, to: u64): bool {
|
||||
for i in from..to {
|
||||
val unit := units[i];
|
||||
return true if unit == '*' as u16 || unit == '?' as u16 || unit == '<' as u16 || unit == '>' as u16
|
||||
|| unit == '"' as u16;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Finds the entry the first units of a path name in its directory, writing it
|
||||
# as `FindFirstFileExW` gives it. A path too long for the functions of the
|
||||
# system to take as it is is looked up as a path of the whole file system,
|
||||
# `\\?\D:\x` or `\\?\UNC\server\share\x`.
|
||||
def find_entry(units: *u16, length: u64, entry: *mut u8): bool {
|
||||
var path: u16[32800];
|
||||
val named := (&mut path) as *mut u16;
|
||||
var at: u64 = 0;
|
||||
var from: u64 = 0;
|
||||
if length >= 248 && !begins_with(units, length, 0, "\\\\?\\") {
|
||||
val shared := begins_with(units, length, 0, "\\\\");
|
||||
val prefix: strview = shared ? "\\\\?\\UNC" : "\\\\?\\";
|
||||
for i in 0..prefix.length {
|
||||
named[at] = prefix[i] as u16;
|
||||
at++;
|
||||
}
|
||||
from = shared ? 1 : 0;
|
||||
}
|
||||
for i in from..length {
|
||||
named[at] = units[i];
|
||||
at++;
|
||||
}
|
||||
named[at] = 0;
|
||||
val find := FindFirstFileExW(named as *u16, basic_find, entry, 0, null, 0);
|
||||
return false if find == invalid_handle;
|
||||
FindClose(find);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#! Milliseconds since a moment the system chose, which only ever grow.
|
||||
#! @returns The time.
|
||||
pub def now_ms(): i64 {
|
||||
return system::clock(clock_kind::steady) / 1000000;
|
||||
}
|
||||
|
||||
#! Waits until a handle can be read, or has ended, for at most a while, as
|
||||
#! `system::poll` waits. On Windows, where it looks at a pipe again and again,
|
||||
#! waiting for as long as it takes is left to the read that follows instead,
|
||||
#! which waits without waking until something arrives.
|
||||
#! @param handle The handle.
|
||||
#! @param wait The most milliseconds to wait, or -1 to wait for as long as
|
||||
#! it takes.
|
||||
#! @returns More than 0 where it can be read, 0 where the wait ran out, and a
|
||||
#! negative error number where it failed.
|
||||
pub def wait_readable(handle: system::fd, wait: i64): i64 {
|
||||
when os == windows {
|
||||
return 1 if wait < 0;
|
||||
}
|
||||
var handles: system::fd[1];
|
||||
var ready: bool[1];
|
||||
handles[0] = handle;
|
||||
val watched := span::<system::fd>::over((&handles) as *system::fd, 1);
|
||||
return system::poll(watched, slice::<bool>::over((&mut ready) as *mut bool, 1), wait) as i64;
|
||||
}
|
||||
|
||||
#! Writes the whole of the text to a handle.
|
||||
#! @param handle The handle.
|
||||
#! @param text The text.
|
||||
#! @returns Whether all of it was written, which it is not where the other end
|
||||
#! is gone.
|
||||
pub def put_all(handle: system::fd, text: strview): bool {
|
||||
var written: u64 = 0;
|
||||
while written < text.length {
|
||||
val size := handle.write(text.data + written, text.length - written);
|
||||
if size == -4 {
|
||||
continue;
|
||||
}
|
||||
return false if size <= 0;
|
||||
written += size as u64;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Writes a number to a handle, as the eight bytes it is held in.
|
||||
#! @param handle The handle.
|
||||
#! @param value The number.
|
||||
#! @returns Whether it was written.
|
||||
pub def put_size(handle: system::fd, value: u64): bool {
|
||||
var held := value;
|
||||
return put_all(handle, strview::over((&held) as *u8, 8));
|
||||
}
|
||||
|
||||
#! Writes text to a handle, as its length and then its bytes.
|
||||
#! @param handle The handle.
|
||||
#! @param text The text.
|
||||
#! @returns Whether it was written.
|
||||
pub def put_text(handle: system::fd, text: strview): bool {
|
||||
return put_size(handle, text.length) && put_all(handle, text);
|
||||
}
|
||||
|
||||
#! Reads as many bytes as asked from a handle into the text, waiting until they
|
||||
#! arrive, or until a moment passes.
|
||||
#! @param handle The handle.
|
||||
#! @param into Where the bytes are appended.
|
||||
#! @param length How many bytes to read.
|
||||
#! @param deadline The moment to stop waiting, as `now_ms` gives it, or 0 to
|
||||
#! wait for as long as it takes.
|
||||
#! @returns Whether they all arrived.
|
||||
pub def get_all(handle: system::fd, into: &mut string, length: u64, deadline: i64): bool {
|
||||
var chunk: u8[16384];
|
||||
var left := length;
|
||||
while left != 0 {
|
||||
if deadline != 0 {
|
||||
val wait := deadline - now_ms();
|
||||
return false if wait <= 0;
|
||||
val ready := wait_readable(handle, wait);
|
||||
if ready == -4 {
|
||||
continue;
|
||||
}
|
||||
return false if ready <= 0;
|
||||
}
|
||||
val asked: u64 = left < 16384 ? left : 16384;
|
||||
val size := handle.read((&mut chunk) as *mut u8, asked);
|
||||
if size == -4 {
|
||||
continue;
|
||||
}
|
||||
return false if size <= 0;
|
||||
into.append(strview::over((&chunk) as *u8, size as u64));
|
||||
left -= size as u64;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Reads a number from a handle, as `put_size` writes one.
|
||||
#! @param handle The handle.
|
||||
#! @param out Where the number is written.
|
||||
#! @param deadline The moment to stop waiting, or 0.
|
||||
#! @returns Whether it arrived.
|
||||
pub def get_size(handle: system::fd, out: &mut u64, deadline: i64): bool {
|
||||
var bytes := string::new(nothing {});
|
||||
return false if !get_all(handle, &mut bytes, 8, deadline);
|
||||
out = *(bytes.view().data as *u64);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Reads text from a handle, as `put_text` writes it.
|
||||
#! @param handle The handle.
|
||||
#! @param out Where the text is written.
|
||||
#! @param deadline The moment to stop waiting, or 0.
|
||||
#! @returns Whether it arrived.
|
||||
pub def get_text(handle: system::fd, out: &mut string, deadline: i64): bool {
|
||||
var length: u64 = 0;
|
||||
return false if !get_size(handle, &mut length, deadline);
|
||||
out.clear();
|
||||
return get_all(handle, out, length, deadline);
|
||||
}
|
||||
|
||||
#! Reads everything a handle gives until it ends.
|
||||
#! @param handle The handle.
|
||||
#! @param into Where it is appended.
|
||||
pub def get_rest(handle: system::fd, into: &mut string): void {
|
||||
var chunk: u8[16384];
|
||||
while true {
|
||||
val size := handle.read((&mut chunk) as *mut u8, 16384);
|
||||
if size == -4 {
|
||||
continue;
|
||||
}
|
||||
break if size <= 0;
|
||||
into.append(strview::over((&chunk) as *u8, size as u64));
|
||||
}
|
||||
}
|
||||
|
||||
#! Reads everything a handle gives until it ends, or until a moment passes.
|
||||
#! @param handle The handle.
|
||||
#! @param into Where it is appended.
|
||||
#! @param deadline The moment to stop waiting, as `now_ms` gives it.
|
||||
#! @returns Whether it ended before the moment passed.
|
||||
pub def get_until(handle: system::fd, into: &mut string, deadline: i64): bool {
|
||||
var chunk: u8[16384];
|
||||
while true {
|
||||
val wait := deadline - now_ms();
|
||||
return false if wait <= 0;
|
||||
val ready := wait_readable(handle, wait);
|
||||
if ready == -4 {
|
||||
continue;
|
||||
}
|
||||
return true if ready < 0;
|
||||
return false if ready == 0;
|
||||
val size := handle.read((&mut chunk) as *mut u8, 16384);
|
||||
if size == -4 {
|
||||
continue;
|
||||
}
|
||||
return true if size <= 0;
|
||||
into.append(strview::over((&chunk) as *u8, size as u64));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! Waits for a process the server started to stop, and says how.
|
||||
#! @param process The process.
|
||||
#! @param status How it stopped.
|
||||
#! @returns Whether it was waited for.
|
||||
pub def reap(process: i64, status: &mut system::process_status): bool {
|
||||
while true {
|
||||
val result := system::wait(process, status);
|
||||
return result >= 0 if result != -4;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Reads the whole of a file.
|
||||
#! @param at The file's path.
|
||||
#! @param out Where its text is written.
|
||||
#! @returns Whether it was read.
|
||||
pub def read_text(at: strview, out: &mut string): bool {
|
||||
var found := path::parse(at) else (_) { return false; };
|
||||
var text := io::read_file(&found) else (_) { return false; };
|
||||
out = move(&mut text);
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The folder of a library the compiler brings with it, as `core`: the one of
|
||||
#! the name beside its executable, or in the nearest folder above that holds
|
||||
#! one, so a compiler built anywhere within a repository finds the
|
||||
#! repository's. No option or variable names it.
|
||||
#! @param name The folder's name.
|
||||
#! @returns Its path, or empty where there is none.
|
||||
pub def toolchain_folder(name: strview): string {
|
||||
val whole := executable_path();
|
||||
var end := whole.length;
|
||||
while end > 0 {
|
||||
end--;
|
||||
continue if whole[end] != '/' && !(is_windows && whole[end] == '\\');
|
||||
var tried := $"{whole.view().substr(0, end)}/{name}";
|
||||
return tried if is_folder(tried.view());
|
||||
}
|
||||
return string::new(nothing {});
|
||||
}
|
||||
|
||||
#! The standard library the compiler finds, as `--lib` would give it, where no
|
||||
#! `--lib` names `std` itself: a `std` folder beside the executable or in the
|
||||
#! nearest folder above it that holds one, as `core` is found, and otherwise
|
||||
#! each of std's libraries in the `share/sakura` folder nearest above it, where
|
||||
#! `blossom install` puts them, as `std.io` gives `std::io`. No option or
|
||||
#! variable names it.
|
||||
#! @param given The libraries `--lib` names, each as `NAME=DIR`.
|
||||
#! @returns Each library found, as `NAME=DIR`, in the order of their names:
|
||||
#! none where `--lib` names `std`, or where none is found.
|
||||
pub def found_std(given: span<strview>): vector<string> {
|
||||
var found := vector::<string>::new();
|
||||
for each in given {
|
||||
return found if each.starts_with("std=");
|
||||
}
|
||||
val tree := toolchain_folder("std");
|
||||
if tree.length != 0 {
|
||||
found.append($"std={tree}");
|
||||
return found;
|
||||
}
|
||||
val shelf := toolchain_folder("share/sakura");
|
||||
return found if shelf.length == 0;
|
||||
val at := path::parse(shelf.view()) else (_) { return found; };
|
||||
var listing := io::directory::open(&at) else (_) { return found; };
|
||||
var names := vector::<string>::new();
|
||||
while listing.next() is some(each) {
|
||||
continue if each.name != "std" && !each.name.starts_with("std.");
|
||||
continue if !is_folder($"{shelf}/{each.name}".view());
|
||||
names.append(string::from(each.name, nothing {}));
|
||||
}
|
||||
names.sort_by(def(a: &string, b: &string): bool { return a.view().compare(b.view()) < 0; });
|
||||
for &name in names {
|
||||
var named := string::new(nothing {});
|
||||
for byte in name.view() {
|
||||
if byte == '.' {
|
||||
named.append("::");
|
||||
} else {
|
||||
named.push(byte);
|
||||
}
|
||||
}
|
||||
found.append($"{named}={shelf}/{name}");
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
#! Whether a path names a folder, following a link it ends in.
|
||||
#! @param at The path.
|
||||
#! @returns Whether it does.
|
||||
pub def is_folder(at: strview): bool {
|
||||
var found := path::parse(at) else (_) { return false; };
|
||||
val info := io::info(&found) else (_) { return false; };
|
||||
return info.kind == file_kind::directory;
|
||||
}
|
||||
|
||||
when os == linux {
|
||||
# Whether the system spells paths with `\` as well as `/`.
|
||||
val is_windows: bool = false;
|
||||
}
|
||||
|
||||
when os == windows {
|
||||
# Whether the system spells paths with `\` as well as `/`.
|
||||
val is_windows: bool = true;
|
||||
}
|
||||
|
||||
#! The file the declarations of the types the syntax makes are shown from, which
|
||||
#! the compiler holds as text rather than in a file: written once, where the
|
||||
#! user's files of the moment are kept, so going to `string` opens it. Editing
|
||||
#! it changes nothing, since no check reads it.
|
||||
#! @returns Its path, or empty where it could not be written.
|
||||
pub def builtin_document(): string {
|
||||
var folder := string::new(nothing {});
|
||||
when os == windows {
|
||||
if process::variable("LOCALAPPDATA") is some(at) {
|
||||
folder = $"{at}/sakura";
|
||||
}
|
||||
} else {
|
||||
if process::variable("XDG_CACHE_HOME") is some(at) {
|
||||
folder = $"{at}/sakura";
|
||||
} else if process::variable("HOME") is some(at) {
|
||||
folder = $"{at}/.cache/sakura";
|
||||
}
|
||||
}
|
||||
return string::new(nothing {}) if folder.length == 0;
|
||||
var made := $"{folder}/built-in.sakura";
|
||||
var held := string::new(nothing {});
|
||||
return made if read_text(made.view(), &mut held) && held.view() == builtin_text;
|
||||
|
||||
var at := path::parse(folder.view()) else (_) { return string::new(nothing {}); };
|
||||
_ = io::create_directories(&at);
|
||||
var file := path::parse(made.view()) else (_) { return string::new(nothing {}); };
|
||||
return io::write_file(&file, builtin_text) is value(_) ? move(&mut made) : string::new(nothing {});
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
#! @file text.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains how the server writes and reads what the protocol carries: JSON,
|
||||
#! written whatever a file holds, positions, whose characters are counted in
|
||||
#! UTF-16 code units, the `file:` URIs files are named by, and the Markdown
|
||||
#! documentation is shown in.
|
||||
|
||||
import std::net;
|
||||
import std::json;
|
||||
import std::serial;
|
||||
import sakura::analyzer;
|
||||
module sakura::server;
|
||||
|
||||
|
||||
#! A writer of the JSON the server sends. It is lossy, writing bytes that are not
|
||||
#! valid UTF-8 as U+FFFD, so a message stays valid whatever a file holds.
|
||||
#! @returns The writer, which holds nothing yet.
|
||||
pub def message_writer(): json::writer {
|
||||
var made := json::writer::new();
|
||||
made.set_lossy(true);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Appends what a writer wrote, once its value is written whole.
|
||||
#! @param out Where it is appended.
|
||||
#! @param written The writer, which a lossy writer given nothing but integers
|
||||
#! and text never fails at.
|
||||
pub def append_written(out: &mut string, written: &mut json::writer): void {
|
||||
val text := written.finish() else (_) { return; };
|
||||
out.append(text);
|
||||
}
|
||||
|
||||
#! Writes Markdown as the protocol's content of that kind, as
|
||||
#! `{"kind":"markdown","value":"..."}`.
|
||||
#! @param out Where it is written.
|
||||
#! @param markdown The Markdown.
|
||||
pub def write_markdown(out: &mut json::writer, markdown: strview): void {
|
||||
out.begin_object();
|
||||
out.member("kind"); out.text("markdown");
|
||||
out.member("value"); out.text(markdown);
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! A position as the protocol gives one: a line, and a character counted in
|
||||
#! UTF-16 code units, both from 0.
|
||||
pub struct position {
|
||||
pub var line: u32; #! The line, from 0.
|
||||
pub var character: u32; #! The UTF-16 code unit within it, from 0.
|
||||
}
|
||||
|
||||
#! The byte of the text a position of the protocol is at.
|
||||
#! @param text The text.
|
||||
#! @param line The line, from 0.
|
||||
#! @param character The UTF-16 code unit within it, from 0.
|
||||
#! @returns The byte, which is the end of the line where the line is shorter.
|
||||
pub def locate(text: strview, line: u64, character: u64): u32 {
|
||||
var offset: u64 = 0;
|
||||
var at: u64 = 0;
|
||||
while at < line && offset < text.length {
|
||||
if text[offset] == '\n' {
|
||||
at++;
|
||||
}
|
||||
offset++;
|
||||
}
|
||||
var units: u64 = 0;
|
||||
return count_units(text, offset, text.length, character, &mut units) as u32;
|
||||
}
|
||||
|
||||
#! The position of the protocol a byte of the text is at.
|
||||
#! @param text The text.
|
||||
#! @param starts Where each line of the text begins, as its source found.
|
||||
#! @param offset The byte.
|
||||
#! @returns The position.
|
||||
pub def position_of(text: strview, starts: &lines, offset: u32): position {
|
||||
val end := (offset as u64) < text.length ? offset as u64 : text.length;
|
||||
val at := starts.place_of(end as u32);
|
||||
var units: u64 = 0;
|
||||
count_units(text, end + 1 - at.column as u64, end, 0xFFFFFFFFFFFFFFFF, &mut units);
|
||||
return position { line = at.line - 1, character = units as u32 };
|
||||
}
|
||||
|
||||
#! Counts the UTF-16 code units of a line's characters from a byte on, until a
|
||||
#! byte, a count of units, or the line's end, whichever comes first. A character
|
||||
#! of four bytes is two units, and a byte that breaks UTF-8 counts as a character
|
||||
#! of its own.
|
||||
#! @param text The text.
|
||||
#! @param from The byte the characters begin at.
|
||||
#! @param to The byte they end before, at the latest.
|
||||
#! @param most How many units are counted, at most.
|
||||
#! @param units Where the count is written.
|
||||
#! @returns The byte the characters counted end before.
|
||||
def count_units(text: strview, from: u64, to: u64, most: u64, units: &mut u64): u64 {
|
||||
var at := from;
|
||||
units = 0;
|
||||
while at < to && text[at] != '\n' && units < most {
|
||||
val size := serial::utf8_length(text, at);
|
||||
units += size == 4 ? 2 : 1;
|
||||
at += size != 0 ? size : 1;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
#! Writes a position of the protocol, as `{"line":0,"character":4}`.
|
||||
#! @param out Where it is written.
|
||||
#! @param text The text the position is of.
|
||||
#! @param starts Where each line of the text begins.
|
||||
#! @param offset The byte it is at.
|
||||
pub def write_position(out: &mut json::writer, text: strview, starts: &lines, offset: u32): void {
|
||||
val at := position_of(text, starts, offset);
|
||||
out.begin_object();
|
||||
out.member("line"); out.unsigned(at.line as u64);
|
||||
out.member("character"); out.unsigned(at.character as u64);
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
#! Writes a range of the protocol, from one byte of the text to another.
|
||||
#! @param out Where it is written.
|
||||
#! @param text The text.
|
||||
#! @param starts Where each line of the text begins.
|
||||
#! @param start The byte it begins at.
|
||||
#! @param end The byte it ends before.
|
||||
pub def write_range(out: &mut json::writer, text: strview, starts: &lines, start: u32, end: u32): void {
|
||||
out.begin_object();
|
||||
out.member("start"); write_position(out, text, starts, start);
|
||||
out.member("end"); write_position(out, text, starts, end);
|
||||
out.end_object();
|
||||
}
|
||||
|
||||
when os == linux {
|
||||
#! The path a `file:` URI names, as files of this machine are named, with an
|
||||
#! empty authority or `localhost`. A folder given with a trailing slash is
|
||||
#! named without it, so paths compare alike. A `%` that two hexadecimal digits
|
||||
#! do not follow is kept as it is.
|
||||
#! @param uri The URI.
|
||||
#! @returns The path, or nothing where the URI is of another scheme.
|
||||
pub def uri_path(uri: strview): maybe<string> {
|
||||
return nothing {} if !uri.starts_with("file://");
|
||||
var at: u64 = 7;
|
||||
while at < uri.length && uri[at] != '/' {
|
||||
at++;
|
||||
}
|
||||
return nothing {} if at >= uri.length;
|
||||
|
||||
var made := uri::decode_lenient(uri.substr(at, uri.length - at));
|
||||
while made.length > 1 && made[made.length - 1] == '/' {
|
||||
made = string::from(made.substr(0, made.length - 1), nothing {});
|
||||
}
|
||||
return move(&mut made);
|
||||
}
|
||||
|
||||
#! Appends the `file:` URI of a path, its bytes escaped where a URI does not
|
||||
#! take them as they are.
|
||||
#! @param out Where it is written.
|
||||
#! @param path The path.
|
||||
pub def append_uri(out: &mut string, path: strview): void {
|
||||
val escaped := uri::encode(path, "/");
|
||||
out.append("file://");
|
||||
out.append(escaped.view());
|
||||
}
|
||||
}
|
||||
|
||||
when os == windows {
|
||||
#! The path a `file:` URI names, spelled as `canonical_path` spells paths, so
|
||||
#! paths compare alike however the editor spells a file. An empty authority,
|
||||
#! or `localhost` before the path of a drive, names this machine, and any
|
||||
#! other the server of a share, whose files are `//server/share/...`: VS Code
|
||||
#! names `\\localhost\c$\x` `file://localhost/c%24/x`, and escapes the name of
|
||||
#! a server as it does the path, as `file://wsl%24/x`. A `%` that two
|
||||
#! hexadecimal digits do not follow is kept as it is.
|
||||
#! @param uri The URI.
|
||||
#! @returns The path, or nothing where the URI is of another scheme, or names
|
||||
#! no path.
|
||||
pub def uri_path(uri: strview): maybe<string> {
|
||||
return nothing {} if !uri.starts_with("file://");
|
||||
val decoded := uri::decode_lenient(uri.substr(7, uri.length - 7));
|
||||
var made := string::from("//", nothing {});
|
||||
made.append(decoded.view());
|
||||
|
||||
# `/c:/x`, as VS Code writes `file:///c%3A/x`, is the path `c:/x` of a
|
||||
# drive, and `/c:` is its root, `c:/`, since `c:` alone is the drive's
|
||||
# working directory.
|
||||
val text := made.view();
|
||||
var named: u64 = 2;
|
||||
while named < text.length && text[named] != '/' {
|
||||
named++;
|
||||
}
|
||||
val authority := text.substr(2, named - 2);
|
||||
val drive := named < text.length && is_drive(text, named + 1);
|
||||
val local := authority.length == 0 || (drive && authority.lower().view() == "localhost");
|
||||
val start: u64 = local ? named + (drive ? 1 : 0) : 0;
|
||||
var path := string::from(text.substr(start, text.length - start), nothing {});
|
||||
if path.length == 2 && is_drive(path.view(), 0) {
|
||||
path.push('/');
|
||||
}
|
||||
return canonical_path(path.view());
|
||||
}
|
||||
|
||||
#! Appends the `file:` URI of a path, as VS Code spells it: `file:///c%3A/x`
|
||||
#! for `C:/x`, and `file://server/x` for `//server/x`, the bytes of the rest
|
||||
#! escaped where a URI does not take them as they are.
|
||||
#! @param out Where it is written.
|
||||
#! @param path The path.
|
||||
pub def append_uri(out: &mut string, path: strview): void {
|
||||
out.append("file://");
|
||||
var from: u64 = 0;
|
||||
if path.starts_with("//") {
|
||||
from = 2;
|
||||
} else if is_drive(path, 0) {
|
||||
val letter: u8 = path[0];
|
||||
out.push('/');
|
||||
out.push(letter >= 'A' && letter <= 'Z' ? letter + 32 : letter);
|
||||
out.append("%3A");
|
||||
from = 2;
|
||||
}
|
||||
val escaped := uri::encode(path.substr(from, path.length - from), "/");
|
||||
out.append(escaped.view());
|
||||
}
|
||||
|
||||
# Whether text from a byte on begins with the letter of a drive and its
|
||||
# colon, as `C:`.
|
||||
def is_drive(text: strview, at: u64): bool {
|
||||
return false if at + 2 > text.length;
|
||||
val letter: u8 = text[at];
|
||||
return ((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z')) && text[at + 1] == ':';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#! Whether a file's name is that of Sakura source, ending in `.sakura`.
|
||||
#! @param name The name, or a path.
|
||||
#! @returns Whether it is.
|
||||
pub def is_sakura(name: strview): bool {
|
||||
return name.length > 7 && name.ends_with(".sakura");
|
||||
}
|
||||
|
||||
#! Appends a documentation comment as Markdown. Doxygen commands, as `@param`,
|
||||
#! begin paragraphs of their own, and the lines of a paragraph are joined.
|
||||
#! @param out Where it is written.
|
||||
#! @param documentation The comment, without its markers.
|
||||
pub def append_markdown(out: &mut string, documentation: strview): void {
|
||||
var first := true;
|
||||
var blank := false;
|
||||
var line: u64 = 0;
|
||||
while line < documentation.length {
|
||||
var stop := line;
|
||||
while stop < documentation.length && documentation[stop] != '\n' {
|
||||
stop++;
|
||||
}
|
||||
var at := line;
|
||||
while at < stop && (documentation[at] == ' ' || documentation[at] == '\t') {
|
||||
at++;
|
||||
}
|
||||
line = stop + 1;
|
||||
if at == stop {
|
||||
blank = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (documentation[at] == '@' || documentation[at] == '\\') && at + 1 < stop && is_word_byte(documentation[at + 1]) {
|
||||
at++;
|
||||
val name := at;
|
||||
while at < stop && is_word_byte(documentation[at]) {
|
||||
at++;
|
||||
}
|
||||
val command := documentation.substr(name, at - name);
|
||||
if !first {
|
||||
out.append("\n\n");
|
||||
}
|
||||
val param := command == "param" || command == "tparam";
|
||||
if param && at < stop && documentation[at] == '[' {
|
||||
while at < stop && documentation[at] != ']' {
|
||||
at++;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
while at < stop && (documentation[at] == ' ' || documentation[at] == '\t') {
|
||||
at++;
|
||||
}
|
||||
|
||||
if command == "brief" {
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
} else if param {
|
||||
val word := at;
|
||||
while at < stop && is_word_byte(documentation[at]) {
|
||||
at++;
|
||||
}
|
||||
out.append("*@");
|
||||
out.append(command);
|
||||
out.append("* `");
|
||||
out.append(documentation.substr(word, at - word));
|
||||
out.append("`");
|
||||
while at < stop && (documentation[at] == ' ' || documentation[at] == '\t') {
|
||||
at++;
|
||||
}
|
||||
if at < stop {
|
||||
out.append(" \xE2\x80\x94 ");
|
||||
}
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
} else {
|
||||
out.append("*@");
|
||||
out.append(command);
|
||||
out.append("*");
|
||||
if at < stop {
|
||||
out.append(" \xE2\x80\x94 ");
|
||||
}
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
}
|
||||
} else {
|
||||
if !first {
|
||||
out.append(blank ? "\n\n" : " ");
|
||||
}
|
||||
out.append(documentation.substr(at, stop - at));
|
||||
}
|
||||
first = false;
|
||||
blank = false;
|
||||
}
|
||||
}
|
||||
|
||||
#! Appends a signature as a block of Sakura code in Markdown.
|
||||
#! @param out Where it is written.
|
||||
#! @param signature The signature.
|
||||
pub def append_code(out: &mut string, signature: strview): void {
|
||||
out.append("```sakura\n");
|
||||
out.append(signature);
|
||||
out.append("\n```");
|
||||
}
|
||||
Reference in new issue
Block a user