From 0dcb1cb569417a17e27a4d8b34813ded41395268 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Wed, 6 Mar 2019 11:34:38 +0200 Subject: Add showWorkspaceLoadedNotification to vscode client This allows users to control whether or not they want to see the "workspace loaded" notification. This is done on the server side using InitializationOptions which are provided by the client. By default show_workspace_loaded is true, meaning the notification is sent. --- crates/ra_lsp_server/src/init.rs | 39 +++++++++++++++++++++++ crates/ra_lsp_server/src/lib.rs | 3 +- crates/ra_lsp_server/src/main.rs | 20 ++++-------- crates/ra_lsp_server/src/main_loop.rs | 13 +++++--- crates/ra_lsp_server/tests/heavy_tests/support.rs | 9 +++++- 5 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 crates/ra_lsp_server/src/init.rs (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/init.rs b/crates/ra_lsp_server/src/init.rs new file mode 100644 index 000000000..0b7a47a0b --- /dev/null +++ b/crates/ra_lsp_server/src/init.rs @@ -0,0 +1,39 @@ +use serde::{Deserialize, Deserializer}; + +/// Client provided initialization options +#[derive(Deserialize, Clone, Copy, Debug)] +#[serde(rename_all = "camelCase")] +pub struct InitializationOptions { + /// Whether the client supports our custom highlighting publishing decorations. + /// This is different to the highlightingOn setting, which is whether the user + /// wants our custom highlighting to be used. + /// + /// Defaults to `true` + #[serde(default = "bool_true", deserialize_with = "nullable_bool_true")] + pub publish_decorations: bool, + + /// Whether or not the workspace loaded notification should be sent + /// + /// Defaults to `true` + #[serde(default = "bool_true", deserialize_with = "nullable_bool_true")] + pub show_workspace_loaded: bool, +} + +impl Default for InitializationOptions { + fn default() -> InitializationOptions { + InitializationOptions { publish_decorations: true, show_workspace_loaded: true } + } +} + +fn bool_true() -> bool { + true +} + +/// Deserializes a null value to a bool true by default +fn nullable_bool_true<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let opt = Option::deserialize(deserializer)?; + Ok(opt.unwrap_or(true)) +} diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index 5b5f3b948..59e16a47c 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -5,7 +5,8 @@ mod main_loop; mod markdown; mod project_model; pub mod req; +pub mod init; mod server_world; pub type Result = ::std::result::Result; -pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError}; +pub use crate::{caps::server_capabilities, main_loop::main_loop, main_loop::LspError, init::InitializationOptions}; diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs index 9dc2e3f87..5a2905207 100644 --- a/crates/ra_lsp_server/src/main.rs +++ b/crates/ra_lsp_server/src/main.rs @@ -2,7 +2,7 @@ use serde::Deserialize; use flexi_logger::{Duplicate, Logger}; use gen_lsp_server::{run_server, stdio_transport}; -use ra_lsp_server::Result; +use ra_lsp_server::{Result, InitializationOptions}; fn main() -> Result<()> { ::std::env::set_var("RUST_BACKTRACE", "short"); @@ -24,26 +24,18 @@ fn main() -> Result<()> { } } -#[derive(Deserialize)] -#[serde(rename_all = "camelCase")] -struct InitializationOptions { - // Whether the client supports our custom highlighting publishing decorations. - // This is different to the highlightingOn setting, which is whether the user - // wants our custom highlighting to be used. - publish_decorations: Option, -} - fn main_inner() -> Result<()> { let (receiver, sender, threads) = stdio_transport(); let cwd = ::std::env::current_dir()?; run_server(ra_lsp_server::server_capabilities(), receiver, sender, |params, r, s| { let root = params.root_uri.and_then(|it| it.to_file_path().ok()).unwrap_or(cwd); - let supports_decorations = params + + let opts = params .initialization_options .and_then(|v| InitializationOptions::deserialize(v).ok()) - .and_then(|it| it.publish_decorations) - == Some(true); - ra_lsp_server::main_loop(root, supports_decorations, r, s) + .unwrap_or(InitializationOptions::default()); + + ra_lsp_server::main_loop(root, opts, r, s) })?; log::info!("shutting down IO..."); threads.join()?; diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index fb2305b26..d0c2a95ef 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -22,6 +22,7 @@ use crate::{ req, server_world::{ServerWorld, ServerWorldState}, Result, + InitializationOptions, }; #[derive(Debug, Fail)] @@ -47,7 +48,7 @@ const THREADPOOL_SIZE: usize = 8; pub fn main_loop( ws_root: PathBuf, - supports_decorations: bool, + options: InitializationOptions, msg_receiver: &Receiver, msg_sender: &Sender, ) -> Result<()> { @@ -80,7 +81,7 @@ pub fn main_loop( let mut pending_requests = FxHashSet::default(); let mut subs = Subscriptions::new(); let main_res = main_loop_inner( - supports_decorations, + options, &pool, msg_sender, msg_receiver, @@ -147,7 +148,7 @@ impl fmt::Debug for Event { } fn main_loop_inner( - supports_decorations: bool, + options: InitializationOptions, pool: &ThreadPool, msg_sender: &Sender, msg_receiver: &Receiver, @@ -247,7 +248,9 @@ fn main_loop_inner( && pending_libraries.is_empty() && in_flight_libraries == 0 { - show_message(req::MessageType::Info, "workspace loaded", msg_sender); + if options.show_workspace_loaded { + show_message(req::MessageType::Info, "workspace loaded", msg_sender); + } // Only send the notification first time send_workspace_notification = false; } @@ -256,7 +259,7 @@ fn main_loop_inner( update_file_notifications_on_threadpool( pool, state.snapshot(), - supports_decorations, + options.publish_decorations, task_sender.clone(), subs.subscriptions(), ) diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index 08f7ad6fd..8bfc8d622 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs @@ -23,6 +23,7 @@ use test_utils::{parse_fixture, find_mismatch}; use ra_lsp_server::{ main_loop, req, + InitializationOptions, }; pub fn project(fixture: &str) -> Server { @@ -57,7 +58,13 @@ impl Server { "test server", 128, move |mut msg_receiver, mut msg_sender| { - main_loop(path, true, &mut msg_receiver, &mut msg_sender).unwrap() + main_loop( + path, + InitializationOptions::default(), + &mut msg_receiver, + &mut msg_sender, + ) + .unwrap() }, ); let res = Server { -- cgit v1.2.3