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 6b0a03a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 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
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
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
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function activate(
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(
async (e: vscode.ConfigurationChangeEvent) => {
const restartTriggeredBy = ["typos.path", "typos.logLevel"].find((s) =>
const restartTriggeredBy = ["typos.path", "typos.logLevel", "typos.diagnosticSeverity"].find((s) =>
e.affectsConfiguration(s)
);

Expand Down Expand Up @@ -96,6 +96,9 @@ async function createClient(
],
outputChannel: outputChannel,
traceOutputChannel: outputChannel,
initializationOptions: {
diagnosticSeverity: config.get("diagnosticSeverity"),
}
};

return new LanguageClient(
Expand Down

0 comments on commit 6b0a03a

Please sign in to comment.