Implement select_line for buffer

This commit is contained in:
Antonio Scandurra
2021-05-07 11:13:21 +02:00
parent cfb7c8770c
commit 06fb4e8200
2 changed files with 76 additions and 9 deletions

View File

@@ -75,6 +75,7 @@ impl Selection {
pub fn buffer_rows_for_display_rows(
&self,
expand_if_ends_at_line_start: bool,
map: &DisplayMap,
ctx: &AppContext,
) -> (Range<u32>, Range<u32>) {
@@ -84,7 +85,8 @@ impl Selection {
.unwrap();
let mut display_end = self.end.to_display_point(map, ctx).unwrap();
if display_end.row() != map.max_point(ctx).row()
if !expand_if_ends_at_line_start
&& display_end.row() != map.max_point(ctx).row()
&& display_start.row() != display_end.row()
&& display_end.column() == 0
{

View File

@@ -139,6 +139,7 @@ pub fn init(app: &mut MutableAppContext) {
),
Binding::new("cmd-shift-down", "buffer:select_to_end", Some("BufferView")),
Binding::new("cmd-a", "buffer:select_all", Some("BufferView")),
Binding::new("cmd-l", "buffer:select_line", Some("BufferView")),
Binding::new("pageup", "buffer:page_up", Some("BufferView")),
Binding::new("pagedown", "buffer:page_down", Some("BufferView")),
Binding::new("alt-cmd-[", "buffer:fold", Some("BufferView")),
@@ -229,6 +230,7 @@ pub fn init(app: &mut MutableAppContext) {
);
app.add_action("buffer:select_to_end", BufferView::select_to_end);
app.add_action("buffer:select_all", BufferView::select_all);
app.add_action("buffer:select_line", BufferView::select_line);
app.add_action("buffer:page_up", BufferView::page_up);
app.add_action("buffer:page_down", BufferView::page_down);
app.add_action("buffer:fold", BufferView::fold);
@@ -712,7 +714,8 @@ impl BufferView {
let mut selections = self.selections(app).iter().peekable();
while let Some(selection) = selections.next() {
let (mut rows, _) = selection.buffer_rows_for_display_rows(&self.display_map, app);
let (mut rows, _) =
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
let goal_display_column = selection
.head()
.to_display_point(&self.display_map, app)
@@ -722,7 +725,7 @@ impl BufferView {
// Accumulate contiguous regions of rows that we want to delete.
while let Some(next_selection) = selections.peek() {
let (next_rows, _) =
next_selection.buffer_rows_for_display_rows(&self.display_map, app);
next_selection.buffer_rows_for_display_rows(false, &self.display_map, app);
if next_rows.start <= rows.end {
rows.end = next_rows.end;
selections.next().unwrap();
@@ -803,10 +806,11 @@ impl BufferView {
let mut selections_iter = selections.iter_mut().peekable();
while let Some(selection) = selections_iter.next() {
// Avoid duplicating the same lines twice.
let (mut rows, _) = selection.buffer_rows_for_display_rows(&self.display_map, app);
let (mut rows, _) =
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
while let Some(next_selection) = selections_iter.peek() {
let (next_rows, _) =
next_selection.buffer_rows_for_display_rows(&self.display_map, app);
next_selection.buffer_rows_for_display_rows(false, &self.display_map, app);
if next_rows.start <= rows.end - 1 {
rows.end = next_rows.end;
selections_iter.next().unwrap();
@@ -860,10 +864,10 @@ impl BufferView {
// Accumulate contiguous regions of rows that we want to move.
contiguous_selections.push(selection.range(buffer));
let (mut buffer_rows, mut display_rows) =
selection.buffer_rows_for_display_rows(&self.display_map, app);
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
while let Some(next_selection) = selections.peek() {
let (next_buffer_rows, next_display_rows) =
next_selection.buffer_rows_for_display_rows(&self.display_map, app);
next_selection.buffer_rows_for_display_rows(false, &self.display_map, app);
if next_buffer_rows.start <= buffer_rows.end {
buffer_rows.end = next_buffer_rows.end;
display_rows.end = next_display_rows.end;
@@ -950,10 +954,10 @@ impl BufferView {
// Accumulate contiguous regions of rows that we want to move.
contiguous_selections.push(selection.range(buffer));
let (mut buffer_rows, mut display_rows) =
selection.buffer_rows_for_display_rows(&self.display_map, app);
selection.buffer_rows_for_display_rows(false, &self.display_map, app);
while let Some(next_selection) = selections.peek() {
let (next_buffer_rows, next_display_rows) =
next_selection.buffer_rows_for_display_rows(&self.display_map, app);
next_selection.buffer_rows_for_display_rows(false, &self.display_map, app);
if next_buffer_rows.start <= buffer_rows.end {
buffer_rows.end = next_buffer_rows.end;
display_rows.end = next_display_rows.end;
@@ -1662,6 +1666,22 @@ impl BufferView {
self.update_selections(vec![selection], false, ctx);
}
pub fn select_line(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
let app = ctx.as_ref();
let buffer = self.buffer.read(app);
let mut selections = self.selections(app).to_vec();
let max_point = buffer.max_point();
for selection in &mut selections {
let (rows, _) = selection.buffer_rows_for_display_rows(true, &self.display_map, app);
selection.start = buffer.anchor_before(Point::new(rows.start, 0)).unwrap();
selection.end = buffer
.anchor_before(cmp::min(max_point, Point::new(rows.end, 0)))
.unwrap();
selection.reversed = false;
}
self.update_selections(selections, true, ctx);
}
pub fn selections_in_range<'a>(
&'a self,
range: Range<DisplayPoint>,
@@ -3284,6 +3304,51 @@ mod tests {
});
}
#[test]
fn test_select_line() {
App::test((), |app| {
let settings = settings::channel(&app.font_cache()).unwrap().1;
let buffer = app.add_model(|_| Buffer::new(0, sample_text(6, 5)));
let (_, view) =
app.add_window(|ctx| BufferView::for_buffer(buffer, None, settings, ctx));
view.update(app, |view, ctx| {
view.select_display_ranges(
&[
DisplayPoint::new(0, 0)..DisplayPoint::new(0, 1),
DisplayPoint::new(0, 2)..DisplayPoint::new(0, 2),
DisplayPoint::new(1, 0)..DisplayPoint::new(1, 0),
DisplayPoint::new(4, 2)..DisplayPoint::new(4, 2),
],
ctx,
)
.unwrap();
view.select_line(&(), ctx);
});
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
vec![
DisplayPoint::new(0, 0)..DisplayPoint::new(2, 0),
DisplayPoint::new(4, 0)..DisplayPoint::new(5, 0),
]
);
view.update(app, |view, ctx| view.select_line(&(), ctx));
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
vec![
DisplayPoint::new(0, 0)..DisplayPoint::new(3, 0),
DisplayPoint::new(4, 0)..DisplayPoint::new(5, 5),
]
);
view.update(app, |view, ctx| view.select_line(&(), ctx));
assert_eq!(
view.read(app).selection_ranges(app.as_ref()),
vec![DisplayPoint::new(0, 0)..DisplayPoint::new(5, 5)]
);
});
}
impl BufferView {
fn selection_ranges(&self, app: &AppContext) -> Vec<Range<DisplayPoint>> {
self.selections_in_range(DisplayPoint::zero()..self.max_point(app), app)