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 b53e44b
Show file tree
Hide file tree
Showing 5 changed files with 58 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/wait-for-status-checks@v0.3.0
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
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
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 b53e44b

Please sign in to comment.