Skip to content

Commit

Permalink
[red-knot] Rename FileSystem to System (#12214)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Jul 9, 2024
1 parent 16a63c8 commit ac04380
Show file tree
Hide file tree
Showing 38 changed files with 1,429 additions and 1,288 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

20 changes: 10 additions & 10 deletions crates/red_knot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_hash::FxHashSet;

use ruff_db::file_system::{FileSystemPath, FileSystemPathBuf};
use ruff_db::vfs::VfsFile;
use ruff_db::files::File;
use ruff_db::system::{SystemPath, SystemPathBuf};

use crate::db::Jar;

Expand All @@ -12,41 +12,41 @@ pub mod watch;

#[derive(Debug, Clone)]
pub struct Workspace {
root: FileSystemPathBuf,
root: SystemPathBuf,
/// The files that are open in the workspace.
///
/// * Editor: The files that are actively being edited in the editor (the user has a tab open with the file).
/// * CLI: The resolved files passed as arguments to the CLI.
open_files: FxHashSet<VfsFile>,
open_files: FxHashSet<File>,
}

impl Workspace {
pub fn new(root: FileSystemPathBuf) -> Self {
pub fn new(root: SystemPathBuf) -> Self {
Self {
root,
open_files: FxHashSet::default(),
}
}

pub fn root(&self) -> &FileSystemPath {
pub fn root(&self) -> &SystemPath {
self.root.as_path()
}

// TODO having the content in workspace feels wrong.
pub fn open_file(&mut self, file_id: VfsFile) {
pub fn open_file(&mut self, file_id: File) {
self.open_files.insert(file_id);
}

pub fn close_file(&mut self, file_id: VfsFile) {
pub fn close_file(&mut self, file_id: File) {
self.open_files.remove(&file_id);
}

// TODO introduce an `OpenFile` type instead of using an anonymous tuple.
pub fn open_files(&self) -> impl Iterator<Item = VfsFile> + '_ {
pub fn open_files(&self) -> impl Iterator<Item = File> + '_ {
self.open_files.iter().copied()
}

pub fn is_file_open(&self, file_id: VfsFile) -> bool {
pub fn is_file_open(&self, file_id: File) -> bool {
self.open_files.contains(&file_id)
}
}
6 changes: 3 additions & 3 deletions crates/red_knot/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use tracing::trace_span;
use red_knot_module_resolver::ModuleName;
use red_knot_python_semantic::types::Type;
use red_knot_python_semantic::{HasTy, SemanticModel};
use ruff_db::files::File;
use ruff_db::parsed::{parsed_module, ParsedModule};
use ruff_db::source::{source_text, SourceText};
use ruff_db::vfs::VfsFile;
use ruff_python_ast as ast;
use ruff_python_ast::visitor::{walk_stmt, Visitor};

Expand All @@ -22,7 +22,7 @@ use crate::db::Db;
pub(crate) fn unwind_if_cancelled(db: &dyn Db) {}

#[salsa::tracked(return_ref)]
pub(crate) fn lint_syntax(db: &dyn Db, file_id: VfsFile) -> Diagnostics {
pub(crate) fn lint_syntax(db: &dyn Db, file_id: File) -> Diagnostics {
#[allow(clippy::print_stdout)]
if std::env::var("RED_KNOT_SLOW_LINT").is_ok() {
for i in 0..10 {
Expand Down Expand Up @@ -74,7 +74,7 @@ fn lint_lines(source: &str, diagnostics: &mut Vec<String>) {
}

#[salsa::tracked(return_ref)]
pub(crate) fn lint_semantic(db: &dyn Db, file_id: VfsFile) -> Diagnostics {
pub(crate) fn lint_semantic(db: &dyn Db, file_id: File) -> Diagnostics {
let _span = trace_span!("lint_semantic", ?file_id).entered();

let source = source_text(db.upcast(), file_id);
Expand Down
14 changes: 7 additions & 7 deletions crates/red_knot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use red_knot::Workspace;
use red_knot_module_resolver::{
set_module_resolution_settings, RawModuleResolutionSettings, TargetVersion,
};
use ruff_db::file_system::{FileSystem, FileSystemPath, OsFileSystem};
use ruff_db::vfs::system_path_to_file;
use ruff_db::files::system_path_to_file;
use ruff_db::system::{OsSystem, System, SystemPath};

#[allow(
clippy::print_stdout,
Expand All @@ -35,15 +35,15 @@ pub fn main() -> anyhow::Result<()> {
return Err(anyhow::anyhow!("Invalid arguments"));
}

let fs = OsFileSystem;
let entry_point = FileSystemPath::new(&arguments[1]);
let system = OsSystem;
let entry_point = SystemPath::new(&arguments[1]);

if !fs.exists(entry_point) {
if !system.path_exists(entry_point) {
eprintln!("The entry point does not exist.");
return Err(anyhow::anyhow!("Invalid arguments"));
}

if !fs.is_file(entry_point) {
if !system.is_file(entry_point) {
eprintln!("The entry point is not a file.");
return Err(anyhow::anyhow!("Invalid arguments"));
}
Expand All @@ -55,7 +55,7 @@ pub fn main() -> anyhow::Result<()> {

let workspace_search_path = workspace.root().to_path_buf();

let mut program = Program::new(workspace, fs);
let mut program = Program::new(workspace, system);

set_module_resolution_settings(
&mut program,
Expand Down
6 changes: 3 additions & 3 deletions crates/red_knot/src/program/check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_db::vfs::VfsFile;
use ruff_db::files::File;
use salsa::Cancelled;

use crate::lint::{lint_semantic, lint_syntax, Diagnostics};
Expand All @@ -19,11 +19,11 @@ impl Program {
}

#[tracing::instrument(level = "debug", skip(self))]
pub fn check_file(&self, file: VfsFile) -> Result<Diagnostics, Cancelled> {
pub fn check_file(&self, file: File) -> Result<Diagnostics, Cancelled> {
self.with_db(|db| db.check_file_impl(file))
}

fn check_file_impl(&self, file: VfsFile) -> Diagnostics {
fn check_file_impl(&self, file: File) -> Diagnostics {
let mut diagnostics = Vec::new();
diagnostics.extend_from_slice(lint_syntax(self, file));
diagnostics.extend_from_slice(lint_semantic(self, file));
Expand Down
43 changes: 24 additions & 19 deletions crates/red_knot/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use std::sync::Arc;

use salsa::{Cancelled, Database};

use red_knot_module_resolver::{Db as ResolverDb, Jar as ResolverJar};
use red_knot_module_resolver::{vendored_typeshed_stubs, Db as ResolverDb, Jar as ResolverJar};
use red_knot_python_semantic::{Db as SemanticDb, Jar as SemanticJar};
use ruff_db::file_system::{FileSystem, FileSystemPathBuf};
use ruff_db::vfs::{Vfs, VfsFile, VfsPath};
use ruff_db::files::{File, FilePath, Files};
use ruff_db::system::{System, SystemPathBuf};
use ruff_db::vendored::VendoredFileSystem;
use ruff_db::{Db as SourceDb, Jar as SourceJar, Upcast};

use crate::db::{Db, Jar};
Expand All @@ -17,20 +18,20 @@ mod check;
#[salsa::db(SourceJar, ResolverJar, SemanticJar, Jar)]
pub struct Program {
storage: salsa::Storage<Program>,
vfs: Vfs,
fs: Arc<dyn FileSystem + Send + Sync + RefUnwindSafe>,
files: Files,
system: Arc<dyn System + Send + Sync + RefUnwindSafe>,
workspace: Workspace,
}

impl Program {
pub fn new<Fs>(workspace: Workspace, file_system: Fs) -> Self
pub fn new<S>(workspace: Workspace, system: S) -> Self
where
Fs: FileSystem + 'static + Send + Sync + RefUnwindSafe,
S: System + 'static + Send + Sync + RefUnwindSafe,
{
Self {
storage: salsa::Storage::default(),
vfs: Vfs::default(),
fs: Arc::new(file_system),
files: Files::default(),
system: Arc::new(system),
workspace,
}
}
Expand All @@ -40,7 +41,7 @@ impl Program {
I: IntoIterator<Item = FileWatcherChange>,
{
for change in changes {
VfsFile::touch_path(self, &VfsPath::file_system(change.path));
File::touch_path(self, &FilePath::system(change.path));
}
}

Expand All @@ -57,7 +58,7 @@ impl Program {
where
F: FnOnce(&Program) -> T + UnwindSafe,
{
// TODO: Catch in `Caancelled::catch`
// TODO: Catch in `Cancelled::catch`
// See https://salsa.zulipchat.com/#narrow/stream/145099-general/topic/How.20to.20use.20.60Cancelled.3A.3Acatch.60
Ok(f(self))
}
Expand Down Expand Up @@ -86,12 +87,16 @@ impl ResolverDb for Program {}
impl SemanticDb for Program {}

impl SourceDb for Program {
fn file_system(&self) -> &dyn FileSystem {
&*self.fs
fn vendored(&self) -> &VendoredFileSystem {
vendored_typeshed_stubs()
}

fn vfs(&self) -> &Vfs {
&self.vfs
fn system(&self) -> &dyn System {
&*self.system
}

fn files(&self) -> &Files {
&self.files
}
}

Expand All @@ -103,22 +108,22 @@ impl salsa::ParallelDatabase for Program {
fn snapshot(&self) -> salsa::Snapshot<Self> {
salsa::Snapshot::new(Self {
storage: self.storage.snapshot(),
vfs: self.vfs.snapshot(),
fs: self.fs.clone(),
files: self.files.snapshot(),
system: self.system.clone(),
workspace: self.workspace.clone(),
})
}
}

#[derive(Clone, Debug)]
pub struct FileWatcherChange {
path: FileSystemPathBuf,
path: SystemPathBuf,
#[allow(unused)]
kind: FileChangeKind,
}

impl FileWatcherChange {
pub fn new(path: FileSystemPathBuf, kind: FileChangeKind) -> Self {
pub fn new(path: SystemPathBuf, kind: FileChangeKind) -> Self {
Self { path, kind }
}
}
Expand Down
8 changes: 5 additions & 3 deletions crates/red_knot/src/watch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::path::Path;

use crate::program::{FileChangeKind, FileWatcherChange};
use anyhow::Context;
use notify::event::{CreateKind, RemoveKind};
use notify::{recommended_watcher, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use ruff_db::file_system::FileSystemPath;

use ruff_db::system::SystemPath;

use crate::program::{FileChangeKind, FileWatcherChange};

pub struct FileWatcher {
watcher: RecommendedWatcher,
Expand Down Expand Up @@ -50,7 +52,7 @@ impl FileWatcher {

for path in event.paths {
if path.is_file() {
if let Some(fs_path) = FileSystemPath::from_std_path(&path) {
if let Some(fs_path) = SystemPath::from_std_path(&path) {
changes.push(FileWatcherChange::new(
fs_path.to_path_buf(),
change_kind,
Expand Down
1 change: 1 addition & 0 deletions crates/red_knot_module_resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ruff_python_stdlib = { workspace = true }

compact_str = { workspace = true }
camino = { workspace = true }
once_cell = { workspace = true }
rustc-hash = { workspace = true }
salsa = { workspace = true }
tracing = { workspace = true }
Expand Down
Loading

0 comments on commit ac04380

Please sign in to comment.