aiken_lsp/
lib.rs

1use crate::server::Server;
2use aiken_project::{config::ProjectConfig, paths};
3use error::Error;
4use lsp_server::Connection;
5use std::env;
6
7mod cast;
8mod edits;
9pub mod error;
10mod quickfix;
11pub mod server;
12pub mod utils;
13
14#[allow(clippy::result_large_err)]
15pub fn start() -> Result<(), Error> {
16    tracing::info!("Aiken language server starting");
17
18    // Forcibly disable colors on outputs for LSP
19    owo_colors::set_override(false);
20
21    let root = env::current_dir()?;
22
23    let config = if paths::project_config().exists() {
24        tracing::info!("Aiken project detected");
25
26        Some(ProjectConfig::load(&root).expect("failed to load aiken.toml"))
27    } else {
28        tracing::info!("Aiken project config not found");
29
30        None
31    };
32
33    // Create the transport. Includes the stdio (stdin and stdout) versions but this could
34    // also be implemented to use sockets or HTTP.
35    let (connection, io_threads) = Connection::stdio();
36
37    // Run the server and wait for the two threads to end (typically by trigger LSP Exit event).
38    let server_capabilities = serde_json::to_value(capabilities())?;
39
40    let initialization_params = connection.initialize(server_capabilities)?;
41    let initialize_params = serde_json::from_value(initialization_params)?;
42
43    let mut server = Server::new(initialize_params, config, root);
44
45    server.listen(connection)?;
46
47    io_threads.join()?;
48
49    tracing::info!("Aiken language server shutting down");
50
51    Ok(())
52}
53
54fn capabilities() -> lsp_types::ServerCapabilities {
55    lsp_types::ServerCapabilities {
56        // THIS IS STILL WEIRD, ONLY ENABLE IF DEVELOPING
57        // completion_provider: Some(lsp_types::CompletionOptions {
58        //     resolve_provider: None,
59        //     trigger_characters: Some(vec![".".into(), " ".into()]),
60        //     all_commit_characters: None,
61        //     work_done_progress_options: lsp_types::WorkDoneProgressOptions {
62        //         work_done_progress: None,
63        //     },
64        // }),
65        code_action_provider: Some(lsp_types::CodeActionProviderCapability::Simple(true)),
66        document_formatting_provider: Some(lsp_types::OneOf::Left(true)),
67        definition_provider: Some(lsp_types::OneOf::Left(true)),
68        hover_provider: Some(lsp_types::HoverProviderCapability::Simple(true)),
69        text_document_sync: Some(lsp_types::TextDocumentSyncCapability::Options(
70            lsp_types::TextDocumentSyncOptions {
71                open_close: None,
72                change: Some(lsp_types::TextDocumentSyncKind::FULL),
73                will_save: None,
74                will_save_wait_until: None,
75                save: Some(lsp_types::TextDocumentSyncSaveOptions::SaveOptions(
76                    lsp_types::SaveOptions {
77                        include_text: Some(false),
78                    },
79                )),
80            },
81        )),
82        ..Default::default()
83    }
84}