mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-28 20:01:33 +00:00
Fix code computing new selections
Co-Authored-By: Mikayla Maki <mikayla.c.maki@gmail.com>
This commit is contained in:
parent
299818cde0
commit
93ec73da29
3 changed files with 47 additions and 22 deletions
|
@ -75,8 +75,6 @@ pub use multi_buffer::{
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use project::{FormatTrigger, Location, LocationLink, Project, ProjectPath, ProjectTransaction};
|
use project::{FormatTrigger, Location, LocationLink, Project, ProjectPath, ProjectTransaction};
|
||||||
use rand::{seq::SliceRandom, thread_rng};
|
use rand::{seq::SliceRandom, thread_rng};
|
||||||
// use rand::seq::SliceRandom;
|
|
||||||
// use rand::thread_rng;
|
|
||||||
use scroll::{
|
use scroll::{
|
||||||
autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide,
|
autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide,
|
||||||
};
|
};
|
||||||
|
@ -4221,7 +4219,7 @@ impl Editor {
|
||||||
_: &SortLinesCaseSensitive,
|
_: &SortLinesCaseSensitive,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) {
|
) {
|
||||||
self.manipulate_lines(cx, |lines| lines.sort())
|
self.manipulate_lines(cx, |text| text.sort())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sort_lines_case_insensitive(
|
pub fn sort_lines_case_insensitive(
|
||||||
|
@ -4229,20 +4227,24 @@ impl Editor {
|
||||||
_: &SortLinesCaseInsensitive,
|
_: &SortLinesCaseInsensitive,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) {
|
) {
|
||||||
self.manipulate_lines(cx, |lines| lines.sort_by_key(|line| line.to_lowercase()))
|
self.manipulate_lines(cx, |text| text.sort_by_key(|line| line.to_lowercase()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reverse_lines(&mut self, _: &ReverseLines, cx: &mut ViewContext<Self>) {
|
pub fn reverse_lines(&mut self, _: &ReverseLines, cx: &mut ViewContext<Self>) {
|
||||||
self.manipulate_lines(cx, |lines| lines.reverse())
|
self.manipulate_lines(cx, |lines| {
|
||||||
|
lines.reverse();
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shuffle_lines(&mut self, _: &ShuffleLines, cx: &mut ViewContext<Self>) {
|
pub fn shuffle_lines(&mut self, _: &ShuffleLines, cx: &mut ViewContext<Self>) {
|
||||||
self.manipulate_lines(cx, |lines| lines.shuffle(&mut thread_rng()))
|
self.manipulate_lines(cx, |lines| {
|
||||||
|
lines.shuffle(&mut thread_rng());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn manipulate_lines<Fn>(&mut self, cx: &mut ViewContext<Self>, mut callback: Fn)
|
fn manipulate_lines<Fn>(&mut self, cx: &mut ViewContext<Self>, mut callback: Fn)
|
||||||
where
|
where
|
||||||
Fn: FnMut(&mut Vec<&str>),
|
Fn: FnMut(&mut [&str]),
|
||||||
{
|
{
|
||||||
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 buffer = self.buffer.read(cx).snapshot(cx);
|
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||||
|
@ -4252,9 +4254,9 @@ impl Editor {
|
||||||
let selections = self.selections.all::<Point>(cx);
|
let selections = self.selections.all::<Point>(cx);
|
||||||
let mut selections = selections.iter().peekable();
|
let mut selections = selections.iter().peekable();
|
||||||
let mut contiguous_row_selections = Vec::new();
|
let mut contiguous_row_selections = Vec::new();
|
||||||
|
let mut new_selections = Vec::new();
|
||||||
|
|
||||||
while let Some(selection) = selections.next() {
|
while let Some(selection) = selections.next() {
|
||||||
// Find all the selections that span a contiguous row range
|
|
||||||
let (start_row, end_row) = consume_contiguous_rows(
|
let (start_row, end_row) = consume_contiguous_rows(
|
||||||
&mut contiguous_row_selections,
|
&mut contiguous_row_selections,
|
||||||
selection,
|
selection,
|
||||||
|
@ -4262,12 +4264,35 @@ impl Editor {
|
||||||
&mut selections,
|
&mut selections,
|
||||||
);
|
);
|
||||||
|
|
||||||
let start = Point::new(start_row, 0);
|
let start_point = Point::new(start_row, 0);
|
||||||
let end = Point::new(end_row - 1, buffer.line_len(end_row - 1));
|
let end_point = Point::new(end_row - 1, buffer.line_len(end_row - 1));
|
||||||
let text = buffer.text_for_range(start..end).collect::<String>();
|
let text = buffer
|
||||||
let mut lines = text.split("\n").collect::<Vec<_>>();
|
.text_for_range(start_point..end_point)
|
||||||
callback(&mut lines);
|
.collect::<String>();
|
||||||
edits.push((start..end, lines.join("\n")));
|
let mut text = text.split("\n").collect_vec();
|
||||||
|
|
||||||
|
let text_len = text.len();
|
||||||
|
callback(&mut text);
|
||||||
|
|
||||||
|
// This is a current limitation with selections.
|
||||||
|
// If we wanted to support removing or adding lines, we'd need to fix the logic associated with selections.
|
||||||
|
debug_assert!(
|
||||||
|
text.len() == text_len,
|
||||||
|
"callback should not change the number of lines"
|
||||||
|
);
|
||||||
|
|
||||||
|
edits.push((start_point..end_point, text.join("\n")));
|
||||||
|
let start_anchor = buffer.anchor_after(start_point);
|
||||||
|
let end_anchor = buffer.anchor_before(end_point);
|
||||||
|
|
||||||
|
// Make selection and push
|
||||||
|
new_selections.push(Selection {
|
||||||
|
id: selection.id,
|
||||||
|
start: start_anchor.to_offset(&buffer),
|
||||||
|
end: end_anchor.to_offset(&buffer),
|
||||||
|
goal: SelectionGoal::None,
|
||||||
|
reversed: selection.reversed,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.transact(cx, |this, cx| {
|
self.transact(cx, |this, cx| {
|
||||||
|
@ -4276,7 +4301,7 @@ impl Editor {
|
||||||
});
|
});
|
||||||
|
|
||||||
this.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
this.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||||
s.select(contiguous_row_selections);
|
s.select(new_selections);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.request_autoscroll(Autoscroll::fit(), cx);
|
this.request_autoscroll(Autoscroll::fit(), cx);
|
||||||
|
|
|
@ -2501,7 +2501,7 @@ fn test_join_lines_with_multi_selection(cx: &mut TestAppContext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
fn test_sort_lines_with_single_selection(cx: &mut TestAppContext) {
|
fn test_manipulate_lines_with_single_selection(cx: &mut TestAppContext) {
|
||||||
init_test(cx, |_| {});
|
init_test(cx, |_| {});
|
||||||
|
|
||||||
cx.add_window(|cx| {
|
cx.add_window(|cx| {
|
||||||
|
@ -2510,7 +2510,7 @@ fn test_sort_lines_with_single_selection(cx: &mut TestAppContext) {
|
||||||
let buffer = buffer.read(cx).as_singleton().unwrap();
|
let buffer = buffer.read(cx).as_singleton().unwrap();
|
||||||
|
|
||||||
editor.change_selections(None, cx, |s| {
|
editor.change_selections(None, cx, |s| {
|
||||||
s.select_ranges([Point::new(0, 2)..Point::new(0, 2)])
|
s.select_ranges([Point::new(0, 1)..Point::new(0, 1)])
|
||||||
});
|
});
|
||||||
editor.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx);
|
editor.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -2520,13 +2520,13 @@ fn test_sort_lines_with_single_selection(cx: &mut TestAppContext) {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
editor.selections.ranges::<Point>(cx),
|
editor.selections.ranges::<Point>(cx),
|
||||||
&[Point::new(0, 2)..Point::new(0, 2)]
|
&[Point::new(0, 1)..Point::new(0, 2)]
|
||||||
);
|
);
|
||||||
|
|
||||||
editor.change_selections(None, cx, |s| {
|
editor.change_selections(None, cx, |s| {
|
||||||
s.select_ranges([Point::new(0, 2)..Point::new(5, 1)])
|
s.select_ranges([Point::new(0, 2)..Point::new(5, 0)])
|
||||||
});
|
});
|
||||||
editor.sort_lines_case_sensitive(&SortLinesCaseSensitive, cx);
|
//editor.sort_lines();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
buffer.read(cx).text(),
|
buffer.read(cx).text(),
|
||||||
"a\nbb\nccc\ndddd\n\n",
|
"a\nbb\nccc\ndddd\n\n",
|
||||||
|
@ -2542,7 +2542,7 @@ fn test_sort_lines_with_single_selection(cx: &mut TestAppContext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
fn test_sort_lines_with_multi_selection(cx: &mut TestAppContext) {
|
fn test_manipulate_lines_with_multi_selection(cx: &mut TestAppContext) {
|
||||||
init_test(cx, |_| {});
|
init_test(cx, |_| {});
|
||||||
|
|
||||||
cx.add_window(|cx| {
|
cx.add_window(|cx| {
|
||||||
|
|
|
@ -138,7 +138,7 @@ impl SelectionsCollection {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns all of the selections, adjusted to take into account the selection line_mode
|
/// Returns all of the selections, adjusted to take into account the selection line_mode
|
||||||
pub fn all_adjusted(&self, cx: &mut AppContext) -> Vec<Selection<Point>> {
|
pub fn all_adjusted(&self, cx: &mut AppContext) -> Vec<Selection<Point>> {
|
||||||
let mut selections = self.all::<Point>(cx);
|
let mut selections = self.all::<Point>(cx);
|
||||||
if self.line_mode {
|
if self.line_mode {
|
||||||
|
|
Loading…
Reference in a new issue