editor: Fix LSP extension commands failing at end of file#52651
Conversation
When the cursor is at the very end of a file, the selection anchor's `buffer_id` can be `None`, causing `find_specific_language_server_in_selection` to silently skip the selection. This affects `switch source header` (clangd) and rust-analyzer commands (expand macro, open docs, open playground). Fall back to the singleton buffer's ID when the anchor has no `buffer_id`. Closes zed-industries#51330 Release Notes: - Fixed `editor: switch source header` and other LSP extension commands not working when the cursor is at the very end of a file.
b150dae to
71f3593
Compare
SomeoneToIgnore
left a comment
There was a problem hiding this comment.
Thank you for the fix, this is a very nice catch overall meaning there's a lot of places like this elsewhere.
The most important one most probably is the diagnostics-related code as there were reports of diagnostics not shown on the very edge of the file?
Either way, let's adjust the code for a proper multi buffer coordinate contract find_specific_language_server_in_selection exposes and merge this.
Use MultiBufferSnapshot::buffer_id_for_anchor to resolve buffer IDs, which properly handles anchors with None buffer_id through excerpt_containing. This replaces the custom resolve_anchor_to_buffer helper with the existing multi-buffer API. Merge all filtering logic into a single find_map to avoid borrow checker issues with cx, using a scoped block to drop the multi-buffer borrow before calling buffer.update. Strengthen test assertions to verify the specific server ID, language, buffer identity, and file presence.
f4607eb to
92061af
Compare
|
Thanks for taking the time to review this! I've addressed all the feedback:
About the diagnostics code you mentioned, I'd be happy to look into that as a follow-up if you can point me to the relevant area. |
SomeoneToIgnore
left a comment
There was a problem hiding this comment.
Thank you, I have pushed another small fix on top to avoid extra snapshot-related creation costs.
I've rechecked the diagnostics-related idea of mine and that does not seem related to this particular issue: #22473
Yet, I think if you check for text_anchor.buffer_id field usages, some of them may come out wrong, e.g.
zed/crates/editor/src/document_symbols.rs
Lines 85 to 88 in 6332655
or even
zed/crates/multi_buffer/src/multi_buffer.rs
Lines 2386 to 2399 in 6332655
are quite suspicious already.
Context
Closes #51330
When the cursor is at the very end of a file,
find_specific_language_server_in_selectionincrates/editor/src/lsp_ext.rssilently skips the selection because the anchor'sbuffer_idisNone. This causeseditor: switch source header(clangd) and rust-analyzer extension commands (expand macro,open docs,open playground) to do nothing.The fix falls back to the singleton buffer's ID when the anchor has no
buffer_id.How to Review
Single file change in
crates/editor/src/lsp_ext.rs. The diff is small — pre-compute the singleton buffer ID, then use it as fallback in thefilter_mapclosure. An integration test verifies the fix.Self-Review Checklist
Test Plan
test_find_language_server_at_end_of_file— verifiesfind_specific_language_server_in_selectionreturnsSomeat both beginning and end of fileswitch source header— now correctly opens the headercargo test -p editor— 639 passed, 0 failedRelease Notes:
editor: switch source headerand other LSP extension commands not working when the cursor is at the very end of a file.