on master help sign in
feat(lsp): configurable diagnostic severity (#1325)

* 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 * Use language_config() method * Add documentation for diagnostic_severity * Use unreachable for unknown severity level * fix: documentation for diagnostic_severity config

commit0e7d757869bbae914a7e832e2511c2071eeacee5 authorMatouš Dzivjak <matousdzivjak@gmail.com> dateSat 25 Dec 2021, 06:32 (5 years ago) parent60f3225
6 files changed+51−26
book/src/guides/adding_languages.md+13−12 book/src/languages.md−1 helix-core/src/diagnostic.rs+11−4 helix-core/src/indent.rs+2 helix-core/src/syntax.rs+3 helix-term/src/application.rs+22−9
book/src/guides/adding_languages.md+13−12
@@ -27,18 +27,19 @@ directory](../configuration.md).
2727
2828These are the available keys and descriptions for the file.
2929
30−| Key | Description |
31−| ---- | ----------- |
32−| name | The name of the language |
33−| 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.<name>` or `text.<name>` in case of markup languages |
34−| 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. |
35−| file-types | The filetypes of the language, for example `["yml", "yaml"]` |
36−| shebangs | The interpreters from the shebang line, for example `["sh", "bash"]` |
37−| roots | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` |
38−| auto-format | Whether to autoformat this language when saving |
39−| comment-token | The token to use as a comment-token |
40−| indent | The indent to use. Has sub keys `tab-width` and `unit` |
41−| config | Language server configuration |
30+| Key | Description |
31+| ---- | ----------- |
32+| name | The name of the language |
33+| 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.<name>` or `text.<name>` in case of markup languages |
34+| 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. |
35+| file-types | The filetypes of the language, for example `["yml", "yaml"]` |
36+| shebangs | The interpreters from the shebang line, for example `["sh", "bash"]` |
37+| roots | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` |
38+| auto-format | Whether to autoformat this language when saving |
39+| diagnostic-severity | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) |
40+| comment-token | The token to use as a comment-token |
41+| indent | The indent to use. Has sub keys `tab-width` and `unit` |
42+| config | Language server configuration |
4243
4344## Queries
4445
book/src/languages.md−1
@@ -11,4 +11,3 @@ Changes made to the `languages.toml` file in a user's [configuration directory](
1111name = "rust"
1212auto-format = false
1313```
14−
helix-core/src/diagnostic.rs+11−4
@@ -1,12 +1,19 @@
11//! LSP diagnostic utility types.
2+use serde::{Deserialize, Serialize};
23
34/// Describes the severity level of a [`Diagnostic`].
4−#[derive(Debug, Clone, Copy, Eq, PartialEq)]
5+#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Deserialize, Serialize)]
56pub enum Severity {
6− Error,
7− Warning,
8− Info,
97 Hint,
8+ Info,
9+ Warning,
10+ Error,
11+}
12+
13+impl Default for Severity {
14+ fn default() -> Self {
15+ Self::Hint
16+ }
1017}
1118
1219/// A range of `char`s within the text.
helix-core/src/indent.rs+2
@@ -442,6 +442,7 @@ where
442442 );
443443
444444 let doc = Rope::from(doc);
445+ use crate::diagnostic::Severity;
445446 use crate::syntax::{
446447 Configuration, IndentationConfiguration, LanguageConfiguration, Loader,
447448 };
@@ -459,6 +460,7 @@ where
459460 roots: vec![],
460461 comment_token: None,
461462 auto_format: false,
463+ diagnostic_severity: Severity::Warning,
462464 language_server: None,
463465 indent: Some(IndentationConfiguration {
464466 tab_width: 4,
helix-core/src/syntax.rs+3
@@ -1,5 +1,6 @@
11use crate::{
22 chars::char_is_line_ending,
3+ diagnostic::Severity,
34 regex::Regex,
45 transaction::{ChangeSet, Operation},
56 Rope, RopeSlice, Tendril,
@@ -63,6 +64,8 @@ pub struct LanguageConfiguration {
6364
6465 #[serde(default)]
6566 pub auto_format: bool,
67+ #[serde(default)]
68+ pub diagnostic_severity: Severity,
6669
6770 // content_regex
6871 #[serde(default, skip_serializing, deserialize_with = "deserialize_regex")]
helix-term/src/application.rs+22−9
@@ -378,6 +378,7 @@ impl Application {
378378 let doc = self.editor.document_by_path_mut(&path);
379379
380380 if let Some(doc) = doc {
381+ let lang_conf = doc.language_config();
381382 let text = doc.text();
382383
383384 let diagnostics = params
@@ -415,19 +416,31 @@ impl Application {
415416 return None;
416417 };
417418
419+ let severity =
420+ diagnostic.severity.map(|severity| match severity {
421+ DiagnosticSeverity::ERROR => Error,
422+ DiagnosticSeverity::WARNING => Warning,
423+ DiagnosticSeverity::INFORMATION => Info,
424+ DiagnosticSeverity::HINT => Hint,
425+ severity => unreachable!(
426+ "unrecognized diagnostic severity: {:?}",
427+ severity
428+ ),
429+ });
430+
431+ if let Some(lang_conf) = lang_conf {
432+ if let Some(severity) = severity {
433+ if severity < lang_conf.diagnostic_severity {
434+ return None;
435+ }
436+ }
437+ };
438+
418439 Some(Diagnostic {
419440 range: Range { start, end },
420441 line: diagnostic.range.start.line as usize,
421442 message: diagnostic.message,
422− severity: diagnostic.severity.map(
423− |severity| match severity {
424− DiagnosticSeverity::ERROR => Error,
425− DiagnosticSeverity::WARNING => Warning,
426− DiagnosticSeverity::INFORMATION => Info,
427− DiagnosticSeverity::HINT => Hint,
428− severity => unimplemented!("{:?}", severity),
429− },
430− ),
443+ severity,
431444 // code
432445 // source
433446 })