mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00

## Summary This PR updates the formatter and linter to use the `PythonVersion` struct from the `ruff_python_ast` crate internally. While this doesn't remove the need for the `linter::PythonVersion` enum, it does remove the `formatter::PythonVersion` enum and limits the use in the linter to deserializing from CLI arguments and config files and moves most of the remaining methods to the `ast::PythonVersion` struct. ## Test Plan Existing tests, with some inputs and outputs updated to reflect the new (de)serialization format. I think these are test-specific and shouldn't affect any external (de)serialization. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
94 lines
2.3 KiB
Rust
94 lines
2.3 KiB
Rust
use anyhow::Result;
|
|
use std::sync::Arc;
|
|
use zip::CompressionMethod;
|
|
|
|
use red_knot_python_semantic::lint::{LintRegistry, RuleSelection};
|
|
use red_knot_python_semantic::{
|
|
default_lint_registry, Db, Program, ProgramSettings, PythonPlatform, SearchPathSettings,
|
|
};
|
|
use ruff_db::files::{File, Files};
|
|
use ruff_db::system::{OsSystem, System, SystemPathBuf};
|
|
use ruff_db::vendored::{VendoredFileSystem, VendoredFileSystemBuilder};
|
|
use ruff_db::{Db as SourceDb, Upcast};
|
|
use ruff_python_ast::PythonVersion;
|
|
|
|
static EMPTY_VENDORED: std::sync::LazyLock<VendoredFileSystem> = std::sync::LazyLock::new(|| {
|
|
let mut builder = VendoredFileSystemBuilder::new(CompressionMethod::Stored);
|
|
builder.add_file("stdlib/VERSIONS", "\n").unwrap();
|
|
builder.finish().unwrap()
|
|
});
|
|
|
|
#[salsa::db]
|
|
#[derive(Default, Clone)]
|
|
pub struct ModuleDb {
|
|
storage: salsa::Storage<Self>,
|
|
files: Files,
|
|
system: OsSystem,
|
|
rule_selection: Arc<RuleSelection>,
|
|
}
|
|
|
|
impl ModuleDb {
|
|
/// Initialize a [`ModuleDb`] from the given source root.
|
|
pub fn from_src_roots(
|
|
src_roots: Vec<SystemPathBuf>,
|
|
python_version: PythonVersion,
|
|
) -> Result<Self> {
|
|
let search_paths = SearchPathSettings::new(src_roots);
|
|
|
|
let db = Self::default();
|
|
Program::from_settings(
|
|
&db,
|
|
ProgramSettings {
|
|
python_version,
|
|
python_platform: PythonPlatform::default(),
|
|
search_paths,
|
|
},
|
|
)?;
|
|
|
|
Ok(db)
|
|
}
|
|
}
|
|
|
|
impl Upcast<dyn SourceDb> for ModuleDb {
|
|
fn upcast(&self) -> &(dyn SourceDb + 'static) {
|
|
self
|
|
}
|
|
fn upcast_mut(&mut self) -> &mut (dyn SourceDb + 'static) {
|
|
self
|
|
}
|
|
}
|
|
|
|
#[salsa::db]
|
|
impl SourceDb for ModuleDb {
|
|
fn vendored(&self) -> &VendoredFileSystem {
|
|
&EMPTY_VENDORED
|
|
}
|
|
|
|
fn system(&self) -> &dyn System {
|
|
&self.system
|
|
}
|
|
|
|
fn files(&self) -> &Files {
|
|
&self.files
|
|
}
|
|
}
|
|
|
|
#[salsa::db]
|
|
impl Db for ModuleDb {
|
|
fn is_file_open(&self, file: File) -> bool {
|
|
!file.path(self).is_vendored_path()
|
|
}
|
|
|
|
fn rule_selection(&self) -> Arc<RuleSelection> {
|
|
self.rule_selection.clone()
|
|
}
|
|
|
|
fn lint_registry(&self) -> &LintRegistry {
|
|
default_lint_registry()
|
|
}
|
|
}
|
|
|
|
#[salsa::db]
|
|
impl salsa::Database for ModuleDb {
|
|
fn salsa_event(&self, _event: &dyn Fn() -> salsa::Event) {}
|
|
}
|