diff --git a/crates/ruff_server/src/lint.rs b/crates/ruff_server/src/lint.rs index 9ad6be5276..bf20172cba 100644 --- a/crates/ruff_server/src/lint.rs +++ b/crates/ruff_server/src/lint.rs @@ -12,7 +12,6 @@ use crate::{ use ruff_diagnostics::{Applicability, Edit, Fix}; use ruff_linter::{ Locator, - codes::Rule, directives::{Flags, extract_directives}, generate_noqa_edits, linter::check_path, @@ -166,26 +165,17 @@ pub(crate) fn check( messages .into_iter() .zip(noqa_edits) - .filter_map(|(message, noqa_edit)| match message.to_rule() { - Some(rule) => Some(to_lsp_diagnostic( - rule, - &message, - noqa_edit, - &source_kind, - locator.to_index(), - encoding, - )), - None => { - if show_syntax_errors { - Some(syntax_error_to_lsp_diagnostic( - &message, - &source_kind, - locator.to_index(), - encoding, - )) - } else { - None - } + .filter_map(|(message, noqa_edit)| { + if message.is_syntax_error() && !show_syntax_errors { + None + } else { + Some(to_lsp_diagnostic( + &message, + noqa_edit, + &source_kind, + locator.to_index(), + encoding, + )) } }); @@ -241,7 +231,6 @@ pub(crate) fn fixes_for_diagnostics( /// Generates an LSP diagnostic with an associated cell index for the diagnostic to go in. /// If the source kind is a text document, the cell index will always be `0`. fn to_lsp_diagnostic( - rule: Rule, diagnostic: &Message, noqa_edit: Option, source_kind: &SourceKind, @@ -253,11 +242,13 @@ fn to_lsp_diagnostic( let body = diagnostic.body().to_string(); let fix = diagnostic.fix(); let suggestion = diagnostic.suggestion(); + let code = diagnostic.to_noqa_code(); let fix = fix.and_then(|fix| fix.applies(Applicability::Unsafe).then_some(fix)); let data = (fix.is_some() || noqa_edit.is_some()) .then(|| { + let code = code?.to_string(); let edits = fix .into_iter() .flat_map(Fix::edits) @@ -274,14 +265,12 @@ fn to_lsp_diagnostic( title: suggestion.unwrap_or(name).to_string(), noqa_edit, edits, - code: rule.noqa_code().to_string(), + code, }) .ok() }) .flatten(); - let code = rule.noqa_code().to_string(); - let range: lsp_types::Range; let cell: usize; @@ -297,14 +286,25 @@ fn to_lsp_diagnostic( range = diagnostic_range.to_range(source_kind.source_code(), index, encoding); } + let (severity, tags, code) = if let Some(code) = code { + let code = code.to_string(); + ( + Some(severity(&code)), + tags(&code), + Some(lsp_types::NumberOrString::String(code)), + ) + } else { + (None, None, None) + }; + ( cell, lsp_types::Diagnostic { range, - severity: Some(severity(&code)), - tags: tags(&code), - code: Some(lsp_types::NumberOrString::String(code)), - code_description: rule.url().and_then(|url| { + severity, + tags, + code, + code_description: diagnostic.to_url().and_then(|url| { Some(lsp_types::CodeDescription { href: lsp_types::Url::parse(&url).ok()?, }) @@ -317,45 +317,6 @@ fn to_lsp_diagnostic( ) } -fn syntax_error_to_lsp_diagnostic( - syntax_error: &Message, - source_kind: &SourceKind, - index: &LineIndex, - encoding: PositionEncoding, -) -> (usize, lsp_types::Diagnostic) { - let range: lsp_types::Range; - let cell: usize; - - if let Some(notebook_index) = source_kind.as_ipy_notebook().map(Notebook::index) { - NotebookRange { cell, range } = syntax_error.range().to_notebook_range( - source_kind.source_code(), - index, - notebook_index, - encoding, - ); - } else { - cell = usize::default(); - range = syntax_error - .range() - .to_range(source_kind.source_code(), index, encoding); - } - - ( - cell, - lsp_types::Diagnostic { - range, - severity: Some(lsp_types::DiagnosticSeverity::ERROR), - tags: None, - code: None, - code_description: None, - source: Some(DIAGNOSTIC_NAME.into()), - message: syntax_error.body().to_string(), - related_information: None, - data: None, - }, - ) -} - fn diagnostic_edit_range( range: TextRange, source_kind: &SourceKind, diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 55c339c53c..24e683042b 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -207,36 +207,23 @@ impl Workspace { let messages: Vec = messages .into_iter() - .map(|msg| { - let message = msg.body().to_string(); - let range = msg.range(); - match msg.to_noqa_code() { - Some(code) => ExpandedMessage { - code: Some(code.to_string()), - message, - start_location: source_code.line_column(range.start()).into(), - end_location: source_code.line_column(range.end()).into(), - fix: msg.fix().map(|fix| ExpandedFix { - message: msg.suggestion().map(ToString::to_string), - edits: fix - .edits() - .iter() - .map(|edit| ExpandedEdit { - location: source_code.line_column(edit.start()).into(), - end_location: source_code.line_column(edit.end()).into(), - content: edit.content().map(ToString::to_string), - }) - .collect(), - }), - }, - None => ExpandedMessage { - code: None, - message, - start_location: source_code.line_column(range.start()).into(), - end_location: source_code.line_column(range.end()).into(), - fix: None, - }, - } + .map(|msg| ExpandedMessage { + code: msg.to_noqa_code().map(|code| code.to_string()), + message: msg.body().to_string(), + start_location: source_code.line_column(msg.start()).into(), + end_location: source_code.line_column(msg.end()).into(), + fix: msg.fix().map(|fix| ExpandedFix { + message: msg.suggestion().map(ToString::to_string), + edits: fix + .edits() + .iter() + .map(|edit| ExpandedEdit { + location: source_code.line_column(edit.start()).into(), + end_location: source_code.line_column(edit.end()).into(), + content: edit.content().map(ToString::to_string), + }) + .collect(), + }), }) .collect();