Skip to content

Commit aaead0a

Browse files
committed
Add ty_server::Db trait
Introduce a new Db trait in ty_server that extends ty_project::Db and adds server-specific database methods: - document(): Access to Document from the LSP index - notebook_document(): Convenient access to NotebookDocument This trait provides the foundation for notebook support by allowing the server to access document and notebook metadata through the database interface. Use ty_server::Db consistently throughout ty_server Replace all uses of ty_project::Db and ty_python_semantic::Db with crate::Db (ty_server::Db) for consistency. This ensures all code within ty_server uses the server-specific Db trait which extends ty_project::Db. Add ty_server::Db trait Introduce a new Db trait in ty_server that extends ty_project::Db and adds server-specific database methods: - document(): Access to Document from the LSP index - notebook_document(): Convenient access to NotebookDocument This trait provides the foundation for notebook support by allowing the server to access document and notebook metadata through the database interface. Use ty_server::Db consistently throughout ty_server Replace all uses of ty_project::Db and ty_python_semantic::Db with crate::Db (ty_server::Db) for consistency. This ensures all code within ty_server uses the server-specific Db trait which extends ty_project::Db.
1 parent 5662955 commit aaead0a

File tree

13 files changed

+73
-30
lines changed

13 files changed

+73
-30
lines changed

crates/ty/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ impl ty_project::ProgressReporter for IndicatifReporter {
450450
self.bar.set_draw_target(self.printer.progress_target());
451451
}
452452

