Skip to content

Commit 022107c

Browse files
committed
unify diagnostic handling in the server
1 parent ce8b744 commit 022107c

File tree

1 file changed

+28
-68
lines changed

1 file changed

+28
-68
lines changed

crates/ruff_server/src/lint.rs

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::{
1212
use ruff_diagnostics::{Applicability, Edit, Fix};
1313
use ruff_linter::{
1414
Locator,
15-
codes::Rule,
1615
directives::{Flags, extract_directives},
1716
generate_noqa_edits,
1817
linter::check_path,
@@ -166,26 +165,17 @@ pub(crate) fn check(
166165
messages
167166
.into_iter()
168167
.zip(noqa_edits)
169-
.filter_map(|(message, noqa_edit)| match message.to_rule() {
170-
Some(rule) => Some(to_lsp_diagnostic(
171-
rule,
172-
&message,
173-
noqa_edit,
174-
&source_kind,
175-
locator.to_index(),
176-
encoding,
177-
)),
178-
None => {
179-
if show_syntax_errors {
180-
Some(syntax_error_to_lsp_diagnostic(
181-
&message,
182-
&source_kind,
183-
locator.to_index(),
184-
encoding,
185-
))
186-
} else {
187-
None
188-
}
168+
.filter_map(|(message, noqa_edit)| {
169+
if message.is_syntax_error() && !show_syntax_errors {
170+
None
171+
} else {
172+
Some(to_lsp_diagnostic(
173+
&message,
174+
noqa_edit,
175+
&source_kind,
176+
locator.to_index(),
177+
encoding,
178+
))
189179
}
190180
});
191181

@@ -241,7 +231,6 @@ pub(crate) fn fixes_for_diagnostics(
241231
/// Generates an LSP diagnostic with an associated cell index for the diagnostic to go in.
242232
/// If the source kind is a text document, the cell index will always be `0`.
243233
fn to_lsp_diagnostic(
244-
rule: Rule,
245234
diagnostic: &Message,
246235
noqa_edit: Option<Edit>,
247236
source_kind: &SourceKind,
@@ -253,6 +242,7 @@ fn to_lsp_diagnostic(
253242
let body = diagnostic.body().to_string();
254243
let fix = diagnostic.fix();
255244
let suggestion = diagnostic.suggestion();
245+
let code = diagnostic.to_noqa_code();
256246

257247
let fix = fix.and_then(|fix| fix.applies(Applicability::Unsafe).then_some(fix));
258248

@@ -274,14 +264,12 @@ fn to_lsp_diagnostic(
274264
title: suggestion.unwrap_or(name).to_string(),
275265
noqa_edit,
276266
edits,
277-
code: rule.noqa_code().to_string(),
267+
code: code?.to_string(),
278268
})
279269
.ok()
280270
})
281271
.flatten();
282272

283-
let code = rule.noqa_code().to_string();
284-
285273
let range: lsp_types::Range;
286274
let cell: usize;
287275

@@ -297,14 +285,25 @@ fn to_lsp_diagnostic(
297285
range = diagnostic_range.to_range(source_kind.source_code(), index, encoding);
298286
}
299287

288+
let (severity, tags, code) = if let Some(code) = code {
289+
let code = code.to_string();
290+
(
291+
Some(severity(&code)),
292+
tags(&code),
293+
Some(lsp_types::NumberOrString::String(code)),
294+
)
295+
} else {
296+
(None, None, None)
297+
};
298+
300299
(
301300
cell,
302301
lsp_types::Diagnostic {
303302
range,
304-
severity: Some(severity(&code)),
305-
tags: tags(&code),
306-
code: Some(lsp_types::NumberOrString::String(code)),
307-
code_description: rule.url().and_then(|url| {
303+
severity,
304+
tags,
305+
code,
306+
code_description: diagnostic.to_url().and_then(|url| {
308307
Some(lsp_types::CodeDescription {
309308
href: lsp_types::Url::parse(&url).ok()?,
310309
})
@@ -317,45 +316,6 @@ fn to_lsp_diagnostic(
317316
)
318317
}
319318

320-
fn syntax_error_to_lsp_diagnostic(
321-
syntax_error: &Message,
322-
source_kind: &SourceKind,
323-
index: &LineIndex,
324-
encoding: PositionEncoding,
325-
) -> (usize, lsp_types::Diagnostic) {
326-
let range: lsp_types::Range;
327-
let cell: usize;
328-
329-
if let Some(notebook_index) = source_kind.as_ipy_notebook().map(Notebook::index) {
330-
NotebookRange { cell, range } = syntax_error.range().to_notebook_range(
331-
source_kind.source_code(),
332-
index,
333-
notebook_index,
334-
encoding,
335-
);
336-
} else {
337-
cell = usize::default();
338-
range = syntax_error
339-
.range()
340-
.to_range(source_kind.source_code(), index, encoding);
341-
}
342-
343-
(
344-
cell,
345-
lsp_types::Diagnostic {
346-
range,
347-
severity: Some(lsp_types::DiagnosticSeverity::ERROR),
348-
tags: None,
349-
code: None,
350-
code_description: None,
351-
source: Some(DIAGNOSTIC_NAME.into()),
352-
message: syntax_error.body().to_string(),
353-
related_information: None,
354-
data: None,
355-
},
356-
)
357-
}
358-
359319
fn diagnostic_edit_range(
360320
range: TextRange,
361321
source_kind: &SourceKind,

0 commit comments

Comments
 (0)