mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:50:38 +00:00
[ty] Move SystemOrVendoredPathRef
This moves the type and adds a few methods so that it can be used elsewhere.
This commit is contained in:
parent
729fa12575
commit
948463aafa
3 changed files with 51 additions and 23 deletions
|
@ -17,6 +17,10 @@ impl VendoredPath {
|
||||||
unsafe { &*(path as *const Utf8Path as *const 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 {
|
pub fn to_path_buf(&self) -> VendoredPathBuf {
|
||||||
VendoredPathBuf(self.0.to_path_buf())
|
VendoredPathBuf(self.0.to_path_buf())
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,12 @@ use std::fmt;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use camino::{Utf8Path, Utf8PathBuf};
|
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::system::{System, SystemPath, SystemPathBuf};
|
||||||
use ruff_db::vendored::{VendoredPath, VendoredPathBuf};
|
use ruff_db::vendored::{VendoredPath, VendoredPathBuf};
|
||||||
|
|
||||||
use super::typeshed::{TypeshedVersionsParseError, TypeshedVersionsQueryResult, typeshed_versions};
|
use super::typeshed::{TypeshedVersionsParseError, TypeshedVersionsQueryResult, typeshed_versions};
|
||||||
|
use crate::Db;
|
||||||
use crate::module_name::ModuleName;
|
use crate::module_name::ModuleName;
|
||||||
use crate::module_resolver::resolver::ResolverContext;
|
use crate::module_resolver::resolver::ResolverContext;
|
||||||
use crate::site_packages::SitePackagesDiscoveryError;
|
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<Self> {
|
||||||
|
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<SystemOrVendoredPathRef<'a>>
|
||||||
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ruff_db::Db;
|
use ruff_db::Db;
|
||||||
|
|
|
@ -8,7 +8,7 @@ use rustc_hash::{FxBuildHasher, FxHashSet};
|
||||||
|
|
||||||
use ruff_db::files::{File, FilePath, FileRootKind};
|
use ruff_db::files::{File, FilePath, FileRootKind};
|
||||||
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
|
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 ruff_python_ast::PythonVersion;
|
||||||
|
|
||||||
use crate::db::Db;
|
use crate::db::Db;
|
||||||
|
@ -17,7 +17,7 @@ use crate::module_resolver::typeshed::{TypeshedVersions, vendored_typeshed_versi
|
||||||
use crate::{Program, SearchPathSettings};
|
use crate::{Program, SearchPathSettings};
|
||||||
|
|
||||||
use super::module::{Module, ModuleKind};
|
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.
|
/// Resolves a module name to a module.
|
||||||
pub fn resolve_module(db: &dyn Db, module_name: &ModuleName) -> Option<Module> {
|
pub fn resolve_module(db: &dyn Db, module_name: &ModuleName) -> Option<Module> {
|
||||||
|
@ -77,21 +77,6 @@ pub(crate) fn path_to_module(db: &dyn Db, path: &FilePath) -> Option<Module> {
|
||||||
file_to_module(db, file)
|
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.
|
/// 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.
|
/// 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<Module> {
|
pub(crate) fn file_to_module(db: &dyn Db, file: File) -> Option<Module> {
|
||||||
let _span = tracing::trace_span!("file_to_module", ?file).entered();
|
let _span = tracing::trace_span!("file_to_module", ?file).entered();
|
||||||
|
|
||||||
let path = match file.path(db) {
|
let path = SystemOrVendoredPathRef::try_from_file(db, file)?;
|
||||||
FilePath::System(system) => SystemOrVendoredPathRef::System(system),
|
|
||||||
FilePath::Vendored(vendored) => SystemOrVendoredPathRef::Vendored(vendored),
|
|
||||||
FilePath::SystemVirtual(_) => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let module_name = search_paths(db).find_map(|candidate| {
|
let module_name = search_paths(db).find_map(|candidate| {
|
||||||
let relative_path = match path {
|
let relative_path = match path {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue