Compare commits

...

5 Commits

Author SHA1 Message Date
Charlie Marsh
b54b1c1f05 Add Windows 2024-01-09 00:53:08 -05:00
Charlie Marsh
ad1b5c52d5 Use ref 2024-01-09 00:23:28 -05:00
Charlie Marsh
bd3fe93f41 Revert "Use radix_trie"
This reverts commit 3c7cbbcb92.
2024-01-09 00:17:19 -05:00
Charlie Marsh
3c7cbbcb92 Use radix_trie 2024-01-09 00:09:27 -05:00
Charlie Marsh
41f1f8a4ca Add matchit 2024-01-09 00:02:26 -05:00
4 changed files with 58 additions and 23 deletions

14
Cargo.lock generated
View File

@@ -1321,6 +1321,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
[[package]]
name = "matchit"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
[[package]]
name = "memchr"
version = "2.6.4"
@@ -1535,6 +1541,12 @@ dependencies = [
"once_cell",
]
[[package]]
name = "path-slash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e91099d4268b0e11973f036e885d652fb0b21fedcf69738c627f94db6a44f42"
[[package]]
name = "pathdiff"
version = "0.2.1"
@@ -2490,8 +2502,10 @@ dependencies = [
"is-macro",
"itertools 0.12.0",
"log",
"matchit",
"once_cell",
"path-absolutize",
"path-slash",
"pep440_rs 0.4.0",
"regex",
"ruff_cache",

View File

@@ -55,12 +55,14 @@ lalrpop-util = { version = "0.20.0", default-features = false }
lexical-parse-float = { version = "0.8.0", features = ["format"] }
libcst = { version = "1.1.0", default-features = false }
log = { version = "0.4.17" }
matchit = { version = "0.7.3" }
memchr = { version = "2.6.4" }
mimalloc = { version ="0.1.39"}
natord = { version = "1.0.9" }
notify = { version = "6.1.1" }
once_cell = { version = "1.19.0" }
path-absolutize = { version = "3.1.1" }
path-slash = { version = "0.2.1" }
pathdiff = { version = "0.2.1" }
pep440_rs = { version = "0.4.0", features = ["serde"] }
pretty_assertions = "1.3.0"

View File

@@ -24,14 +24,16 @@ ruff_macros = { path = "../ruff_macros" }
anyhow = { workspace = true }
colored = { workspace = true }
dirs = { workspace = true }
glob = { workspace = true }
globset = { workspace = true }
ignore = { workspace = true }
is-macro = { workspace = true }
itertools = { workspace = true }
log = { workspace = true }
glob = { workspace = true }
globset = { workspace = true }
matchit = { workspace = true }
once_cell = { workspace = true }
path-absolutize = { workspace = true }
path-slash = { workspace = true }
pep440_rs = { workspace = true, features = ["serde"] }
regex = { workspace = true }
rustc-hash = { workspace = true }

View File

@@ -2,7 +2,6 @@
//! filesystem.
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
@@ -13,7 +12,9 @@ use globset::{Candidate, GlobSet};
use ignore::{WalkBuilder, WalkState};
use itertools::Itertools;
use log::debug;
use matchit::{InsertError, Router};
use path_absolutize::path_dedot;
use path_slash::PathExt;
use rustc_hash::{FxHashMap, FxHashSet};
use ruff_linter::fs;
@@ -83,26 +84,37 @@ pub enum Relativity {
}
impl Relativity {
pub fn resolve(self, path: &Path) -> PathBuf {
pub fn resolve(self, path: &Path) -> &Path {
match self {
Relativity::Parent => path
.parent()
.expect("Expected pyproject.toml file to be in parent directory")
.to_path_buf(),
Relativity::Cwd => path_dedot::CWD.clone(),
.expect("Expected `pyproject.toml` file to be in parent directory"),
Relativity::Cwd => &path_dedot::CWD,
}
}
}
#[derive(Default)]
pub struct Resolver {
settings: BTreeMap<PathBuf, Settings>,
/// All [`Settings`] that have been inserted into this [`Resolver`].
settings: Vec<Settings>,
/// A router from path to index in the [`Settings`] vector.
router: Router<usize>,
}
impl Resolver {
/// Add a resolved [`Settings`] under a given [`PathBuf`] scope.
fn add(&mut self, path: PathBuf, settings: Settings) {
self.settings.insert(path, settings);
fn add(&mut self, path: &Path, settings: Settings) -> Result<()> {
self.settings.push(settings);
println!("path: {:?}", path.to_slash_lossy());
match self.router.insert(
format!("{}/*filepath", path.to_slash_lossy()),
self.settings.len() - 1,
) {
Ok(()) => Ok(()),
Err(InsertError::Conflict { .. }) => Ok(()),
Err(err) => Err(anyhow!("Failed to insert path into router: {err}")),
}
}
/// Return the appropriate [`Settings`] for a given [`Path`].
@@ -114,10 +126,9 @@ impl Resolver {
match pyproject_config.strategy {
PyprojectDiscoveryStrategy::Fixed => &pyproject_config.settings,
PyprojectDiscoveryStrategy::Hierarchical => self
.settings
.iter()
.rev()
.find_map(|(root, settings)| path.starts_with(root).then_some(settings))
.router
.at(&path.to_slash_lossy())
.map(|match_| &self.settings[*match_.value])
.unwrap_or(&pyproject_config.settings),
}
}
@@ -162,7 +173,7 @@ impl Resolver {
/// Return an iterator over the resolved [`Settings`] in this [`Resolver`].
pub fn settings(&self) -> impl Iterator<Item = &Settings> {
self.settings.values()
self.settings.iter()
}
}
@@ -223,7 +234,7 @@ fn resolve_configuration(
let options = pyproject::load_options(&path)?;
let project_root = relativity.resolve(&path);
let configuration = Configuration::from_options(options, &project_root)?;
let configuration = Configuration::from_options(options, project_root)?;
// If extending, continue to collect.
next = configuration.extend.as_ref().map(|extend| {
@@ -251,14 +262,14 @@ fn resolve_configuration(
/// Extract the project root (scope) and [`Settings`] from a given
/// `pyproject.toml`.
fn resolve_scoped_settings(
pyproject: &Path,
fn resolve_scoped_settings<'a>(
pyproject: &'a Path,
relativity: Relativity,
transformer: &dyn ConfigurationTransformer,
) -> Result<(PathBuf, Settings)> {
) -> Result<(&'a Path, Settings)> {
let configuration = resolve_configuration(pyproject, relativity, transformer)?;
let project_root = relativity.resolve(pyproject);
let settings = configuration.into_settings(&project_root)?;
let settings = configuration.into_settings(project_root)?;
Ok((project_root, settings))
}
@@ -292,7 +303,7 @@ pub fn python_files_in_path(
if let Some(pyproject) = settings_toml(ancestor)? {
let (root, settings) =
resolve_scoped_settings(&pyproject, Relativity::Parent, transformer)?;
resolver.add(root, settings);
resolver.add(root, settings)?;
break;
}
}
@@ -373,7 +384,13 @@ pub fn python_files_in_path(
transformer,
) {
Ok((root, settings)) => {
resolver.write().unwrap().add(root, settings);
match resolver.write().unwrap().add(root, settings) {
Ok(()) => {}
Err(err) => {
*error.lock().unwrap() = Err(err);
return WalkState::Quit;
}
}
}
Err(err) => {
*error.lock().unwrap() = Err(err);
@@ -493,7 +510,7 @@ pub fn python_file_at_path(
if let Some(pyproject) = settings_toml(ancestor)? {
let (root, settings) =
resolve_scoped_settings(&pyproject, Relativity::Parent, transformer)?;
resolver.add(root, settings);
resolver.add(root, settings)?;
break;
}
}