From a2943b17b374696faff70fbb8c03b546a36942dc Mon Sep 17 00:00:00 2001 From: Koby Date: Fri, 15 Dec 2023 17:43:20 +0100 Subject: [PATCH] feat(lsp): accept option to disable code lens --- tooling/lsp/src/requests/mod.rs | 39 ++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/tooling/lsp/src/requests/mod.rs b/tooling/lsp/src/requests/mod.rs index d3032c06917..f4cc3b11b0a 100644 --- a/tooling/lsp/src/requests/mod.rs +++ b/tooling/lsp/src/requests/mod.rs @@ -4,6 +4,8 @@ use crate::types::{CodeLensOptions, InitializeParams}; use async_lsp::ResponseError; use lsp_types::{Position, TextDocumentSyncCapability, TextDocumentSyncKind}; use nargo_fmt::Config; +use serde::{Deserialize, Serialize}; + use crate::{ types::{InitializeResult, NargoCapability, NargoTestsOptions, ServerCapabilities}, @@ -32,16 +34,47 @@ pub(crate) use { test_run::on_test_run_request, tests::on_tests_request, }; +#[derive(Debug, Deserialize, Serialize)] +struct LspInitializationOptions { + + #[serde(rename = "enableCodeLens", default = "default_enable_code_lens")] + enable_code_lens: bool, +} + +fn default_enable_code_lens() -> bool { + true +} + +impl Default for LspInitializationOptions { + fn default() -> Self { + Self { + enable_code_lens: default_enable_code_lens(), + } + } +} + pub(crate) fn on_initialize( state: &mut LspState, params: InitializeParams, ) -> impl Future> { + state.root_path = params.root_uri.and_then(|root_uri| root_uri.to_file_path().ok()); - async { + let initialization_options = match params.initialization_options { + Some(initialization_options) => serde_json::from_value::(initialization_options).unwrap_or_default(), + None => LspInitializationOptions::default(), + }; + + async move { let text_document_sync = TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL); - let code_lens = CodeLensOptions { resolve_provider: Some(false) }; + + let code_lens = match initialization_options.enable_code_lens { + true => Some(CodeLensOptions { resolve_provider: Some(false) }), + false => None, + }; + + CodeLensOptions { resolve_provider: Some(false) }; let nargo = NargoCapability { tests: Some(NargoTestsOptions { @@ -54,7 +87,7 @@ pub(crate) fn on_initialize( Ok(InitializeResult { capabilities: ServerCapabilities { text_document_sync: Some(text_document_sync), - code_lens_provider: Some(code_lens), + code_lens_provider: code_lens, document_formatting_provider: true, nargo: Some(nargo), definition_provider: Some(lsp_types::OneOf::Left(true)),