[ty] Invert ty_ide and ty_project dependency (#19501)

This commit is contained in:
Micha Reiser 2025-07-23 09:37:46 +02:00 committed by GitHub
parent 53d795da67
commit c281891b5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 27 additions and 143 deletions

4
Cargo.lock generated
View file

@ -4220,11 +4220,10 @@ dependencies = [
"ruff_source_file", "ruff_source_file",
"ruff_text_size", "ruff_text_size",
"rustc-hash", "rustc-hash",
"salsa",
"smallvec", "smallvec",
"tracing", "tracing",
"ty_project",
"ty_python_semantic", "ty_python_semantic",
"ty_vendored",
] ]
[[package]] [[package]]
@ -4258,7 +4257,6 @@ dependencies = [
"thiserror 2.0.12", "thiserror 2.0.12",
"toml 0.9.2", "toml 0.9.2",
"tracing", "tracing",
"ty_ide",
"ty_python_semantic", "ty_python_semantic",
"ty_vendored", "ty_vendored",
] ]

View file

@ -19,16 +19,15 @@ ruff_python_trivia = { workspace = true }
ruff_source_file = { workspace = true } ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true } ruff_text_size = { workspace = true }
ty_python_semantic = { workspace = true } ty_python_semantic = { workspace = true }
ty_project = { workspace = true, features = ["testing"] }
itertools = { workspace = true } itertools = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
salsa = { workspace = true }
smallvec = { workspace = true } smallvec = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
[dev-dependencies] [dev-dependencies]
ty_vendored = { workspace = true }
insta = { workspace = true, features = ["filters"] } insta = { workspace = true, features = ["filters"] }

View file

@ -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<Mutex<Vec<salsa::Event>>>;
#[salsa::db]
#[derive(Clone)]
pub(crate) struct TestDb {
storage: salsa::Storage<Self>,
files: Files,
system: TestSystem,
vendored: VendoredFileSystem,
events: Events,
rule_selection: Arc<RuleSelection>,
}
#[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<salsa::Event> {
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 {}
}

View file

@ -156,9 +156,8 @@ mod tests {
}; };
use ruff_text_size::TextSize; use ruff_text_size::TextSize;
use crate::db::tests::TestDb;
use ruff_db::system::{DbWithWritableSystem, SystemPathBuf}; use ruff_db::system::{DbWithWritableSystem, SystemPathBuf};
use ty_project::ProjectMetadata;
use ty_python_semantic::{ use ty_python_semantic::{
Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings, Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings,
}; };
@ -167,7 +166,10 @@ mod tests {
const START: &str = "<START>"; const START: &str = "<START>";
const END: &str = "<END>"; const END: &str = "<END>";
let mut db = TestDb::new(); let mut db = ty_project::TestDb::new(ProjectMetadata::new(
"test".into(),
SystemPathBuf::from("/"),
));
let start = source.find(START); let start = source.find(START);
let end = source let end = source
@ -205,7 +207,7 @@ mod tests {
} }
pub(super) struct InlayHintTest { pub(super) struct InlayHintTest {
pub(super) db: TestDb, pub(super) db: ty_project::TestDb,
pub(super) file: File, pub(super) file: File,
pub(super) range: TextRange, pub(super) range: TextRange,
} }

View file

