Synchronize FoldMap with buffer's contents lazily
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
co-authored by
Nathan Sobo
parent
c135c84ef0
commit
05ab1bdddf
+289
-187
@@ -254,7 +254,7 @@ pub enum SelectAction {
|
||||
pub struct BufferView {
|
||||
handle: WeakViewHandle<Self>,
|
||||
buffer: ModelHandle<Buffer>,
|
||||
display_map: ModelHandle<DisplayMap>,
|
||||
display_map: DisplayMap,
|
||||
selection_set_id: SelectionSetId,
|
||||
pending_selection: Option<Selection>,
|
||||
scroll_position: Mutex<Vector2F>,
|
||||
@@ -290,14 +290,11 @@ impl BufferView {
|
||||
|
||||
ctx.observe(&buffer, Self::on_buffer_changed);
|
||||
ctx.subscribe_to_model(&buffer, Self::on_buffer_event);
|
||||
let display_map = ctx.add_model(|ctx| {
|
||||
DisplayMap::new(
|
||||
buffer.clone(),
|
||||
smol::block_on(settings.read()).tab_size,
|
||||
ctx,
|
||||
)
|
||||
});
|
||||
ctx.observe(&display_map, Self::on_display_map_changed);
|
||||
let display_map = DisplayMap::new(
|
||||
buffer.clone(),
|
||||
smol::block_on(settings.read()).tab_size,
|
||||
ctx.as_ref(),
|
||||
);
|
||||
|
||||
let (selection_set_id, _) = buffer.update(ctx, |buffer, ctx| {
|
||||
buffer.add_selection_set(
|
||||
@@ -367,14 +364,13 @@ impl BufferView {
|
||||
return false;
|
||||
}
|
||||
|
||||
let map = self.display_map.read(app);
|
||||
let visible_lines = viewport_height / line_height;
|
||||
let first_cursor_top = self
|
||||
.selections(app)
|
||||
.first()
|
||||
.unwrap()
|
||||
.head()
|
||||
.to_display_point(map, app)
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap()
|
||||
.row() as f32;
|
||||
let last_cursor_bottom = self
|
||||
@@ -382,7 +378,7 @@ impl BufferView {
|
||||
.last()
|
||||
.unwrap()
|
||||
.head()
|
||||
.to_display_point(map, app)
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap()
|
||||
.row() as f32
|
||||
+ 1.0;
|
||||
@@ -415,16 +411,20 @@ impl BufferView {
|
||||
scroll_width: f32,
|
||||
max_glyph_width: f32,
|
||||
layouts: &[Arc<text_layout::Line>],
|
||||
app: &AppContext,
|
||||
ctx: &AppContext,
|
||||
) {
|
||||
let map = self.display_map.read(app);
|
||||
|
||||
let mut target_left = std::f32::INFINITY;
|
||||
let mut target_right = 0.0_f32;
|
||||
for selection in self.selections(app) {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
for selection in self.selections(ctx) {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, ctx)
|
||||
.unwrap();
|
||||
let start_column = head.column().saturating_sub(3);
|
||||
let end_column = cmp::min(map.line_len(head.row(), app).unwrap(), head.column() + 3);
|
||||
let end_column = cmp::min(
|
||||
self.display_map.line_len(head.row(), ctx).unwrap(),
|
||||
head.column() + 3,
|
||||
);
|
||||
target_left = target_left
|
||||
.min(layouts[(head.row() - start_row) as usize].x_for_index(start_column as usize));
|
||||
target_right = target_right.max(
|
||||
@@ -466,8 +466,8 @@ impl BufferView {
|
||||
ctx.emit(Event::Activate);
|
||||
}
|
||||
|
||||
let display_map = self.display_map.read(ctx);
|
||||
let cursor = display_map
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(position, Bias::Left, ctx.as_ref())
|
||||
.unwrap();
|
||||
let selection = Selection {
|
||||
@@ -492,8 +492,8 @@ impl BufferView {
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) {
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
let cursor = map
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(position, Bias::Left, ctx.as_ref())
|
||||
.unwrap();
|
||||
if let Some(selection) = self.pending_selection.as_mut() {
|
||||
@@ -554,7 +554,6 @@ impl BufferView {
|
||||
where
|
||||
T: IntoIterator<Item = &'a Range<DisplayPoint>>,
|
||||
{
|
||||
let map = self.display_map.read(ctx);
|
||||
let mut selections = Vec::new();
|
||||
for range in ranges {
|
||||
let mut start = range.start;
|
||||
@@ -567,8 +566,12 @@ impl BufferView {
|
||||
};
|
||||
|
||||
selections.push(Selection {
|
||||
start: map.anchor_before(start, Bias::Left, ctx.as_ref())?,
|
||||
end: map.anchor_before(end, Bias::Left, ctx.as_ref())?,
|
||||
start: self
|
||||
.display_map
|
||||
.anchor_before(start, Bias::Left, ctx.as_ref())?,
|
||||
end: self
|
||||
.display_map
|
||||
.anchor_before(end, Bias::Left, ctx.as_ref())?,
|
||||
reversed,
|
||||
goal_column: None,
|
||||
});
|
||||
@@ -634,17 +637,17 @@ impl BufferView {
|
||||
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
for selection in &mut selections {
|
||||
let range = selection.range(buffer);
|
||||
if range.start == range.end {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(map, ctx.as_ref())
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = map
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::left(map, head, ctx.as_ref()).unwrap(),
|
||||
movement::left(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Left,
|
||||
ctx.as_ref(),
|
||||
)
|
||||
@@ -665,17 +668,17 @@ impl BufferView {
|
||||
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
for selection in &mut selections {
|
||||
let range = selection.range(buffer);
|
||||
if range.start == range.end {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(map, ctx.as_ref())
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = map
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::right(map, head, ctx.as_ref()).unwrap(),
|
||||
movement::right(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Right,
|
||||
ctx.as_ref(),
|
||||
)
|
||||
@@ -695,7 +698,6 @@ impl BufferView {
|
||||
self.start_transaction(ctx);
|
||||
|
||||
let app = ctx.as_ref();
|
||||
let map = self.display_map.read(app);
|
||||
let buffer = self.buffer.read(app);
|
||||
|
||||
let mut new_cursors = Vec::new();
|
||||
@@ -703,16 +705,17 @@ 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(map, app);
|
||||
let (mut rows, _) = selection.buffer_rows_for_display_rows(&self.display_map, app);
|
||||
let goal_display_column = selection
|
||||
.head()
|
||||
.to_display_point(map, app)
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap()
|
||||
.column();
|
||||
|
||||
// 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(map, app);
|
||||
let (next_rows, _) =
|
||||
next_selection.buffer_rows_for_display_rows(&self.display_map, app);
|
||||
if next_rows.start <= rows.end {
|
||||
rows.end = next_rows.end;
|
||||
selections.next().unwrap();
|
||||
@@ -738,14 +741,18 @@ impl BufferView {
|
||||
}
|
||||
|
||||
let mut cursor = Point::new(cursor_buffer_row, 0)
|
||||
.to_display_point(map, app)
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
*cursor.column_mut() = cmp::min(
|
||||
goal_display_column,
|
||||
map.line_len(cursor.row(), app).unwrap(),
|
||||
self.display_map.line_len(cursor.row(), app).unwrap(),
|
||||
);
|
||||
|
||||
new_cursors.push(cursor.to_buffer_point(map, Bias::Left, app).unwrap());
|
||||
new_cursors.push(
|
||||
cursor
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap(),
|
||||
);
|
||||
edit_ranges.push(edit_start..edit_end);
|
||||
}
|
||||
|
||||
@@ -784,15 +791,15 @@ impl BufferView {
|
||||
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
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(map, app);
|
||||
let (mut rows, _) = selection.buffer_rows_for_display_rows(&self.display_map, app);
|
||||
while let Some(next_selection) = selections_iter.peek() {
|
||||
let (next_rows, _) = next_selection.buffer_rows_for_display_rows(map, app);
|
||||
let (next_rows, _) =
|
||||
next_selection.buffer_rows_for_display_rows(&self.display_map, app);
|
||||
if next_rows.start <= rows.end - 1 {
|
||||
rows.end = next_rows.end;
|
||||
selections_iter.next().unwrap();
|
||||
@@ -834,7 +841,6 @@ impl BufferView {
|
||||
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
let mut new_selection_ranges = Vec::new();
|
||||
@@ -847,10 +853,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(map, app);
|
||||
selection.buffer_rows_for_display_rows(&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(map, app);
|
||||
next_selection.buffer_rows_for_display_rows(&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;
|
||||
@@ -873,7 +879,7 @@ impl BufferView {
|
||||
|
||||
let prev_row_display_start = DisplayPoint::new(display_rows.start - 1, 0);
|
||||
let prev_row_start = prev_row_display_start
|
||||
.to_buffer_offset(map, Bias::Left, app)
|
||||
.to_buffer_offset(&self.display_map, Bias::Left, app)
|
||||
.unwrap();
|
||||
|
||||
let mut text = String::new();
|
||||
@@ -884,7 +890,7 @@ impl BufferView {
|
||||
|
||||
let row_delta = buffer_rows.start
|
||||
- prev_row_display_start
|
||||
.to_buffer_point(map, Bias::Left, app)
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap()
|
||||
.row;
|
||||
|
||||
@@ -896,7 +902,7 @@ impl BufferView {
|
||||
|
||||
// Move folds up.
|
||||
old_folds.push(start..end);
|
||||
for fold in map.folds_in_range(start..end, app).unwrap() {
|
||||
for fold in self.display_map.folds_in_range(start..end, app).unwrap() {
|
||||
let mut start = fold.start.to_point(buffer).unwrap();
|
||||
let mut end = fold.end.to_point(buffer).unwrap();
|
||||
start.row -= row_delta;
|
||||
@@ -925,7 +931,6 @@ impl BufferView {
|
||||
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
let mut new_selection_ranges = Vec::new();
|
||||
@@ -938,10 +943,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(map, app);
|
||||
selection.buffer_rows_for_display_rows(&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(map, app);
|
||||
next_selection.buffer_rows_for_display_rows(&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;
|
||||
@@ -953,7 +958,7 @@ impl BufferView {
|
||||
}
|
||||
|
||||
// Cut the text from the selected rows and paste it at the end of the next line.
|
||||
if display_rows.end <= map.max_point(app).row() {
|
||||
if display_rows.end <= self.display_map.max_point(app).row() {
|
||||
let start = Point::new(buffer_rows.start, 0).to_offset(buffer).unwrap();
|
||||
let end = Point::new(
|
||||
buffer_rows.end - 1,
|
||||
@@ -964,10 +969,10 @@ impl BufferView {
|
||||
|
||||
let next_row_display_end = DisplayPoint::new(
|
||||
display_rows.end,
|
||||
map.line_len(display_rows.end, app).unwrap(),
|
||||
self.display_map.line_len(display_rows.end, app).unwrap(),
|
||||
);
|
||||
let next_row_end = next_row_display_end
|
||||
.to_buffer_offset(map, Bias::Right, app)
|
||||
.to_buffer_offset(&self.display_map, Bias::Right, app)
|
||||
.unwrap();
|
||||
|
||||
let mut text = String::new();
|
||||
@@ -977,7 +982,7 @@ impl BufferView {
|
||||
edits.push((next_row_end..next_row_end, text));
|
||||
|
||||
let row_delta = next_row_display_end
|
||||
.to_buffer_point(map, Bias::Right, app)
|
||||
.to_buffer_point(&self.display_map, Bias::Right, app)
|
||||
.unwrap()
|
||||
.row
|
||||
- buffer_rows.end
|
||||
@@ -991,7 +996,7 @@ impl BufferView {
|
||||
|
||||
// Move folds down.
|
||||
old_folds.push(start..end);
|
||||
for fold in map.folds_in_range(start..end, app).unwrap() {
|
||||
for fold in self.display_map.folds_in_range(start..end, app).unwrap() {
|
||||
let mut start = fold.start.to_point(buffer).unwrap();
|
||||
let mut end = fold.end.to_point(buffer).unwrap();
|
||||
start.row += row_delta;
|
||||
@@ -1155,16 +1160,26 @@ impl BufferView {
|
||||
let app = ctx.as_ref();
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
|
||||
if start != end {
|
||||
selection.end = selection.start.clone();
|
||||
} else {
|
||||
let cursor = map
|
||||
.anchor_before(movement::left(map, start, app).unwrap(), Bias::Left, app)
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::left(&self.display_map, start, app).unwrap(),
|
||||
Bias::Left,
|
||||
app,
|
||||
)
|
||||
.unwrap();
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
@@ -1180,15 +1195,15 @@ impl BufferView {
|
||||
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(map, ctx.as_ref())
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = map
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::left(map, head, ctx.as_ref()).unwrap(),
|
||||
movement::left(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Left,
|
||||
ctx.as_ref(),
|
||||
)
|
||||
@@ -1204,16 +1219,26 @@ impl BufferView {
|
||||
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
|
||||
if start != end {
|
||||
selection.start = selection.end.clone();
|
||||
} else {
|
||||
let cursor = map
|
||||
.anchor_before(movement::right(map, end, app).unwrap(), Bias::Right, app)
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::right(&self.display_map, end, app).unwrap(),
|
||||
Bias::Right,
|
||||
app,
|
||||
)
|
||||
.unwrap();
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
@@ -1230,14 +1255,18 @@ impl BufferView {
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(map, ctx.as_ref())
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = map
|
||||
.anchor_before(movement::right(map, head, app).unwrap(), Bias::Right, app)
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::right(&self.display_map, head, app).unwrap(),
|
||||
Bias::Right,
|
||||
app,
|
||||
)
|
||||
.unwrap();
|
||||
selection.set_head(&buffer, cursor);
|
||||
selection.goal_column = None;
|
||||
@@ -1253,17 +1282,25 @@ impl BufferView {
|
||||
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
if start != end {
|
||||
selection.goal_column = None;
|
||||
}
|
||||
|
||||
let (start, goal_column) =
|
||||
movement::up(map, start, selection.goal_column, app).unwrap();
|
||||
let cursor = map.anchor_before(start, Bias::Left, app).unwrap();
|
||||
movement::up(&self.display_map, start, selection.goal_column, app).unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(start, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
selection.goal_column = goal_column;
|
||||
@@ -1279,12 +1316,19 @@ impl BufferView {
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let (head, goal_column) =
|
||||
movement::up(map, head, selection.goal_column, app).unwrap();
|
||||
selection.set_head(&buffer, map.anchor_before(head, Bias::Left, app).unwrap());
|
||||
movement::up(&self.display_map, head, selection.goal_column, app).unwrap();
|
||||
selection.set_head(
|
||||
&buffer,
|
||||
self.display_map
|
||||
.anchor_before(head, Bias::Left, app)
|
||||
.unwrap(),
|
||||
);
|
||||
selection.goal_column = goal_column;
|
||||
}
|
||||
}
|
||||
@@ -1298,17 +1342,25 @@ impl BufferView {
|
||||
let mut selections = self.selections(ctx.as_ref()).to_vec();
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
if start != end {
|
||||
selection.goal_column = None;
|
||||
}
|
||||
|
||||
let (start, goal_column) =
|
||||
movement::down(map, end, selection.goal_column, app).unwrap();
|
||||
let cursor = map.anchor_before(start, Bias::Right, app).unwrap();
|
||||
movement::down(&self.display_map, end, selection.goal_column, app).unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(start, Bias::Right, app)
|
||||
.unwrap();
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
selection.goal_column = goal_column;
|
||||
@@ -1324,12 +1376,19 @@ impl BufferView {
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let (head, goal_column) =
|
||||
movement::down(map, head, selection.goal_column, app).unwrap();
|
||||
selection.set_head(&buffer, map.anchor_before(head, Bias::Right, app).unwrap());
|
||||
movement::down(&self.display_map, head, selection.goal_column, app).unwrap();
|
||||
selection.set_head(
|
||||
&buffer,
|
||||
self.display_map
|
||||
.anchor_before(head, Bias::Right, app)
|
||||
.unwrap(),
|
||||
);
|
||||
selection.goal_column = goal_column;
|
||||
}
|
||||
}
|
||||
@@ -1340,11 +1399,16 @@ impl BufferView {
|
||||
let app = ctx.as_ref();
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::prev_word_boundary(map, head, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head = movement::prev_word_boundary(&self.display_map, head, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1359,11 +1423,16 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::prev_word_boundary(map, head, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head = movement::prev_word_boundary(&self.display_map, head, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal_column = None;
|
||||
}
|
||||
@@ -1382,11 +1451,16 @@ impl BufferView {
|
||||
let app = ctx.as_ref();
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::next_word_boundary(map, head, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head = movement::next_word_boundary(&self.display_map, head, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1401,11 +1475,16 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::next_word_boundary(map, head, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head = movement::next_word_boundary(&self.display_map, head, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal_column = None;
|
||||
}
|
||||
@@ -1424,11 +1503,17 @@ impl BufferView {
|
||||
let app = ctx.as_ref();
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::line_beginning(map, head, true, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head =
|
||||
movement::line_beginning(&self.display_map, head, true, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1447,11 +1532,17 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::line_beginning(map, head, *toggle_indent, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head =
|
||||
movement::line_beginning(&self.display_map, head, *toggle_indent, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal_column = None;
|
||||
}
|
||||
@@ -1470,11 +1561,16 @@ impl BufferView {
|
||||
let app = ctx.as_ref();
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::line_end(map, head, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head = movement::line_end(&self.display_map, head, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1489,11 +1585,16 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let new_head = movement::line_end(map, head, app).unwrap();
|
||||
let anchor = map.anchor_before(new_head, Bias::Left, app).unwrap();
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let new_head = movement::line_end(&self.display_map, head, app).unwrap();
|
||||
let anchor = self
|
||||
.display_map
|
||||
.anchor_before(new_head, Bias::Left, app)
|
||||
.unwrap();
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal_column = None;
|
||||
}
|
||||
@@ -1559,12 +1660,13 @@ impl BufferView {
|
||||
range: Range<DisplayPoint>,
|
||||
app: &'a AppContext,
|
||||
) -> impl 'a + Iterator<Item = Range<DisplayPoint>> {
|
||||
let map = self.display_map.read(app);
|
||||
|
||||
let start = map.anchor_before(range.start, Bias::Left, app).unwrap();
|
||||
let start = self
|
||||
.display_map
|
||||
.anchor_before(range.start, Bias::Left, app)
|
||||
.unwrap();
|
||||
let start_index = self.selection_insertion_index(&start, app);
|
||||
let pending_selection = self.pending_selection.as_ref().and_then(|s| {
|
||||
let selection_range = s.display_range(map, app);
|
||||
let selection_range = s.display_range(&self.display_map, app);
|
||||
if selection_range.start <= range.end || selection_range.end <= range.end {
|
||||
Some(selection_range)
|
||||
} else {
|
||||
@@ -1573,7 +1675,7 @@ impl BufferView {
|
||||
});
|
||||
self.selections(app)[start_index..]
|
||||
.iter()
|
||||
.map(move |s| s.display_range(map, app))
|
||||
.map(move |s| s.display_range(&self.display_map, app))
|
||||
.take_while(move |r| r.start <= range.end || r.end <= range.end)
|
||||
.chain(pending_selection)
|
||||
}
|
||||
@@ -1673,17 +1775,18 @@ impl BufferView {
|
||||
let mut fold_ranges = Vec::new();
|
||||
|
||||
let app = ctx.as_ref();
|
||||
let map = self.display_map.read(app);
|
||||
for selection in self.selections(app) {
|
||||
let range = selection.display_range(map, app).sorted();
|
||||
let range = selection.display_range(&self.display_map, app).sorted();
|
||||
let buffer_start_row = range
|
||||
.start
|
||||
.to_buffer_point(map, Bias::Left, app)
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap()
|
||||
.row;
|
||||
|
||||
for row in (0..=range.end.row()).rev() {
|
||||
if self.is_line_foldable(row, app) && !map.is_line_folded(row) {
|
||||
if self.is_line_foldable(row, app)
|
||||
&& !self.display_map.is_line_folded(row, ctx.as_ref())
|
||||
{
|
||||
let fold_range = self.foldable_range_for_line(row, app).unwrap();
|
||||
if fold_range.end.row >= buffer_start_row {
|
||||
fold_ranges.push(fold_range);
|
||||
@@ -1702,15 +1805,20 @@ impl BufferView {
|
||||
use super::RangeExt;
|
||||
|
||||
let app = ctx.as_ref();
|
||||
let map = self.display_map.read(app);
|
||||
let buffer = self.buffer.read(app);
|
||||
let ranges = self
|
||||
.selections(app)
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let range = s.display_range(map, app).sorted();
|
||||
let mut start = range.start.to_buffer_point(map, Bias::Left, app).unwrap();
|
||||
let mut end = range.end.to_buffer_point(map, Bias::Left, app).unwrap();
|
||||
let range = s.display_range(&self.display_map, app).sorted();
|
||||
let mut start = range
|
||||
.start
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap();
|
||||
let mut end = range
|
||||
.end
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap();
|
||||
start.column = 0;
|
||||
end.column = buffer.line_len(end.row).unwrap();
|
||||
start..end
|
||||
@@ -1720,17 +1828,17 @@ impl BufferView {
|
||||
}
|
||||
|
||||
fn is_line_foldable(&self, display_row: u32, app: &AppContext) -> bool {
|
||||
let map = self.display_map.read(app);
|
||||
let max_point = self.max_point(app);
|
||||
if display_row >= max_point.row() {
|
||||
false
|
||||
} else {
|
||||
let (start_indent, is_blank) = map.line_indent(display_row, app).unwrap();
|
||||
let (start_indent, is_blank) = self.display_map.line_indent(display_row, app).unwrap();
|
||||
if is_blank {
|
||||
false
|
||||
} else {
|
||||
for display_row in display_row + 1..=max_point.row() {
|
||||
let (indent, is_blank) = map.line_indent(display_row, app).unwrap();
|
||||
let (indent, is_blank) =
|
||||
self.display_map.line_indent(display_row, app).unwrap();
|
||||
if !is_blank {
|
||||
return indent > start_indent;
|
||||
}
|
||||
@@ -1741,14 +1849,13 @@ impl BufferView {
|
||||
}
|
||||
|
||||
fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Result<Range<Point>> {
|
||||
let map = self.display_map.read(app);
|
||||
let max_point = self.max_point(app);
|
||||
|
||||
let (start_indent, _) = map.line_indent(start_row, app)?;
|
||||
let (start_indent, _) = self.display_map.line_indent(start_row, app)?;
|
||||
let start = DisplayPoint::new(start_row, self.line_len(start_row, app)?);
|
||||
let mut end = None;
|
||||
for row in start_row + 1..=max_point.row() {
|
||||
let (indent, is_blank) = map.line_indent(row, app)?;
|
||||
let (indent, is_blank) = self.display_map.line_indent(row, app)?;
|
||||
if !is_blank && indent <= start_indent {
|
||||
end = Some(DisplayPoint::new(row - 1, self.line_len(row - 1, app)?));
|
||||
break;
|
||||
@@ -1756,60 +1863,57 @@ impl BufferView {
|
||||
}
|
||||
|
||||
let end = end.unwrap_or(max_point);
|
||||
return Ok(start.to_buffer_point(map, Bias::Left, app)?
|
||||
..end.to_buffer_point(map, Bias::Left, app)?);
|
||||
return Ok(start.to_buffer_point(&self.display_map, Bias::Left, app)?
|
||||
..end.to_buffer_point(&self.display_map, Bias::Left, app)?);
|
||||
}
|
||||
|
||||
pub fn fold_selected_ranges(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
use super::RangeExt;
|
||||
|
||||
self.display_map.update(ctx, |map, ctx| {
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let ranges = self
|
||||
.selections(ctx.as_ref())
|
||||
.iter()
|
||||
.map(|s| s.range(buffer).sorted())
|
||||
.collect::<Vec<_>>();
|
||||
map.fold(ranges, ctx).unwrap();
|
||||
});
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let ranges = self
|
||||
.selections(ctx.as_ref())
|
||||
.iter()
|
||||
.map(|s| s.range(buffer).sorted())
|
||||
.collect::<Vec<_>>();
|
||||
self.display_map.fold(ranges, ctx.as_ref()).unwrap();
|
||||
ctx.notify();
|
||||
}
|
||||
|
||||
fn fold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, ctx: &mut ViewContext<Self>) {
|
||||
if !ranges.is_empty() {
|
||||
self.display_map.update(ctx, |map, ctx| {
|
||||
map.fold(ranges, ctx).unwrap();
|
||||
});
|
||||
self.display_map.fold(ranges, ctx.as_ref()).unwrap();
|
||||
*self.autoscroll_requested.lock() = true;
|
||||
ctx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
fn unfold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, ctx: &mut ViewContext<Self>) {
|
||||
if !ranges.is_empty() {
|
||||
self.display_map.update(ctx, |map, ctx| {
|
||||
map.unfold(ranges, ctx).unwrap();
|
||||
});
|
||||
self.display_map.unfold(ranges, ctx.as_ref()).unwrap();
|
||||
*self.autoscroll_requested.lock() = true;
|
||||
ctx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn line(&self, display_row: u32, app: &AppContext) -> Result<String> {
|
||||
self.display_map.read(app).line(display_row, app)
|
||||
pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result<String> {
|
||||
self.display_map.line(display_row, ctx)
|
||||
}
|
||||
|
||||
pub fn line_len(&self, display_row: u32, app: &AppContext) -> Result<u32> {
|
||||
self.display_map.read(app).line_len(display_row, app)
|
||||
pub fn line_len(&self, display_row: u32, ctx: &AppContext) -> Result<u32> {
|
||||
self.display_map.line_len(display_row, ctx)
|
||||
}
|
||||
|
||||
pub fn rightmost_point(&self, app: &AppContext) -> DisplayPoint {
|
||||
self.display_map.read(app).rightmost_point()
|
||||
pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
self.display_map.rightmost_point(ctx)
|
||||
}
|
||||
|
||||
pub fn max_point(&self, app: &AppContext) -> DisplayPoint {
|
||||
self.display_map.read(app).max_point(app)
|
||||
pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
self.display_map.max_point(ctx)
|
||||
}
|
||||
|
||||
pub fn text(&self, app: &AppContext) -> String {
|
||||
self.display_map.read(app).text(app)
|
||||
pub fn text(&self, ctx: &AppContext) -> String {
|
||||
self.display_map.text(ctx)
|
||||
}
|
||||
|
||||
pub fn font_size(&self) -> f32 {
|
||||
@@ -1872,10 +1976,8 @@ impl BufferView {
|
||||
viewport_height: f32,
|
||||
font_cache: &FontCache,
|
||||
layout_cache: &TextLayoutCache,
|
||||
app: &AppContext,
|
||||
ctx: &AppContext,
|
||||
) -> Result<Vec<Arc<text_layout::Line>>> {
|
||||
let display_map = self.display_map.read(app);
|
||||
|
||||
let settings = smol::block_on(self.settings.read());
|
||||
let font_size = settings.buffer_font_size;
|
||||
let font_id =
|
||||
@@ -1883,14 +1985,19 @@ impl BufferView {
|
||||
|
||||
let start_row = self.scroll_position().y() as usize;
|
||||
let end_row = cmp::min(
|
||||
self.max_point(app).row() as usize,
|
||||
self.max_point(ctx).row() as usize,
|
||||
start_row + (viewport_height / self.line_height(font_cache)).ceil() as usize,
|
||||
);
|
||||
let line_count = end_row - start_row + 1;
|
||||
|
||||
let mut layouts = Vec::with_capacity(line_count);
|
||||
let mut line_number = String::new();
|
||||
for buffer_row in display_map.buffer_rows(start_row as u32)?.take(line_count) {
|
||||
for buffer_row in self
|
||||
.display_map
|
||||
.snapshot(ctx)
|
||||
.buffer_rows(start_row as u32)?
|
||||
.take(line_count)
|
||||
{
|
||||
line_number.clear();
|
||||
write!(&mut line_number, "{}", buffer_row + 1).unwrap();
|
||||
layouts.push(layout_cache.layout_str(
|
||||
@@ -1908,11 +2015,9 @@ impl BufferView {
|
||||
mut rows: Range<u32>,
|
||||
font_cache: &FontCache,
|
||||
layout_cache: &TextLayoutCache,
|
||||
app: &AppContext,
|
||||
ctx: &AppContext,
|
||||
) -> Result<Vec<Arc<text_layout::Line>>> {
|
||||
let display_map = self.display_map.read(app);
|
||||
|
||||
rows.end = cmp::min(rows.end, display_map.max_point(app).row() + 1);
|
||||
rows.end = cmp::min(rows.end, self.display_map.max_point(ctx).row() + 1);
|
||||
if rows.start >= rows.end {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
@@ -1926,8 +2031,9 @@ impl BufferView {
|
||||
let mut line = String::new();
|
||||
let mut line_len = 0;
|
||||
let mut row = rows.start;
|
||||
let chars = display_map
|
||||
.chars_at(DisplayPoint::new(rows.start, 0), app)
|
||||
let snapshot = self.display_map.snapshot(ctx);
|
||||
let chars = snapshot
|
||||
.chars_at(DisplayPoint::new(rows.start, 0), ctx)
|
||||
.unwrap();
|
||||
for char in chars.chain(Some('\n')) {
|
||||
if char == '\n' {
|
||||
@@ -2019,10 +2125,6 @@ impl BufferView {
|
||||
ctx.notify();
|
||||
}
|
||||
|
||||
fn on_display_map_changed(&mut self, _: ModelHandle<DisplayMap>, ctx: &mut ViewContext<Self>) {
|
||||
ctx.notify();
|
||||
}
|
||||
|
||||
fn on_buffer_event(
|
||||
&mut self,
|
||||
_: ModelHandle<Buffer>,
|
||||
@@ -2030,7 +2132,7 @@ impl BufferView {
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) {
|
||||
match event {
|
||||
buffer::Event::Edited(_) => ctx.emit(Event::Edited),
|
||||
buffer::Event::Edited => ctx.emit(Event::Edited),
|
||||
buffer::Event::Dirtied => ctx.emit(Event::Dirtied),
|
||||
buffer::Event::Saved => ctx.emit(Event::Saved),
|
||||
buffer::Event::FileHandleChanged => ctx.emit(Event::FileHandleChanged),
|
||||
|
||||
Reference in New Issue
Block a user