Skip to content

Commit f66546a

Browse files
committed
Add docs
1 parent 8209499 commit f66546a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

crates/red_knot_server/src/session.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ mod settings;
3131
/// The global state for the LSP
3232
pub struct Session {
3333
/// Used to retrieve information about open documents and settings.
34+
///
35+
/// This will be [`None`] when a mutable reference is held to the index via [`index_mut`]
36+
/// to prevent the index from being accessed while it is being modified. It will be restored
37+
/// when the mutable reference ([`MutIndexGuard`]) is dropped.
38+
///
39+
/// [`index_mut`]: Session::index_mut
3440
index: Option<Arc<index::Index>>,
41+
3542
/// Maps workspace root paths to their respective databases.
3643
workspaces: BTreeMap<PathBuf, salsa::Handle<RootDatabase>>,
3744
/// The global position encoding, negotiated during LSP initialization.
@@ -138,10 +145,22 @@ impl Session {
138145
Ok(())
139146
}
140147

148+
/// Returns a reference to the index.
149+
///
150+
/// # Panics
151+
///
152+
/// Panics if there's a mutable reference to the index via [`index_mut`].
153+
///
154+
/// [`index_mut`]: Session::index_mut
141155
fn index(&self) -> &index::Index {
142156
self.index.as_ref().unwrap()
143157
}
144158

159+
/// Returns a mutable reference to the index.
160+
///
161+
/// This method drops all references to the index and returns a guard that will restore the
162+
/// references when dropped. This guard holds the only reference to the index and allows
163+
/// modifying it.
145164
fn index_mut(&mut self) -> MutIndexGuard {
146165
let index = self.index.take().unwrap();
147166

@@ -167,6 +186,9 @@ impl Session {
167186
}
168187
}
169188

189+
/// A guard that holds the only reference to the index and allows modifying it.
190+
///
191+
/// When dropped, this guard restores all references to the index.
170192
struct MutIndexGuard<'a> {
171193
session: &'a mut Session,
172194
index: Option<index::Index>,

crates/red_knot_server/src/system.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub(crate) fn url_to_system_path(url: &Url) -> std::result::Result<SystemPathBuf
3232
#[derive(Debug)]
3333
pub(crate) struct LSPSystem {
3434
/// A read-only copy of the index where the server stores all the open documents and settings.
35+
///
36+
/// This will be [`None`] when a mutable reference is held to the index via [`index_mut`]
37+
/// method to prevent the index from being accessed while it is being modified. It will be
38+
/// restored when the mutable reference is dropped.
39+
///
40+
/// [`index_mut`]: crate::Session::index_mut
3541
index: Option<Arc<Index>>,
3642

3743
/// A system implementation that uses the local file system.

0 commit comments

Comments
 (0)