|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use ruff_linter::settings::LinterSettings; |
| 4 | +use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion}; |
| 5 | +use ruff_workspace::{FileResolverSettings, FormatterSettings}; |
| 6 | + |
| 7 | +/// Return `true` if the document at the given [`Path`] should be excluded. |
| 8 | +/// |
| 9 | +/// The tool-specific settings should be provided if the request for the document is specific to |
| 10 | +/// that tool. For example, a diagnostics request should provide the linter settings while the |
| 11 | +/// formatting request should provide the formatter settings. |
| 12 | +/// |
| 13 | +/// The logic for the resolution considers both inclusion and exclusion and is as follows: |
| 14 | +/// 1. Check for global `exclude` and `extend-exclude` options along with tool specific `exclude` |
| 15 | +/// option (`lint.exclude`, `format.exclude`). |
| 16 | +/// 2. Check for global `include` and `extend-include` options. |
| 17 | +pub(crate) fn is_document_excluded( |
| 18 | + path: &Path, |
| 19 | + resolver_settings: &FileResolverSettings, |
| 20 | + linter_settings: Option<&LinterSettings>, |
| 21 | + formatter_settings: Option<&FormatterSettings>, |
| 22 | +) -> bool { |
| 23 | + if let Some(exclusion) = match_any_exclusion( |
| 24 | + path, |
| 25 | + &resolver_settings.exclude, |
| 26 | + &resolver_settings.extend_exclude, |
| 27 | + linter_settings.map(|s| &*s.exclude), |
| 28 | + formatter_settings.map(|s| &*s.exclude), |
| 29 | + ) { |
| 30 | + tracing::debug!("Ignored path via `{}`: {}", exclusion, path.display()); |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + if let Some(inclusion) = match_any_inclusion( |
| 35 | + path, |
| 36 | + &resolver_settings.include, |
| 37 | + &resolver_settings.extend_include, |
| 38 | + ) { |
| 39 | + tracing::debug!("Included path via `{}`: {}", inclusion, path.display()); |
| 40 | + false |
| 41 | + } else { |
| 42 | + // Path is excluded by not being in the inclusion set. |
| 43 | + true |
| 44 | + } |
| 45 | +} |
0 commit comments