mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-28 21:32:39 +00:00
minor tweaks to selections collection api
This commit is contained in:
parent
c3a36e6d8a
commit
20c97637a4
3 changed files with 46 additions and 60 deletions
|
@ -1392,20 +1392,6 @@ impl Editor {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn display_selections(
|
|
||||||
&mut self,
|
|
||||||
cx: &mut ViewContext<Self>,
|
|
||||||
) -> (DisplaySnapshot, Vec<Selection<DisplayPoint>>) {
|
|
||||||
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
|
||||||
let selections = self
|
|
||||||
.selections
|
|
||||||
.interleaved::<Point>(cx)
|
|
||||||
.into_iter()
|
|
||||||
.map(|selection| selection.map(|point| point.to_display_point(&display_map)))
|
|
||||||
.collect();
|
|
||||||
(display_map, selections)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut ViewContext<Self>)
|
pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut ViewContext<Self>)
|
||||||
where
|
where
|
||||||
I: IntoIterator<Item = (Range<S>, T)>,
|
I: IntoIterator<Item = (Range<S>, T)>,
|
||||||
|
@ -1464,25 +1450,27 @@ impl Editor {
|
||||||
let position = position.to_offset(&display_map, Bias::Left);
|
let position = position.to_offset(&display_map, Bias::Left);
|
||||||
let tail_anchor = display_map.buffer_snapshot.anchor_before(tail);
|
let tail_anchor = display_map.buffer_snapshot.anchor_before(tail);
|
||||||
|
|
||||||
|
let mut pending_selection = self
|
||||||
|
.selections
|
||||||
|
.pending_anchor()
|
||||||
|
.expect("extend_selection not called with pending selection");
|
||||||
|
if position >= tail {
|
||||||
|
pending_selection.start = tail_anchor.clone();
|
||||||
|
} else {
|
||||||
|
pending_selection.end = tail_anchor.clone();
|
||||||
|
pending_selection.reversed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pending_mode = self.selections.pending_mode().unwrap();
|
||||||
|
match &mut pending_mode {
|
||||||
|
SelectMode::Word(range) | SelectMode::Line(range) => {
|
||||||
|
*range = tail_anchor.clone()..tail_anchor
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
self.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
self.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
||||||
let mut pending = s
|
s.set_pending(pending_selection, pending_mode)
|
||||||
.pending_mut()
|
|
||||||
.as_mut()
|
|
||||||
.expect("extend_selection not called with pending selection");
|
|
||||||
|
|
||||||
if position >= tail {
|
|
||||||
pending.selection.start = tail_anchor.clone();
|
|
||||||
} else {
|
|
||||||
pending.selection.end = tail_anchor.clone();
|
|
||||||
pending.selection.reversed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
match &mut pending.mode {
|
|
||||||
SelectMode::Word(range) | SelectMode::Line(range) => {
|
|
||||||
*range = tail_anchor.clone()..tail_anchor
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1591,7 +1579,8 @@ impl Editor {
|
||||||
let buffer = self.buffer.read(cx).snapshot(cx);
|
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||||
let head;
|
let head;
|
||||||
let tail;
|
let tail;
|
||||||
match &self.selections.pending_mode().unwrap() {
|
let mode = self.selections.pending_mode().unwrap();
|
||||||
|
match &mode {
|
||||||
SelectMode::Character => {
|
SelectMode::Character => {
|
||||||
head = position.to_point(&display_map);
|
head = position.to_point(&display_map);
|
||||||
tail = pending.tail().to_point(&buffer);
|
tail = pending.tail().to_point(&buffer);
|
||||||
|
@ -1660,7 +1649,7 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.change_selections(None, cx, |s| {
|
self.change_selections(None, cx, |s| {
|
||||||
s.pending_mut().as_mut().unwrap().selection = pending;
|
s.set_pending(pending, mode);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
log::error!("update_selection dispatched with no pending selection");
|
log::error!("update_selection dispatched with no pending selection");
|
||||||
|
@ -1745,8 +1734,7 @@ impl Editor {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.change_selections(None, cx, |s| s.try_cancel()) {
|
if self.change_selections(Some(Autoscroll::Fit), cx, |s| s.try_cancel()) {
|
||||||
self.request_autoscroll(Autoscroll::Fit, cx);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1851,8 +1839,6 @@ impl Editor {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
this.change_selections(Some(Autoscroll::Fit), cx, |s| s.select(new_selections));
|
this.change_selections(Some(Autoscroll::Fit), cx, |s| s.select(new_selections));
|
||||||
|
|
||||||
this.request_autoscroll(Autoscroll::Fit, cx);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1972,13 +1958,7 @@ impl Editor {
|
||||||
let mut snapshot = buffer.snapshot(cx);
|
let mut snapshot = buffer.snapshot(cx);
|
||||||
let left_biased_selections = selections
|
let left_biased_selections = selections
|
||||||
.iter()
|
.iter()
|
||||||
.map(|selection| Selection {
|
.map(|selection| selection.map(|p| snapshot.anchor_before(p)))
|
||||||
id: selection.id,
|
|
||||||
start: snapshot.anchor_before(selection.start),
|
|
||||||
end: snapshot.anchor_before(selection.end),
|
|
||||||
reversed: selection.reversed,
|
|
||||||
goal: selection.goal,
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let autoclose_pair = snapshot.language().and_then(|language| {
|
let autoclose_pair = snapshot.language().and_then(|language| {
|
||||||
|
@ -2730,13 +2710,6 @@ impl Editor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(current_ranges) = snippet.ranges.get(snippet.active_index) {
|
if let Some(current_ranges) = snippet.ranges.get(snippet.active_index) {
|
||||||
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| {
|
self.change_selections(Some(Autoscroll::Fit), cx, |s| {
|
||||||
s.select_anchor_ranges(current_ranges.into_iter().cloned())
|
s.select_anchor_ranges(current_ranges.into_iter().cloned())
|
||||||
});
|
});
|
||||||
|
@ -4024,7 +3997,6 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_selection(&mut self, above: bool, cx: &mut ViewContext<Self>) {
|
fn add_selection(&mut self, above: bool, cx: &mut ViewContext<Self>) {
|
||||||
self.push_to_selection_history();
|
|
||||||
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
|
||||||
let mut selections = self.selections.interleaved::<Point>(cx);
|
let mut selections = self.selections.interleaved::<Point>(cx);
|
||||||
let mut state = self.add_selections_state.take().unwrap_or_else(|| {
|
let mut state = self.add_selections_state.take().unwrap_or_else(|| {
|
||||||
|
|
|
@ -169,6 +169,19 @@ impl SelectionsCollection {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn display_interleaved(
|
||||||
|
&mut self,
|
||||||
|
cx: &mut MutableAppContext,
|
||||||
|
) -> (DisplaySnapshot, Vec<Selection<DisplayPoint>>) {
|
||||||
|
let display_map = self.display_map(cx);
|
||||||
|
let selections = self
|
||||||
|
.interleaved::<Point>(cx)
|
||||||
|
.into_iter()
|
||||||
|
.map(|selection| selection.map(|point| point.to_display_point(&display_map)))
|
||||||
|
.collect();
|
||||||
|
(display_map, selections)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn newest_anchor(&self) -> &Selection<Anchor> {
|
pub fn newest_anchor(&self) -> &Selection<Anchor> {
|
||||||
self.pending
|
self.pending
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
@ -308,12 +321,12 @@ impl<'a> MutableSelectionsCollection<'a> {
|
||||||
mode,
|
mode,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn pending_mut(&mut self) -> &mut Option<PendingSelection> {
|
|
||||||
&mut self.collection.pending
|
pub fn set_pending(&mut self, selection: Selection<Anchor>, mode: SelectMode) {
|
||||||
|
self.collection.pending = Some(PendingSelection { selection, mode });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_cancel(&mut self) -> bool {
|
pub fn try_cancel(&mut self) -> bool {
|
||||||
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
|
|
||||||
if let Some(pending) = self.collection.pending.take() {
|
if let Some(pending) = self.collection.pending.take() {
|
||||||
if self.disjoint.is_empty() {
|
if self.disjoint.is_empty() {
|
||||||
self.collection.disjoint = Arc::from([pending.selection]);
|
self.collection.disjoint = Arc::from([pending.selection]);
|
||||||
|
@ -327,7 +340,7 @@ impl<'a> MutableSelectionsCollection<'a> {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !oldest.start.cmp(&oldest.end, &buffer).is_eq() {
|
if !oldest.start.cmp(&oldest.end, &self.buffer()).is_eq() {
|
||||||
let head = oldest.head();
|
let head = oldest.head();
|
||||||
oldest.start = head.clone();
|
oldest.start = head.clone();
|
||||||
oldest.end = head;
|
oldest.end = head;
|
||||||
|
@ -584,7 +597,8 @@ impl<'a> MutableSelectionsCollection<'a> {
|
||||||
pub fn refresh(&mut self) -> HashMap<usize, ExcerptId> {
|
pub fn refresh(&mut self) -> HashMap<usize, ExcerptId> {
|
||||||
// TODO: Pull disjoint constraint out of update_selections so we don't have to
|
// TODO: Pull disjoint constraint out of update_selections so we don't have to
|
||||||
// store the pending_selection here.
|
// store the pending_selection here.
|
||||||
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
|
let buffer = self.buffer();
|
||||||
|
|
||||||
let mut pending = self.collection.pending.take();
|
let mut pending = self.collection.pending.take();
|
||||||
let mut selections_with_lost_position = HashMap::default();
|
let mut selections_with_lost_position = HashMap::default();
|
||||||
|
|
||||||
|
|
|
@ -130,7 +130,7 @@ fn insert_line_above(_: &mut Workspace, _: &InsertLineAbove, cx: &mut ViewContex
|
||||||
vim.switch_mode(Mode::Insert, cx);
|
vim.switch_mode(Mode::Insert, cx);
|
||||||
vim.update_active_editor(cx, |editor, cx| {
|
vim.update_active_editor(cx, |editor, cx| {
|
||||||
editor.transact(cx, |editor, cx| {
|
editor.transact(cx, |editor, cx| {
|
||||||
let (map, old_selections) = editor.display_selections(cx);
|
let (map, old_selections) = editor.selections.display_interleaved(cx);
|
||||||
let selection_start_rows: HashSet<u32> = old_selections
|
let selection_start_rows: HashSet<u32> = old_selections
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|selection| selection.start.row())
|
.map(|selection| selection.start.row())
|
||||||
|
@ -162,7 +162,7 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex
|
||||||
vim.switch_mode(Mode::Insert, cx);
|
vim.switch_mode(Mode::Insert, cx);
|
||||||
vim.update_active_editor(cx, |editor, cx| {
|
vim.update_active_editor(cx, |editor, cx| {
|
||||||
editor.transact(cx, |editor, cx| {
|
editor.transact(cx, |editor, cx| {
|
||||||
let (map, old_selections) = editor.display_selections(cx);
|
let (map, old_selections) = editor.selections.display_interleaved(cx);
|
||||||
let selection_end_rows: HashSet<u32> = old_selections
|
let selection_end_rows: HashSet<u32> = old_selections
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|selection| selection.end.row())
|
.map(|selection| selection.end.row())
|
||||||
|
|
Loading…
Reference in a new issue