453-
fn report_checked_file(&self, db: &dyn Db, file: File, diagnostics: &[Diagnostic]) {
453+
fn report_checked_file(&self, db: &ProjectDatabase, file: File, diagnostics: &[Diagnostic]) {
454454
self.collector.report_checked_file(db, file, diagnostics);
455455
self.bar.inc(1);
456456
}
457457

458-
fn report_diagnostics(&mut self, db: &dyn Db, diagnostics: Vec<Diagnostic>) {
458+
fn report_diagnostics(&mut self, db: &ProjectDatabase, diagnostics: Vec<Diagnostic>) {
459459
self.collector.report_diagnostics(db, diagnostics);
460460
}
461461
}

crates/ty_project/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ pub trait ProgressReporter: Send + Sync {
124124
fn set_files(&mut self, files: usize);
125125

126126
/// Report the completion of checking a given file along with its diagnostics.
127-
fn report_checked_file(&self, db: &dyn Db, file: File, diagnostics: &[Diagnostic]);
127+
fn report_checked_file(&self, db: &ProjectDatabase, file: File, diagnostics: &[Diagnostic]);
128128

129129
/// Reports settings or IO related diagnostics. The diagnostics
130130
/// can belong to different files or no file at all.
131131
/// But it's never a file for which [`Self::report_checked_file`] gets called.
132-
fn report_diagnostics(&mut self, db: &dyn Db, diagnostics: Vec<Diagnostic>);
132+
fn report_diagnostics(&mut self, db: &ProjectDatabase, diagnostics: Vec<Diagnostic>);
133133
}
134134

135135
/// Reporter that collects all diagnostics into a `Vec`.
@@ -149,7 +149,7 @@ impl CollectReporter {
149149

150150
impl ProgressReporter for CollectReporter {
151151
fn set_files(&mut self, _files: usize) {}
152-
fn report_checked_file(&self, _db: &dyn Db, _file: File, diagnostics: &[Diagnostic]) {
152+
fn report_checked_file(&self, _db: &ProjectDatabase, _file: File, diagnostics: &[Diagnostic]) {
153153
if diagnostics.is_empty() {
154154
return;
155155
}
@@ -160,7 +160,7 @@ impl ProgressReporter for CollectReporter {
160160
.extend(diagnostics.iter().map(Clone::clone));
161161
}
162162

163-
fn report_diagnostics(&mut self, _db: &dyn Db, diagnostics: Vec<Diagnostic>) {
163+
fn report_diagnostics(&mut self, _db: &ProjectDatabase, diagnostics: Vec<Diagnostic>) {
164164
self.0.get_mut().unwrap().extend(diagnostics);
165165
}
166166
}

crates/ty_server/src/db.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::NotebookDocument;
2+
use crate::session::index::Document;
3+
use crate::system::LSPSystem;
4+
use ruff_db::Db as _;
5+
use ruff_db::files::{File, FilePath};
6+
use ty_project::{Db as ProjectDb, ProjectDatabase};
7+
8+
#[salsa::db]
9+
pub(crate) trait Db: ProjectDb {
10+
fn document(&self, file: File) -> Option<&Document>;
11+
12+
fn notebook_document(&self, file: File) -> Option<&NotebookDocument> {
13+
let document = self.document(file)?;
14+
document.as_notebook()
15+
}
16+
}
17+
18+
#[salsa::db]
19+
impl Db for ProjectDatabase {
20+
fn document(&self, file: File) -> Option<&Document> {
21+
self.system()
22+
.as_any()
23+
.downcast_ref::<LSPSystem>()
24+
.and_then(|system| match file.path(self) {
25+
FilePath::System(path) => system.system_path_to_document(path),
26+
FilePath::SystemVirtual(path) => system.system_virtual_path_to_document(path),
27+
FilePath::Vendored(_) => None,
28+
})
29+
}
30+
}

crates/ty_server/src/document/location.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use crate::PositionEncoding;
2-
use crate::document::{FileRangeExt, ToRangeExt};
31
use lsp_types::Location;
42
use ruff_db::files::FileRange;
53
use ty_ide::{NavigationTarget, ReferenceTarget};
6-
use ty_python_semantic::Db;
4+
5+
use crate::Db;
6+
use crate::PositionEncoding;
7+
use crate::document::{FileRangeExt, ToRangeExt};
78

89
pub(crate) trait ToLink {
910
fn to_location(&self, db: &dyn Db, encoding: PositionEncoding) -> Option<Location>;

crates/ty_server/src/document/range.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::PositionEncoding;
2+
use crate::Db;
23
use crate::system::file_to_url;
3-
use ty_python_semantic::Db;
44

55
use lsp_types as types;
66
use lsp_types::{Location, Position, Url};

crates/ty_server/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use anyhow::Context;
44
use lsp_server::Connection;
55
use ruff_db::system::{OsSystem, SystemPathBuf};
66

7+
use crate::db::Db;
78
pub use crate::logging::{LogLevel, init_logging};
89
pub use crate::server::{PartialWorkspaceProgress, PartialWorkspaceProgressParams, Server};
910
pub use crate::session::{ClientOptions, DiagnosticMode};
1011
pub use document::{NotebookDocument, PositionEncoding, TextDocument};
1112
pub(crate) use session::Session;
1213

1314
mod capabilities;
15+
mod db;
1416
mod document;
1517
mod logging;
1618
mod server;

crates/ty_server/src/server/api/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use rustc_hash::FxHashMap;
1010
use ruff_db::diagnostic::{Annotation, Severity, SubDiagnostic};
1111
use ruff_db::files::FileRange;
1212
use ruff_db::system::SystemPathBuf;
13-
use ty_project::{Db, ProjectDatabase};
13+
use ty_project::{Db as _, ProjectDatabase};
1414

15+
use crate::Db;
1516
use crate::document::{FileRangeExt, ToRangeExt};
1617
use crate::session::DocumentSnapshot;
1718
use crate::session::client::Client;

crates/ty_server/src/server/api/notifications/did_change_watched_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::system::AnySystemPath;
88
use lsp_types as types;
99
use lsp_types::{FileChangeType, notification as notif};
1010
use rustc_hash::FxHashMap;
11-
use ty_project::Db;
11+
use ty_project::Db as _;
1212
use ty_project::watch::{ChangeEvent, ChangedKind, CreatedKind, DeletedKind};
1313

1414
pub(crate) struct DidChangeWatchedFiles;

crates/ty_server/src/server/api/requests/document_symbols.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use lsp_types::request::DocumentSymbolRequest;
44
use lsp_types::{DocumentSymbol, DocumentSymbolParams, SymbolInformation, Url};
55
use ruff_db::files::File;
66
use ty_ide::{HierarchicalSymbols, SymbolId, SymbolInfo, document_symbols};
7-
use ty_project::{Db, ProjectDatabase};
7+
use ty_project::ProjectDatabase;
88

9+
use crate::Db;
910
use crate::document::{PositionEncoding, ToRangeExt};
1011
use crate::server::api::symbols::{convert_symbol_kind, convert_to_lsp_symbol_information};
1112
use crate::server::api::traits::{

crates/ty_server/src/server/api/requests/execute_command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use lsp_server::ErrorCode;
99
use lsp_types::{self as types, request as req};
1010
use std::fmt::Write;
1111
use std::str::FromStr;
12-
use ty_project::Db;
12+
use ty_project::Db as _;
1313

1414
pub(crate) struct ExecuteCommand;
1515

0 commit comments

Comments
 (0)