Skip to content

Commit

Permalink
[red-knot]: Add a VendoredFileSystem implementation (#11863)
Browse files Browse the repository at this point in the history
Co-authored-by: Micha Reiser <micha@reiser.io>
  • Loading branch information
AlexWaygood and MichaReiser authored Jun 18, 2024
1 parent f666d79 commit 1d73d60
Show file tree
Hide file tree
Showing 10 changed files with 582 additions and 172 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/ruff_db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ camino = { workspace = true }
countme = { workspace = true }
dashmap = { workspace = true }
filetime = { workspace = true }
itertools = { workspace = true }
salsa = { workspace = true }
tracing = { workspace = true }
rustc-hash = { workspace = true }
zip = { workspace = true }

[dev-dependencies]
once_cell = { workspace = true }
69 changes: 69 additions & 0 deletions crates/ruff_db/src/file_revision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// A number representing the revision of a file.
///
/// Two revisions that don't compare equal signify that the file has been modified.
/// Revisions aren't guaranteed to be monotonically increasing or in any specific order.
///
/// Possible revisions are:
/// * The last modification time of the file.
/// * The hash of the file's content.
/// * The revision as it comes from an external system, for example the LSP.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct FileRevision(u128);

impl FileRevision {
pub fn new(value: u128) -> Self {
Self(value)
}

pub const fn zero() -> Self {
Self(0)
}

#[must_use]
pub fn as_u128(self) -> u128 {
self.0
}
}

impl From<u128> for FileRevision {
fn from(value: u128) -> Self {
FileRevision(value)
}
}

impl From<u64> for FileRevision {
fn from(value: u64) -> Self {
FileRevision(u128::from(value))
}
}

impl From<filetime::FileTime> for FileRevision {
fn from(value: filetime::FileTime) -> Self {
let seconds = value.seconds() as u128;
let seconds = seconds << 64;
let nanos = u128::from(value.nanoseconds());

FileRevision(seconds | nanos)
}
}

#[cfg(test)]
mod tests {
use filetime::FileTime;

use super::*;

#[test]
fn revision_from_file_time() {
let file_time = FileTime::now();
let revision = FileRevision::from(file_time);

let revision = revision.as_u128();

let nano = revision & 0xFFFF_FFFF_FFFF_FFFF;
let seconds = revision >> 64;

assert_eq!(file_time.nanoseconds(), nano as u32);
assert_eq!(file_time.seconds(), seconds as i64);
}
}
72 changes: 1 addition & 71 deletions crates/ruff_db/src/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::ops::Deref;
use std::path::{Path, StripPrefixError};

use camino::{Utf8Path, Utf8PathBuf};
use filetime::FileTime;

use crate::file_revision::FileRevision;
pub use memory::MemoryFileSystem;
pub use os::OsFileSystem;

Expand Down Expand Up @@ -514,55 +514,6 @@ impl Metadata {
}
}

/// A number representing the revision of a file.
///
/// Two revisions that don't compare equal signify that the file has been modified.
/// Revisions aren't guaranteed to be monotonically increasing or in any specific order.
///
/// Possible revisions are:
/// * The last modification time of the file.
/// * The hash of the file's content.
/// * The revision as it comes from an external system, for example the LSP.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct FileRevision(u128);

impl FileRevision {
pub fn new(value: u128) -> Self {
Self(value)
}

pub const fn zero() -> Self {
Self(0)
}

#[must_use]
pub fn as_u128(self) -> u128 {
self.0
}
}

impl From<u128> for FileRevision {
fn from(value: u128) -> Self {
FileRevision(value)
}
}

impl From<u64> for FileRevision {
fn from(value: u64) -> Self {
FileRevision(u128::from(value))
}
}

impl From<FileTime> for FileRevision {
fn from(value: FileTime) -> Self {
let seconds = value.seconds() as u128;
let seconds = seconds << 64;
let nanos = u128::from(value.nanoseconds());

FileRevision(seconds | nanos)
}
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub enum FileType {
File,
Expand All @@ -583,24 +534,3 @@ impl FileType {
matches!(self, FileType::Symlink)
}
}

#[cfg(test)]
mod tests {
use filetime::FileTime;

use crate::file_system::FileRevision;

#[test]
fn revision_from_file_time() {
let file_time = FileTime::now();
let revision = FileRevision::from(file_time);

let revision = revision.as_u128();

let nano = revision & 0xFFFF_FFFF_FFFF_FFFF;
let seconds = revision >> 64;

assert_eq!(file_time.nanoseconds(), nano as u32);
assert_eq!(file_time.seconds(), seconds as i64);
}
}
2 changes: 2 additions & 0 deletions crates/ruff_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use crate::parsed::parsed_module;
use crate::source::{line_index, source_text};
use crate::vfs::{Vfs, VfsFile};

mod file_revision;
pub mod file_system;
pub mod parsed;
pub mod source;
pub mod vendored;
pub mod vfs;

pub(crate) type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_db/src/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod tests {
use crate::file_system::FileSystemPath;
use crate::parsed::parsed_module;
use crate::tests::TestDb;
use crate::vfs::VendoredPath;
use crate::vendored::VendoredPath;
use crate::vfs::{system_path_to_file, vendored_path_to_file};

#[test]
Expand Down
Loading

0 comments on commit 1d73d60

Please sign in to comment.