Skip to main content

Crate lspresso

Crate lspresso 

Source
Expand description

§lspresso

Use existing test helpers and benchmarking tools to validate and performance and correctness!

an orange axolotl in a blue coffee cup falling over. coffee is spilling out and the axolotl looks distressed

use lspresso::{Client, capabilities, gen_lsp_types::*};

let mut client = Client::spawn_initialized(
    Command::new("my-lsp"),
    // use capabilities captured from real editors
    // emacs, helix, neovim, and vscode are supported
    capabilities::vscode::latest(),
)?
// default 15s, set to a reasonable upper bound so test timeouts are short
.with_timeout(Duration::from_secs(15));
let uri: Uri = "hi.json".parse()?;

// convenience methods provide a simpler API
client.did_open(uri.clone(), "{}", LanguageKind::JSON)?;

// for example, `Client::diagnostics` pulls when the server supports it and
// waits for pushed diagnostics otherwise
let diags = client.diagnostics(&uri)?;
assert_eq!(diags, vec![]);

// if you need lower level control, import the `Protocol` trait
// each method is prefixed with `protocol_` to differentiate from the
// convenience layer
use lspresso::Protocol as _;

// pull diagnostics
let diags = client.protocol_diagnostic(DocumentDiagnosticParams {
    text_document: TextDocumentIdentifier { uri: uri.clone() },
    identifier: Some("my-lsp".into()),
    previous_result_id: Some("diagnostics-1".into()),
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: Some(ProgressToken::String("work-done-1".into())),
    },
    partial_result_params: PartialResultParams {
        partial_result_token: Some(ProgressToken::String("partial-1".into())),
    },
})?;

// push diagnostics wait for a notification or the timeout
let published =
    client.wait_for_notification_where::<PublishDiagnosticsNotification>(|p| p.uri == uri)?;
assert!(published.diagnostics.is_empty());

§Benchmarking

Use existing benchmarking tools like criterion with Client::timings. Timings::server is time spent in the language server. request and response are worth a look too since large messages take time

fn bench_diagnostics(c: &mut Criterion) {
    let mut client =
        Client::spawn_initialized(Command::new("my-lsp"), capabilities::vscode::latest())
            .unwrap();
    let uri: Uri = "bench.json".parse().unwrap();
    client
        .did_open(uri.clone(), r#"{"a": [1, 2, 3]}"#, LanguageKind::JSON)
        .unwrap();

    c.bench_function("diagnostics", |b| {
        b.iter_custom(|iters| {
            let before = client.timings();
            for _ in 0..iters {
                client.diagnostics(&uri).unwrap();
            }
            client.timings().since(before).server
        });
    });

    // peak RSS over the session
    let rss = client.rss_bytes().expect("the server is running");
    println!("server RSS: {} MiB", rss / (1024 * 1024));
}

criterion_group!(benches, bench_diagnostics);
criterion_main!(benches);

§limitations

  • server to client requests get a canned response. They could be scriptable and assertable
  • stdout is supported but not node ipc or tcp

Re-exports§

pub use gen_lsp_types;

Modules§

capabilities
editor capabilities captured from real editors

Structs§

Client
Error
{0}
Initialize
Timings
measures granular time spent receiving, processing, and responding to requests. Real clients vary in speed so split out the language server’s work from IO

Enums§

Awaited
What the client was waiting for when the connection broke
ErrorKind

Traits§

Protocol
Every client-to-server LSP method, taking its raw params.

Functions§

server_command
A command for a prebuilt server. env_var overrides default, so a test or bench can point at a different build without code changes

Type Aliases§

Result