@@ -537,6 +537,7 @@ struct BracketPairState {
pair: BracketPair,
}
+#[derive(Debug)]
struct SnippetState {
ranges: Vec<Vec<Range<Anchor>>>,
active_index: usize,
@@ -1304,25 +1305,12 @@ impl Editor {
}
}
- pub fn change_selections<R>(
+ fn selections_did_change(
&mut self,
local: bool,
+ old_cursor_position: &Anchor,
cx: &mut ViewContext<Self>,
- change: impl FnOnce(&mut MutableSelectionsCollection<'_>) -> R,
- ) -> R {
- let old_cursor_position = self.selections.newest_anchor().head();
- self.push_to_selection_history();
-
- #[allow(deprecated)]
- // Using deprecated to prevent using change_with outside of this function
- let (autoscroll, result) =
- self.selections
- .change_with(self.display_map.clone(), self.buffer.clone(), cx, change);
-
- if let Some(autoscroll) = autoscroll {
- self.request_autoscroll(autoscroll, cx);
- }
-
+ ) {
if self.focused && self.leader_replica_id.is_none() {
self.buffer.update(cx, |buffer, cx| {
buffer.set_active_selections(&self.selections.disjoint_anchors(), cx)
@@ -1337,20 +1325,21 @@ impl Editor {
self.select_next_state = None;
self.select_larger_syntax_node_stack.clear();
self.autoclose_stack
- .invalidate(&self.selections.disjoint_anchors(), &buffer);
+ .invalidate(&self.selections.disjoint_anchors(), buffer);
self.snippet_stack
- .invalidate(&self.selections.disjoint_anchors(), &buffer);
+ .invalidate(&self.selections.disjoint_anchors(), buffer);
self.take_rename(false, cx);
let new_cursor_position = self.selections.newest_anchor().head();
self.push_to_nav_history(
old_cursor_position.clone(),
- Some(new_cursor_position.to_point(&buffer)),
+ Some(new_cursor_position.to_point(buffer)),
cx,
);
if local {
+ let new_cursor_position = self.selections.newest_anchor().head();
let completion_menu = match self.context_menu.as_mut() {
Some(ContextMenu::Completions(menu)) => Some(menu),
_ => {
@@ -1360,13 +1349,13 @@ impl Editor {
};
if let Some(completion_menu) = completion_menu {
- let cursor_position = new_cursor_position.to_offset(&buffer);
+ let cursor_position = new_cursor_position.to_offset(buffer);
let (word_range, kind) =
buffer.surrounding_word(completion_menu.initial_position.clone());
if kind == Some(CharKind::Word)
&& word_range.to_inclusive().contains(&cursor_position)
{
- let query = Self::completion_query(&buffer, cursor_position);
+ let query = Self::completion_query(buffer, cursor_position);
cx.background()
.block(completion_menu.filter(query.as_deref(), cx.background().clone()));
self.show_completions(&ShowCompletions, cx);
@@ -1387,6 +1376,25 @@ impl Editor {
self.pause_cursor_blinking(cx);
cx.emit(Event::SelectionsChanged { local });
cx.notify();
+ }
+
+ pub fn change_selections<R>(
+ &mut self,
+ autoscroll: Option<Autoscroll>,
+ cx: &mut ViewContext<Self>,
+ change: impl FnOnce(&mut MutableSelectionsCollection<'_>) -> R,
+ ) -> R {
+ let old_cursor_position = self.selections.newest_anchor().head();
+ self.push_to_selection_history();
+
+ let result =
+ self.selections
+ .change_with(self.display_map.clone(), self.buffer.clone(), cx, change);
+
+ if let Some(autoscroll) = autoscroll {
+ self.request_autoscroll(autoscroll, cx);
+ }
+ self.selections_did_change(true, &old_cursor_position, cx);
result
}
@@ -1466,7 +1474,7 @@ impl Editor {
let position = position.to_offset(&display_map, Bias::Left);
let tail_anchor = display_map.buffer_snapshot.anchor_before(tail);
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
let mut pending = s
.pending_mut()
.as_mut()
@@ -1540,7 +1548,7 @@ impl Editor {
}
}
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
if add {
if click_count > 1 {
s.delete(newest_selection.id);
@@ -1664,7 +1672,7 @@ impl Editor {
pending.reversed = false;
}
- self.change_selections(true, cx, |s| {
+ self.change_selections(None, cx, |s| {
s.pending_mut().as_mut().unwrap().selection = pending;
});
} else {
@@ -1682,8 +1690,8 @@ impl Editor {
let selections = self
.selections
.interleaved::<usize>(&self.buffer.read(cx).snapshot(cx));
- self.change_selections(true, cx, |s| {
- s.select(selections, None);
+ self.change_selections(None, cx, |s| {
+ s.select(selections);
s.clear_pending();
});
}
@@ -1723,8 +1731,8 @@ impl Editor {
})
.collect::<Vec<_>>();
- self.change_selections(true, cx, |s| {
- s.select_ranges(selection_ranges, None);
+ self.change_selections(None, cx, |s| {
+ s.select_ranges(selection_ranges);
});
cx.notify();
}
@@ -1752,7 +1760,7 @@ impl Editor {
return;
}
- if self.change_selections(true, cx, |s| s.try_cancel()) {
+ if self.change_selections(None, cx, |s| s.try_cancel()) {
self.request_autoscroll(Autoscroll::Fit, cx);
return;
}
@@ -1762,7 +1770,7 @@ impl Editor {
}
#[cfg(any(test, feature = "test-support"))]
- pub fn selected_ranges<D: TextDimension + Ord + Sub<D, Output = D>>(
+ pub fn selected_ranges<D: TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug>(
&self,
cx: &AppContext,
) -> Vec<Range<D>> {
@@ -1893,9 +1901,7 @@ impl Editor {
})
.collect();
- this.change_selections(true, cx, |s| {
- s.select(new_selections, Some(Autoscroll::Fit))
- });
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| s.select(new_selections));
this.request_autoscroll(Autoscroll::Fit, cx);
});
@@ -1941,8 +1947,8 @@ impl Editor {
.collect()
};
- this.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections);
})
});
}
@@ -2004,7 +2010,7 @@ impl Editor {
}
drop(snapshot);
- self.change_selections(true, cx, |s| s.select_anchors(selections, None));
+ self.change_selections(None, cx, |s| s.select_anchors(selections));
true
} else {
false
@@ -2102,8 +2108,8 @@ impl Editor {
});
if let Some(new_selections) = new_selections {
- self.change_selections(true, cx, |s| {
- s.select(new_selections, None);
+ self.change_selections(None, cx, |s| {
+ s.select(new_selections);
});
}
if let Some(bracket_pair_state) = bracket_pair_state {
@@ -2147,8 +2153,8 @@ impl Editor {
})
.collect();
self.autoclose_stack.pop();
- self.change_selections(true, cx, |s| {
- s.select(new_selections, Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(new_selections);
});
true
} else {
@@ -2739,8 +2745,8 @@ impl Editor {
});
if let Some(tabstop) = tabstops.first() {
- self.change_selections(true, cx, |s| {
- s.select_ranges(tabstop.iter().cloned(), Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select_ranges(tabstop.iter().cloned());
});
self.snippet_stack.push(SnippetState {
active_index: 0,
@@ -2780,15 +2786,18 @@ impl Editor {
}
}
if let Some(current_ranges) = snippet.ranges.get(snippet.active_index) {
- self.change_selections(true, cx, |s| {
- s.select_anchor_ranges(
- current_ranges.into_iter().cloned(),
- Some(Autoscroll::Fit),
- )
- });
+ let snapshot = self.buffer.read(cx).snapshot(cx);
+ let current_ranges_resolved = current_ranges
+ .iter()
+ .map(|range| range.start.to_point(&snapshot)..range.end.to_point(&snapshot))
+ .collect::<Vec<_>>();
+ dbg!(¤t_ranges, current_ranges_resolved);
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select_anchor_ranges(current_ranges.into_iter().cloned())
+ });
// If snippet state is not at the last tabstop, push it back on the stack
- if snippet.active_index + 1 != snippet.ranges.len() {
+ if snippet.active_index + 1 < snippet.ranges.len() {
self.snippet_stack.push(snippet);
}
return true;
@@ -2836,14 +2845,14 @@ impl Editor {
}
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| s.select(selections, Some(Autoscroll::Fit)));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| s.select(selections));
this.insert("", cx);
});
}
pub fn delete(&mut self, _: &Delete, cx: &mut ViewContext<Self>) {
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| {
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
if selection.is_empty() {
let cursor = movement::right(map, selection.head());
@@ -2895,8 +2904,8 @@ impl Editor {
selection.end = selection.start;
}
});
- this.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections);
});
});
} else {
@@ -2962,8 +2971,8 @@ impl Editor {
}
});
- this.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections);
});
});
}
@@ -3016,8 +3025,8 @@ impl Editor {
);
});
let snapshot = this.buffer.read(cx).snapshot(cx);
- this.change_selections(true, cx, |s| {
- s.select(s.interleaved::<usize>(&snapshot), Some(Autoscroll::Fit))
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(s.interleaved::<usize>(&snapshot))
});
});
}
@@ -3099,8 +3108,8 @@ impl Editor {
})
.collect();
- this.change_selections(true, cx, |s| {
- s.select(new_selections, Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(new_selections);
});
});
}
@@ -3251,8 +3260,8 @@ impl Editor {
}
});
this.fold_ranges(refold_ranges, cx);
- this.change_selections(true, cx, |s| {
- s.select(new_selections, Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(new_selections);
})
});
}
@@ -3356,15 +3365,13 @@ impl Editor {
}
});
this.fold_ranges(refold_ranges, cx);
- this.change_selections(true, cx, |s| {
- s.select(new_selections, Some(Autoscroll::Fit))
- });
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| s.select(new_selections));
});
}
pub fn transpose(&mut self, _: &Transpose, cx: &mut ViewContext<Self>) {
self.transact(cx, |this, cx| {
- let edits = this.change_selections(true, cx, |s| {
+ let edits = this.change_selections(Some(Autoscroll::Fit), cx, |s| {
let mut edits: Vec<(Range<usize>, String)> = Default::default();
s.move_with(|display_map, selection| {
if !selection.is_empty() {
@@ -3406,8 +3413,8 @@ impl Editor {
});
this.buffer.update(cx, |buffer, cx| buffer.edit(edits, cx));
let buffer = this.buffer.read(cx).snapshot(cx);
- this.change_selections(true, cx, |s| {
- s.select(s.interleaved::<usize>(&buffer), Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(s.interleaved::<usize>(&buffer));
});
});
}
@@ -3439,8 +3446,8 @@ impl Editor {
}
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections);
});
this.insert("", cx);
cx.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
@@ -3540,9 +3547,7 @@ impl Editor {
let selections = this
.selections
.interleaved::<usize>(&this.buffer.read(cx).read(cx));
- this.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit))
- });
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| s.select(selections));
} else {
this.insert(&clipboard_text, cx);
}
@@ -3553,10 +3558,10 @@ impl Editor {
pub fn undo(&mut self, _: &Undo, cx: &mut ViewContext<Self>) {
if let Some(tx_id) = self.buffer.update(cx, |buffer, cx| buffer.undo(cx)) {
if let Some((selections, _)) = self.selection_history.transaction(tx_id).cloned() {
- self.change_selections(true, cx, |s| {
+ self.change_selections(None, cx, |s| {
// TODO: move to SelectionsCollection to preserve selection
// invariants without rechecking
- s.select_anchors(selections.to_vec(), None);
+ s.select_anchors(selections.to_vec());
});
}
self.request_autoscroll(Autoscroll::Fit, cx);
@@ -3568,10 +3573,10 @@ impl Editor {
if let Some(tx_id) = self.buffer.update(cx, |buffer, cx| buffer.redo(cx)) {
if let Some((_, Some(selections))) = self.selection_history.transaction(tx_id).cloned()
{
- self.change_selections(true, cx, |s| {
+ self.change_selections(None, cx, |s| {
// TODO: move to SelectionsCollection to preserve selection
// invariants without rechecking
- s.select_anchors(selections.to_vec(), None);
+ s.select_anchors(selections.to_vec());
});
}
self.request_autoscroll(Autoscroll::Fit, cx);
@@ -3585,7 +3590,7 @@ impl Editor {
}
pub fn move_left(&mut self, _: &MoveLeft, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
let cursor = if selection.is_empty() {
movement::left(map, selection.start)
@@ -3598,13 +3603,13 @@ impl Editor {
}
pub fn select_left(&mut self, _: &SelectLeft, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| (movement::left(map, head), SelectionGoal::None));
})
}
pub fn move_right(&mut self, _: &MoveRight, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
let cursor = if selection.is_empty() {
movement::right(map, selection.end)
@@ -3617,7 +3622,7 @@ impl Editor {
}
pub fn select_right(&mut self, _: &SelectRight, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| (movement::right(map, head), SelectionGoal::None));
})
}
@@ -3638,7 +3643,7 @@ impl Editor {
return;
}
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
if !selection.is_empty() {
selection.goal = SelectionGoal::None;
@@ -3650,7 +3655,7 @@ impl Editor {
}
pub fn select_up(&mut self, _: &SelectUp, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, goal| movement::up(map, head, goal, false))
})
}
@@ -3669,7 +3674,7 @@ impl Editor {
return;
}
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
if !selection.is_empty() {
selection.goal = SelectionGoal::None;
@@ -3681,7 +3686,7 @@ impl Editor {
}
pub fn select_down(&mut self, _: &SelectDown, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, goal| movement::down(map, head, goal, false))
});
}
@@ -3691,7 +3696,7 @@ impl Editor {
_: &MoveToPreviousWordStart,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_cursors_with(|map, head, _| {
(
movement::previous_word_start(map, head),
@@ -3706,7 +3711,7 @@ impl Editor {
_: &MoveToPreviousSubwordStart,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_cursors_with(|map, head, _| {
(
movement::previous_subword_start(map, head),
@@ -3721,7 +3726,7 @@ impl Editor {
_: &SelectToPreviousWordStart,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| {
(
movement::previous_word_start(map, head),
@@ -3736,7 +3741,7 @@ impl Editor {
_: &SelectToPreviousSubwordStart,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| {
(
movement::previous_subword_start(map, head),
@@ -3752,7 +3757,7 @@ impl Editor {
cx: &mut ViewContext<Self>,
) {
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| {
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
if selection.is_empty() {
let cursor = movement::previous_word_start(map, selection.head());
@@ -3770,7 +3775,7 @@ impl Editor {
cx: &mut ViewContext<Self>,
) {
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| {
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
if selection.is_empty() {
let cursor = movement::previous_subword_start(map, selection.head());
@@ -3783,7 +3788,7 @@ impl Editor {
}
pub fn move_to_next_word_end(&mut self, _: &MoveToNextWordEnd, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_cursors_with(|map, head, _| {
(movement::next_word_end(map, head), SelectionGoal::None)
});
@@ -3795,7 +3800,7 @@ impl Editor {
_: &MoveToNextSubwordEnd,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_cursors_with(|map, head, _| {
(movement::next_subword_end(map, head), SelectionGoal::None)
});
@@ -3803,7 +3808,7 @@ impl Editor {
}
pub fn select_to_next_word_end(&mut self, _: &SelectToNextWordEnd, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| {
(movement::next_word_end(map, head), SelectionGoal::None)
});
@@ -3815,7 +3820,7 @@ impl Editor {
_: &SelectToNextSubwordEnd,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| {
(movement::next_subword_end(map, head), SelectionGoal::None)
});
@@ -3824,7 +3829,7 @@ impl Editor {
pub fn delete_to_next_word_end(&mut self, _: &DeleteToNextWordEnd, cx: &mut ViewContext<Self>) {
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| {
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
if selection.is_empty() {
let cursor = movement::next_word_end(map, selection.head());
@@ -3842,7 +3847,7 @@ impl Editor {
cx: &mut ViewContext<Self>,
) {
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| {
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|map, selection| {
if selection.is_empty() {
let cursor = movement::next_subword_end(map, selection.head());
@@ -3859,7 +3864,7 @@ impl Editor {
_: &MoveToBeginningOfLine,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_cursors_with(|map, head, _| {
(
movement::line_beginning(map, head, true),
@@ -3874,7 +3879,7 @@ impl Editor {
action: &SelectToBeginningOfLine,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| {
(
movement::line_beginning(map, head, action.stop_at_soft_wraps),
@@ -3890,7 +3895,7 @@ impl Editor {
cx: &mut ViewContext<Self>,
) {
self.transact(cx, |this, cx| {
- this.change_selections(true, cx, |s| {
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_with(|_, selection| {
selection.reversed = true;
});
@@ -3907,7 +3912,7 @@ impl Editor {
}
pub fn move_to_end_of_line(&mut self, _: &MoveToEndOfLine, cx: &mut ViewContext<Self>) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_cursors_with(|map, head, _| {
(movement::line_end(map, head, true), SelectionGoal::None)
});
@@ -3919,7 +3924,7 @@ impl Editor {
action: &SelectToEndOfLine,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
s.move_heads_with(|map, head, _| {
(
movement::line_end(map, head, action.stop_at_soft_wraps),
@@ -3959,8 +3964,8 @@ impl Editor {
return;
}
- self.change_selections(true, cx, |s| {
- s.select_ranges(vec![0..0], Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select_ranges(vec![0..0]);
});
}
@@ -3970,8 +3975,8 @@ impl Editor {
.last::<Point>(&self.buffer.read(cx).read(cx));
selection.set_head(Point::zero(), SelectionGoal::None);
- self.change_selections(true, cx, |s| {
- s.select(vec![selection], Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(vec![selection]);
});
}
@@ -3982,8 +3987,8 @@ impl Editor {
}
let cursor = self.buffer.read(cx).read(cx).len();
- self.change_selections(true, cx, |s| {
- s.select_ranges(vec![cursor..cursor], Some(Autoscroll::Fit))
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select_ranges(vec![cursor..cursor])
});
}
@@ -4028,15 +4033,15 @@ impl Editor {
let buffer = self.buffer.read(cx).snapshot(cx);
let mut selection = self.selections.first::<usize>(&buffer);
selection.set_head(buffer.len(), SelectionGoal::None);
- self.change_selections(true, cx, |s| {
- s.select(vec![selection], Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(vec![selection]);
});
}
pub fn select_all(&mut self, _: &SelectAll, cx: &mut ViewContext<Self>) {
let end = self.buffer.read(cx).read(cx).len();
- self.change_selections(true, cx, |s| {
- s.select_ranges(vec![0..end], Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select_ranges(vec![0..end]);
});
}
@@ -4052,8 +4057,8 @@ impl Editor {
selection.end = cmp::min(max_point, Point::new(rows.end, 0));
selection.reversed = false;
}
- self.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections);
});
}
@@ -4077,8 +4082,8 @@ impl Editor {
}
}
self.unfold_ranges(to_unfold, true, cx);
- self.change_selections(true, cx, |s| {
- s.select_ranges(new_selection_ranges, Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select_ranges(new_selection_ranges);
});
}
@@ -4180,8 +4185,8 @@ impl Editor {
state.stack.pop();
}
- self.change_selections(true, cx, |s| {
- s.select(new_selections, Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(new_selections);
});
if state.stack.len() > 1 {
self.add_selections_state = Some(state);
@@ -4229,11 +4234,11 @@ impl Editor {
if let Some(next_selected_range) = next_selected_range {
self.unfold_ranges([next_selected_range.clone()], false, cx);
- self.change_selections(true, cx, |s| {
+ self.change_selections(Some(Autoscroll::Newest), cx, |s| {
if action.replace_newest {
s.delete(s.newest_anchor().id);
}
- s.insert_range(next_selected_range, Some(Autoscroll::Newest));
+ s.insert_range(next_selected_range);
});
} else {
select_next_state.done = true;
@@ -4262,8 +4267,8 @@ impl Editor {
done: false,
};
self.unfold_ranges([selection.start..selection.end], false, cx);
- self.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Newest));
+ self.change_selections(Some(Autoscroll::Newest), cx, |s| {
+ s.select(selections);
});
self.select_next_state = Some(select_state);
} else {
@@ -4388,8 +4393,8 @@ impl Editor {
let selections = this
.selections
.interleaved::<usize>(&this.buffer.read(cx).read(cx));
- this.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit));
+ this.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections);
});
});
}
@@ -4437,8 +4442,8 @@ impl Editor {
if selected_larger_node {
stack.push(old_selections);
- self.change_selections(true, cx, |s| {
- s.select(new_selections, Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(new_selections);
});
}
self.select_larger_syntax_node_stack = stack;
@@ -4451,8 +4456,8 @@ impl Editor {
) {
let mut stack = mem::take(&mut self.select_larger_syntax_node_stack);
if let Some(selections) = stack.pop() {
- self.change_selections(true, cx, |s| {
- s.select(selections.to_vec(), Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections.to_vec());
});
}
self.select_larger_syntax_node_stack = stack;
@@ -4482,8 +4487,8 @@ impl Editor {
}
}
- self.change_selections(true, cx, |s| {
- s.select(selections, Some(Autoscroll::Fit));
+ self.change_selections(Some(Autoscroll::Fit), cx, |s| {
+ s.select(selections);
});
}
@@ -4491,10 +4496,10 @@ impl Editor {
self.end_selection(cx);
self.selection_history.mode = SelectionHistoryMode::Undoing;
if let Some(entry) = self.selection_history.undo_stack.pop_back() {
- self.change_selections(true, cx, |s| {
+ self.change_selections(None, cx, |s| {
// TODO: Move to selections so selections invariants can be preserved rather than
// rechecking them.
- s.select_anchors(entry.selections.to_vec(), None)
+ s.select_anchors(entry.selections.to_vec())
});
self.select_next_state = entry.select_next_state;
self.add_selections_state = entry.add_selections_state;
@@ -4507,10 +4512,10 @@ impl Editor {
self.end_selection(cx);
self.selection_history.mode = SelectionHistoryMode::Redoing;
if let Some(entry) = self.selection_history.redo_stack.pop_back() {
- self.change_selections(true, cx, |s| {
+ self.change_selections(None, cx, |s| {
// TODO: Move to selections so selections invariants can be preserved rather than
// rechecking them.
- s.select_anchors(entry.selections.to_vec(), None)
+ s.select_anchors(entry.selections.to_vec())
});
self.select_next_state = entry.select_next_state;
self.add_selections_state = entry.add_selections_state;
@@ -4566,17 +4571,14 @@ impl Editor {
if let Some((primary_range, group_id)) = group {
self.activate_diagnostics(group_id, cx);
- self.change_selections(true, cx, |s| {
- s.select(
- vec![Selection {
- id: selection.id,
- start: primary_range.start,
- end: primary_range.start,
- reversed: false,
- goal: SelectionGoal::None,
- }],
- Some(Autoscroll::Center),
- );
+ self.change_selections(Some(Autoscroll::Center), cx, |s| {
+ s.select(vec![Selection {
+ id: selection.id,
+ start: primary_range.start,
+ end: primary_range.start,
+ reversed: false,
+ goal: SelectionGoal::None,
+ }]);
});
break;
} else {
@@ -4640,8 +4642,8 @@ impl Editor {
if editor_handle != target_editor_handle {
nav_history.borrow_mut().disable();
}
- target_editor.change_selections(true, cx, |s| {
- s.select_ranges([range], Some(Autoscroll::Center));
+ target_editor.change_selections(Some(Autoscroll::Center), cx, |s| {
+ s.select_ranges([range]);
});
nav_history.borrow_mut().enable();
@@ -4945,7 +4947,7 @@ impl Editor {
reversed: false,
goal: SelectionGoal::None,
};
- self.change_selections(true, cx, |s| s.select(vec![new_selection], None));
+ self.change_selections(None, cx, |s| s.select(vec![new_selection]));
}
Some(rename)
@@ -5089,9 +5091,12 @@ impl Editor {
selections: Vec<Selection<Anchor>>,
cx: &mut ViewContext<Self>,
) {
- self.change_selections(false, cx, |s| {
- s.select_anchors(selections, None);
- });
+ let old_cursor_position = self.selections.newest_anchor().head();
+ self.selections
+ .change_with(self.display_map.clone(), self.buffer.clone(), cx, |s| {
+ s.select_anchors(selections);
+ });
+ self.selections_did_change(false, &old_cursor_position, cx);
}
fn push_to_selection_history(&mut self) {
@@ -5650,8 +5655,8 @@ impl Editor {
for (buffer, ranges) in new_selections_by_buffer.into_iter() {
let editor = workspace.open_project_item::<Self>(buffer, cx);
editor.update(cx, |editor, cx| {
- editor.change_selections(true, cx, |s| {
- s.select_ranges(ranges, Some(Autoscroll::Newest));
+ editor.change_selections(Some(Autoscroll::Newest), cx, |s| {
+ s.select_ranges(ranges);
});
});
}
@@ -6294,7 +6299,7 @@ mod tests {
// No event is emitted when the mutation is a no-op.
editor2.update(cx, |editor, cx| {
- editor.change_selections(true, cx, |s| s.select_ranges([0..0], None));
+ editor.change_selections(None, cx, |s| s.select_ranges([0..0]));
editor.backspace(&Backspace, cx);
});
@@ -6312,7 +6317,7 @@ mod tests {
editor.update(cx, |editor, cx| {
editor.start_transaction_at(now, cx);
- editor.change_selections(true, cx, |s| s.select_ranges([2..4], None));
+ editor.change_selections(None, cx, |s| s.select_ranges([2..4]));
editor.insert("cd", cx);
editor.end_transaction_at(now, cx);
@@ -6320,14 +6325,14 @@ mod tests {
assert_eq!(editor.selected_ranges(cx), vec![4..4]);
editor.start_transaction_at(now, cx);
- editor.change_selections(true, cx, |s| s.select_ranges([4..5], None));
+ editor.change_selections(None, cx, |s| s.select_ranges([4..5]));
editor.insert("e", cx);
editor.end_transaction_at(now, cx);
assert_eq!(editor.text(cx), "12cde6");
assert_eq!(editor.selected_ranges(cx), vec![5..5]);
now += group_interval + Duration::from_millis(1);
- editor.change_selections(true, cx, |s| s.select_ranges([2..2], None));
+ editor.change_selections(None, cx, |s| s.select_ranges([2..2]));
// Simulate an edit in another editor
buffer.update(cx, |buffer, cx| {
@@ -6481,17 +6486,17 @@ mod tests {
// Move the cursor a small distance.
// Nothing is added to the navigation history.
- editor.change_selections(true, cx, |s| {
+ editor.change_selections(None, cx, |s| {
s.select_display_ranges([DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0)])
});
- editor.change_selections(true, cx, |s| {
+ editor.change_selections(None, cx, |s| {
s.select_display_ranges([DisplayPoint::new(3, 0)..DisplayPoint::new(3, 0)])
});
assert!(nav_history.borrow_mut().pop_backward().is_none());
// Move the cursor a large distance.
// The history can jump back to the previous position.
- editor.change_selections(true, cx, |s| {
+ editor.change_selections(None, cx, |s| {
s.select_display_ranges([DisplayPoint::new(13, 0)..DisplayPoint::new(13, 3)])
});
let nav_entry = nav_history.borrow_mut().pop_backward().unwrap();
@@ -6640,7 +6645,7 @@ mod tests {
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer.clone(), cx));
view.update(cx, |view, cx| {
- view.change_selections(true, cx, |s| {
+ view.change_selections(None, cx, |s| {
s.select_display_ranges([DisplayPoint::new(8, 0)..DisplayPoint::new(12, 0)]);
});
view.fold(&Fold, cx);
@@ -6758,7 +6763,7 @@ mod tests {
&[DisplayPoint::new(0, 0)..DisplayPoint::new(0, 0)]
);
- view.change_selections(true, cx, |s| {
+ view.change_selections(None, cx, |s| {
s.select_display_ranges([DisplayPoint::new(0, 1)..DisplayPoint::new(0, 2)]);
});
view.select_to_beginning(&SelectToBeginning, cx);
@@ -6882,7 +6887,7 @@ mod tests {
let buffer = MultiBuffer::build_simple("āāāāā\nabcd\nαβγ\nabcd\nāāāāā\n", cx);
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer.clone(), cx));
view.update(cx, |view, cx| {
- view.change_selections(true, cx, |s| {
+ view.change_selections(None, cx, |s| {
s.select_display_ranges([empty_range(0, "āāāāā".len())]);
});
view.move_down(&MoveDown, cx);
@@ -6929,7 +6934,7 @@ mod tests {
let buffer = MultiBuffer::build_simple("abc\n def", cx);
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer, cx));
view.update(cx, |view, cx| {
- view.change_selections(true, cx, |s| {
+ view.change_selections(None, cx, |s| {
s.select_display_ranges([
DisplayPoint::new(0, 1)..DisplayPoint::new(0, 1),
DisplayPoint::new(1, 4)..DisplayPoint::new(1, 4),
@@ -7089,7 +7094,7 @@ mod tests {
let buffer = MultiBuffer::build_simple("use std::str::{foo, bar}\n\n {baz.qux()}", cx);
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer, cx));
view.update(cx, |view, cx| {
- view.change_selections(true, cx, |s| {
+ view.change_selections(None, cx, |s| {
s.select_display_ranges([
DisplayPoint::new(0, 11)..DisplayPoint::new(0, 11),
DisplayPoint::new(2, 4)..DisplayPoint::new(2, 4),
@@ -7200,7 +7205,7 @@ mod tests {
"use one::{\n two::three::\n four::five\n};"
);
- view.change_selections(true, cx, |s| {
+ view.change_selections(None, cx, |s| {
s.select_display_ranges([DisplayPoint::new(1, 7)..DisplayPoint::new(1, 7)]);
});
@@ -7251,7 +7256,7 @@ mod tests {
let (_, editor) = cx.add_window(Default::default(), |cx| build_editor(buffer.clone(), cx));
editor.update(cx, |editor, cx| {
- editor.change_selections(true, cx, |s| s.select_ranges(ranges, None));
+ editor.change_selections(None, cx, |s| s.select_ranges(ranges));
editor.delete_to_beginning_of_line(&DeleteToBeginningOfLine, cx);
assert_eq!(editor.text(cx), " four");
});