From f562f7ca2720b751d6574afbcaf9a46300bf2787 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 26 Jun 2024 22:18:21 +0530 Subject: [PATCH 1/2] Add server config to filter out syntax error diagnostics --- crates/ruff_server/src/lint.rs | 32 ++++++++++++------- .../ruff_server/src/server/api/diagnostics.rs | 6 +++- crates/ruff_server/src/session/settings.rs | 20 ++++++++++++ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/crates/ruff_server/src/lint.rs b/crates/ruff_server/src/lint.rs index 6a366c531fc50..39b3f54aa3acc 100644 --- a/crates/ruff_server/src/lint.rs +++ b/crates/ruff_server/src/lint.rs @@ -60,7 +60,11 @@ pub(crate) struct DiagnosticFix { /// A series of diagnostics across a single text document or an arbitrary number of notebook cells. pub(crate) type DiagnosticsMap = FxHashMap>; -pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> DiagnosticsMap { +pub(crate) fn check( + query: &DocumentQuery, + encoding: PositionEncoding, + show_syntax_errors: bool, +) -> DiagnosticsMap { let source_kind = query.make_source_kind(); let file_resolver_settings = query.settings().file_resolver(); let linter_settings = query.settings().linter(); @@ -156,10 +160,18 @@ pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagno .zip(noqa_edits) .map(|(diagnostic, noqa_edit)| { to_lsp_diagnostic(diagnostic, &noqa_edit, &source_kind, &index, encoding) - }) - .chain(parsed.errors().iter().map(|parse_error| { - parse_error_to_lsp_diagnostic(parse_error, &source_kind, &index, encoding) - })); + }); + + let lsp_diagnostics = lsp_diagnostics.chain( + show_syntax_errors + .then(|| { + parsed.errors().iter().map(|parse_error| { + parse_error_to_lsp_diagnostic(parse_error, &source_kind, &index, encoding) + }) + }) + .into_iter() + .flatten(), + ); if let Some(notebook) = query.as_notebook() { for (index, diagnostic) in lsp_diagnostics { @@ -173,12 +185,10 @@ pub(crate) fn check(query: &DocumentQuery, encoding: PositionEncoding) -> Diagno .push(diagnostic); } } else { - for (_, diagnostic) in lsp_diagnostics { - diagnostics_map - .entry(query.make_key().into_url()) - .or_default() - .push(diagnostic); - } + diagnostics_map + .entry(query.make_key().into_url()) + .or_default() + .extend(lsp_diagnostics.map(|(_, diagnostic)| diagnostic)); } diagnostics_map diff --git a/crates/ruff_server/src/server/api/diagnostics.rs b/crates/ruff_server/src/server/api/diagnostics.rs index 950827ca14126..5f0b9f468d6e3 100644 --- a/crates/ruff_server/src/server/api/diagnostics.rs +++ b/crates/ruff_server/src/server/api/diagnostics.rs @@ -9,7 +9,11 @@ use super::LSPResult; pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> DiagnosticsMap { if snapshot.client_settings().lint() { let document = snapshot.query(); - crate::lint::check(document, snapshot.encoding()) + crate::lint::check( + document, + snapshot.encoding(), + snapshot.client_settings().show_syntax_errors(), + ) } else { DiagnosticsMap::default() } diff --git a/crates/ruff_server/src/session/settings.rs b/crates/ruff_server/src/session/settings.rs index 80ac4995a1bb4..d8737f7207009 100644 --- a/crates/ruff_server/src/session/settings.rs +++ b/crates/ruff_server/src/session/settings.rs @@ -21,6 +21,7 @@ pub(crate) struct ResolvedClientSettings { lint_enable: bool, disable_rule_comment_enable: bool, fix_violation_enable: bool, + show_syntax_errors: bool, editor_settings: ResolvedEditorSettings, } @@ -70,6 +71,13 @@ pub struct ClientSettings { exclude: Option>, line_length: Option, configuration_preference: Option, + + /// If `true` or [`None`], show syntax errors as diagnostics. + /// + /// This is useful when using Ruff with other language servers, allowing the user to refer + /// to syntax errors from only one source. + show_syntax_errors: Option, + // These settings are only needed for tracing, and are only read from the global configuration. // These will not be in the resolved settings. #[serde(flatten)] @@ -244,6 +252,11 @@ impl ResolvedClientSettings { }, true, ), + show_syntax_errors: Self::resolve_or( + all_settings, + |settings| settings.show_syntax_errors, + true, + ), editor_settings: ResolvedEditorSettings { configuration: Self::resolve_optional(all_settings, |settings| { settings @@ -345,6 +358,10 @@ impl ResolvedClientSettings { self.fix_violation_enable } + pub(crate) fn show_syntax_errors(&self) -> bool { + self.show_syntax_errors + } + pub(crate) fn editor_settings(&self) -> &ResolvedEditorSettings { &self.editor_settings } @@ -602,6 +619,7 @@ mod tests { lint_enable: true, disable_rule_comment_enable: false, fix_violation_enable: false, + show_syntax_errors: true, editor_settings: ResolvedEditorSettings { configuration: None, lint_preview: Some(true), @@ -633,6 +651,7 @@ mod tests { lint_enable: true, disable_rule_comment_enable: true, fix_violation_enable: false, + show_syntax_errors: true, editor_settings: ResolvedEditorSettings { configuration: None, lint_preview: Some(false), @@ -726,6 +745,7 @@ mod tests { lint_enable: true, disable_rule_comment_enable: false, fix_violation_enable: true, + show_syntax_errors: true, editor_settings: ResolvedEditorSettings { configuration: None, lint_preview: None, From 11fead966020dd6e742ad8fbdcd53dd676c68a72 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 27 Jun 2024 12:18:10 +0530 Subject: [PATCH 2/2] Update server test snapshots --- crates/ruff_server/src/session/settings.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/ruff_server/src/session/settings.rs b/crates/ruff_server/src/session/settings.rs index d8737f7207009..3ec0a04c2fe85 100644 --- a/crates/ruff_server/src/session/settings.rs +++ b/crates/ruff_server/src/session/settings.rs @@ -456,6 +456,7 @@ mod tests { exclude: None, line_length: None, configuration_preference: None, + show_syntax_errors: None, tracing: TracingSettings { log_level: None, log_file: None, @@ -508,6 +509,7 @@ mod tests { exclude: None, line_length: None, configuration_preference: None, + show_syntax_errors: None, tracing: TracingSettings { log_level: None, log_file: None, @@ -573,6 +575,7 @@ mod tests { exclude: None, line_length: None, configuration_preference: None, + show_syntax_errors: None, tracing: TracingSettings { log_level: None, log_file: None, @@ -719,6 +722,7 @@ mod tests { ), ), configuration_preference: None, + show_syntax_errors: None, tracing: TracingSettings { log_level: Some( Warn,