Respect file exclusions in ruff server (#11590)

## Summary

Closes https://github.com/astral-sh/ruff/issues/11587.

## Test Plan

- Added a lint error to `test_server.py` in `vscode-ruff`.
- Validated that, prior to this change, diagnostics appeared in the
file.
- Validated that, with this change, no diagnostics were shown.
- Validated that, with this change, no diagnostics were fixed on-save.
This commit is contained in:
Charlie Marsh 2024-05-28 22:58:36 -04:00 committed by GitHub
parent 531ae5227c
commit 204c59e353
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 235 additions and 43 deletions

View file

@ -626,6 +626,63 @@ pub fn match_candidate_exclusion(
exclusion.is_match_candidate(file_path) || exclusion.is_match_candidate(file_basename)
}
#[derive(Debug, Copy, Clone)]
pub enum ExclusionKind {
/// The exclusion came from the `exclude` setting.
Exclude,
/// The exclusion came from the `extend-exclude` setting.
ExtendExclude,
/// The exclusion came from the `lint.exclude` setting.
LintExclude,
/// The exclusion came from the `lint.extend-exclude` setting.
FormatExclude,
}
impl std::fmt::Display for ExclusionKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExclusionKind::Exclude => write!(f, "exclude"),
ExclusionKind::ExtendExclude => write!(f, "extend-exclude"),
ExclusionKind::LintExclude => write!(f, "lint.exclude"),
ExclusionKind::FormatExclude => write!(f, "lint.extend-exclude"),
}
}
}
/// Return the [`ExclusionKind`] for a given [`Path`], if the path or any of its ancestors match
/// any of the exclusion criteria.
pub fn match_any_exclusion(
path: &Path,
exclude: &GlobSet,
extend_exclude: &GlobSet,
lint_exclude: Option<&GlobSet>,
format_exclude: Option<&GlobSet>,
) -> Option<ExclusionKind> {
for path in path.ancestors() {
if let Some(basename) = path.file_name() {
let path = Candidate::new(path);
let basename = Candidate::new(basename);
if match_candidate_exclusion(&path, &basename, exclude) {
return Some(ExclusionKind::Exclude);
}
if match_candidate_exclusion(&path, &basename, extend_exclude) {
return Some(ExclusionKind::ExtendExclude);
}
if let Some(lint_exclude) = lint_exclude {
if match_candidate_exclusion(&path, &basename, lint_exclude) {
return Some(ExclusionKind::LintExclude);
}
}
if let Some(format_exclude) = format_exclude {
if match_candidate_exclusion(&path, &basename, format_exclude) {
return Some(ExclusionKind::FormatExclude);
}
}
}
}
None
}
#[cfg(test)]
mod tests {
use std::fs::{create_dir, File};