@@ -31,7 +31,14 @@ mod settings;
31
31
/// The global state for the LSP
32
32
pub struct Session {
33
33
/// 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
34
40
index : Option < Arc < index:: Index > > ,
41
+
35
42
/// Maps workspace root paths to their respective databases.
36
43
workspaces : BTreeMap < PathBuf , salsa:: Handle < RootDatabase > > ,
37
44
/// The global position encoding, negotiated during LSP initialization.
@@ -138,10 +145,22 @@ impl Session {
138
145
Ok ( ( ) )
139
146
}
140
147
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
141
155
fn index ( & self ) -> & index:: Index {
142
156
self . index . as_ref ( ) . unwrap ( )
143
157
}
144
158
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.
145
164
fn index_mut ( & mut self ) -> MutIndexGuard {
146
165
let index = self . index . take ( ) . unwrap ( ) ;
147
166
@@ -167,6 +186,9 @@ impl Session {
167
186
}
168
187
}
169
188
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.
170
192
struct MutIndexGuard < ' a > {
171
193
session : & ' a mut Session ,
172
194
index : Option < index:: Index > ,
0 commit comments