Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conside include, extend-include for the native server #12252

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/ruff_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ruff_workspace = { workspace = true }

anyhow = { workspace = true }
crossbeam = { workspace = true }
globset = { workspace = true }
jod-thread = { workspace = true }
lsp-server = { workspace = true }
lsp-types = { workspace = true }
Expand Down
16 changes: 15 additions & 1 deletion crates/ruff_server/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ruff_linter::{
};
use ruff_notebook::SourceValue;
use ruff_source_file::LineIndex;
use ruff_workspace::resolver::match_any_exclusion;
use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion};

use crate::{
edit::{Replacement, ToRangeExt},
Expand Down Expand Up @@ -48,6 +48,20 @@ pub(crate) fn fix_all(
return Ok(Fixes::default());
}

if let Some(inclusion) = match_any_inclusion(
document_path,
&file_resolver_settings.include,
&file_resolver_settings.extend_include,
) {
tracing::debug!(
"Included path via `{}`: {}",
inclusion,
document_path.display()
);
} else {
return Ok(Fixes::default());
}

detect_package_root(
document_path
.parent()
Expand Down
16 changes: 15 additions & 1 deletion crates/ruff_server/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ruff_python_codegen::Stylist;
use ruff_python_index::Indexer;
use ruff_source_file::{LineIndex, Locator};
use ruff_text_size::{Ranged, TextRange};
use ruff_workspace::resolver::match_any_exclusion;
use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion};

use crate::{
edit::{NotebookRange, ToRangeExt},
Expand Down Expand Up @@ -87,6 +87,20 @@ pub(crate) fn check(
return DiagnosticsMap::default();
}

if let Some(inclusion) = match_any_inclusion(
document_path,
&file_resolver_settings.include,
&file_resolver_settings.extend_include,
) {
tracing::debug!(
"Included path via `{}`: {}",
inclusion,
document_path.display()
);
} else {
return DiagnosticsMap::default();
}

detect_package_root(
document_path
.parent()
Expand Down
12 changes: 11 additions & 1 deletion crates/ruff_server/src/server/api/requests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use lsp_types::{self as types, request as req};
use types::TextEdit;

use ruff_source_file::LineIndex;
use ruff_workspace::resolver::match_any_exclusion;
use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion};

use crate::edit::{Replacement, ToRangeExt};
use crate::fix::Fixes;
Expand Down Expand Up @@ -95,6 +95,16 @@ fn format_text_document(
tracing::debug!("Ignored path via `{}`: {}", exclusion, file_path.display());
return Ok(None);
}

if let Some(inclusion) = match_any_inclusion(
&file_path,
&file_resolver_settings.include,
&file_resolver_settings.extend_include,
) {
tracing::debug!("Included path via `{}`: {}", inclusion, file_path.display());
} else {
return Ok(None);
}
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
}

let source = text_document.contents();
Expand Down
12 changes: 11 additions & 1 deletion crates/ruff_server/src/server/api/requests/format_range.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lsp_types::{self as types, request as req, Range};

use ruff_workspace::resolver::match_any_exclusion;
use ruff_workspace::resolver::{match_any_exclusion, match_any_inclusion};

use crate::edit::{RangeExt, ToRangeExt};
use crate::server::api::LSPResult;
Expand Down Expand Up @@ -60,6 +60,16 @@ fn format_text_document_range(
tracing::debug!("Ignored path via `{}`: {}", exclusion, file_path.display());
return Ok(None);
}

if let Some(inclusion) = match_any_inclusion(
&file_path,
&file_resolver_settings.include,
&file_resolver_settings.extend_include,
) {
tracing::debug!("Included path via `{}`: {}", inclusion, file_path.display());
} else {
return Ok(None);
}
}

let text = text_document.contents();
Expand Down
18 changes: 6 additions & 12 deletions crates/ruff_server/src/session/index/ruff_settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use globset::Candidate;
use ruff_linter::{
display_settings, fs::normalize_path_to, settings::types::FilePattern,
settings::types::PreviewMode,
};
use ruff_workspace::resolver::match_candidate_exclusion;
use ruff_workspace::resolver::match_exclusion;
use ruff_workspace::{
configuration::{Configuration, FormatConfiguration, LintConfiguration, RuleSelection},
pyproject::{find_user_settings_toml, settings_toml},
Expand Down Expand Up @@ -41,6 +40,7 @@ impl std::fmt::Display for RuffSettings {
display_settings! {
formatter = f,
fields = [
self.file_resolver,
self.linter,
self.formatter
]
Expand Down Expand Up @@ -146,20 +146,14 @@ impl RuffSettingsIndex {
.range(..directory.clone())
.rfind(|(path, _)| directory.starts_with(path))
{
let candidate = Candidate::new(&directory);
let basename = Candidate::new(file_name);
if match_candidate_exclusion(
&candidate,
&basename,
&settings.file_resolver.exclude,
) {
if match_exclusion(&directory, file_name, &settings.file_resolver.exclude) {
tracing::debug!("Ignored path via `exclude`: {}", directory.display());

walker.skip_current_dir();
continue;
} else if match_candidate_exclusion(
&candidate,
&basename,
} else if match_exclusion(
&directory,
file_name,
&settings.file_resolver.extend_exclude,
) {
tracing::debug!(
Expand Down
33 changes: 33 additions & 0 deletions crates/ruff_workspace/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,39 @@ pub fn match_any_exclusion(
None
}

#[derive(Debug, Copy, Clone)]
pub enum InclusionKind {
/// The inclusion came from the `include` setting.
Include,
/// The inclusion came from the `extend-include` setting.
ExtendInclude,
}

impl std::fmt::Display for InclusionKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
InclusionKind::Include => write!(f, "include"),
InclusionKind::ExtendInclude => write!(f, "extend-include"),
}
}
}

/// Return the [`InclusionKind`] for a given [`Path`], if the path match any of the inclusion
/// criteria.
pub fn match_any_inclusion(
path: &Path,
include: &GlobSet,
extend_include: &GlobSet,
) -> Option<InclusionKind> {
if include.is_match(path) {
Some(InclusionKind::Include)
} else if extend_include.is_match(path) {
Some(InclusionKind::ExtendInclude)
} else {
None
}
}

#[cfg(test)]
mod tests {
use std::fs::{create_dir, File};
Expand Down
Loading