Skip to content

Commit

Permalink
Add server config to filter out syntax error diagnostics (#12059)
Browse files Browse the repository at this point in the history
## 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">
  • Loading branch information
dhruvmanila authored and MichaReiser committed Jun 27, 2024
1 parent 72b6c26 commit 22cebdf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
32 changes: 21 additions & 11 deletions crates/ruff_server/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<lsp_types::Url, Vec<lsp_types::Diagnostic>>;

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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion crates/ruff_server/src/server/api/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
24 changes: 24 additions & 0 deletions crates/ruff_server/src/session/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -70,6 +71,13 @@ pub struct ClientSettings {
exclude: Option<Vec<String>>,
line_length: Option<LineLength>,
configuration_preference: Option<ConfigurationPreference>,

/// 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<bool>,

// 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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -439,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,
Expand Down Expand Up @@ -491,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,
Expand Down Expand Up @@ -556,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,
Expand Down Expand Up @@ -602,6 +622,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),
Expand Down Expand Up @@ -633,6 +654,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),
Expand Down Expand Up @@ -700,6 +722,7 @@ mod tests {
),
),
configuration_preference: None,
show_syntax_errors: None,
tracing: TracingSettings {
log_level: Some(
Warn,
Expand All @@ -726,6 +749,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,
Expand Down

0 comments on commit 22cebdf

Please sign in to comment.