Skip to content

Commit 1d73d60

Browse files
[red-knot]: Add a VendoredFileSystem implementation (#11863)
Co-authored-by: Micha Reiser <[email protected]>
1 parent f666d79 commit 1d73d60

File tree

10 files changed

+582
-172
lines changed

10 files changed

+582
-172
lines changed

Cargo.lock

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff_db/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ camino = { workspace = true }
2020
countme = { workspace = true }
2121
dashmap = { workspace = true }
2222
filetime = { workspace = true }
23+
itertools = { workspace = true }
2324
salsa = { workspace = true }
2425
tracing = { workspace = true }
2526
rustc-hash = { workspace = true }
27+
zip = { workspace = true }
28+
29+
[dev-dependencies]
30+
once_cell = { workspace = true }

crates/ruff_db/src/file_revision.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/// A number representing the revision of a file.
2+
///
3+
/// Two revisions that don't compare equal signify that the file has been modified.
4+
/// Revisions aren't guaranteed to be monotonically increasing or in any specific order.
5+
///
6+
/// Possible revisions are:
7+
/// * The last modification time of the file.
8+
/// * The hash of the file's content.
9+
/// * The revision as it comes from an external system, for example the LSP.
10+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
11+
pub struct FileRevision(u128);
12+
13+
impl FileRevision {
14+
pub fn new(value: u128) -> Self {
15+
Self(value)
16+
}
17+
18+
pub const fn zero() -> Self {
19+
Self(0)
20+
}
21+
22+
#[must_use]
23+
pub fn as_u128(self) -> u128 {
24+
self.0
25+
}
26+
}
27+
28+
impl From<u128> for FileRevision {
29+
fn from(value: u128) -> Self {
30+
FileRevision(value)
31+
}
32+
}
33+
34+
impl From<u64> for FileRevision {
35+
fn from(value: u64) -> Self {
36+
FileRevision(u128::from(value))
37+
}
38+
}
39+
40+
impl From<filetime::FileTime> for FileRevision {
41+
fn from(value: filetime::FileTime) -> Self {
42+
let seconds = value.seconds() as u128;
43+
let seconds = seconds << 64;
44+
let nanos = u128::from(value.nanoseconds());
45+
46+
FileRevision(seconds | nanos)
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use filetime::FileTime;
53+
54+
use super::*;
55+
56+
#[test]
57+
fn revision_from_file_time() {
58+
let file_time = FileTime::now();
59+
let revision = FileRevision::from(file_time);
60+
61+
let revision = revision.as_u128();
62+
63+
let nano = revision & 0xFFFF_FFFF_FFFF_FFFF;
64+
let seconds = revision >> 64;
65+
66+
assert_eq!(file_time.nanoseconds(), nano as u32);
67+
assert_eq!(file_time.seconds(), seconds as i64);
68+
}
69+
}

crates/ruff_db/src/file_system.rs

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::ops::Deref;
33
use std::path::{Path, StripPrefixError};
44

55
use camino::{Utf8Path, Utf8PathBuf};
6-
use filetime::FileTime;
76

7+
use crate::file_revision::FileRevision;
88
pub use memory::MemoryFileSystem;
99
pub use os::OsFileSystem;
1010

@@ -514,55 +514,6 @@ impl Metadata {
514514
}
515515
}
516516

517-
/// A number representing the revision of a file.
518-
///
519-
/// Two revisions that don't compare equal signify that the file has been modified.
520-
/// Revisions aren't guaranteed to be monotonically increasing or in any specific order.
521-
///
522-
/// Possible revisions are:
523-
/// * The last modification time of the file.
524-
/// * The hash of the file's content.
525-
/// * The revision as it comes from an external system, for example the LSP.
526-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
527-
pub struct FileRevision(u128);
528-
529-
impl FileRevision {
530-
pub fn new(value: u128) -> Self {
531-
Self(value)
532-
}
533-
534-
pub const fn zero() -> Self {
535-
Self(0)
536-
}
537-
538-
#[must_use]
539-
pub fn as_u128(self) -> u128 {
540-
self.0
541-
}
542-
}
543-
544-
impl From<u128> for FileRevision {
545-
fn from(value: u128) -> Self {
546-
FileRevision(value)
547-
}
548-
}
549-
550-
impl From<u64> for FileRevision {
551-
fn from(value: u64) -> Self {
552-
FileRevision(u128::from(value))
553-
}
554-
}
555-
556-
impl From<FileTime> for FileRevision {
557-
fn from(value: FileTime) -> Self {
558-
let seconds = value.seconds() as u128;
559-
let seconds = seconds << 64;
560-
let nanos = u128::from(value.nanoseconds());
561-
562-
FileRevision(seconds | nanos)
563-
}
564-
}
565-
566517
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
567518
pub enum FileType {
568519
File,
@@ -583,24 +534,3 @@ impl FileType {
583534
matches!(self, FileType::Symlink)
584535
}
585536
}
586-
587-
#[cfg(test)]
588-
mod tests {
589-
use filetime::FileTime;
590-
591-
use crate::file_system::FileRevision;
592-
593-
#[test]
594-
fn revision_from_file_time() {
595-
let file_time = FileTime::now();
596-
let revision = FileRevision::from(file_time);
597-
598-
let revision = revision.as_u128();
599-
600-
let nano = revision & 0xFFFF_FFFF_FFFF_FFFF;
601-
let seconds = revision >> 64;
602-
603-
assert_eq!(file_time.nanoseconds(), nano as u32);
604-
assert_eq!(file_time.seconds(), seconds as i64);
605-
}
606-
}

crates/ruff_db/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ use crate::parsed::parsed_module;
88
use crate::source::{line_index, source_text};
99
use crate::vfs::{Vfs, VfsFile};
1010

11+
mod file_revision;
1112
pub mod file_system;
1213
pub mod parsed;
1314
pub mod source;
15+
pub mod vendored;
1416
pub mod vfs;
1517

1618
pub(crate) type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;

crates/ruff_db/src/parsed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod tests {
7373
use crate::file_system::FileSystemPath;
7474
use crate::parsed::parsed_module;
7575
use crate::tests::TestDb;
76-
use crate::vfs::VendoredPath;
76+
use crate::vendored::VendoredPath;
7777
use crate::vfs::{system_path_to_file, vendored_path_to_file};
7878

7979
#[test]

0 commit comments

Comments
 (0)