Skip to content

Commit a434464

Browse files
committed
Add and sync files to the database
1 parent 9dfa69f commit a434464

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ use lsp_server::ErrorCode;
22
use lsp_types::notification::DidCloseTextDocument;
33
use lsp_types::DidCloseTextDocumentParams;
44

5+
use ruff_db::files::File;
6+
57
use crate::server::api::diagnostics::clear_diagnostics;
68
use crate::server::api::traits::{NotificationHandler, SyncNotificationHandler};
79
use crate::server::api::LSPResult;
810
use crate::server::client::{Notifier, Requester};
911
use crate::server::Result;
1012
use crate::session::Session;
13+
use crate::system::url_to_system_path;
1114

1215
pub(crate) struct DidCloseTextDocumentHandler;
1316

@@ -22,12 +25,18 @@ impl SyncNotificationHandler for DidCloseTextDocumentHandler {
2225
_requester: &mut Requester,
2326
params: DidCloseTextDocumentParams,
2427
) -> Result<()> {
28+
let Ok(path) = url_to_system_path(&params.text_document.uri) else {
29+
return Ok(());
30+
};
31+
2532
let key = session.key_from_url(params.text_document.uri);
2633
session
2734
.close_document(&key)
2835
.with_failure_code(ErrorCode::InternalError)?;
2936

30-
// TODO(dhruvmanila): Close the file in the `RootDatabase`
37+
if let Some(db) = session.workspace_db_for_path_mut(path.as_std_path()) {
38+
File::sync_path(db.get_mut(), &path);
39+
}
3140

3241
clear_diagnostics(key.url(), &notifier)?;
3342

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use lsp_types::notification::DidOpenTextDocument;
22
use lsp_types::DidOpenTextDocumentParams;
3+
use ruff_db::files::{system_path_to_file, File};
34

45
use crate::server::api::traits::{NotificationHandler, SyncNotificationHandler};
56
use crate::server::client::{Notifier, Requester};
67
use crate::server::Result;
78
use crate::session::Session;
9+
use crate::system::url_to_system_path;
810
use crate::TextDocument;
911

1012
pub(crate) struct DidOpenTextDocumentHandler;
@@ -20,10 +22,18 @@ impl SyncNotificationHandler for DidOpenTextDocumentHandler {
2022
_requester: &mut Requester,
2123
params: DidOpenTextDocumentParams,
2224
) -> Result<()> {
25+
let Ok(path) = url_to_system_path(&params.text_document.uri) else {
26+
return Ok(());
27+
};
28+
2329
let document = TextDocument::new(params.text_document.text, params.text_document.version);
2430
session.open_text_document(params.text_document.uri, document);
2531

26-
// TODO(dhruvmanila): Open the file in the `RootDatabase`
32+
if let Some(db) = session.workspace_db_for_path_mut(path.as_std_path()) {
33+
// TODO(dhruvmanila): Store the `file` in `TextDocument`
34+
let _file = system_path_to_file(&**db, &path).unwrap();
35+
File::sync_path(db.get_mut(), &path);
36+
}
2737

2838
// Publish diagnostics if the client doesn't support pull diagnostics
2939

crates/red_knot_server/src/session.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ impl Session {
9595
.map(|(_, db)| db)
9696
}
9797

98+
pub(crate) fn workspace_db_for_path_mut(
99+
&mut self,
100+
path: impl AsRef<Path>,
101+
) -> Option<&mut salsa::Handle<RootDatabase>> {
102+
self.workspaces
103+
.range_mut(..=path.as_ref().to_path_buf())
104+
.next_back()
105+
.map(|(_, db)| db)
106+
}
107+
98108
pub fn key_from_url(&self, url: Url) -> DocumentKey {
99109
self.index().key_from_url(url)
100110
}

crates/red_knot_server/src/session/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl DocumentController {
250250
/// A read-only query to an open document.
251251
/// This query can 'select' a text document, full notebook, or a specific notebook cell.
252252
/// It also includes document settings.
253-
#[derive(Clone)]
253+
#[derive(Debug, Clone)]
254254
pub enum DocumentQuery {
255255
Text {
256256
file_url: Url,

0 commit comments

Comments
 (0)