diff --git a/README.md b/README.md index f235d99..3c1eaf0 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,7 @@ Editors can pass `initializationOptions` when starting the Codebook LSP for LSP- - `logLevel` (`"trace" | "debug" | "info" | "warn" | "error"`, default `"info"`): sets the verbosity of logs. - `globalConfigPath` (string): overrides the auto-detected global `codebook.toml` path, useful if you sync configs from another location. On macOS and Linux, the `~/` prefix for the current user's home directory is supported. - `checkWhileTyping` (bool, default `true`): when `false`, spelling diagnostics are only published on save instead of each keystroke. This is useful for example if performance is a problem, or the real-time diagnostics are annoying (sorry!). +- `diagnosticSeverity` (`"error" | "warning" | "information" | "hint"`, default `"information"`): sets the severity of spell check diagnostics. Example payload: @@ -346,7 +347,8 @@ Example payload: { "logLevel": "debug", "globalConfigPath": "~/dotfiles/codebook.toml", - "checkWhileTyping": false + "checkWhileTyping": false, + "diagnosticSeverity": "information" } ``` diff --git a/crates/codebook-lsp/src/init_options.rs b/crates/codebook-lsp/src/init_options.rs index 09e2e35..9a574a3 100644 --- a/crates/codebook-lsp/src/init_options.rs +++ b/crates/codebook-lsp/src/init_options.rs @@ -3,6 +3,7 @@ use serde::Deserialize; use serde::de::Deserializer; use serde_json::Value; use std::path::PathBuf; +use tower_lsp::lsp_types::DiagnosticSeverity; fn default_log_level() -> LevelFilter { LevelFilter::Info @@ -44,6 +45,24 @@ fn default_check_while_typing() -> bool { true } +fn default_diagnostic_severity() -> DiagnosticSeverity { + DiagnosticSeverity::INFORMATION +} + +fn deserialize_diagnostic_severity<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let s: Option = Option::deserialize(deserializer)?; + match s.as_deref() { + Some("error") => Ok(DiagnosticSeverity::ERROR), + Some("warning") => Ok(DiagnosticSeverity::WARNING), + Some("information") => Ok(DiagnosticSeverity::INFORMATION), + Some("hint") => Ok(DiagnosticSeverity::HINT), + _ => Ok(default_diagnostic_severity()), + } +} + #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub(crate) struct ClientInitializationOptions { @@ -56,6 +75,11 @@ pub(crate) struct ClientInitializationOptions { pub(crate) global_config_path: Option, #[serde(default = "default_check_while_typing")] pub(crate) check_while_typing: bool, + #[serde( + default = "default_diagnostic_severity", + deserialize_with = "deserialize_diagnostic_severity" + )] + pub(crate) diagnostic_severity: DiagnosticSeverity, } impl Default for ClientInitializationOptions { @@ -64,6 +88,7 @@ impl Default for ClientInitializationOptions { log_level: default_log_level(), global_config_path: None, check_while_typing: true, + diagnostic_severity: default_diagnostic_severity(), } } } @@ -109,9 +134,11 @@ mod tests { #[test] fn test_json() { - let json = r#"{"logLevel": "debug", "checkWhileTyping": false}"#; + let json = + r#"{"logLevel": "debug", "checkWhileTyping": false, "diagnosticSeverity": "warning"}"#; let options: ClientInitializationOptions = serde_json::from_str(json).unwrap(); assert_eq!(options.log_level, LevelFilter::Debug); assert!(!options.check_while_typing); + assert_eq!(options.diagnostic_severity, DiagnosticSeverity::WARNING); } } diff --git a/crates/codebook-lsp/src/lsp.rs b/crates/codebook-lsp/src/lsp.rs index 13c2ebe..bbd0890 100644 --- a/crates/codebook-lsp/src/lsp.rs +++ b/crates/codebook-lsp/src/lsp.rs @@ -354,7 +354,7 @@ impl Backend { character: end_pos.col as u32, }, }, - severity: Some(DiagnosticSeverity::INFORMATION), + severity: Some(self.initialize_options.read().unwrap().diagnostic_severity), code: None, code_description: None, source: Some(SOURCE_NAME.to_string()),