Skip to content

Commit 209b113

Browse files
remove session closure methods (#288)
1 parent cdbe25d commit 209b113

File tree

3 files changed

+61
-79
lines changed

3 files changed

+61
-79
lines changed

crates/djls-server/src/server.rs

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl DjangoLanguageServer {
8282
}
8383

8484
let path = self
85-
.with_session(|session| session.with_db(|db| document.file().path(db).to_owned()))
85+
.with_session(|session| document.path(session.db()).to_owned())
8686
.await;
8787

8888
if FileKind::from(&path) != FileKind::Template {
@@ -91,11 +91,10 @@ impl DjangoLanguageServer {
9191

9292
let diagnostics: Vec<lsp_types::Diagnostic> = self
9393
.with_session_mut(|session| {
94-
session.with_db(|db| {
95-
let file = db.get_or_create_file(&path);
96-
let nodelist = djls_templates::parse_template(db, file);
97-
djls_ide::collect_diagnostics(db, file, nodelist)
98-
})
94+
let db = session.db();
95+
let file = db.get_or_create_file(&path);
96+
let nodelist = djls_templates::parse_template(db, file);
97+
djls_ide::collect_diagnostics(db, file, nodelist)
9998
})
10099
.await;
101100

@@ -263,22 +262,21 @@ impl LanguageServer for DjangoLanguageServer {
263262
let position = params.text_document_position.position;
264263
let encoding = session.client_capabilities().position_encoding();
265264
let file_kind = FileKind::from(&path);
266-
let template_tags = session.with_db(|db| {
267-
if let Some(project) = db.project() {
268-
tracing::debug!("Fetching templatetags for project");
269-
let tags = djls_project::templatetags(db, project);
270-
if let Some(ref t) = tags {
271-
tracing::debug!("Got {} templatetags", t.len());
272-
} else {
273-
tracing::warn!("No templatetags returned from project");
274-
}
275-
tags
265+
let db = session.db();
266+
let template_tags = if let Some(project) = db.project() {
267+
tracing::debug!("Fetching templatetags for project");
268+
let tags = djls_project::templatetags(db, project);
269+
if let Some(ref t) = tags {
270+
tracing::debug!("Got {} templatetags", t.len());
276271
} else {
277-
tracing::warn!("No project available for templatetags");
278-
None
272+
tracing::warn!("No templatetags returned from project");
279273
}
280-
});
281-
let tag_specs = session.with_db(SemanticDb::tag_specs);
274+
tags
275+
} else {
276+
tracing::warn!("No project available for templatetags");
277+
None
278+
};
279+
let tag_specs = db.tag_specs();
282280
let supports_snippets = session.client_capabilities().supports_snippets();
283281

284282
let completions = djls_ide::handle_completion(
@@ -314,11 +312,10 @@ impl LanguageServer for DjangoLanguageServer {
314312
let diagnostics = if let Some(path) = params.text_document.uri.to_utf8_path_buf() {
315313
if FileKind::from(&path) == FileKind::Template {
316314
self.with_session_mut(move |session| {
317-
session.with_db_mut(|db| {
318-
let file = db.get_or_create_file(&path);
319-
let nodelist = djls_templates::parse_template(db, file);
320-
djls_ide::collect_diagnostics(db, file, nodelist)
321-
})
315+
let db = session.db_mut();
316+
let file = db.get_or_create_file(&path);
317+
let nodelist = djls_templates::parse_template(db, file);
318+
djls_ide::collect_diagnostics(db, file, nodelist)
322319
})
323320
.await
324321
} else {
@@ -353,21 +350,19 @@ impl LanguageServer for DjangoLanguageServer {
353350
let response = self
354351
.with_session_mut(|session| {
355352
let encoding = session.client_capabilities().position_encoding();
356-
357-
session.with_db_mut(|db| {
358-
let file = params
359-
.text_document_position_params
360-
.text_document
361-
.to_file(db)?;
362-
let source = file.source(db);
363-
let line_index = file.line_index(db);
364-
let offset = params.text_document_position_params.position.to_offset(
365-
source.as_str(),
366-
line_index,
367-
encoding,
368-
);
369-
djls_ide::goto_definition(db, file, offset)
370-
})
353+
let db = session.db_mut();
354+
let file = params
355+
.text_document_position_params
356+
.text_document
357+
.to_file(db)?;
358+
let source = file.source(db);
359+
let line_index = file.line_index(db);
360+
let offset = params.text_document_position_params.position.to_offset(
361+
source.as_str(),
362+
line_index,
363+
encoding,
364+
);
365+
djls_ide::goto_definition(db, file, offset)
371366
})
372367
.await;
373368

@@ -381,18 +376,16 @@ impl LanguageServer for DjangoLanguageServer {
381376
let response = self
382377
.with_session_mut(|session| {
383378
let encoding = session.client_capabilities().position_encoding();
384-
385-
session.with_db_mut(|db| {
386-
let file = params.text_document_position.text_document.to_file(db)?;
387-
let source = file.source(db);
388-
let line_index = file.line_index(db);
389-
let offset = params.text_document_position.position.to_offset(
390-
source.as_str(),
391-
line_index,
392-
encoding,
393-
);
394-
djls_ide::find_references(db, file, offset)
395-
})
379+
let db = session.db_mut();
380+
let file = params.text_document_position.text_document.to_file(db)?;
381+
let source = file.source(db);
382+
let line_index = file.line_index(db);
383+
let offset = params.text_document_position.position.to_offset(
384+
source.as_str(),
385+
line_index,
386+
encoding,
387+
);
388+
djls_ide::find_references(db, file, offset)
396389
})
397390
.await;
398391

crates/djls-server/src/session.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,20 @@ impl Session {
7171
}
7272
}
7373

74-
#[must_use]
7574
pub fn client_capabilities(&self) -> ClientCapabilities {
7675
self.client_capabilities
7776
}
7877

79-
#[must_use]
8078
pub fn db(&self) -> &DjangoDatabase {
8179
&self.db
8280
}
8381

84-
pub fn set_settings(&mut self, settings: Settings) {
85-
self.db.set_settings(settings);
82+
pub fn db_mut(&mut self) -> &mut DjangoDatabase {
83+
&mut self.db
8684
}
8785

88-
/// Execute a read-only operation with access to the database.
89-
pub fn with_db<F, R>(&self, f: F) -> R
90-
where
91-
F: FnOnce(&DjangoDatabase) -> R,
92-
{
93-
f(&self.db)
94-
}
95-
96-
/// Execute a mutable operation with exclusive access to the database.
97-
pub fn with_db_mut<F, R>(&mut self, f: F) -> R
98-
where
99-
F: FnOnce(&mut DjangoDatabase) -> R,
100-
{
101-
f(&mut self.db)
86+
pub fn set_settings(&mut self, settings: Settings) {
87+
self.db.set_settings(settings);
10288
}
10389

10490
/// Get the current project for this session
@@ -187,7 +173,6 @@ impl Session {
187173
}
188174

189175
/// Get a document from the buffer if it's open.
190-
#[must_use]
191176
pub fn get_document(&self, path: &Utf8Path) -> Option<TextDocument> {
192177
self.workspace.get_document(path)
193178
}
@@ -308,10 +293,9 @@ mod tests {
308293

309294
assert!(session.get_document(&path).is_some());
310295

311-
let content = session.with_db(|db| {
312-
let file = db.get_or_create_file(&path);
313-
file.source(db).to_string()
314-
});
296+
let db = session.db();
297+
let file = db.get_or_create_file(&path);
298+
let content = file.source(db).to_string();
315299
assert_eq!(content, "print('hello')");
316300

317301
let close_doc = lsp_types::TextDocumentIdentifier { uri };
@@ -344,10 +328,9 @@ mod tests {
344328
assert_eq!(doc.content(), "updated");
345329
assert_eq!(doc.version(), 2);
346330

347-
let content = session.with_db(|db| {
348-
let file = db.get_or_create_file(&path);
349-
file.source(db).to_string()
350-
});
331+
let db = session.db();
332+
let file = db.get_or_create_file(&path);
333+
let content = file.source(db).to_string();
351334
assert_eq!(content, "updated");
352335
}
353336

crates/djls-workspace/src/document.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! performance when handling frequent position-based operations like hover, completion,
66
//! and diagnostics.
77
8+
use camino::Utf8Path;
9+
use djls_source::Db as SourceDb;
810
use djls_source::File;
911
use djls_source::LineIndex;
1012
use djls_source::PositionEncoding;
@@ -72,6 +74,10 @@ impl TextDocument {
7274
self.file
7375
}
7476

77+
pub fn path<'db>(&self, db: &'db dyn SourceDb) -> &'db Utf8Path {
78+
self.file.path(db)
79+
}
80+
7581
pub fn update(
7682
&mut self,
7783
changes: Vec<DocumentChange>,

0 commit comments

Comments
 (0)