Capture a weak reference to the Project in fake LSP

This commit is contained in:
Antonio Scandurra 2022-03-01 13:34:03 +01:00
parent 8d078ed4e2
commit 3e9dbe10d6

View file

@ -4175,7 +4175,7 @@ mod tests {
.unwrap(); .unwrap();
clients.push(cx.foreground().spawn(host.simulate_host( clients.push(cx.foreground().spawn(host.simulate_host(
host_project.clone(), host_project,
language_server_config, language_server_config,
operations.clone(), operations.clone(),
max_operations, max_operations,
@ -4231,7 +4231,8 @@ mod tests {
let mut clients = futures::future::join_all(clients).await; let mut clients = futures::future::join_all(clients).await;
cx.foreground().run_until_parked(); cx.foreground().run_until_parked();
let (_, host_cx) = clients.remove(0); let (host_client, host_cx) = clients.remove(0);
let host_project = host_client.project.as_ref().unwrap();
let host_worktree_snapshots = host_project.read_with(&host_cx, |project, cx| { let host_worktree_snapshots = host_project.read_with(&host_cx, |project, cx| {
project project
.worktrees(cx) .worktrees(cx)
@ -4519,7 +4520,7 @@ mod tests {
language_server_config.set_fake_initializer({ language_server_config.set_fake_initializer({
let rng = rng.clone(); let rng = rng.clone();
let files = files.clone(); let files = files.clone();
let project = project.clone(); let project = project.downgrade();
move |fake_server| { move |fake_server| {
fake_server.handle_request::<lsp::request::Completion, _>(|_, _| { fake_server.handle_request::<lsp::request::Completion, _>(|_, _| {
Some(lsp::CompletionResponse::Array(vec![lsp::CompletionItem { Some(lsp::CompletionResponse::Array(vec![lsp::CompletionItem {
@ -4579,37 +4580,44 @@ mod tests {
let rng = rng.clone(); let rng = rng.clone();
let project = project.clone(); let project = project.clone();
move |params, mut cx| { move |params, mut cx| {
project.update(&mut cx, |project, cx| { if let Some(project) = project.upgrade(&cx) {
let path = params project.update(&mut cx, |project, cx| {
.text_document_position_params let path = params
.text_document .text_document_position_params
.uri .text_document
.to_file_path() .uri
.unwrap(); .to_file_path()
let (worktree, relative_path) = .unwrap();
project.find_local_worktree(&path, cx)?; let (worktree, relative_path) =
let project_path = project.find_local_worktree(&path, cx)?;
ProjectPath::from((worktree.read(cx).id(), relative_path)); let project_path =
let buffer = project.get_open_buffer(&project_path, cx)?.read(cx); ProjectPath::from((worktree.read(cx).id(), relative_path));
let buffer =
project.get_open_buffer(&project_path, cx)?.read(cx);
let mut highlights = Vec::new(); let mut highlights = Vec::new();
let highlight_count = rng.lock().gen_range(1..=5); let highlight_count = rng.lock().gen_range(1..=5);
let mut prev_end = 0; let mut prev_end = 0;
for _ in 0..highlight_count { for _ in 0..highlight_count {
let range = let range =
buffer.random_byte_range(prev_end, &mut *rng.lock()); buffer.random_byte_range(prev_end, &mut *rng.lock());
let start = let start = buffer
buffer.offset_to_point_utf16(range.start).to_lsp_position(); .offset_to_point_utf16(range.start)
let end = .to_lsp_position();
buffer.offset_to_point_utf16(range.end).to_lsp_position(); let end = buffer
highlights.push(lsp::DocumentHighlight { .offset_to_point_utf16(range.end)
range: lsp::Range::new(start, end), .to_lsp_position();
kind: Some(lsp::DocumentHighlightKind::READ), highlights.push(lsp::DocumentHighlight {
}); range: lsp::Range::new(start, end),
prev_end = range.end; kind: Some(lsp::DocumentHighlightKind::READ),
} });
Some(highlights) prev_end = range.end;
}) }
Some(highlights)
})
} else {
None
}
} }
}); });
} }