Skip to content

Commit

Permalink
feat: configurable diagnostic severity
Browse files Browse the repository at this point in the history
resolves #17
  • Loading branch information
tekumara committed Oct 22, 2023
1 parent 38253bf commit 7e7e743
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
31 changes: 29 additions & 2 deletions crates/typos-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Backend<'s, 'p> {

#[derive(Default)]
struct BackendState<'s> {
severity: Option<DiagnosticSeverity>,
workspace_folders: Vec<WorkspaceFolder>,
router: Router<TyposCli<'s>>,
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions crates/typos-lsp/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ async fn test_e2e() {
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"initializationOptions": {
"diagnosticSeverity": "Warning"
},
"capabilities": {
"textDocument": { "publishDiagnostics": { "dataSupport": true } }
}
Expand Down Expand Up @@ -202,6 +205,9 @@ async fn test_config_file_e2e() {
"jsonrpc": "2.0",
"method": "initialize",
"params": {{
"initializationOptions": {{
"diagnosticSeverity": "Warning"
}},
"capabilities": {{
"textDocument": {{ "publishDiagnostics": {{ "dataSupport": true }} }}
}},
Expand Down
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 8 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -96,6 +98,9 @@ async function createClient(
],
outputChannel: outputChannel,
traceOutputChannel: outputChannel,
initializationOptions: {
diagnosticSeverity: config.get("diagnosticSeverity"),
},
};

return new LanguageClient(
Expand Down

0 comments on commit 7e7e743

Please sign in to comment.