From 7e7e74397e77bc23b07e3d10ea863af4cdc1dccb Mon Sep 17 00:00:00 2001 From: Oliver Mannion <125105+tekumara@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:51:22 +1100 Subject: [PATCH] feat: configurable diagnostic severity resolves #17 --- .github/workflows/release.yml | 2 +- README.md | 3 ++- crates/typos-lsp/src/lsp.rs | 31 ++++++++++++++++++++-- crates/typos-lsp/tests/integration_test.rs | 6 +++++ package.json | 18 +++++++++++++ src/extension.ts | 11 +++++--- 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 55bc205..8d1083b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,7 +9,7 @@ jobs: wait: runs-on: ubuntu-latest steps: - - name: Wait for build check to succeed + - name: Wait for checks to succeed uses: poseidon/wait-for-status-checks@v0.3.0 with: token: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index 7899f7e..d461220 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ Config files will be read from the workspace folder or its parents. If there is This extension contributes the following settings: -- `typos.logLevel`: Logging level of the language server. +- `typos.diagnosticSeverity`: How typos are rendered in the editor, eg: as errors, warnings, information, or hints. +- `typos.logLevel`: Logging level of the language server. Logs appear in the Output -> Typos pane. - `typos.path`: Path to the `typos-lsp` binary. If empty the bundled binary will be used. - `typos.trace.server`: Traces the communication between VS Code and the language server. diff --git a/crates/typos-lsp/src/lsp.rs b/crates/typos-lsp/src/lsp.rs index bcfccb1..1097af4 100644 --- a/crates/typos-lsp/src/lsp.rs +++ b/crates/typos-lsp/src/lsp.rs @@ -22,6 +22,7 @@ pub struct Backend<'s, 'p> { #[derive(Default)] struct BackendState<'s> { + severity: Option, workspace_folders: Vec, router: Router>, } @@ -138,11 +139,37 @@ impl LanguageServer for Backend<'static, 'static> { tracing::debug!("Client supports diagnostics data") } else { tracing::warn!( - "Client does not support diagnostics data.. code actions will not be available" + "Client does not support diagnostics data. Code actions will not be available" ) } let mut state = self.state.lock().unwrap(); + + if let Some(ops) = params.initialization_options { + if let Some(value) = ops + .as_object() + .and_then(|o| o.get("diagnosticSeverity").cloned()) + { + match value.as_str().unwrap_or("").to_lowercase().as_str() { + "error" => { + state.severity = Some(DiagnosticSeverity::ERROR); + } + "warning" => { + state.severity = Some(DiagnosticSeverity::WARNING); + } + "information" => { + state.severity = Some(DiagnosticSeverity::INFORMATION); + } + "hint" => { + state.severity = Some(DiagnosticSeverity::HINT); + } + _ => { + tracing::warn!("Unknown diagnostic severity: {}", value); + } + } + } + } + if let Err(e) = state.set_workspace_folders(params.workspace_folders.unwrap_or_default()) { tracing::warn!("Cannot set workspace folders: {}", e); } @@ -362,7 +389,7 @@ impl<'s, 'p> Backend<'s, 'p> { Position::new(line_num as u32, line_pos as u32), Position::new(line_num as u32, (line_pos + typo.typo.len()) as u32), ), - severity: Some(DiagnosticSeverity::WARNING), + severity: state.severity, source: Some("typos".to_string()), message: match &typo.corrections { typos::Status::Invalid => format!("`{}` is disallowed", typo.typo), diff --git a/crates/typos-lsp/tests/integration_test.rs b/crates/typos-lsp/tests/integration_test.rs index d8dd5ff..43a0539 100644 --- a/crates/typos-lsp/tests/integration_test.rs +++ b/crates/typos-lsp/tests/integration_test.rs @@ -30,6 +30,9 @@ async fn test_e2e() { "jsonrpc": "2.0", "method": "initialize", "params": { + "initializationOptions": { + "diagnosticSeverity": "Warning" + }, "capabilities": { "textDocument": { "publishDiagnostics": { "dataSupport": true } } } @@ -202,6 +205,9 @@ async fn test_config_file_e2e() { "jsonrpc": "2.0", "method": "initialize", "params": {{ + "initializationOptions": {{ + "diagnosticSeverity": "Warning" + }}, "capabilities": {{ "textDocument": {{ "publishDiagnostics": {{ "dataSupport": true }} }} }}, diff --git a/package.json b/package.json index cf7a842..cdc0da9 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,24 @@ "type": "string", "description": "Path to the `typos-lsp` binary. If empty the bundled binary will be used." }, + "typos.diagnosticSeverity": { + "scope": "window", + "type": "string", + "enum": [ + "Error", + "Warning", + "Information", + "Hint" + ], + "enumDescriptions": [ + "Red squiggle", + "Yellow squiggle", + "Blue squiggle", + "Dots" + ], + "default": "Warning", + "description": "How typos are rendered in the editor." + }, "typos.logLevel": { "scope": "window", "type": "string", diff --git a/src/extension.ts b/src/extension.ts index e41c81b..561480d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -24,9 +24,11 @@ export async function activate( context.subscriptions.push( vscode.workspace.onDidChangeConfiguration( async (e: vscode.ConfigurationChangeEvent) => { - const restartTriggeredBy = ["typos.path", "typos.logLevel"].find((s) => - e.affectsConfiguration(s) - ); + const restartTriggeredBy = [ + "typos.path", + "typos.logLevel", + "typos.diagnosticSeverity", + ].find((s) => e.affectsConfiguration(s)); if (restartTriggeredBy) { await vscode.commands.executeCommand("typos.restart"); @@ -96,6 +98,9 @@ async function createClient( ], outputChannel: outputChannel, traceOutputChannel: outputChannel, + initializationOptions: { + diagnosticSeverity: config.get("diagnosticSeverity"), + }, }; return new LanguageClient(