Skip to content

Commit c14535c

Browse files
committed
[ty] Add virtual files to the only project database
1 parent dca594f commit c14535c

File tree

9 files changed

+60
-57
lines changed

9 files changed

+60
-57
lines changed

crates/ty_server/src/server/api.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,7 @@ where
236236
});
237237
};
238238

239-
let db = match &path {
240-
AnySystemPath::System(path) => match session.project_db_for_path(path) {
241-
Some(db) => db.clone(),
242-
None => session.default_project_db().clone(),
243-
},
244-
AnySystemPath::SystemVirtual(_) => session.default_project_db().clone(),
245-
};
246-
239+
let db = session.project_db(&path).clone();
247240
let snapshot = session.take_document_snapshot(url);
248241

249242
Box::new(move |client| {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(super) fn publish_diagnostics(session: &Session, key: &DocumentKey, client:
8282
}
8383
};
8484

85-
let db = session.project_db_or_default(key.path());
85+
let db = session.project_db(key.path());
8686

8787
let Some(diagnostics) = compute_diagnostics(db, &snapshot) else {
8888
return;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ impl SyncNotificationHandler for DidChangeTextDocumentHandler {
4040
.update_text_document(&key, content_changes, version)
4141
.with_failure_code(ErrorCode::InternalError)?;
4242

43-
match key.path() {
43+
let path = key.path();
44+
let db = session.project_db_mut(path);
45+
46+
match path {
4447
AnySystemPath::System(path) => {
45-
let db = match session.project_db_for_path_mut(path) {
46-
Some(db) => db,
47-
None => session.default_project_db_mut(),
48-
};
4948
db.apply_changes(vec![ChangeEvent::file_content_changed(path.clone())], None);
5049
}
5150
AnySystemPath::SystemVirtual(virtual_path) => {
52-
let db = session.default_project_db_mut();
5351
db.apply_changes(
5452
vec![ChangeEvent::ChangedVirtual(virtual_path.clone())],
5553
None,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ impl SyncNotificationHandler for DidCloseTextDocumentHandler {
3838
.close_document(&key)
3939
.with_failure_code(ErrorCode::InternalError)?;
4040

41-
if let AnySystemPath::SystemVirtual(virtual_path) = key.path() {
42-
let db = session.default_project_db_mut();
41+
let path = key.path();
42+
let db = session.project_db_mut(path);
43+
44+
if let AnySystemPath::SystemVirtual(virtual_path) = path {
4345
db.apply_changes(
4446
vec![ChangeEvent::DeletedVirtual(virtual_path.clone())],
4547
None,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ impl SyncNotificationHandler for DidCloseNotebookHandler {
3838
.close_document(&key)
3939
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
4040

41-
if let AnySystemPath::SystemVirtual(virtual_path) = key.path() {
42-
let db = session.default_project_db_mut();
41+
let path = key.path();
42+
let db = session.project_db_mut(path);
43+
44+
if let AnySystemPath::SystemVirtual(virtual_path) = path {
4345
db.apply_changes(
4446
vec![ChangeEvent::DeletedVirtual(virtual_path.clone())],
4547
None,

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,14 @@ impl SyncNotificationHandler for DidOpenTextDocumentHandler {
4444
let document = TextDocument::new(text, version).with_language_id(&language_id);
4545
session.open_text_document(key.path(), document);
4646

47-
match key.path() {
47+
let path = key.path();
48+
let db = session.project_db_mut(path);
49+
50+
match path {
4851
AnySystemPath::System(system_path) => {
49-
let db = match session.project_db_for_path_mut(system_path) {
50-
Some(db) => db,
51-
None => session.default_project_db_mut(),
52-
};
5352
db.apply_changes(vec![ChangeEvent::Opened(system_path.clone())], None);
5453
}
5554
AnySystemPath::SystemVirtual(virtual_path) => {
56-
let db = session.default_project_db_mut();
5755
db.files().virtual_file(db, virtual_path);
5856
}
5957
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,13 @@ impl SyncNotificationHandler for DidOpenNotebookHandler {
3838
.with_failure_code(ErrorCode::InternalError)?;
3939
session.open_notebook_document(&path, notebook);
4040

41+
let db = session.project_db_mut(&path);
42+
4143
match &path {
4244
AnySystemPath::System(system_path) => {
43-
let db = match session.project_db_for_path_mut(system_path) {
44-
Some(db) => db,
45-
None => session.default_project_db_mut(),
46-
};
4745
db.apply_changes(vec![ChangeEvent::Opened(system_path.clone())], None);
4846
}
4947
AnySystemPath::SystemVirtual(virtual_path) => {
50-
let db = session.default_project_db_mut();
5148
db.files().virtual_file(db, virtual_path);
5249
}
5350
}

crates/ty_server/src/session.rs

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,47 @@ impl Session {
168168
&self.workspaces
169169
}
170170

171-
// TODO(dhruvmanila): Ideally, we should have a single method for `workspace_db_for_path_mut`
172-
// and `default_workspace_db_mut` but the borrow checker doesn't allow that.
173-
// https://github.com/astral-sh/ruff/pull/13041#discussion_r1726725437
171+
/// Returns a reference to the project's [`ProjectDatabase`] in which the given `path` belongs.
172+
///
173+
/// If the path is a system path, it will return the project database that is closest to the
174+
/// given path, or the default project if no project is found for the path.
175+
///
176+
/// If the path is a virtual path, it will return the first project database in the session.
177+
pub(crate) fn project_db(&self, path: &AnySystemPath) -> &ProjectDatabase {
178+
match path {
179+
AnySystemPath::System(system_path) => self
180+
.project_db_for_path(system_path)
181+
.unwrap_or(&self.default_project),
182+
AnySystemPath::SystemVirtual(_virtual_path) => {
183+
// TODO: Currently, ty only supports single workspace but we need to figure out
184+
// which project should this virtual path belong to when there are multiple
185+
// projects: https://github.com/astral-sh/ty/issues/794
186+
self.projects.iter().next().map(|(_, db)| db).unwrap()
187+
}
188+
}
189+
}
174190

175-
/// Returns a reference to the project's [`ProjectDatabase`] corresponding to the given path,
176-
/// or the default project if no project is found for the path.
177-
pub(crate) fn project_db_or_default(&self, path: &AnySystemPath) -> &ProjectDatabase {
178-
path.as_system()
179-
.and_then(|path| self.project_db_for_path(path))
180-
.unwrap_or_else(|| self.default_project_db())
191+
/// Returns a mutable reference to the project's [`ProjectDatabase`] in which the given `path`
192+
/// belongs.
193+
///
194+
/// Refer to [`project_db`] for more details on how the project is selected.
195+
///
196+
/// [`project_db`]: Session::project_db
197+
pub(crate) fn project_db_mut(&mut self, path: &AnySystemPath) -> &mut ProjectDatabase {
198+
match path {
199+
AnySystemPath::System(system_path) => self
200+
.projects
201+
.range_mut(..=system_path.to_path_buf())
202+
.next_back()
203+
.map(|(_, db)| db)
204+
.unwrap_or(&mut self.default_project),
205+
AnySystemPath::SystemVirtual(_virtual_path) => {
206+
// TODO: Currently, ty only supports single workspace but we need to figure out
207+
// which project should this virtual path belong to when there are multiple
208+
// projects: https://github.com/astral-sh/ty/issues/794
209+
self.projects.iter_mut().next().map(|(_, db)| db).unwrap()
210+
}
211+
}
181212
}
182213

183214
/// Returns a reference to the project's [`ProjectDatabase`] corresponding to the given path, if
@@ -204,17 +235,6 @@ impl Session {
204235
.map(|(_, db)| db)
205236
}
206237

207-
/// Returns a reference to the default project [`ProjectDatabase`]. The default project is the
208-
/// minimum root path in the project map.
209-
pub(crate) fn default_project_db(&self) -> &ProjectDatabase {
210-
&self.default_project
211-
}
212-
213-
/// Returns a mutable reference to the default project [`ProjectDatabase`].
214-
pub(crate) fn default_project_db_mut(&mut self) -> &mut ProjectDatabase {
215-
&mut self.default_project
216-
}
217-
218238
fn projects_mut(&mut self) -> impl Iterator<Item = &'_ mut ProjectDatabase> + '_ {
219239
self.projects
220240
.values_mut()

crates/ty_server/src/system.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ impl AnySystemPath {
7272
}
7373
}
7474

75-
pub(crate) const fn as_system(&self) -> Option<&SystemPathBuf> {
76-
match self {
77-
AnySystemPath::System(system_path_buf) => Some(system_path_buf),
78-
AnySystemPath::SystemVirtual(_) => None,
79-
}
80-
}
81-
8275
/// Returns the extension of the path, if any.
8376
pub(crate) fn extension(&self) -> Option<&str> {
8477
match self {

0 commit comments

Comments
 (0)