Make SelectionsCollection::disjoint_anchor_ranges return an iterator (#22948)

This helps discourage unnecessary collection to Vec

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-01-10 02:37:46 -07:00 committed by GitHub
parent 690ad29ba9
commit 1b44398967
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 31 deletions

View file

@ -2559,7 +2559,7 @@ impl Editor {
if start_offset > buffer_snapshot.len() || end_offset > buffer_snapshot.len() { if start_offset > buffer_snapshot.len() || end_offset > buffer_snapshot.len() {
continue; continue;
} }
if self.selections.disjoint_anchor_ranges().iter().any(|s| { if self.selections.disjoint_anchor_ranges().any(|s| {
if s.start.buffer_id != selection.start.buffer_id if s.start.buffer_id != selection.start.buffer_id
|| s.end.buffer_id != selection.end.buffer_id || s.end.buffer_id != selection.end.buffer_id
{ {
@ -10567,12 +10567,9 @@ impl Editor {
} else { } else {
let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx); let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx);
let mut toggled_buffers = HashSet::default(); let mut toggled_buffers = HashSet::default();
for (_, buffer_snapshot, _) in multi_buffer_snapshot.excerpts_in_ranges( for (_, buffer_snapshot, _) in
self.selections multi_buffer_snapshot.excerpts_in_ranges(self.selections.disjoint_anchor_ranges())
.disjoint_anchors() {
.into_iter()
.map(|selection| selection.range()),
) {
let buffer_id = buffer_snapshot.remote_id(); let buffer_id = buffer_snapshot.remote_id();
if toggled_buffers.insert(buffer_id) { if toggled_buffers.insert(buffer_id) {
if self.buffer_folded(buffer_id, cx) { if self.buffer_folded(buffer_id, cx) {
@ -10653,12 +10650,9 @@ impl Editor {
} else { } else {
let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx); let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx);
let mut folded_buffers = HashSet::default(); let mut folded_buffers = HashSet::default();
for (_, buffer_snapshot, _) in multi_buffer_snapshot.excerpts_in_ranges( for (_, buffer_snapshot, _) in
self.selections multi_buffer_snapshot.excerpts_in_ranges(self.selections.disjoint_anchor_ranges())
.disjoint_anchors() {
.into_iter()
.map(|selection| selection.range()),
) {
let buffer_id = buffer_snapshot.remote_id(); let buffer_id = buffer_snapshot.remote_id();
if folded_buffers.insert(buffer_id) { if folded_buffers.insert(buffer_id) {
self.fold_buffer(buffer_id, cx); self.fold_buffer(buffer_id, cx);
@ -10819,12 +10813,9 @@ impl Editor {
} else { } else {
let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx); let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx);
let mut unfolded_buffers = HashSet::default(); let mut unfolded_buffers = HashSet::default();
for (_, buffer_snapshot, _) in multi_buffer_snapshot.excerpts_in_ranges( for (_, buffer_snapshot, _) in
self.selections multi_buffer_snapshot.excerpts_in_ranges(self.selections.disjoint_anchor_ranges())
.disjoint_anchors() {
.into_iter()
.map(|selection| selection.range()),
) {
let buffer_id = buffer_snapshot.remote_id(); let buffer_id = buffer_snapshot.remote_id();
if unfolded_buffers.insert(buffer_id) { if unfolded_buffers.insert(buffer_id) {
self.unfold_buffer(buffer_id, cx); self.unfold_buffer(buffer_id, cx);

View file

@ -1260,8 +1260,8 @@ impl SearchableItem for Editor {
return; return;
} }
let ranges = self.selections.disjoint_anchor_ranges(); let ranges = self.selections.disjoint_anchor_ranges().collect::<Vec<_>>();
if ranges.iter().any(|range| range.start != range.end) { if ranges.iter().any(|s| s.start != s.end) {
self.set_search_within_ranges(&ranges, cx); self.set_search_within_ranges(&ranges, cx);
} else if let Some(previous_search_ranges) = self.previous_search_ranges.take() { } else if let Some(previous_search_ranges) = self.previous_search_ranges.take() {
self.set_search_within_ranges(&previous_search_ranges, cx) self.set_search_within_ranges(&previous_search_ranges, cx)

View file

@ -88,6 +88,12 @@ impl SelectionsCollection {
self.disjoint.clone() self.disjoint.clone()
} }
pub fn disjoint_anchor_ranges(&self) -> impl Iterator<Item = Range<Anchor>> {
// Mapping the Arc slice would borrow it, whereas indexing captures it.
let disjoint = self.disjoint_anchors();
(0..disjoint.len()).map(move |ix| disjoint[ix].range())
}
pub fn pending_anchor(&self) -> Option<Selection<Anchor>> { pub fn pending_anchor(&self) -> Option<Selection<Anchor>> {
self.pending self.pending
.as_ref() .as_ref()
@ -317,13 +323,6 @@ impl SelectionsCollection {
self.all(cx).last().unwrap().clone() self.all(cx).last().unwrap().clone()
} }
pub fn disjoint_anchor_ranges(&self) -> Vec<Range<Anchor>> {
self.disjoint_anchors()
.iter()
.map(|s| s.start..s.end)
.collect()
}
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub fn ranges<D: TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug>( pub fn ranges<D: TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug>(
&self, &self,

View file

@ -185,9 +185,12 @@ pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
let previous_selections = vim let previous_selections = vim
.update_editor(cx, |_, editor, cx| { .update_editor(cx, |_, editor, cx| {
let selections = action let selections = action.restore_selection.then(|| {
.restore_selection editor
.then(|| editor.selections.disjoint_anchor_ranges()); .selections
.disjoint_anchor_ranges()
.collect::<Vec<_>>()
});
editor.change_selections(None, cx, |s| { editor.change_selections(None, cx, |s| {
let end = Point::new(range.end.0, s.buffer().line_len(range.end)); let end = Point::new(range.end.0, s.buffer().line_len(range.end));
s.select_ranges([end..Point::new(range.start.0, 0)]); s.select_ranges([end..Point::new(range.start.0, 0)]);