use std::path::Path;
use ruff_linter::settings::LinterSettings;
use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion};
use ruff_workspace::{FileResolverSettings, FormatterSettings};
use crate::edit::LanguageId;
pub(crate) fn is_document_excluded_for_linting(
path: &Path,
resolver_settings: &FileResolverSettings,
linter_settings: &LinterSettings,
language_id: Option<LanguageId>,
) -> bool {
is_document_excluded(
path,
resolver_settings,
Some(linter_settings),
None,
language_id,
)
}
pub(crate) fn is_document_excluded_for_formatting(
path: &Path,
resolver_settings: &FileResolverSettings,
formatter_settings: &FormatterSettings,
language_id: Option<LanguageId>,
) -> bool {
is_document_excluded(
path,
resolver_settings,
None,
Some(formatter_settings),
language_id,
)
}
fn is_document_excluded(
path: &Path,
resolver_settings: &FileResolverSettings,
linter_settings: Option<&LinterSettings>,
formatter_settings: Option<&FormatterSettings>,
language_id: Option<LanguageId>,
) -> bool {
if let Some(exclusion) = match_any_exclusion(
path,
resolver_settings,
linter_settings.map(|s| &*s.exclude),
formatter_settings.map(|s| &*s.exclude),
) {
tracing::debug!("Ignored path via `{}`: {}", exclusion, path.display());
return true;
}
if let Some(inclusion) = match_any_inclusion(path, resolver_settings) {
tracing::debug!("Included path via `{}`: {}", inclusion, path.display());
false
} else if let Some(LanguageId::Python) = language_id {
tracing::debug!("Included path via Python language ID: {}", path.display());
false
} else if let Some(LanguageId::Markdown) = language_id
&& formatter_settings.is_some()
{
tracing::debug!("Included path via Markdown language ID: {}", path.display());
false
} else {
tracing::debug!(
"Ignored path as it's not in the inclusion set: {}",
path.display()
);
true
}
}