diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index a08e26520c4545..460b9d5340b5c2 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4636,6 +4636,11 @@ impl Editor { snippet: Snippet, cx: &mut ViewContext, ) -> Result<()> { + struct Tabstop { + is_end_tabstop: bool, + ranges: Vec>, + } + let tabstops = self.buffer.update(cx, |buffer, cx| { let snippet_text: Arc = snippet.text.clone().into(); buffer.edit( @@ -4653,6 +4658,9 @@ impl Editor { .tabstops .iter() .map(|tabstop| { + let is_end_tabstop = tabstop.first().map_or(false, |tabstop| { + tabstop.is_empty() && tabstop.start == snippet.text.len() as isize + }); let mut tabstop_ranges = tabstop .iter() .flat_map(|tabstop_range| { @@ -4672,20 +4680,33 @@ impl Editor { }) .collect::>(); tabstop_ranges.sort_unstable_by(|a, b| a.start.cmp(&b.start, snapshot)); - tabstop_ranges + + Tabstop { + is_end_tabstop, + ranges: tabstop_ranges, + } }) .collect::>() }); if let Some(tabstop) = tabstops.first() { self.change_selections(Some(Autoscroll::fit()), cx, |s| { - s.select_ranges(tabstop.iter().cloned()); - }); - self.snippet_stack.push(SnippetState { - active_index: 0, - ranges: tabstops, + s.select_ranges(tabstop.ranges.iter().cloned()); }); + // If we're already at the last tabstop and it's at the end of the snippet, + // we're done, we don't need to keep the state around. + if !tabstop.is_end_tabstop { + let ranges = tabstops + .into_iter() + .map(|tabstop| tabstop.ranges) + .collect::>(); + self.snippet_stack.push(SnippetState { + active_index: 0, + ranges, + }); + } + // Check whether the just-entered snippet ends with an auto-closable bracket. if self.autoclose_regions.is_empty() { let snapshot = self.buffer.read(cx).snapshot(cx);