Skip to content

Commit

Permalink
Start to split out initialization and registration (#21787)
Browse files Browse the repository at this point in the history
Still TODO:

* [x] Factor out `start_language_server` so we can call it on register
(instead of on detect language)
* [x] Only call register in singleton editors (or when
editing/go-to-definition etc. in a multibuffer?)
* [x] Refcount on register so we can unregister when no buffer remain
* [ ] (maybe) Stop language servers that are no longer needed after some
time

Release Notes:

- Fixed language servers starting when doing project search
- Fixed high CPU usage when ignoring warnings in the diagnostics view

---------

Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
Co-authored-by: Cole <cole@zed.dev>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent de89f8c commit 13a81e4
Show file tree
Hide file tree
Showing 19 changed files with 7,472 additions and 7,120 deletions.
6 changes: 4 additions & 2 deletions crates/assistant/src/assistant_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5113,9 +5113,11 @@ fn make_lsp_adapter_delegate(
return Ok(None::<Arc<dyn LspAdapterDelegate>>);
};
let http_client = project.client().http_client().clone();
project.lsp_store().update(cx, |lsp_store, cx| {
project.lsp_store().update(cx, |_, cx| {
Ok(Some(LocalLspAdapterDelegate::new(
lsp_store,
project.languages().clone(),
project.environment(),
cx.weak_model(),
&worktree,
http_client,
project.fs().clone(),
Expand Down
3 changes: 3 additions & 0 deletions crates/collab/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ impl Server {
.add_request_handler(forward_read_only_project_request::<proto::OpenBufferByPath>)
.add_request_handler(forward_read_only_project_request::<proto::GitBranches>)
.add_request_handler(forward_read_only_project_request::<proto::GetStagedText>)
.add_request_handler(
forward_mutating_project_request::<proto::RegisterBufferWithLanguageServers>,
)
.add_request_handler(forward_mutating_project_request::<proto::UpdateGitBranch>)
.add_request_handler(forward_mutating_project_request::<proto::GetCompletions>)
.add_request_handler(
Expand Down
9 changes: 6 additions & 3 deletions crates/collab/src/tests/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,12 @@ async fn test_language_server_statuses(cx_a: &mut TestAppContext, cx_b: &mut Tes
}),
)
.await;
let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
let (project_a, _) = client_a.build_local_project("/dir", cx_a).await;

let _buffer_a = project_a
.update(cx_a, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx))
.update(cx_a, |p, cx| {
p.open_local_buffer_with_lsp("/dir/main.rs", cx)
})
.await
.unwrap();

Expand Down Expand Up @@ -1587,7 +1589,6 @@ async fn test_mutual_editor_inlay_hint_cache_update(
})
.await
.unwrap();
let fake_language_server = fake_language_servers.next().await.unwrap();
let editor_a = workspace_a
.update(cx_a, |workspace, cx| {
workspace.open_path((worktree_id, "main.rs"), None, true, cx)
Expand All @@ -1597,6 +1598,8 @@ async fn test_mutual_editor_inlay_hint_cache_update(
.downcast::<Editor>()
.unwrap();

let fake_language_server = fake_language_servers.next().await.unwrap();

// Set up the language server to return an additional inlay hint on each request.
let edits_made = Arc::new(AtomicUsize::new(0));
let closure_edits_made = Arc::clone(&edits_made);
Expand Down
99 changes: 69 additions & 30 deletions crates/collab/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3891,13 +3891,7 @@ async fn test_collaborating_with_diagnostics(
// Cause the language server to start.
let _buffer = project_a
.update(cx_a, |project, cx| {
project.open_buffer(
ProjectPath {
worktree_id,
path: Path::new("other.rs").into(),
},
cx,
)
project.open_local_buffer_with_lsp("/a/other.rs", cx)
})
.await
.unwrap();
Expand Down Expand Up @@ -4176,7 +4170,9 @@ async fn test_collaborating_with_lsp_progress_updates_and_diagnostics_ordering(
// Join the project as client B and open all three files.
let project_b = client_b.join_remote_project(project_id, cx_b).await;
let guest_buffers = futures::future::try_join_all(file_names.iter().map(|file_name| {
project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, file_name), cx))
project_b.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, file_name), cx)
})
}))
.await
.unwrap();
Expand Down Expand Up @@ -4230,7 +4226,7 @@ async fn test_collaborating_with_lsp_progress_updates_and_diagnostics_ordering(
cx.subscribe(&project_b, move |_, _, event, cx| {
if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
disk_based_diagnostics_finished.store(true, SeqCst);
for buffer in &guest_buffers {
for (buffer, _) in &guest_buffers {
assert_eq!(
buffer
.read(cx)
Expand Down Expand Up @@ -4351,7 +4347,6 @@ async fn test_formatting_buffer(
cx_a: &mut TestAppContext,
cx_b: &mut TestAppContext,
) {
executor.allow_parking();
let mut server = TestServer::start(executor.clone()).await;
let client_a = server.create_client(cx_a, "user_a").await;
let client_b = server.create_client(cx_b, "user_b").await;
Expand Down Expand Up @@ -4379,10 +4374,16 @@ async fn test_formatting_buffer(
.await
.unwrap();
let project_b = client_b.join_remote_project(project_id, cx_b).await;
let lsp_store_b = project_b.update(cx_b, |p, _| p.lsp_store());

let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
let buffer_b = project_b
.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx))
.await
.unwrap();

let _handle = lsp_store_b.update(cx_b, |lsp_store, cx| {
lsp_store.register_buffer_with_language_servers(&buffer_b, cx)
});
let fake_language_server = fake_language_servers.next().await.unwrap();
fake_language_server.handle_request::<lsp::request::Formatting, _, _>(|_, _| async move {
Ok(Some(vec![
Expand Down Expand Up @@ -4431,6 +4432,8 @@ async fn test_formatting_buffer(
});
});
});

executor.allow_parking();
project_b
.update(cx_b, |project, cx| {
project.format(
Expand Down Expand Up @@ -4503,8 +4506,12 @@ async fn test_prettier_formatting_buffer(
.await
.unwrap();
let project_b = client_b.join_remote_project(project_id, cx_b).await;
let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.ts"), cx));
let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
let (buffer_b, _) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "a.ts"), cx)
})
.await
.unwrap();

cx_a.update(|cx| {
SettingsStore::update_global(cx, |store, cx| {
Expand Down Expand Up @@ -4620,8 +4627,12 @@ async fn test_definition(
let project_b = client_b.join_remote_project(project_id, cx_b).await;

// Open the file on client B.
let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
let (buffer_b, _handle) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "a.rs"), cx)
})
.await
.unwrap();

// Request the definition of a symbol as the guest.
let fake_language_server = fake_language_servers.next().await.unwrap();
Expand Down Expand Up @@ -4765,8 +4776,12 @@ async fn test_references(
let project_b = client_b.join_remote_project(project_id, cx_b).await;

// Open the file on client B.
let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "one.rs"), cx));
let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
let (buffer_b, _handle) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "one.rs"), cx)
})
.await
.unwrap();

// Request references to a symbol as the guest.
let fake_language_server = fake_language_servers.next().await.unwrap();
Expand Down Expand Up @@ -5012,8 +5027,12 @@ async fn test_document_highlights(
let project_b = client_b.join_remote_project(project_id, cx_b).await;

// Open the file on client B.
let open_b = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx));
let buffer_b = cx_b.executor().spawn(open_b).await.unwrap();
let (buffer_b, _handle) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "main.rs"), cx)
})
.await
.unwrap();