@ -1,5 +1,4 @@
mod completion; mod completion;
mod db;
mod docstring; mod docstring;
mod find_node; mod find_node;
mod goto; mod goto;
@ -14,7 +13,6 @@ mod signature_help;
mod stub_mapping; mod stub_mapping;
pub use completion::completion; pub use completion::completion;
pub use db::Db;
pub use docstring::get_parameter_documentation; pub use docstring::get_parameter_documentation;
pub use goto::{goto_declaration, goto_definition, goto_type_definition}; pub use goto::{goto_declaration, goto_definition, goto_type_definition};
pub use hover::hover; pub use hover::hover;
@ -29,6 +27,7 @@ use ruff_db::files::{File, FileRange};
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use ty_project::Db;
use ty_python_semantic::types::{Type, TypeDefinition}; use ty_python_semantic::types::{Type, TypeDefinition};
/// Information associated with a text range. /// Information associated with a text range.
@ -211,13 +210,13 @@ impl HasNavigationTargets for TypeDefinition<'_> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::db::tests::TestDb;
use insta::internals::SettingsBindDropGuard; use insta::internals::SettingsBindDropGuard;
use ruff_db::Db; use ruff_db::Db;
use ruff_db::diagnostic::{Diagnostic, DiagnosticFormat, DisplayDiagnosticConfig}; use ruff_db::diagnostic::{Diagnostic, DiagnosticFormat, DisplayDiagnosticConfig};
use ruff_db::files::{File, system_path_to_file}; use ruff_db::files::{File, system_path_to_file};
use ruff_db::system::{DbWithWritableSystem, SystemPath, SystemPathBuf}; use ruff_db::system::{DbWithWritableSystem, SystemPath, SystemPathBuf};
use ruff_text_size::TextSize; use ruff_text_size::TextSize;
use ty_project::ProjectMetadata;
use ty_python_semantic::{ use ty_python_semantic::{
Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings, Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings,
}; };
@ -231,7 +230,7 @@ mod tests {
} }
pub(super) struct CursorTest { pub(super) struct CursorTest {
pub(super) db: TestDb, pub(super) db: ty_project::TestDb,
pub(super) cursor: Cursor, pub(super) cursor: Cursor,
_insta_settings_guard: SettingsBindDropGuard, _insta_settings_guard: SettingsBindDropGuard,
} }
@ -286,7 +285,11 @@ mod tests {
impl CursorTestBuilder { impl CursorTestBuilder {
pub(super) fn build(&self) -> CursorTest { 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<Cursor> = None; let mut cursor: Option<Cursor> = None;
for &Source { for &Source {
ref path, ref path,

View file

@ -19,7 +19,6 @@ ruff_options_metadata = { workspace = true }
ruff_python_ast = { workspace = true, features = ["serde"] } ruff_python_ast = { workspace = true, features = ["serde"] }
ruff_python_formatter = { workspace = true, optional = true } ruff_python_formatter = { workspace = true, optional = true }
ruff_text_size = { workspace = true } ruff_text_size = { workspace = true }
ty_ide = { workspace = true }
ty_python_semantic = { workspace = true, features = ["serde"] } ty_python_semantic = { workspace = true, features = ["serde"] }
ty_vendored = { workspace = true } ty_vendored = { workspace = true }
@ -53,6 +52,7 @@ deflate = ["ty_vendored/deflate"]
schemars = ["dep:schemars", "ruff_db/schemars", "ty_python_semantic/schemars"] schemars = ["dep:schemars", "ruff_db/schemars", "ty_python_semantic/schemars"]
zstd = ["ty_vendored/zstd"] zstd = ["ty_vendored/zstd"]
format = ["ruff_python_formatter"] format = ["ruff_python_formatter"]
testing = []
[lints] [lints]
workspace = true workspace = true

View file

@ -14,7 +14,6 @@ use ruff_db::system::System;
use ruff_db::vendored::VendoredFileSystem; use ruff_db::vendored::VendoredFileSystem;
use salsa::plumbing::ZalsaDatabase; use salsa::plumbing::ZalsaDatabase;
use salsa::{Event, Setter}; use salsa::{Event, Setter};
use ty_ide::Db as IdeDb;
use ty_python_semantic::lint::{LintRegistry, RuleSelection}; use ty_python_semantic::lint::{LintRegistry, RuleSelection};
use ty_python_semantic::{Db as SemanticDb, Program}; use ty_python_semantic::{Db as SemanticDb, Program};
@ -404,9 +403,6 @@ impl SalsaMemoryDump {
} }
} }
#[salsa::db]
impl IdeDb for ProjectDatabase {}
#[salsa::db] #[salsa::db]
impl SemanticDb for ProjectDatabase { impl SemanticDb for ProjectDatabase {
fn should_check_file(&self, file: File) -> bool { 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 { pub(crate) mod tests {
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -487,7 +483,7 @@ pub(crate) mod tests {
#[salsa::db] #[salsa::db]
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct TestDb { pub struct TestDb {
storage: salsa::Storage<Self>, storage: salsa::Storage<Self>,
events: Events, events: Events,
files: Files, files: Files,
@ -497,7 +493,7 @@ pub(crate) mod tests {
} }
impl TestDb { impl TestDb {
pub(crate) fn new(project: ProjectMetadata) -> Self { pub fn new(project: ProjectMetadata) -> Self {
let events = Events::default(); let events = Events::default();
let mut db = Self { let mut db = Self {
storage: salsa::Storage::new(Some(Box::new({ storage: salsa::Storage::new(Some(Box::new({
@ -522,7 +518,7 @@ pub(crate) mod tests {
impl TestDb { impl TestDb {
/// Takes the salsa events. /// Takes the salsa events.
pub(crate) fn take_salsa_events(&mut self) -> Vec<salsa::Event> { pub fn take_salsa_events(&mut self) -> Vec<salsa::Event> {
let mut events = self.events.lock().unwrap(); let mut events = self.events.lock().unwrap();
std::mem::take(&mut *events) std::mem::take(&mut *events)

View file

@ -1,6 +1,8 @@
use crate::glob::{GlobFilterCheckMode, IncludeResult}; use crate::glob::{GlobFilterCheckMode, IncludeResult};
use crate::metadata::options::{OptionDiagnostic, ToSettingsError}; use crate::metadata::options::{OptionDiagnostic, ToSettingsError};
use crate::walk::{ProjectFilesFilter, ProjectFilesWalker}; use crate::walk::{ProjectFilesFilter, ProjectFilesWalker};
#[cfg(feature = "testing")]
pub use db::tests::TestDb;
pub use db::{ChangeResult, CheckMode, Db, ProjectDatabase, SalsaMemoryDump}; pub use db::{ChangeResult, CheckMode, Db, ProjectDatabase, SalsaMemoryDump};
use files::{Index, Indexed, IndexedFiles}; use files::{Index, Indexed, IndexedFiles};
use metadata::settings::Settings; use metadata::settings::Settings;

View file

@ -5,14 +5,15 @@ use lsp_types::Location;
use ruff_db::files::FileRange; use ruff_db::files::FileRange;
use ruff_db::source::{line_index, source_text}; use ruff_db::source::{line_index, source_text};
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
use ty_ide::{Db, NavigationTarget}; use ty_ide::NavigationTarget;
use ty_project::Db;
pub(crate) trait ToLink { pub(crate) trait ToLink {
fn to_location(&self, db: &dyn ty_ide::Db, encoding: PositionEncoding) -> Option<Location>; fn to_location(&self, db: &dyn Db, encoding: PositionEncoding) -> Option<Location>;
fn to_link( fn to_link(
&self, &self,
db: &dyn ty_ide::Db, db: &dyn Db,
src: Option<FileRange>, src: Option<FileRange>,
encoding: PositionEncoding, encoding: PositionEncoding,
) -> Option<lsp_types::LocationLink>; ) -> Option<lsp_types::LocationLink>;