refactor(lsp): remove Documents mutex and require Documents to be mutated to change it (#12747)

This commit is contained in:
David Sherret 2021-11-18 13:50:24 -05:00 committed by GitHub
parent 14f83da221
commit c82ce74133
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 483 additions and 477 deletions

View file

@ -71,7 +71,7 @@ const FILE_EXTENSION_KIND_MODIFIERS: &[&str] =
type Request = (
RequestMethod,
StateSnapshot,
Arc<StateSnapshot>,
oneshot::Sender<Result<Value, AnyError>>,
);
@ -107,7 +107,7 @@ impl TsServer {
pub(crate) async fn request<R>(
&self,
snapshot: StateSnapshot,
snapshot: Arc<StateSnapshot>,
req: RequestMethod,
) -> Result<R, AnyError>
where
@ -211,7 +211,7 @@ impl Assets {
self.0.insert(k, v)
}
pub fn set_navigation_tree(
pub fn cache_navigation_tree(
&mut self,
specifier: &ModuleSpecifier,
navigation_tree: Arc<NavigationTree>,
@ -234,7 +234,7 @@ impl Assets {
pub(crate) async fn get_asset(
specifier: &ModuleSpecifier,
ts_server: &TsServer,
state_snapshot: StateSnapshot,
state_snapshot: Arc<StateSnapshot>,
) -> Result<Option<AssetDocument>, AnyError> {
let specifier_str = specifier.to_string().replace("asset:///", "");
if let Some(text) = tsc::get_asset(&specifier_str) {
@ -892,7 +892,7 @@ impl RenameLocations {
lsp::TextDocumentEdit {
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
uri: uri.clone(),
version: asset_or_doc.document_version(),
version: asset_or_doc.document_lsp_version(),
},
edits:
Vec::<lsp::OneOf<lsp::TextEdit, lsp::AnnotatedTextEdit>>::new(),
@ -1058,7 +1058,7 @@ impl FileTextChanges {
Ok(lsp::TextDocumentEdit {
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
uri: specifier.clone(),
version: asset_or_doc.document_version(),
version: asset_or_doc.document_lsp_version(),
},
edits,
})
@ -1104,7 +1104,7 @@ impl FileTextChanges {
text_document: lsp::OptionalVersionedTextDocumentIdentifier {
uri: specifier.clone(),
version: maybe_asset_or_document
.map(|d| d.document_version())
.map(|d| d.document_lsp_version())
.flatten(),
},
edits,
@ -2091,13 +2091,13 @@ struct Response {
struct State<'a> {
last_id: usize,
response: Option<Response>,
state_snapshot: StateSnapshot,
state_snapshot: Arc<StateSnapshot>,
snapshots: HashMap<(ModuleSpecifier, Cow<'a, str>), String>,
specifiers: HashMap<String, String>,
}
impl<'a> State<'a> {
fn new(state_snapshot: StateSnapshot) -> Self {
fn new(state_snapshot: Arc<StateSnapshot>) -> Self {
Self {
last_id: 1,
response: None,
@ -2454,7 +2454,7 @@ fn load() -> Result<JsRuntime, AnyError> {
{
let op_state = runtime.op_state();
let mut op_state = op_state.borrow_mut();
op_state.put(State::new(StateSnapshot::default()));
op_state.put(State::new(Arc::new(StateSnapshot::default())));
}
runtime.register_op("op_dispose", op(op_dispose));
@ -2888,7 +2888,7 @@ impl RequestMethod {
/// Send a request into a runtime and return the JSON value of the response.
pub(crate) fn request(
runtime: &mut JsRuntime,
state_snapshot: StateSnapshot,
state_snapshot: Arc<StateSnapshot>,
method: RequestMethod,
) -> Result<Value, AnyError> {
let performance = state_snapshot.performance.clone();
@ -2937,7 +2937,7 @@ mod tests {
fixtures: &[(&str, &str, i32, LanguageId)],
location: &Path,
) -> StateSnapshot {
let documents = Documents::new(location);
let mut documents = Documents::new(location);
for (specifier, source, version, language_id) in fixtures {
let specifier =
resolve_url(specifier).expect("failed to create specifier");
@ -2958,10 +2958,10 @@ mod tests {
debug: bool,
config: Value,
sources: &[(&str, &str, i32, LanguageId)],
) -> (JsRuntime, StateSnapshot, PathBuf) {
) -> (JsRuntime, Arc<StateSnapshot>, PathBuf) {
let temp_dir = TempDir::new().expect("could not create temp dir");
let location = temp_dir.path().join("deps");
let state_snapshot = mock_state_snapshot(sources, &location);
let state_snapshot = Arc::new(mock_state_snapshot(sources, &location));
let mut runtime = load().expect("could not start server");
start(&mut runtime, debug, &state_snapshot)
.expect("could not start server");