// Request document highlights as the guest.
let fake_language_server = fake_language_servers.next().await.unwrap();
Expand Down Expand Up @@ -5130,8 +5149,12 @@ async fn test_lsp_hover(
let project_b = client_b.join_remote_project(project_id, cx_b).await;

// Open the file as the guest
let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx));
let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
let (buffer_b, _handle) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "main.rs"), cx)
})
.await
.unwrap();

let mut servers_with_hover_requests = HashMap::default();
for i in 0..language_server_names.len() {
Expand Down Expand Up @@ -5306,9 +5329,12 @@ async fn test_project_symbols(
let project_b = client_b.join_remote_project(project_id, cx_b).await;

// Cause the language server to start.
let open_buffer_task =
project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "one.rs"), cx));
let _buffer = cx_b.executor().spawn(open_buffer_task).await.unwrap();
let _buffer = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "one.rs"), cx)
})
.await
.unwrap();

let fake_language_server = fake_language_servers.next().await.unwrap();
fake_language_server.handle_request::<lsp::WorkspaceSymbolRequest, _, _>(|_, _| async move {
Expand Down Expand Up @@ -5400,8 +5426,12 @@ async fn test_open_buffer_while_getting_definition_pointing_to_it(
.unwrap();
let project_b = client_b.join_remote_project(project_id, cx_b).await;

let open_buffer_task = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
let buffer_b1 = cx_b.executor().spawn(open_buffer_task).await.unwrap();
let (buffer_b1, _lsp) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "a.rs"), cx)
})
.await
.unwrap();

let fake_language_server = fake_language_servers.next().await.unwrap();
fake_language_server.handle_request::<lsp::request::GotoDefinition, _, _>(|_, _| async move {
Expand All @@ -5417,13 +5447,22 @@ async fn test_open_buffer_while_getting_definition_pointing_to_it(
let buffer_b2;
if rng.gen() {
definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx));
buffer_b2 = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.rs"), cx));
(buffer_b2, _) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "b.rs"), cx)
})
.await
.unwrap();
} else {
buffer_b2 = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.rs"), cx));
(buffer_b2, _) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "b.rs"), cx)
})
.await
.unwrap();
definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx));
}

let buffer_b2 = buffer_b2.await.unwrap();
let definitions = definitions.await.unwrap();
assert_eq!(definitions.len(), 1);
assert_eq!(definitions[0].target.buffer, buffer_b2);
Expand Down
6 changes: 4 additions & 2 deletions crates/collab/src/tests/remote_editing_collaboration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,10 @@ async fn test_ssh_collaboration_formatting_with_prettier(
executor.run_until_parked();

// Opens the buffer and formats it
let buffer_b = project_b
.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.ts"), cx))
let (buffer_b, _handle) = project_b
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "a.ts"), cx)
})
.await
.expect("user B opens buffer for formatting");

Expand Down
Loading

0 comments on commit 13a81e4

Please sign in to comment.