TODO:
- [x] merge main
- [x] nonshrinking `set_excerpts_for_path`
- [x] Test-drive potential problem areas in the app
- [x] prepare cloud side
- [x] test collaboration
- [ ] docstrings
- [ ] ???
## Context
### Background
Currently, a multibuffer consists of an arbitrary list of
anchor-delimited excerpts from individual buffers. Excerpt ranges for a
fixed buffer are permitted to overlap, and can appear in any order in
the multibuffer, possibly separated by excerpts from other buffers.
However, in practice all code that constructs multibuffers does so using
the APIs defined in the `path_key` submodule of the `multi_buffer` crate
(`set_excerpts_for_path` etc.) If you only use these APIs, the resulting
multibuffer will maintain the following invariants:
- All excerpts for the same buffer appear contiguously in the
multibuffer
- Excerpts for the same buffer cannot overlap
- Excerpts for the same buffer appear in order
- The placement of the excerpts for a specific buffer in the multibuffer
are determined by the `PathKey` passed to `set_excerpts_for_path`. There
is exactly one `PathKey` per buffer in the multibuffer
### Purpose of this PR
This PR changes the multibuffer so that the invariants maintained by the
`path_key` APIs *always* hold. It's no longer possible to construct a
multibuffer with overlapping excerpts, etc. The APIs that permitted
this, like `insert_excerpts_with_ids_after`, have been removed in favor
of the `path_key` suite.
The main upshot of this is that given a `text::Anchor` and a
multibuffer, it's possible to efficiently figure out the unique excerpt
that includes that anchor, if any:
```
impl MultiBufferSnapshot {
fn buffer_anchor_to_anchor(&self, anchor: text::Anchor) -> Option<multi_buffer::Anchor>;
}
```
And in the other direction, given a `multi_buffer::Anchor`, we can look
at its `text::Anchor` to locate the excerpt that contains it. That means
we don't need an `ExcerptId` to create or resolve
`multi_buffer::Anchor`, and in fact we can delete `ExcerptId` entirely,
so that excerpts no longer have any identity outside their
`Range<text::Anchor>`.
There are a large number of changes to `editor` and other downstream
crates as a result of removing `ExcerptId` and multibuffer APIs that
assumed it.
### Other changes
There are some other improvements that are not immediate consequences of
that big change, but helped make it smoother. Notably:
- The `buffer_id` field of `text::Anchor` is no longer optional.
`text::Anchor::{MIN, MAX}` have been removed in favor of
`min_for_buffer`, etc.
- `multi_buffer::Anchor` is now a three-variant enum (inlined slightly):
```
enum Anchor {
Min,
Excerpt {
text_anchor: text::Anchor,
path_key_index: PathKeyIndex,
diff_base_anchor: Option<text::Anchor>,
},
Max,
}
```
That means it's no longer possible to unconditionally access the
`text_anchor` field, which is good because most of the places that were
doing that were buggy for min/max! Instead, we have a new API that
correctly resolves min/max to the start of the first excerpt or the end
of the last excerpt:
```
impl MultiBufferSnapshot {
fn anchor_to_buffer_anchor(&self, anchor: multi_buffer::Anchor) -> Option<text::Anchor>;
}
```
- `MultiBufferExcerpt` has been removed in favor of a new
`map_excerpt_ranges` API directly on `MultiBufferSnapshot`.
## Self-Review Checklist
<!-- Check before requesting review: -->
- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable
Release Notes:
- N/A
---------
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
Co-authored-by: Conrad <conrad@zed.dev>
402 lines
13 KiB
Rust
402 lines
13 KiB
Rust
use std::{fs, path::Path};
|
|
|
|
use anyhow::Context as _;
|
|
use gpui::{App, AppContext as _, Context, Entity, Window};
|
|
use language::{Capability, Language, proto::serialize_anchor};
|
|
use multi_buffer::MultiBuffer;
|
|
use project::{
|
|
ProjectItem,
|
|
lsp_command::location_link_from_proto,
|
|
lsp_store::{
|
|
lsp_ext_command::{DocsUrls, ExpandMacro, ExpandedMacro},
|
|
rust_analyzer_ext::{RUST_ANALYZER_NAME, cancel_flycheck, clear_flycheck, run_flycheck},
|
|
},
|
|
};
|
|
use rpc::proto;
|
|
use text::ToPointUtf16;
|
|
|
|
use crate::{
|
|
CancelFlycheck, ClearFlycheck, Editor, ExpandMacroRecursively, GoToParentModule,
|
|
GotoDefinitionKind, OpenDocs, RunFlycheck, element::register_action, hover_links::HoverLink,
|
|
lsp_ext::find_specific_language_server_in_selection,
|
|
};
|
|
|
|
fn is_rust_language(language: &Language) -> bool {
|
|
language.name() == "Rust"
|
|
}
|
|
|
|
pub fn apply_related_actions(editor: &Entity<Editor>, window: &mut Window, cx: &mut App) {
|
|
if editor.read(cx).project().is_some_and(|project| {
|
|
project
|
|
.read(cx)
|
|
.language_server_statuses(cx)
|
|
.any(|(_, status)| status.name == RUST_ANALYZER_NAME)
|
|
}) {
|
|
register_action(editor, window, cancel_flycheck_action);
|
|
register_action(editor, window, run_flycheck_action);
|
|
register_action(editor, window, clear_flycheck_action);
|
|
}
|
|
|
|
if editor
|
|
.read(cx)
|
|
.buffer()
|
|
.read(cx)
|
|
.all_buffers()
|
|
.into_iter()
|
|
.filter_map(|buffer| buffer.read(cx).language())
|
|
.any(|language| is_rust_language(language))
|
|
{
|
|
register_action(editor, window, go_to_parent_module);
|
|
register_action(editor, window, expand_macro_recursively);
|
|
register_action(editor, window, open_docs);
|
|
}
|
|
}
|
|
|
|
pub fn go_to_parent_module(
|
|
editor: &mut Editor,
|
|
_: &GoToParentModule,
|
|
window: &mut Window,
|
|
cx: &mut Context<Editor>,
|
|
) {
|
|
if editor.selections.count() == 0 {
|
|
return;
|
|
}
|
|
let Some(project) = &editor.project else {
|
|
return;
|
|
};
|
|
|
|
let Some((trigger_anchor, _, server_to_query, buffer)) =
|
|
find_specific_language_server_in_selection(
|
|
editor,
|
|
cx,
|
|
is_rust_language,
|
|
RUST_ANALYZER_NAME,
|
|
)
|
|
else {
|
|
return;
|
|
};
|
|
|
|
let nav_entry = editor.navigation_entry(editor.selections.newest_anchor().head(), cx);
|
|
|
|
let project = project.clone();
|
|
let lsp_store = project.read(cx).lsp_store();
|
|
let upstream_client = lsp_store.read(cx).upstream_client();
|
|
cx.spawn_in(window, async move |editor, cx| {
|
|
let location_links = if let Some((client, project_id)) = upstream_client {
|
|
let buffer_id = buffer.read_with(cx, |buffer, _| buffer.remote_id());
|
|
|
|
let request = proto::LspExtGoToParentModule {
|
|
project_id,
|
|
buffer_id: buffer_id.to_proto(),
|
|
position: Some(serialize_anchor(&trigger_anchor)),
|
|
};
|
|
let response = client
|
|
.request(request)
|
|
.await
|
|
.context("lsp ext go to parent module proto request")?;
|
|
futures::future::join_all(
|
|
response
|
|
.links
|
|
.into_iter()
|
|
.map(|link| location_link_from_proto(link, lsp_store.clone(), cx)),
|
|
)
|
|
.await
|
|
.into_iter()
|
|
.collect::<anyhow::Result<_>>()
|
|
.context("go to parent module via collab")?
|
|
} else {
|
|
let buffer_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
|
|
let position = trigger_anchor.to_point_utf16(&buffer_snapshot);
|
|
project
|
|
.update(cx, |project, cx| {
|
|
project.request_lsp(
|
|
buffer,
|
|
project::LanguageServerToQuery::Other(server_to_query),
|
|
project::lsp_store::lsp_ext_command::GoToParentModule { position },
|
|
cx,
|
|
)
|
|
})
|
|
.await
|
|
.context("go to parent module")?
|
|
};
|
|
|
|
editor
|
|
.update_in(cx, |editor, window, cx| {
|
|
editor.navigate_to_hover_links(
|
|
Some(GotoDefinitionKind::Declaration),
|
|
location_links.into_iter().map(HoverLink::Text).collect(),
|
|
nav_entry,
|
|
false,
|
|
window,
|
|
cx,
|
|
)
|
|
})?
|
|
.await?;
|
|
anyhow::Ok(())
|
|
})
|
|
.detach_and_log_err(cx);
|
|
}
|
|
|
|
pub fn expand_macro_recursively(
|
|
editor: &mut Editor,
|
|
_: &ExpandMacroRecursively,
|
|
window: &mut Window,
|
|
cx: &mut Context<Editor>,
|
|
) {
|
|
let Some(project) = &editor.project else {
|
|
return;
|
|
};
|
|
let Some(workspace) = editor.workspace() else {
|
|
return;
|
|
};
|
|
|
|
let Some((trigger_anchor, rust_language, server_to_query, buffer)) =
|
|
find_specific_language_server_in_selection(
|
|
editor,
|
|
cx,
|
|
is_rust_language,
|
|
RUST_ANALYZER_NAME,
|
|
)
|
|
else {
|
|
return;
|
|
};
|
|
let project = project.clone();
|
|
let upstream_client = project.read(cx).lsp_store().read(cx).upstream_client();
|
|
cx.spawn_in(window, async move |_editor, cx| {
|
|
let macro_expansion = if let Some((client, project_id)) = upstream_client {
|
|
let buffer_id = buffer.update(cx, |buffer, _| buffer.remote_id());
|
|
let request = proto::LspExtExpandMacro {
|
|
project_id,
|
|
buffer_id: buffer_id.to_proto(),
|
|
position: Some(serialize_anchor(&trigger_anchor)),
|
|
};
|
|
let response = client
|
|
.request(request)
|
|
.await
|
|
.context("lsp ext expand macro proto request")?;
|
|
ExpandedMacro {
|
|
name: response.name,
|
|
expansion: response.expansion,
|
|
}
|
|
} else {
|
|
let buffer_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
|
|
let position = trigger_anchor.to_point_utf16(&buffer_snapshot);
|
|
project
|
|
.update(cx, |project, cx| {
|
|
project.request_lsp(
|
|
buffer,
|
|
project::LanguageServerToQuery::Other(server_to_query),
|
|
ExpandMacro { position },
|
|
cx,
|
|
)
|
|
})
|
|
.await
|
|
.context("expand macro")?
|
|
};
|
|
|
|
if macro_expansion.is_empty() {
|
|
log::info!("Empty macro expansion for position {:?}", trigger_anchor);
|
|
return Ok(());
|
|
}
|
|
|
|
let buffer = project
|
|
.update(cx, |project, cx| {
|
|
project.create_buffer(Some(rust_language), false, cx)
|
|
})
|
|
.await?;
|
|
workspace.update_in(cx, |workspace, window, cx| {
|
|
buffer.update(cx, |buffer, cx| {
|
|
buffer.set_text(macro_expansion.expansion, cx);
|
|
buffer.set_capability(Capability::ReadOnly, cx);
|
|
});
|
|
let multibuffer =
|
|
cx.new(|cx| MultiBuffer::singleton(buffer, cx).with_title(macro_expansion.name));
|
|
workspace.add_item_to_active_pane(
|
|
Box::new(cx.new(|cx| {
|
|
let mut editor = Editor::for_multibuffer(multibuffer, None, window, cx);
|
|
editor.set_read_only(true);
|
|
editor
|
|
})),
|
|
None,
|
|
true,
|
|
window,
|
|
cx,
|
|
);
|
|
})
|
|
})
|
|
.detach_and_log_err(cx);
|
|
}
|
|
|
|
pub fn open_docs(editor: &mut Editor, _: &OpenDocs, window: &mut Window, cx: &mut Context<Editor>) {
|
|
if editor.selections.count() == 0 {
|
|
return;
|
|
}
|
|
let Some(project) = &editor.project else {
|
|
return;
|
|
};
|
|
let Some(workspace) = editor.workspace() else {
|
|
return;
|
|
};
|
|
|
|
let Some((trigger_anchor, _, server_to_query, buffer)) =
|
|
find_specific_language_server_in_selection(
|
|
editor,
|
|
cx,
|
|
is_rust_language,
|
|
RUST_ANALYZER_NAME,
|
|
)
|
|
else {
|
|
return;
|
|
};
|
|
|
|
let project = project.clone();
|
|
let upstream_client = project.read(cx).lsp_store().read(cx).upstream_client();
|
|
cx.spawn_in(window, async move |_editor, cx| {
|
|
let docs_urls = if let Some((client, project_id)) = upstream_client {
|
|
let buffer_id = buffer.read_with(cx, |buffer, _| buffer.remote_id());
|
|
let request = proto::LspExtOpenDocs {
|
|
project_id,
|
|
buffer_id: buffer_id.to_proto(),
|
|
position: Some(serialize_anchor(&trigger_anchor)),
|
|
};
|
|
let response = client
|
|
.request(request)
|
|
.await
|
|
.context("lsp ext open docs proto request")?;
|
|
DocsUrls {
|
|
web: response.web,
|
|
local: response.local,
|
|
}
|
|
} else {
|
|
let buffer_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
|
|
let position = trigger_anchor.to_point_utf16(&buffer_snapshot);
|
|
project
|
|
.update(cx, |project, cx| {
|
|
project.request_lsp(
|
|
buffer,
|
|
project::LanguageServerToQuery::Other(server_to_query),
|
|
project::lsp_store::lsp_ext_command::OpenDocs { position },
|
|
cx,
|
|
)
|
|
})
|
|
.await
|
|
.context("open docs")?
|
|
};
|
|
|
|
if docs_urls.is_empty() {
|
|
log::debug!("Empty docs urls for position {:?}", trigger_anchor);
|
|
return Ok(());
|
|
}
|
|
|
|
workspace.update(cx, |_workspace, cx| {
|
|
// Check if the local document exists, otherwise fallback to the online document.
|
|
// Open with the default browser.
|
|
if let Some(local_url) = docs_urls.local
|
|
&& fs::metadata(Path::new(&local_url[8..])).is_ok()
|
|
{
|
|
cx.open_url(&local_url);
|
|
return;
|
|
}
|
|
|
|
if let Some(web_url) = docs_urls.web {
|
|
cx.open_url(&web_url);
|
|
}
|
|
});
|
|
anyhow::Ok(())
|
|
})
|
|
.detach_and_log_err(cx);
|
|
}
|
|
|
|
fn cancel_flycheck_action(
|
|
editor: &mut Editor,
|
|
_: &CancelFlycheck,
|
|
_: &mut Window,
|
|
cx: &mut Context<Editor>,
|
|
) {
|
|
let Some(project) = &editor.project else {
|
|
return;
|
|
};
|
|
let multibuffer_snapshot = editor
|
|
.buffer
|
|
.read_with(cx, |buffer, cx| buffer.snapshot(cx));
|
|
let buffer_id = editor
|
|
.selections
|
|
.disjoint_anchors_arc()
|
|
.iter()
|
|
.find_map(|selection| {
|
|
let buffer_id = multibuffer_snapshot
|
|
.anchor_to_buffer_anchor(selection.start)?
|
|
.0
|
|
.buffer_id;
|
|
let project = project.read(cx);
|
|
let entry_id = project
|
|
.buffer_for_id(buffer_id, cx)?
|
|
.read(cx)
|
|
.entry_id(cx)?;
|
|
project.path_for_entry(entry_id, cx)
|
|
});
|
|
cancel_flycheck(project.clone(), buffer_id, cx).detach_and_log_err(cx);
|
|
}
|
|
|
|
fn run_flycheck_action(
|
|
editor: &mut Editor,
|
|
_: &RunFlycheck,
|
|
_: &mut Window,
|
|
cx: &mut Context<Editor>,
|
|
) {
|
|
let Some(project) = &editor.project else {
|
|
return;
|
|
};
|
|
let multibuffer_snapshot = editor
|
|
.buffer
|
|
.read_with(cx, |buffer, cx| buffer.snapshot(cx));
|
|
let buffer_id = editor
|
|
.selections
|
|
.disjoint_anchors_arc()
|
|
.iter()
|
|
.find_map(|selection| {
|
|
let buffer_id = multibuffer_snapshot
|
|
.anchor_to_buffer_anchor(selection.head())?
|
|
.0
|
|
.buffer_id;
|
|
let project = project.read(cx);
|
|
let entry_id = project
|
|
.buffer_for_id(buffer_id, cx)?
|
|
.read(cx)
|
|
.entry_id(cx)?;
|
|
project.path_for_entry(entry_id, cx)
|
|
});
|
|
run_flycheck(project.clone(), buffer_id, cx).detach_and_log_err(cx);
|
|
}
|
|
|
|
fn clear_flycheck_action(
|
|
editor: &mut Editor,
|
|
_: &ClearFlycheck,
|
|
_: &mut Window,
|
|
cx: &mut Context<Editor>,
|
|
) {
|
|
let Some(project) = &editor.project else {
|
|
return;
|
|
};
|
|
let multibuffer_snapshot = editor
|
|
.buffer
|
|
.read_with(cx, |buffer, cx| buffer.snapshot(cx));
|
|
let buffer_id = editor
|
|
.selections
|
|
.disjoint_anchors_arc()
|
|
.iter()
|
|
.find_map(|selection| {
|
|
let buffer_id = multibuffer_snapshot
|
|
.anchor_to_buffer_anchor(selection.head())?
|
|
.0
|
|
.buffer_id;
|
|
let project = project.read(cx);
|
|
let entry_id = project
|
|
.buffer_for_id(buffer_id, cx)?
|
|
.read(cx)
|
|
.entry_id(cx)?;
|
|
project.path_for_entry(entry_id, cx)
|
|
});
|
|
clear_flycheck(project.clone(), buffer_id, cx).detach_and_log_err(cx);
|
|
}
|