diff --git a/crates/ruff_db/src/vendored/path.rs b/crates/ruff_db/src/vendored/path.rs index f32ce08239..b68a487246 100644 --- a/crates/ruff_db/src/vendored/path.rs +++ b/crates/ruff_db/src/vendored/path.rs @@ -17,6 +17,10 @@ impl VendoredPath { unsafe { &*(path as *const Utf8Path as *const VendoredPath) } } + pub fn file_name(&self) -> Option<&str> { + self.0.file_name() + } + pub fn to_path_buf(&self) -> VendoredPathBuf { VendoredPathBuf(self.0.to_path_buf()) } diff --git a/crates/ty_python_semantic/src/module_resolver/path.rs b/crates/ty_python_semantic/src/module_resolver/path.rs index bb451b3630..92113426c7 100644 --- a/crates/ty_python_semantic/src/module_resolver/path.rs +++ b/crates/ty_python_semantic/src/module_resolver/path.rs @@ -4,11 +4,12 @@ use std::fmt; use std::sync::Arc; use camino::{Utf8Path, Utf8PathBuf}; -use ruff_db::files::{File, FileError, system_path_to_file, vendored_path_to_file}; +use ruff_db::files::{File, FileError, FilePath, system_path_to_file, vendored_path_to_file}; use ruff_db::system::{System, SystemPath, SystemPathBuf}; use ruff_db::vendored::{VendoredPath, VendoredPathBuf}; use super::typeshed::{TypeshedVersionsParseError, TypeshedVersionsQueryResult, typeshed_versions}; +use crate::Db; use crate::module_name::ModuleName; use crate::module_resolver::resolver::ResolverContext; use crate::site_packages::SitePackagesDiscoveryError; @@ -652,6 +653,48 @@ impl fmt::Display for SearchPath { } } +#[derive(Debug, Clone)] +pub(super) enum SystemOrVendoredPathRef<'db> { + System(&'db SystemPath), + Vendored(&'db VendoredPath), +} + +impl<'db> SystemOrVendoredPathRef<'db> { + pub(super) fn try_from_file(db: &'db dyn Db, file: File) -> Option { + match file.path(db) { + FilePath::System(system) => Some(Self::System(system)), + FilePath::Vendored(vendored) => Some(Self::Vendored(vendored)), + FilePath::SystemVirtual(_) => None, + } + } + + pub(super) fn file_name(&self) -> Option<&str> { + match self { + Self::System(system) => system.file_name(), + Self::Vendored(vendored) => vendored.file_name(), + } + } + + pub(super) fn parent<'a>(&'a self) -> Option> + where + 'a: 'db, + { + match self { + Self::System(system) => system.parent().map(Self::System), + Self::Vendored(vendored) => vendored.parent().map(Self::Vendored), + } + } +} + +impl std::fmt::Display for SystemOrVendoredPathRef<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SystemOrVendoredPathRef::System(system) => system.fmt(f), + SystemOrVendoredPathRef::Vendored(vendored) => vendored.fmt(f), + } + } +} + #[cfg(test)] mod tests { use ruff_db::Db; diff --git a/crates/ty_python_semantic/src/module_resolver/resolver.rs b/crates/ty_python_semantic/src/module_resolver/resolver.rs index be6f40c246..5c670e6d2d 100644 --- a/crates/ty_python_semantic/src/module_resolver/resolver.rs +++ b/crates/ty_python_semantic/src/module_resolver/resolver.rs @@ -8,7 +8,7 @@ use rustc_hash::{FxBuildHasher, FxHashSet}; use ruff_db::files::{File, FilePath, FileRootKind}; use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf}; -use ruff_db::vendored::{VendoredFileSystem, VendoredPath}; +use ruff_db::vendored::VendoredFileSystem; use ruff_python_ast::PythonVersion; use crate::db::Db; @@ -17,7 +17,7 @@ use crate::module_resolver::typeshed::{TypeshedVersions, vendored_typeshed_versi use crate::{Program, SearchPathSettings}; use super::module::{Module, ModuleKind}; -use super::path::{ModulePath, SearchPath, SearchPathValidationError}; +use super::path::{ModulePath, SearchPath, SearchPathValidationError, SystemOrVendoredPathRef}; /// Resolves a module name to a module. pub fn resolve_module(db: &dyn Db, module_name: &ModuleName) -> Option { @@ -77,21 +77,6 @@ pub(crate) fn path_to_module(db: &dyn Db, path: &FilePath) -> Option { file_to_module(db, file) } -#[derive(Debug, Clone, Copy)] -enum SystemOrVendoredPathRef<'a> { - System(&'a SystemPath), - Vendored(&'a VendoredPath), -} - -impl std::fmt::Display for SystemOrVendoredPathRef<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - SystemOrVendoredPathRef::System(system) => system.fmt(f), - SystemOrVendoredPathRef::Vendored(vendored) => vendored.fmt(f), - } - } -} - /// Resolves the module for the file with the given id. /// /// Returns `None` if the file is not a module locatable via any of the known search paths. @@ -99,11 +84,7 @@ impl std::fmt::Display for SystemOrVendoredPathRef<'_> { pub(crate) fn file_to_module(db: &dyn Db, file: File) -> Option { let _span = tracing::trace_span!("file_to_module", ?file).entered(); - let path = match file.path(db) { - FilePath::System(system) => SystemOrVendoredPathRef::System(system), - FilePath::Vendored(vendored) => SystemOrVendoredPathRef::Vendored(vendored), - FilePath::SystemVirtual(_) => return None, - }; + let path = SystemOrVendoredPathRef::try_from_file(db, file)?; let module_name = search_paths(db).find_map(|candidate| { let relative_path = match path {