Skip to content

Commit 22cebdf

Browse files
dhruvmanilaMichaReiser
authored andcommitted
Add server config to filter out syntax error diagnostics (#12059)
## Summary Follow-up from #11901 This PR adds a new server setting to show / hide syntax errors. ## Test Plan ### VS Code Using astral-sh/ruff-vscode#504 with the following config: ```json { "ruff.nativeServer": true, "ruff.path": ["/Users/dhruv/work/astral/ruff/target/debug/ruff"], "ruff.showSyntaxErrors": true } ``` First, set `ruff.showSyntaxErrors` to `true`: <img width="1177" alt="Screenshot 2024-06-27 at 08 34 58" src="https://github.com/astral-sh/ruff/assets/67177269/5d77547a-a908-4a00-8714-7c00784e8679"> And then set it to `false`: <img width="1185" alt="Screenshot 2024-06-27 at 08 35 19" src="https://github.com/astral-sh/ruff/assets/67177269/9720f089-f10c-420b-a2c1-2bbb2245be35"> ### Neovim Using the following Ruff server config: ```lua require('lspconfig').ruff.setup { init_options = { settings = { showSyntaxErrors = false, }, }, } ``` First, set `showSyntaxErrors` to `true`: <img width="1279" alt="Screenshot 2024-06-27 at 08 28 03" src="https://github.com/astral-sh/ruff/assets/67177269/e694e231-91ba-47f8-8e8a-ad2e82b85a45"> And then set it to `false`: <img width="1284" alt="Screenshot 2024-06-27 at 08 28 20" src="https://github.com/astral-sh/ruff/assets/67177269/25b86a57-02b1-44f7-9f65-cf5fdde93b0c">
1 parent 72b6c26 commit 22cebdf

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

crates/ruff_server/src/lint.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ pub(crate) struct DiagnosticFix {
6060
/// A series of diagnostics across a single text document or an arbitrary number of notebook cells.
6161
pub(crate) type DiagnosticsMap = FxHashMap<lsp_types::Url, Vec<lsp_types::Diagnostic>>;
6262

63-
pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> DiagnosticsMap {
63+
pub(crate) fn check(
64+
query: &DocumentQuery,
65+
encoding: PositionEncoding,
66+
show_syntax_errors: bool,
67+
) -> DiagnosticsMap {
6468
let source_kind = query.make_source_kind();
6569
let file_resolver_settings = query.settings().file_resolver();
6670
let linter_settings = query.settings().linter();
@@ -156,10 +160,18 @@ pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagno
156160
.zip(noqa_edits)
157161
.map(|(diagnostic, noqa_edit)| {
158162
to_lsp_diagnostic(diagnostic, &noqa_edit, &source_kind, &index, encoding)
159-
})
160-
.chain(parsed.errors().iter().map(|parse_error| {
161-
parse_error_to_lsp_diagnostic(parse_error, &source_kind, &index, encoding)
162-
}));
163+
});
164+
165+
let lsp_diagnostics = lsp_diagnostics.chain(
166+
show_syntax_errors
167+
.then(|| {
168+
parsed.errors().iter().map(|parse_error| {
169+
parse_error_to_lsp_diagnostic(parse_error, &source_kind, &index, encoding)
170+
})
171+
})
172+
.into_iter()
173+
.flatten(),
174+
);
163175

164176
if let Some(notebook) = query.as_notebook() {
165177
for (index, diagnostic) in lsp_diagnostics {
@@ -173,12 +185,10 @@ pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagno
173185
.push(diagnostic);
174186
}
175187
} else {
176-
for (_, diagnostic) in lsp_diagnostics {
177-
diagnostics_map
178-
.entry(query.make_key().into_url())
179-
.or_default()
180-
.push(diagnostic);
181-
}
188+
diagnostics_map
189+
.entry(query.make_key().into_url())
190+
.or_default()
191+
.extend(lsp_diagnostics.map(|(_, diagnostic)| diagnostic));
182192
}
183193

184194
diagnostics_map

crates/ruff_server/src/server/api/diagnostics.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ use super::LSPResult;
99
pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> DiagnosticsMap {
1010
if snapshot.client_settings().lint() {
1111
let document = snapshot.query();
12-
crate::lint::check(document, snapshot.encoding())
12+
crate::lint::check(
13+
document,
14+
snapshot.encoding(),
15+
snapshot.client_settings().show_syntax_errors(),
16+
)
1317
} else {
1418
DiagnosticsMap::default()
1519
}

crates/ruff_server/src/session/settings.rs

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub(crate) struct ResolvedClientSettings {
2121
lint_enable: bool,
2222
disable_rule_comment_enable: bool,
2323
fix_violation_enable: bool,
24+
show_syntax_errors: bool,
2425
editor_settings: ResolvedEditorSettings,
2526
}
2627

@@ -70,6 +71,13 @@ pub struct ClientSettings {
7071
exclude: Option<Vec<String>>,
7172
line_length: Option<LineLength>,
7273
configuration_preference: Option<ConfigurationPreference>,
74+
75+
/// If `true` or [`None`], show syntax errors as diagnostics.
76+
///
77+
/// This is useful when using Ruff with other language servers, allowing the user to refer
78+
/// to syntax errors from only one source.
79+
show_syntax_errors: Option<bool>,
80+
7381
// These settings are only needed for tracing, and are only read from the global configuration.
7482
// These will not be in the resolved settings.
7583
#[serde(flatten)]
@@ -244,6 +252,11 @@ impl ResolvedClientSettings {
244252
},
245253
true,
246254
),
255+
show_syntax_errors: Self::resolve_or(
256+
all_settings,
257+
|settings| settings.show_syntax_errors,
258+
true,
259+
),
247260
editor_settings: ResolvedEditorSettings {
248261
configuration: Self::resolve_optional(all_settings, |settings| {
249262
settings
@@ -345,6 +358,10 @@ impl ResolvedClientSettings {
345358
self.fix_violation_enable
346359
}
347360

361+
pub(crate) fn show_syntax_errors(&self) -> bool {
362+
self.show_syntax_errors
363+
}
364+
348365
pub(crate) fn editor_settings(&self) -> &ResolvedEditorSettings {
349366
&self.editor_settings
350367
}
@@ -439,6 +456,7 @@ mod tests {
439456
exclude: None,
440457
line_length: None,
441458
configuration_preference: None,
459+
show_syntax_errors: None,
442460
tracing: TracingSettings {
443461
log_level: None,
444462
log_file: None,
@@ -491,6 +509,7 @@ mod tests {
491509
exclude: None,
492510
line_length: None,
493511
configuration_preference: None,
512+
show_syntax_errors: None,
494513
tracing: TracingSettings {
495514
log_level: None,
496515
log_file: None,
@@ -556,6 +575,7 @@ mod tests {
556575
exclude: None,
557576
line_length: None,
558577
configuration_preference: None,
578+
show_syntax_errors: None,
559579
tracing: TracingSettings {
560580
log_level: None,
561581
log_file: None,
@@ -602,6 +622,7 @@ mod tests {
602622
lint_enable: true,
603623
disable_rule_comment_enable: false,
604624
fix_violation_enable: false,
625+
show_syntax_errors: true,
605626
editor_settings: ResolvedEditorSettings {
606627
configuration: None,
607628
lint_preview: Some(true),
@@ -633,6 +654,7 @@ mod tests {
633654
lint_enable: true,
634655
disable_rule_comment_enable: true,
635656
fix_violation_enable: false,
657+
show_syntax_errors: true,
636658
editor_settings: ResolvedEditorSettings {
637659
configuration: None,
638660
lint_preview: Some(false),
@@ -700,6 +722,7 @@ mod tests {
700722
),
701723
),
702724
configuration_preference: None,
725+
show_syntax_errors: None,
703726
tracing: TracingSettings {
704727
log_level: Some(
705728
Warn,
@@ -726,6 +749,7 @@ mod tests {
726749
lint_enable: true,
727750
disable_rule_comment_enable: false,
728751
fix_violation_enable: true,
752+
show_syntax_errors: true,
729753
editor_settings: ResolvedEditorSettings {
730754
configuration: None,
731755
lint_preview: None,

0 commit comments

Comments
 (0)