diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 820595afe07a946351e1a97ff90e380f5bcc96b1..0a9baa1f139414a02f4607df53e5da747f2c5e5b 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -824,9 +824,18 @@ impl Editor { buffer, display_map, selections: Arc::from([]), - pending_selection: None, + pending_selection: Some(PendingSelection { + selection: Selection { + id: 0, + start: Anchor::min(), + end: Anchor::min(), + reversed: false, + goal: SelectionGoal::None, + }, + mode: SelectMode::Character, + }), columnar_selection_tail: None, - next_selection_id: 0, + next_selection_id: 1, add_selections_state: None, select_next_state: None, selection_history: Default::default(), @@ -853,14 +862,7 @@ impl Editor { completion_tasks: Default::default(), next_completion_id: 0, }; - let selection = Selection { - id: post_inc(&mut this.next_selection_id), - start: 0, - end: 0, - reversed: false, - goal: SelectionGoal::None, - }; - this.update_selections(vec![selection], None, cx); + this.end_selection(cx); this } @@ -1170,7 +1172,7 @@ impl Editor { let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx)); let buffer = &display_map.buffer_snapshot; - let newest_selection = self.newest_anchor_selection().unwrap().clone(); + let newest_selection = self.newest_anchor_selection().clone(); let start; let end; @@ -1722,16 +1724,15 @@ impl Editor { } fn trigger_completion_on_input(&mut self, text: &str, cx: &mut ViewContext) { - if let Some(selection) = self.newest_anchor_selection() { - if self - .buffer - .read(cx) - .is_completion_trigger(selection.head(), text, cx) - { - self.show_completions(&ShowCompletions, cx); - } else { - self.hide_context_menu(cx); - } + let selection = self.newest_anchor_selection(); + if self + .buffer + .read(cx) + .is_completion_trigger(selection.head(), text, cx) + { + self.show_completions(&ShowCompletions, cx); + } else { + self.hide_context_menu(cx); } } @@ -1882,11 +1883,7 @@ impl Editor { return; }; - let position = if let Some(selection) = self.newest_anchor_selection() { - selection.head() - } else { - return; - }; + let position = self.newest_anchor_selection().head(); let (buffer, buffer_position) = self .buffer .read(cx) @@ -1987,7 +1984,7 @@ impl Editor { let old_text = buffer.text_for_range(old_range.clone()).collect::(); let selections = self.local_selections::(cx); - let newest_selection = self.newest_anchor_selection()?; + let newest_selection = self.newest_anchor_selection(); if newest_selection.start.buffer_id != buffer_handle.id() { return None; } @@ -2060,11 +2057,7 @@ impl Editor { } fn show_code_actions(&mut self, _: &ShowCodeActions, cx: &mut ViewContext) { - let head = if let Some(selection) = self.newest_anchor_selection() { - selection.head() - } else { - return; - }; + let head = self.newest_anchor_selection().head(); let project = if let Some(project) = self.project.clone() { project } else { @@ -4203,14 +4196,15 @@ impl Editor { &self, snapshot: &MultiBufferSnapshot, ) -> Selection { - self.resolve_selection(self.newest_anchor_selection().unwrap(), snapshot) + self.resolve_selection(self.newest_anchor_selection(), snapshot) } - pub fn newest_anchor_selection(&self) -> Option<&Selection> { + pub fn newest_anchor_selection(&self) -> &Selection { self.pending_selection .as_ref() .map(|s| &s.selection) .or_else(|| self.selections.iter().max_by_key(|s| s.id)) + .unwrap() } pub fn update_selections( @@ -4313,7 +4307,8 @@ impl Editor { } fn set_selections(&mut self, selections: Arc<[Selection]>, cx: &mut ViewContext) { - let old_cursor_position = self.newest_anchor_selection().map(|s| s.head()); + let old_cursor_position = self.newest_anchor_selection().head(); + self.selections = selections; if self.focused { self.buffer.update(cx, |buffer, cx| { @@ -4334,14 +4329,13 @@ impl Editor { .iter() .max_by_key(|s| s.id) .map(|s| s.head()); - if let Some(old_cursor_position) = old_cursor_position { - if let Some(new_cursor_position) = new_cursor_position.as_ref() { - self.push_to_nav_history( - old_cursor_position, - Some(new_cursor_position.to_point(&buffer)), - cx, - ); - } + + if let Some(new_cursor_position) = new_cursor_position.as_ref() { + self.push_to_nav_history( + old_cursor_position, + Some(new_cursor_position.to_point(&buffer)), + cx, + ); } let completion_menu = match self.context_menu.as_mut() { diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index d27f607d1d2571481dee42c84a99ceb3f872332c..28ddac536bffb7cfc0b3587fc2e726cb1063d2a0 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -204,9 +204,8 @@ impl ItemView for Editor { } fn deactivated(&mut self, cx: &mut ViewContext) { - if let Some(selection) = self.newest_anchor_selection() { - self.push_to_nav_history(selection.head(), None, cx); - } + let selection = self.newest_anchor_selection(); + self.push_to_nav_history(selection.head(), None, cx); } fn is_dirty(&self, cx: &AppContext) -> bool { diff --git a/crates/find/src/find.rs b/crates/find/src/find.rs index cb50c80123e6f1cb39df2fe6afb9c0afbbc40ec1..524af7d183809e9bf751c1fa55ea9751f8439850 100644 --- a/crates/find/src/find.rs +++ b/crates/find/src/find.rs @@ -355,11 +355,8 @@ impl FindBar { if let Some(mut index) = self.active_match_index { if let Some(editor) = self.active_editor.as_ref() { editor.update(cx, |editor, cx| { - let newest_selection = editor.newest_anchor_selection().cloned(); - if let Some(((_, ranges), newest_selection)) = editor - .highlighted_ranges_for_type::() - .zip(newest_selection) - { + let newest_selection = editor.newest_anchor_selection().clone(); + if let Some((_, ranges)) = editor.highlighted_ranges_for_type::() { let position = newest_selection.head(); let buffer = editor.buffer().read(cx).read(cx); if ranges[index].start.cmp(&position, &buffer).unwrap().is_gt() { @@ -502,7 +499,7 @@ impl FindBar { fn active_match_index(&mut self, cx: &mut ViewContext) -> Option { let editor = self.active_editor.as_ref()?; let editor = editor.read(cx); - let position = editor.newest_anchor_selection()?.head(); + let position = editor.newest_anchor_selection().head(); let ranges = editor.highlighted_ranges_for_type::()?.1; if ranges.is_empty() { None