jsx-tag-auto-close: Remove potential source of bugs and panics (#28119)

Switch to using anchors for storing edited ranges rather than offsets as
they have to be used with multiple buffer snapshots

Release Notes:

- N/A
This commit is contained in:
Ben Kunkle 2025-04-04 15:01:08 -04:00 committed by GitHub
parent 75b9a3b6a8
commit 2747915569
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,7 +29,7 @@ const ALREADY_CLOSED_PARENT_ELEMENT_WALK_BACK_LIMIT: usize = 2;
pub(crate) fn should_auto_close( pub(crate) fn should_auto_close(
buffer: &BufferSnapshot, buffer: &BufferSnapshot,
edited_ranges: &[Range<usize>], edited_ranges: &[Range<Anchor>],
config: &JsxTagAutoCloseConfig, config: &JsxTagAutoCloseConfig,
) -> Option<Vec<JsxTagCompletionState>> { ) -> Option<Vec<JsxTagCompletionState>> {
let mut to_auto_edit = vec![]; let mut to_auto_edit = vec![];
@ -37,6 +37,7 @@ pub(crate) fn should_auto_close(
let text = buffer let text = buffer
.text_for_range(edited_range.clone()) .text_for_range(edited_range.clone())
.collect::<String>(); .collect::<String>();
let edited_range = edited_range.to_offset(&buffer);
if !text.ends_with(">") { if !text.ends_with(">") {
continue; continue;
} }
@ -94,7 +95,7 @@ pub(crate) fn should_auto_close(
pub(crate) fn generate_auto_close_edits( pub(crate) fn generate_auto_close_edits(
buffer: &BufferSnapshot, buffer: &BufferSnapshot,
ranges: &[Range<usize>], ranges: &[Range<Anchor>],
config: &JsxTagAutoCloseConfig, config: &JsxTagAutoCloseConfig,
state: Vec<JsxTagCompletionState>, state: Vec<JsxTagCompletionState>,
) -> Result<Vec<(Range<Anchor>, String)>> { ) -> Result<Vec<(Range<Anchor>, String)>> {
@ -381,7 +382,7 @@ pub(crate) fn handle_from(
struct JsxAutoCloseEditContext { struct JsxAutoCloseEditContext {
buffer: Entity<language::Buffer>, buffer: Entity<language::Buffer>,
config: language::JsxTagAutoCloseConfig, config: language::JsxTagAutoCloseConfig,
edits: Vec<Range<usize>>, edits: Vec<Range<Anchor>>,
} }
let mut edit_contexts = let mut edit_contexts =
@ -392,7 +393,10 @@ pub(crate) fn handle_from(
continue; continue;
}; };
let snapshot = buffer.read(cx).snapshot(); let snapshot = buffer.read(cx).snapshot();
for edit in buffer.read(cx).edits_since(&buffer_version_initial) { for (edit, range) in buffer
.read(cx)
.anchored_edits_since::<usize>(&buffer_version_initial)
{
let Some(language) = snapshot.language_at(edit.new.end) else { let Some(language) = snapshot.language_at(edit.new.end) else {
continue; continue;
}; };
@ -414,7 +418,7 @@ pub(crate) fn handle_from(
edits: vec![], edits: vec![],
}) })
.edits .edits
.push(edit.new); .push(range);
} }
} }
@ -448,7 +452,6 @@ pub(crate) fn handle_from(
}; };
let ensure_no_edits_since_start = || -> Option<()> { let ensure_no_edits_since_start = || -> Option<()> {
// <div>wef,wefwef
let has_edits_since_start = this let has_edits_since_start = this
.read_with(cx, |this, cx| { .read_with(cx, |this, cx| {
this.buffer.read_with(cx, |buffer, cx| { this.buffer.read_with(cx, |buffer, cx| {