
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 int fn_branch(bool do_branch1, bool do_branch2); ``` 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>
155 lines
4.6 KiB
Rust
155 lines
4.6 KiB
Rust
use crate::{
|
|
BufferId, BufferSnapshot, Point, PointUtf16, TextDimension, ToOffset, ToPoint, ToPointUtf16,
|
|
locator::Locator,
|
|
};
|
|
use std::{cmp::Ordering, fmt::Debug, ops::Range};
|
|
use sum_tree::Bias;
|
|
|
|
/// A timestamped position in a buffer
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, Default)]
|
|
pub struct Anchor {
|
|
pub timestamp: clock::Lamport,
|
|
/// The byte offset in the buffer
|
|
pub offset: usize,
|
|
/// Describes which character the anchor is biased towards
|
|
pub bias: Bias,
|
|
pub buffer_id: Option<BufferId>,
|
|
}
|
|
|
|
impl Anchor {
|
|
pub const MIN: Self = Self {
|
|
timestamp: clock::Lamport::MIN,
|
|
offset: usize::MIN,
|
|
bias: Bias::Left,
|
|
buffer_id: None,
|
|
};
|
|
|
|
pub const MAX: Self = Self {
|
|
timestamp: clock::Lamport::MAX,
|
|
offset: usize::MAX,
|
|
bias: Bias::Right,
|
|
buffer_id: None,
|
|
};
|
|
|
|
pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Ordering {
|
|
let fragment_id_comparison = if self.timestamp == other.timestamp {
|
|
Ordering::Equal
|
|
} else {
|
|
buffer
|
|
.fragment_id_for_anchor(self)
|
|
.cmp(buffer.fragment_id_for_anchor(other))
|
|
};
|
|
|
|
fragment_id_comparison
|
|
.then_with(|| self.offset.cmp(&other.offset))
|
|
.then_with(|| self.bias.cmp(&other.bias))
|
|
}
|
|
|
|
pub fn min(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
|
|
if self.cmp(other, buffer).is_le() {
|
|
*self
|
|
} else {
|
|
*other
|
|
}
|
|
}
|
|
|
|
pub fn max(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
|
|
if self.cmp(other, buffer).is_ge() {
|
|
*self
|
|
} else {
|
|
*other
|
|
}
|
|
}
|
|
|
|
pub fn bias(&self, bias: Bias, buffer: &BufferSnapshot) -> Anchor {
|
|
if bias == Bias::Left {
|
|
self.bias_left(buffer)
|
|
} else {
|
|
self.bias_right(buffer)
|
|
}
|
|
}
|
|
|
|
pub fn bias_left(&self, buffer: &BufferSnapshot) -> Anchor {
|
|
if self.bias == Bias::Left {
|
|
*self
|
|
} else {
|
|
buffer.anchor_before(self)
|
|
}
|
|
}
|
|
|
|
pub fn bias_right(&self, buffer: &BufferSnapshot) -> Anchor {
|
|
if self.bias == Bias::Right {
|
|
*self
|
|
} else {
|
|
buffer.anchor_after(self)
|
|
}
|
|
}
|
|
|
|
pub fn summary<D>(&self, content: &BufferSnapshot) -> D
|
|
where
|
|
D: TextDimension,
|
|
{
|
|
content.summary_for_anchor(self)
|
|
}
|
|
|
|
/// Returns true when the [`Anchor`] is located inside a visible fragment.
|
|
pub fn is_valid(&self, buffer: &BufferSnapshot) -> bool {
|
|
if *self == Anchor::MIN || *self == Anchor::MAX {
|
|
true
|
|
} else if self.buffer_id != Some(buffer.remote_id) {
|
|
false
|
|
} else {
|
|
let Some(fragment_id) = buffer.try_fragment_id_for_anchor(self) else {
|
|
return false;
|
|
};
|
|
let mut fragment_cursor = buffer.fragments.cursor::<(Option<&Locator>, usize)>(&None);
|
|
fragment_cursor.seek(&Some(fragment_id), Bias::Left);
|
|
fragment_cursor
|
|
.item()
|
|
.map_or(false, |fragment| fragment.visible)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait OffsetRangeExt {
|
|
fn to_offset(&self, snapshot: &BufferSnapshot) -> Range<usize>;
|
|
fn to_point(&self, snapshot: &BufferSnapshot) -> Range<Point>;
|
|
fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> Range<PointUtf16>;
|
|
}
|
|
|
|
impl<T> OffsetRangeExt for Range<T>
|
|
where
|
|
T: ToOffset,
|
|
{
|
|
fn to_offset(&self, snapshot: &BufferSnapshot) -> Range<usize> {
|
|
self.start.to_offset(snapshot)..self.end.to_offset(snapshot)
|
|
}
|
|
|
|
fn to_point(&self, snapshot: &BufferSnapshot) -> Range<Point> {
|
|
self.start.to_offset(snapshot).to_point(snapshot)
|
|
..self.end.to_offset(snapshot).to_point(snapshot)
|
|
}
|
|
|
|
fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> Range<PointUtf16> {
|
|
self.start.to_offset(snapshot).to_point_utf16(snapshot)
|
|
..self.end.to_offset(snapshot).to_point_utf16(snapshot)
|
|
}
|
|
}
|
|
|
|
pub trait AnchorRangeExt {
|
|
fn cmp(&self, b: &Range<Anchor>, buffer: &BufferSnapshot) -> Ordering;
|
|
fn overlaps(&self, b: &Range<Anchor>, buffer: &BufferSnapshot) -> bool;
|
|
}
|
|
|
|
impl AnchorRangeExt for Range<Anchor> {
|
|
fn cmp(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> Ordering {
|
|
match self.start.cmp(&other.start, buffer) {
|
|
Ordering::Equal => other.end.cmp(&self.end, buffer),
|
|
ord => ord,
|
|
}
|
|
}
|
|
|
|
fn overlaps(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> bool {
|
|
self.start.cmp(&other.end, buffer).is_lt() && other.start.cmp(&self.end, buffer).is_lt()
|
|
}
|
|
}
|