diff --git a/Cargo.lock b/Cargo.lock index 16d4ad9de1..aca0235ddf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4220,11 +4220,10 @@ dependencies = [ "ruff_source_file", "ruff_text_size", "rustc-hash", - "salsa", "smallvec", "tracing", + "ty_project", "ty_python_semantic", - "ty_vendored", ] [[package]] @@ -4258,7 +4257,6 @@ dependencies = [ "thiserror 2.0.12", "toml 0.9.2", "tracing", - "ty_ide", "ty_python_semantic", "ty_vendored", ] diff --git a/crates/ty_ide/Cargo.toml b/crates/ty_ide/Cargo.toml index 13efcda3ca..5f1b245397 100644 --- a/crates/ty_ide/Cargo.toml +++ b/crates/ty_ide/Cargo.toml @@ -19,16 +19,15 @@ ruff_python_trivia = { workspace = true } ruff_source_file = { workspace = true } ruff_text_size = { workspace = true } ty_python_semantic = { workspace = true } +ty_project = { workspace = true, features = ["testing"] } itertools = { workspace = true } regex = { workspace = true } rustc-hash = { workspace = true } -salsa = { workspace = true } smallvec = { workspace = true } tracing = { workspace = true } [dev-dependencies] -ty_vendored = { workspace = true } insta = { workspace = true, features = ["filters"] } diff --git a/crates/ty_ide/src/db.rs b/crates/ty_ide/src/db.rs deleted file mode 100644 index 6baa74dcc6..0000000000 --- a/crates/ty_ide/src/db.rs +++ /dev/null @@ -1,117 +0,0 @@ -use ty_python_semantic::Db as SemanticDb; - -#[salsa::db] -pub trait Db: SemanticDb {} - -#[cfg(test)] -pub(crate) mod tests { - use std::sync::{Arc, Mutex}; - - use super::Db; - use ruff_db::Db as SourceDb; - use ruff_db::files::{File, Files}; - use ruff_db::system::{DbWithTestSystem, System, TestSystem}; - use ruff_db::vendored::VendoredFileSystem; - use ty_python_semantic::lint::{LintRegistry, RuleSelection}; - use ty_python_semantic::{Db as SemanticDb, Program, default_lint_registry}; - - type Events = Arc>>; - - #[salsa::db] - #[derive(Clone)] - pub(crate) struct TestDb { - storage: salsa::Storage, - files: Files, - system: TestSystem, - vendored: VendoredFileSystem, - events: Events, - rule_selection: Arc, - } - - #[expect(dead_code)] - impl TestDb { - pub(crate) fn new() -> Self { - let events = Events::default(); - Self { - storage: salsa::Storage::new(Some(Box::new({ - let events = events.clone(); - move |event| { - tracing::trace!("event: {event:?}"); - let mut events = events.lock().unwrap(); - events.push(event); - } - }))), - system: TestSystem::default(), - vendored: ty_vendored::file_system().clone(), - events, - files: Files::default(), - rule_selection: Arc::new(RuleSelection::from_registry(default_lint_registry())), - } - } - - /// Takes the salsa events. - pub(crate) fn take_salsa_events(&mut self) -> Vec { - let mut events = self.events.lock().unwrap(); - - std::mem::take(&mut *events) - } - - /// Clears the salsa events. - /// - /// ## Panics - /// If there are any pending salsa snapshots. - pub(crate) fn clear_salsa_events(&mut self) { - self.take_salsa_events(); - } - } - - impl DbWithTestSystem for TestDb { - fn test_system(&self) -> &TestSystem { - &self.system - } - - fn test_system_mut(&mut self) -> &mut TestSystem { - &mut self.system - } - } - - #[salsa::db] - impl SourceDb for TestDb { - fn vendored(&self) -> &VendoredFileSystem { - &self.vendored - } - - fn system(&self) -> &dyn System { - &self.system - } - - fn files(&self) -> &Files { - &self.files - } - - fn python_version(&self) -> ruff_python_ast::PythonVersion { - Program::get(self).python_version(self) - } - } - - #[salsa::db] - impl SemanticDb for TestDb { - fn should_check_file(&self, file: File) -> bool { - !file.path(self).is_vendored_path() - } - - fn rule_selection(&self, _file: File) -> &RuleSelection { - &self.rule_selection - } - - fn lint_registry(&self) -> &LintRegistry { - default_lint_registry() - } - } - - #[salsa::db] - impl Db for TestDb {} - - #[salsa::db] - impl salsa::Database for TestDb {} -} diff --git a/crates/ty_ide/src/inlay_hints.rs b/crates/ty_ide/src/inlay_hints.rs index 670e83267f..aa1e170709 100644 --- a/crates/ty_ide/src/inlay_hints.rs +++ b/crates/ty_ide/src/inlay_hints.rs @@ -156,9 +156,8 @@ mod tests { }; use ruff_text_size::TextSize; - use crate::db::tests::TestDb; - use ruff_db::system::{DbWithWritableSystem, SystemPathBuf}; + use ty_project::ProjectMetadata; use ty_python_semantic::{ Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings, }; @@ -167,7 +166,10 @@ mod tests { const START: &str = ""; const END: &str = ""; - let mut db = TestDb::new(); + let mut db = ty_project::TestDb::new(ProjectMetadata::new( + "test".into(), + SystemPathBuf::from("/"), + )); let start = source.find(START); let end = source @@ -205,7 +207,7 @@ mod tests { } pub(super) struct InlayHintTest { - pub(super) db: TestDb, + pub(super) db: ty_project::TestDb, pub(super) file: File, pub(super) range: TextRange, } diff --git a/crates/ty_ide/src/lib.rs b/crates/ty_ide/src/lib.rs index ebb7c83e4c..206160bb67 100644 --- a/crates/ty_ide/src/lib.rs +++ b/crates/ty_ide/src/lib.rs @@ -1,5 +1,4 @@ mod completion; -mod db; mod docstring; mod find_node; mod goto; @@ -14,7 +13,6 @@ mod signature_help; mod stub_mapping; pub use completion::completion; -pub use db::Db; pub use docstring::get_parameter_documentation; pub use goto::{goto_declaration, goto_definition, goto_type_definition}; pub use hover::hover; @@ -29,6 +27,7 @@ use ruff_db::files::{File, FileRange}; use ruff_text_size::{Ranged, TextRange}; use rustc_hash::FxHashSet; use std::ops::{Deref, DerefMut}; +use ty_project::Db; use ty_python_semantic::types::{Type, TypeDefinition}; /// Information associated with a text range. @@ -211,13 +210,13 @@ impl HasNavigationTargets for TypeDefinition<'_> { #[cfg(test)] mod tests { - use crate::db::tests::TestDb; use insta::internals::SettingsBindDropGuard; use ruff_db::Db; use ruff_db::diagnostic::{Diagnostic, DiagnosticFormat, DisplayDiagnosticConfig}; use ruff_db::files::{File, system_path_to_file}; use ruff_db::system::{DbWithWritableSystem, SystemPath, SystemPathBuf}; use ruff_text_size::TextSize; + use ty_project::ProjectMetadata; use ty_python_semantic::{ Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings, }; @@ -231,7 +230,7 @@ mod tests { } pub(super) struct CursorTest { - pub(super) db: TestDb, + pub(super) db: ty_project::TestDb, pub(super) cursor: Cursor, _insta_settings_guard: SettingsBindDropGuard, } @@ -286,7 +285,11 @@ mod tests { impl CursorTestBuilder { pub(super) fn build(&self) -> CursorTest { - let mut db = TestDb::new(); + let mut db = ty_project::TestDb::new(ProjectMetadata::new( + "test".into(), + SystemPathBuf::from("/"), + )); + let mut cursor: Option = None; for &Source { ref path, diff --git a/crates/ty_project/Cargo.toml b/crates/ty_project/Cargo.toml index dcef49bd4a..18684975e9 100644 --- a/crates/ty_project/Cargo.toml +++ b/crates/ty_project/Cargo.toml @@ -19,7 +19,6 @@ ruff_options_metadata = { workspace = true } ruff_python_ast = { workspace = true, features = ["serde"] } ruff_python_formatter = { workspace = true, optional = true } ruff_text_size = { workspace = true } -ty_ide = { workspace = true } ty_python_semantic = { workspace = true, features = ["serde"] } ty_vendored = { workspace = true } @@ -53,6 +52,7 @@ deflate = ["ty_vendored/deflate"] schemars = ["dep:schemars", "ruff_db/schemars", "ty_python_semantic/schemars"] zstd = ["ty_vendored/zstd"] format = ["ruff_python_formatter"] +testing = [] [lints] workspace = true diff --git a/crates/ty_project/src/db.rs b/crates/ty_project/src/db.rs index 5270495e86..94531e4310 100644 --- a/crates/ty_project/src/db.rs +++ b/crates/ty_project/src/db.rs @@ -14,7 +14,6 @@ use ruff_db::system::System; use ruff_db::vendored::VendoredFileSystem; use salsa::plumbing::ZalsaDatabase; use salsa::{Event, Setter}; -use ty_ide::Db as IdeDb; use ty_python_semantic::lint::{LintRegistry, RuleSelection}; use ty_python_semantic::{Db as SemanticDb, Program}; @@ -404,9 +403,6 @@ impl SalsaMemoryDump { } } -#[salsa::db] -impl IdeDb for ProjectDatabase {} - #[salsa::db] impl SemanticDb for ProjectDatabase { fn should_check_file(&self, file: File) -> bool { @@ -468,7 +464,7 @@ mod format { } } -#[cfg(test)] +#[cfg(any(test, feature = "testing"))] pub(crate) mod tests { use std::sync::{Arc, Mutex}; @@ -487,7 +483,7 @@ pub(crate) mod tests { #[salsa::db] #[derive(Clone)] - pub(crate) struct TestDb { + pub struct TestDb { storage: salsa::Storage, events: Events, files: Files, @@ -497,7 +493,7 @@ pub(crate) mod tests { } impl TestDb { - pub(crate) fn new(project: ProjectMetadata) -> Self { + pub fn new(project: ProjectMetadata) -> Self { let events = Events::default(); let mut db = Self { storage: salsa::Storage::new(Some(Box::new({ @@ -522,7 +518,7 @@ pub(crate) mod tests { impl TestDb { /// Takes the salsa events. - pub(crate) fn take_salsa_events(&mut self) -> Vec { + pub fn take_salsa_events(&mut self) -> Vec { let mut events = self.events.lock().unwrap(); std::mem::take(&mut *events) diff --git a/crates/ty_project/src/lib.rs b/crates/ty_project/src/lib.rs index 84a9e40228..4f967dcc6d 100644 --- a/crates/ty_project/src/lib.rs +++ b/crates/ty_project/src/lib.rs @@ -1,6 +1,8 @@ use crate::glob::{GlobFilterCheckMode, IncludeResult}; use crate::metadata::options::{OptionDiagnostic, ToSettingsError}; use crate::walk::{ProjectFilesFilter, ProjectFilesWalker}; +#[cfg(feature = "testing")] +pub use db::tests::TestDb; pub use db::{ChangeResult, CheckMode, Db, ProjectDatabase, SalsaMemoryDump}; use files::{Index, Indexed, IndexedFiles}; use metadata::settings::Settings; diff --git a/crates/ty_server/src/document/location.rs b/crates/ty_server/src/document/location.rs index eb3279000e..467f35d8cf 100644 --- a/crates/ty_server/src/document/location.rs +++ b/crates/ty_server/src/document/location.rs @@ -5,14 +5,15 @@ use lsp_types::Location; use ruff_db::files::FileRange; use ruff_db::source::{line_index, source_text}; use ruff_text_size::Ranged; -use ty_ide::{Db, NavigationTarget}; +use ty_ide::NavigationTarget; +use ty_project::Db; pub(crate) trait ToLink { - fn to_location(&self, db: &dyn ty_ide::Db, encoding: PositionEncoding) -> Option; + fn to_location(&self, db: &dyn Db, encoding: PositionEncoding) -> Option; fn to_link( &self, - db: &dyn ty_ide::Db, + db: &dyn Db, src: Option, encoding: PositionEncoding, ) -> Option;