Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selections.all_row_ranges utility based on code from editor.join_lines #20449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6278,30 +6278,13 @@ impl Editor {
if self.read_only(cx) {
return;
}
let mut row_ranges = Vec::<Range<MultiBufferRow>>::new();
for selection in self.selections.all::<Point>(cx) {
let start = MultiBufferRow(selection.start.row);
let end = if selection.start.row == selection.end.row {
MultiBufferRow(selection.start.row + 1)
} else {
MultiBufferRow(selection.end.row)
};

if let Some(last_row_range) = row_ranges.last_mut() {
if start <= last_row_range.end {
last_row_range.end = end;
continue;
}
}
row_ranges.push(start..end);
}

let row_ranges = self.selections.all_row_ranges(cx);
let snapshot = self.buffer.read(cx).snapshot(cx);
let mut cursor_positions = Vec::new();
for row_range in &row_ranges {
let anchor = snapshot.anchor_before(Point::new(
row_range.end.previous_row().0,
snapshot.line_len(row_range.end.previous_row()),
row_range.end().0,
snapshot.line_len(*row_range.end()),
));
cursor_positions.push(anchor..anchor);
}
Expand Down Expand Up @@ -14899,6 +14882,18 @@ impl RowRangeExt for Range<MultiBufferRow> {
}
}

impl RowRangeExt for RangeInclusive<MultiBufferRow> {
type Row = MultiBufferRow;

fn len(&self) -> usize {
(self.end().0 - self.start().0 + 1) as usize
}

fn iter_rows(&self) -> impl DoubleEndedIterator<Item = MultiBufferRow> {
(self.start().0..=self.end().0).map(MultiBufferRow)
}
}

impl RowRangeExt for Range<DisplayRow> {
type Row = DisplayRow;

Expand Down
23 changes: 20 additions & 3 deletions crates/editor/src/selections_collection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
cell::Ref,
cmp, iter, mem,
ops::{Deref, DerefMut, Range, Sub},
ops::{Deref, DerefMut, Range, RangeInclusive, Sub},
sync::Arc,
};

Expand All @@ -14,8 +14,8 @@ use util::post_inc;
use crate::{
display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
movement::TextLayoutDetails,
Anchor, DisplayPoint, DisplayRow, ExcerptId, MultiBuffer, MultiBufferSnapshot, SelectMode,
ToOffset, ToPoint,
Anchor, DisplayPoint, DisplayRow, ExcerptId, MultiBuffer, MultiBufferRow, MultiBufferSnapshot,
SelectMode, ToOffset, ToPoint,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -250,6 +250,23 @@ impl SelectionsCollection {
(map, selections)
}

pub fn all_row_ranges(&self, cx: &mut AppContext) -> Vec<RangeInclusive<MultiBufferRow>> {
let mut row_ranges = Vec::<RangeInclusive<MultiBufferRow>>::new();
for selection in self.all::<Point>(cx) {
let start = MultiBufferRow(selection.start.row);
let end = MultiBufferRow(selection.end.row);

if let Some(last_row_range) = row_ranges.last_mut() {
if start <= *last_row_range.end() {
*last_row_range = (*last_row_range.start())..=end;
continue;
}
}
row_ranges.push(start..=end);
}
row_ranges
}

pub fn newest_anchor(&self) -> &Selection<Anchor> {
self.pending
.as_ref()
Expand Down
Loading