ZIm/crates/editor/src
gcp-cherry-pick-bot[bot] 059a409235
Fix panic with completion ranges and autoclose regions interop (cherry-pick #35408) (#35414)
Cherry-picked Fix panic with completion ranges and autoclose regions
interop (#35408)

As reported [in

Discord](https://discord.com/channels/869392257814519848/1106226198494859355/1398470747227426948)
C projects with `"` as "brackets" that autoclose, may invoke panics when
edited at the end of the file.

With a single selection-caret (`ˇ`), at the end of the file,
```c
ifndef BAR_H
#define BAR_H

#include <stdbool.h>

int fn_branch(bool do_branch1, bool do_branch2);

#endif // BAR_H
#include"ˇ"
```
gets an LSP response from clangd
```jsonc
{
  "filterText": "AGL/",
  "insertText": "AGL/",
  "insertTextFormat": 1,
  "kind": 17,
  "label": " AGL/",
  "labelDetails": {},
  "score": 0.78725427389144897,
  "sortText": "40b67681AGL/",
  "textEdit": {
    "newText": "AGL/",
    "range": { "end": { "character": 11, "line": 8 }, "start": { "character": 10, "line": 8 } }
  }
}
```

which replaces `"` after the caret (character/column 11, 0-indexed).
This is reasonable, as regular follow-up (proposed in further
completions), is a suffix + a closing `"`:

<img width="842" height="259" alt="image"

src="https://github.com/user-attachments/assets/ea56f621-7008-4ce2-99ba-87344ddf33d2"
/>

Yet when Zed handles user input of `"`, it panics due to multiple
reasons:

* after applying any snippet text edit, Zed did a selection change:

5537987630/crates/editor/src/editor.rs (L9539-L9545)
which caused eventual autoclose region invalidation:

5537987630/crates/editor/src/editor.rs (L2970)

This covers all cases that insert the `include""` text.

* after applying any user input and "plain" text edit, Zed did not
invalidate any autoclose regions at all, relying on the "bracket" (which
includes `"`) autoclose logic to rule edge cases out

* bracket autoclose logic detects previous `"` and considers the new
user input as a valid closure, hence no autoclose region needed.
But there is an autoclose bracket data after the plaintext completion
insertion (`AGL/`) really, and it's not invalidated after `"` handling

* in addition to that, `Anchor::is_valid` method in `text` panicked, and
required `fn try_fragment_id_for_anchor` to handle "pointing at odd,
after the end of the file, offset" cases as `false`

A test reproducing the feedback and 2 fixes added: proper, autoclose
region invalidation call which required the invalidation logic tweaked a
bit, and "superficial", "do not apply bad selections that cause panics"
fix in the editor to be more robust

Release Notes:

- Fixed panic with completion ranges and autoclose regions interop

---------

Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>

Co-authored-by: Kirill Bulatov <kirill@zed.dev>
Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
2025-08-01 08:39:10 +03:00
..
display_map sum_tree: Store context on cursor (#34904) 2025-07-22 18:20:48 +02:00
git sum_tree: Store context on cursor (#34904) 2025-07-22 18:20:48 +02:00
scroll Reduce number of snapshots and notifies during editor scrolling (#34228) 2025-07-11 17:34:45 +00:00
test language: Update block_comment and documentation comment (#34861) 2025-07-23 20:38:52 +05:30
actions.rs Add editor: convert to sentence case (#35015) 2025-07-24 08:49:04 +00:00
blink_manager.rs Remove unneeded anonymous lifetimes from gpui::Context (#27686) 2025-03-28 19:26:30 +00:00
clangd_ext.rs Use read-only access methods for read-only entity operations (#31479) 2025-05-26 23:04:31 -04:00
code_completion_tests.rs editor: Prioritize fuzzy score over sort positions in code completion sort (#35229) 2025-07-29 01:15:29 +05:30
code_context_menus.rs editor: Prioritize fuzzy score over sort positions in code completion sort (#35229) 2025-07-29 01:15:29 +05:30
display_map.rs editor: Improve minimap performance (#33067) 2025-07-15 00:29:27 +03:00
editor.rs Fix panic with completion ranges and autoclose regions interop (cherry-pick #35408) (#35414) 2025-08-01 08:39:10 +03:00
editor_settings.rs Allow disabling snippet completion by setting snippet_sort_order to none (#34565) 2025-07-17 16:22:26 +05:30
editor_settings_controls.rs Migrate to schemars version 1.0 (#33635) 2025-06-30 21:07:28 +00:00
editor_tests.rs Fix panic with completion ranges and autoclose regions interop (cherry-pick #35408) (#35414) 2025-08-01 08:39:10 +03:00
element.rs Do not subtract gutter margin twice from the editor width (#34564) 2025-07-25 00:00:59 +00:00
git.rs Project Diff 2 (#23891) 2025-02-03 13:18:50 -07:00
highlight_matching_bracket.rs debugger: Parse and highlight text with ANSI escape sequences (#32915) 2025-06-17 23:39:31 -04:00
hover_links.rs Remove into SelectionEffects from .change_selections (#33554) 2025-06-27 14:31:31 -06:00
hover_popover.rs editor: Fix diagnostic popovers not being scrollable (#33581) 2025-07-08 16:14:22 +03:00
indent_guides.rs Fix editor rendering slowness with large folds (#31569) 2025-05-28 23:05:06 +00:00
inlay_hint_cache.rs Fix document colors issues with other inlays and multi buffers (#33598) 2025-06-28 21:10:49 +00:00
inline_completion_tests.rs Restore scroll after undo edit prediction (#31162) 2025-05-22 09:16:11 +00:00
items.rs Recall empty, unsaved buffers on app load (#33475) 2025-07-11 22:23:04 +00:00
jsx_tag_auto_close.rs Remove into SelectionEffects from .change_selections (#33554) 2025-06-27 14:31:31 -06:00
linked_editing_ranges.rs editor: Fix extra characters were being written at the end of an HTML tag (#28529) 2025-04-11 00:17:34 +05:30
lsp_colors.rs Cache LSP code lens requests (#35207) 2025-07-29 09:51:58 +03:00
lsp_ext.rs Select applicable positions for lsp_ext methods more leniently (#32272) 2025-06-06 22:47:20 +00:00
mouse_context_menu.rs keymap_ui: Add context menu for table rows (#33747) 2025-07-02 03:06:45 +00:00
movement.rs Cleanup handling of surrounding word logic, fixing crash in editor::SelectAllMatches (#33353) 2025-06-24 23:18:35 -06:00
persistence.rs Simplify the SerializableItem::cleanup implementation (#29567) 2025-04-28 22:15:24 +00:00
proposed_changes_editor.rs Remove into SelectionEffects from .change_selections (#33554) 2025-06-27 14:31:31 -06:00
rust_analyzer_ext.rs Select applicable positions for lsp_ext methods more leniently (#32272) 2025-06-06 22:47:20 +00:00
scroll.rs editor: Ensure topmost buffer header can be properly folded (#34721) 2025-07-18 19:16:31 +02:00
selections_collection.rs Fix panic when editor::OpenSelectionsInMultibuffer only has pending selection (#32842) 2025-06-17 08:35:14 +00:00
signature_help.rs lsp: Complete overloaded signature help implementation (#33199) 2025-07-02 20:51:08 +03:00
tasks.rs Support tasks from rust-analyzer (#28359) 2025-04-08 15:07:56 -06:00
test.rs Remove into SelectionEffects from .change_selections (#33554) 2025-06-27 14:31:31 -06:00