From 7ef98fb9359518caa59ef9df1a298d4959376dd2 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sun, 27 Feb 2022 13:02:09 -0700 Subject: [PATCH] Make versions oldest_selection and newest_selection that don't require snapshots I thought I needed this but actually didn't, but I still kinda think it's a good change for the public interface of Editor. --- crates/editor/src/editor.rs | 40 +++++++++++++++++------------ crates/editor/src/element.rs | 2 +- crates/editor/src/items.rs | 4 ++- crates/go_to_line/src/go_to_line.rs | 2 +- crates/outline/src/outline.rs | 4 ++- crates/search/src/buffer_search.rs | 2 +- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index f8f623485a..1dd4a51425 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -1061,7 +1061,8 @@ impl Editor { first_cursor_top = highlighted_rows.start as f32; last_cursor_bottom = first_cursor_top + 1.; } else if autoscroll == Autoscroll::Newest { - let newest_selection = self.newest_selection::(&display_map.buffer_snapshot); + let newest_selection = + self.newest_selection_with_snapshot::(&display_map.buffer_snapshot); first_cursor_top = newest_selection.head().to_display_point(&display_map).row() as f32; last_cursor_bottom = first_cursor_top + 1.; } else { @@ -1208,7 +1209,7 @@ impl Editor { ) { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let tail = self - .newest_selection::(&display_map.buffer_snapshot) + .newest_selection_with_snapshot::(&display_map.buffer_snapshot) .tail(); self.begin_selection(position, false, click_count, cx); @@ -1328,7 +1329,7 @@ impl Editor { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let tail = self - .newest_selection::(&display_map.buffer_snapshot) + .newest_selection_with_snapshot::(&display_map.buffer_snapshot) .tail(); self.columnar_selection_tail = Some(display_map.buffer_snapshot.anchor_before(tail)); @@ -1514,8 +1515,7 @@ impl Editor { self.set_selections(selections, None, cx); self.request_autoscroll(Autoscroll::Fit, cx); } else { - let buffer = self.buffer.read(cx).snapshot(cx); - let mut oldest_selection = self.oldest_selection::(&buffer); + let mut oldest_selection = self.oldest_selection::(&cx); if self.selection_count() == 1 { if oldest_selection.is_empty() { cx.propagate_action(); @@ -4086,7 +4086,7 @@ impl Editor { pub fn show_next_diagnostic(&mut self, _: &ShowNextDiagnostic, cx: &mut ViewContext) { let buffer = self.buffer.read(cx).snapshot(cx); - let selection = self.newest_selection::(&buffer); + let selection = self.newest_selection_with_snapshot::(&buffer); let mut active_primary_range = self.active_diagnostics.as_ref().map(|active_diagnostics| { active_diagnostics .primary_range @@ -4158,8 +4158,7 @@ impl Editor { }; let editor = editor_handle.read(cx); - let buffer = editor.buffer.read(cx); - let head = editor.newest_selection::(&buffer.read(cx)).head(); + let head = editor.newest_selection::(cx).head(); let (buffer, head) = if let Some(text_anchor) = editor.buffer.read(cx).text_anchor_for_position(head, cx) { text_anchor @@ -4207,8 +4206,7 @@ impl Editor { let editor_handle = active_item.act_as::(cx)?; let editor = editor_handle.read(cx); - let buffer = editor.buffer.read(cx); - let head = editor.newest_selection::(&buffer.read(cx)).head(); + let head = editor.newest_selection::(cx).head(); let (buffer, head) = editor.buffer.read(cx).text_anchor_for_position(head, cx)?; let replica_id = editor.replica_id(cx); @@ -4432,12 +4430,11 @@ impl Editor { self.clear_highlighted_ranges::(cx); let editor = rename.editor.read(cx); - let buffer = editor.buffer.read(cx).snapshot(cx); - let selection = editor.newest_selection::(&buffer); + let snapshot = self.buffer.read(cx).snapshot(cx); + let selection = editor.newest_selection_with_snapshot::(&snapshot); // Update the selection to match the position of the selection inside // the rename editor. - let snapshot = self.buffer.read(cx).snapshot(cx); let rename_range = rename.range.to_offset(&snapshot); let start = snapshot .clip_offset(rename_range.start + selection.start, Bias::Left) @@ -4748,17 +4745,28 @@ impl Editor { pub fn oldest_selection>( &self, - snapshot: &MultiBufferSnapshot, + cx: &AppContext, ) -> Selection { + let snapshot = self.buffer.read(cx).read(cx); self.selections .iter() .min_by_key(|s| s.id) - .map(|selection| self.resolve_selection(selection, snapshot)) - .or_else(|| self.pending_selection(snapshot)) + .map(|selection| self.resolve_selection(selection, &snapshot)) + .or_else(|| self.pending_selection(&snapshot)) .unwrap() } pub fn newest_selection>( + &self, + cx: &AppContext, + ) -> Selection { + self.resolve_selection( + self.newest_anchor_selection(), + &self.buffer.read(cx).read(cx), + ) + } + + pub fn newest_selection_with_snapshot>( &self, snapshot: &MultiBufferSnapshot, ) -> Selection { diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index f4277713b1..dcf716e0bb 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -951,7 +951,7 @@ impl Element for EditorElement { } let newest_selection_head = view - .newest_selection::(&snapshot.buffer_snapshot) + .newest_selection_with_snapshot::(&snapshot.buffer_snapshot) .head() .to_display_point(&snapshot); diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index dedd712867..4e25a5a24b 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -382,7 +382,9 @@ impl DiagnosticMessage { fn update(&mut self, editor: ViewHandle, cx: &mut ViewContext) { let editor = editor.read(cx); let buffer = editor.buffer().read(cx); - let cursor_position = editor.newest_selection::(&buffer.read(cx)).head(); + let cursor_position = editor + .newest_selection_with_snapshot::(&buffer.read(cx)) + .head(); let new_diagnostic = buffer .read(cx) .diagnostics_in_range::<_, usize>(cursor_position..cursor_position) diff --git a/crates/go_to_line/src/go_to_line.rs b/crates/go_to_line/src/go_to_line.rs index 0b35ef7dbd..c3e9cdcbf2 100644 --- a/crates/go_to_line/src/go_to_line.rs +++ b/crates/go_to_line/src/go_to_line.rs @@ -54,7 +54,7 @@ impl GoToLine { let buffer = editor.buffer().read(cx).read(cx); ( Some(scroll_position), - editor.newest_selection(&buffer).head(), + editor.newest_selection_with_snapshot(&buffer).head(), buffer.max_point(), ) }); diff --git a/crates/outline/src/outline.rs b/crates/outline/src/outline.rs index 4d2dc125c6..3607f1af87 100644 --- a/crates/outline/src/outline.rs +++ b/crates/outline/src/outline.rs @@ -259,7 +259,9 @@ impl OutlineView { let editor = self.active_editor.read(cx); let buffer = editor.buffer().read(cx).read(cx); - let cursor_offset = editor.newest_selection::(&buffer).head(); + let cursor_offset = editor + .newest_selection_with_snapshot::(&buffer) + .head(); selected_index = self .outline .items diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index 792d2a866e..7a5e3d1514 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -270,7 +270,7 @@ impl SearchBar { .display_snapshot; let selection = editor .read(cx) - .newest_selection::(&display_map.buffer_snapshot); + .newest_selection_with_snapshot::(&display_map.buffer_snapshot); let mut text: String; if selection.start == selection.end {