From fbbc92af4ab5574c3dacaa916fe37c9531f2125a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Tue, 21 Dec 2021 12:09:45 +0100 Subject: [PATCH 1/5] feat(lsp): configurable diagnostic severity Allow severity of diagnostic messages to be configured. E.g. allow turning of Hint level diagnostics. Fixes: https://github.com/helix-editor/helix/issues/1007 --- helix-core/src/diagnostic.rs | 15 +++++++++++---- helix-core/src/indent.rs | 2 ++ helix-core/src/syntax.rs | 3 +++ helix-term/src/application.rs | 28 +++++++++++++++++++--------- helix-view/src/document.rs | 2 +- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/helix-core/src/diagnostic.rs b/helix-core/src/diagnostic.rs index 4fcf51c9cd31..210ad639197d 100644 --- a/helix-core/src/diagnostic.rs +++ b/helix-core/src/diagnostic.rs @@ -1,12 +1,19 @@ //! LSP diagnostic utility types. +use serde::{Deserialize, Serialize}; /// Describes the severity level of a [`Diagnostic`]. -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Deserialize, Serialize)] pub enum Severity { - Error, - Warning, - Info, Hint, + Info, + Warning, + Error, +} + +impl Default for Severity { + fn default() -> Self { + Self::Hint + } } /// A range of `char`s within the text. diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index b6f5081acd63..c2baf3ccc6d7 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -442,6 +442,7 @@ where ); let doc = Rope::from(doc); + use crate::diagnostic::Severity; use crate::syntax::{ Configuration, IndentationConfiguration, LanguageConfiguration, Loader, }; @@ -459,6 +460,7 @@ where roots: vec![], comment_token: None, auto_format: false, + diagnostic_severity: Severity::Warning, language_server: None, indent: Some(IndentationConfiguration { tab_width: 4, diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index ef35fc756d0d..cdae02103d59 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -1,5 +1,6 @@ use crate::{ chars::char_is_line_ending, + diagnostic::Severity, regex::Regex, transaction::{ChangeSet, Operation}, Rope, RopeSlice, Tendril, @@ -63,6 +64,8 @@ pub struct LanguageConfiguration { #[serde(default)] pub auto_format: bool, + #[serde(default)] + pub diagnostic_severity: Severity, // content_regex #[serde(default, skip_serializing, deserialize_with = "deserialize_regex")] diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 9a46a7fe6303..a902d0294e66 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -378,6 +378,7 @@ impl Application { let doc = self.editor.document_by_path_mut(&path); if let Some(doc) = doc { + let lang_conf = &doc.language; let text = doc.text(); let diagnostics = params @@ -415,19 +416,28 @@ impl Application { return None; }; + let severity = + diagnostic.severity.map(|severity| match severity { + DiagnosticSeverity::ERROR => Error, + DiagnosticSeverity::WARNING => Warning, + DiagnosticSeverity::INFORMATION => Info, + DiagnosticSeverity::HINT => Hint, + severity => unimplemented!("{:?}", severity), + }); + + if let Some(lang_conf) = lang_conf { + if let Some(severity) = severity { + if severity < lang_conf.diagnostic_severity { + return None; + } + } + }; + Some(Diagnostic { range: Range { start, end }, line: diagnostic.range.start.line as usize, message: diagnostic.message, - severity: diagnostic.severity.map( - |severity| match severity { - DiagnosticSeverity::ERROR => Error, - DiagnosticSeverity::WARNING => Warning, - DiagnosticSeverity::INFORMATION => Info, - DiagnosticSeverity::HINT => Hint, - severity => unimplemented!("{:?}", severity), - }, - ), + severity, // code // source }) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index c71d1850a35b..0b2d49131f34 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -88,7 +88,7 @@ pub struct Document { syntax: Option, // /// Corresponding language scope name. Usually `source.`. - pub(crate) language: Option>, + pub language: Option>, /// Pending changes since last history commit. changes: ChangeSet, From e30e3eb6e4c94562a4fbed11e1ce116dc0ff3348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Tue, 21 Dec 2021 15:47:41 +0100 Subject: [PATCH 2/5] Use language_config() method --- helix-term/src/application.rs | 2 +- helix-view/src/document.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index a902d0294e66..c5a08e33f40d 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -378,7 +378,7 @@ impl Application { let doc = self.editor.document_by_path_mut(&path); if let Some(doc) = doc { - let lang_conf = &doc.language; + let lang_conf = doc.language_config(); let text = doc.text(); let diagnostics = params diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 0b2d49131f34..c71d1850a35b 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -88,7 +88,7 @@ pub struct Document { syntax: Option, // /// Corresponding language scope name. Usually `source.`. - pub language: Option>, + pub(crate) language: Option>, /// Pending changes since last history commit. changes: ChangeSet, From 4e4ddad77234b203d41b66d04ac3414c1dd61e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Tue, 21 Dec 2021 17:47:27 +0100 Subject: [PATCH 3/5] Add documentation for diagnostic_severity --- book/src/guides/adding_languages.md | 25 +++++++++++++------------ book/src/languages.md | 1 - 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/book/src/guides/adding_languages.md b/book/src/guides/adding_languages.md index 987ad088c32b..cac8981772db 100644 --- a/book/src/guides/adding_languages.md +++ b/book/src/guides/adding_languages.md @@ -27,18 +27,19 @@ directory](../configuration.md). These are the available keys and descriptions for the file. -| Key | Description | -| ---- | ----------- | -| name | The name of the language | -| scope | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.` or `text.` in case of markup languages | -| injection-regex | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. | -| file-types | The filetypes of the language, for example `["yml", "yaml"]` | -| shebangs | The interpreters from the shebang line, for example `["sh", "bash"]` | -| roots | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` | -| auto-format | Whether to autoformat this language when saving | -| comment-token | The token to use as a comment-token | -| indent | The indent to use. Has sub keys `tab-width` and `unit` | -| config | Language server configuration | +| Key | Description | +| ---- | ----------- | +| name | The name of the language | +| scope | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.` or `text.` in case of markup languages | +| injection-regex | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. | +| file-types | The filetypes of the language, for example `["yml", "yaml"]` | +| shebangs | The interpreters from the shebang line, for example `["sh", "bash"]` | +| roots | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` | +| auto-format | Whether to autoformat this language when saving | +| diagnostic_severity | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) | +| comment-token | The token to use as a comment-token | +| indent | The indent to use. Has sub keys `tab-width` and `unit` | +| config | Language server configuration | ## Queries diff --git a/book/src/languages.md b/book/src/languages.md index cef61501f2d1..4c4dc326d6eb 100644 --- a/book/src/languages.md +++ b/book/src/languages.md @@ -11,4 +11,3 @@ Changes made to the `languages.toml` file in a user's [configuration directory]( name = "rust" auto-format = false ``` - From 73529dd1dd676d4aa30d13a90468dd66a31e81b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Wed, 22 Dec 2021 08:40:02 +0100 Subject: [PATCH 4/5] Use unreachable for unknown severity level --- helix-term/src/application.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index c5a08e33f40d..c7202febadb0 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -422,7 +422,10 @@ impl Application { DiagnosticSeverity::WARNING => Warning, DiagnosticSeverity::INFORMATION => Info, DiagnosticSeverity::HINT => Hint, - severity => unimplemented!("{:?}", severity), + severity => unreachable!( + "unrecognized diagnostic severity: {:?}", + severity + ), }); if let Some(lang_conf) = lang_conf { From 40516d4d750f585a222c592c2c8d556c8429bfde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Wed, 22 Dec 2021 17:12:28 +0100 Subject: [PATCH 5/5] fix: documentation for diagnostic_severity config --- book/src/guides/adding_languages.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/src/guides/adding_languages.md b/book/src/guides/adding_languages.md index cac8981772db..5844a48eeff0 100644 --- a/book/src/guides/adding_languages.md +++ b/book/src/guides/adding_languages.md @@ -36,7 +36,7 @@ These are the available keys and descriptions for the file. | shebangs | The interpreters from the shebang line, for example `["sh", "bash"]` | | roots | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` | | auto-format | Whether to autoformat this language when saving | -| diagnostic_severity | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) | +| diagnostic-severity | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) | | comment-token | The token to use as a comment-token | | indent | The indent to use. Has sub keys `tab-width` and `unit` | | config | Language server configuration |