Don't return Results from position methods in FoldMap and DisplayMap
This commit is contained in:
committed by
Antonio Scandurra
parent
72b98ad688
commit
b3d2a70834
@@ -72,8 +72,8 @@ impl Selection {
|
||||
}
|
||||
|
||||
pub fn display_range(&self, map: &DisplayMap, app: &AppContext) -> Range<DisplayPoint> {
|
||||
let start = self.start.to_display_point(map, app).unwrap();
|
||||
let end = self.end.to_display_point(map, app).unwrap();
|
||||
let start = self.start.to_display_point(map, app);
|
||||
let end = self.end.to_display_point(map, app);
|
||||
if self.reversed {
|
||||
end..start
|
||||
} else {
|
||||
@@ -87,12 +87,11 @@ impl Selection {
|
||||
map: &DisplayMap,
|
||||
ctx: &AppContext,
|
||||
) -> (Range<u32>, Range<u32>) {
|
||||
let display_start = self.start.to_display_point(map, ctx).unwrap();
|
||||
let buffer_start = DisplayPoint::new(display_start.row(), 0)
|
||||
.to_buffer_point(map, Bias::Left, ctx)
|
||||
.unwrap();
|
||||
let display_start = self.start.to_display_point(map, ctx);
|
||||
let buffer_start =
|
||||
DisplayPoint::new(display_start.row(), 0).to_buffer_point(map, Bias::Left, ctx);
|
||||
|
||||
let mut display_end = self.end.to_display_point(map, ctx).unwrap();
|
||||
let mut display_end = self.end.to_display_point(map, ctx);
|
||||
if !include_end_if_at_line_start
|
||||
&& display_end.row() != map.max_point(ctx).row()
|
||||
&& display_start.row() != display_end.row()
|
||||
@@ -100,12 +99,8 @@ impl Selection {
|
||||
{
|
||||
*display_end.row_mut() -= 1;
|
||||
}
|
||||
let buffer_end = DisplayPoint::new(
|
||||
display_end.row(),
|
||||
map.line_len(display_end.row(), ctx).unwrap(),
|
||||
)
|
||||
.to_buffer_point(map, Bias::Left, ctx)
|
||||
.unwrap();
|
||||
let buffer_end = DisplayPoint::new(display_end.row(), map.line_len(display_end.row(), ctx))
|
||||
.to_buffer_point(map, Bias::Left, ctx);
|
||||
|
||||
(
|
||||
buffer_start.row..buffer_end.row + 1,
|
||||
|
||||
@@ -569,7 +569,7 @@ impl PaintState {
|
||||
let column = if x >= 0.0 {
|
||||
line.index_for_x(x)
|
||||
.map(|ix| ix as u32)
|
||||
.unwrap_or(view.line_len(row, app).unwrap())
|
||||
.unwrap_or(view.line_len(row, app))
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
+105
-245
@@ -411,7 +411,6 @@ impl BufferView {
|
||||
.unwrap()
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap()
|
||||
.row() as f32;
|
||||
let last_cursor_bottom = self
|
||||
.selections(app)
|
||||
@@ -419,7 +418,6 @@ impl BufferView {
|
||||
.unwrap()
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap()
|
||||
.row() as f32
|
||||
+ 1.0;
|
||||
|
||||
@@ -456,13 +454,10 @@ impl BufferView {
|
||||
let mut target_left = std::f32::INFINITY;
|
||||
let mut target_right = 0.0_f32;
|
||||
for selection in self.selections(ctx) {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, ctx)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, ctx);
|
||||
let start_column = head.column().saturating_sub(3);
|
||||
let end_column = cmp::min(
|
||||
self.display_map.line_len(head.row(), ctx).unwrap(),
|
||||
self.display_map.line_len(head.row(), ctx),
|
||||
head.column() + 3,
|
||||
);
|
||||
target_left = target_left
|
||||
@@ -508,8 +503,7 @@ impl BufferView {
|
||||
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(position, Bias::Left, ctx.as_ref())
|
||||
.unwrap();
|
||||
.anchor_before(position, Bias::Left, ctx.as_ref());
|
||||
let selection = Selection {
|
||||
id: post_inc(&mut self.next_selection_id),
|
||||
start: cursor.clone(),
|
||||
@@ -535,8 +529,7 @@ impl BufferView {
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(position, Bias::Left, ctx.as_ref())
|
||||
.unwrap();
|
||||
.anchor_before(position, Bias::Left, ctx.as_ref());
|
||||
if let Some(selection) = self.pending_selection.as_mut() {
|
||||
selection.set_head(buffer, cursor);
|
||||
} else {
|
||||
@@ -627,10 +620,10 @@ impl BufferView {
|
||||
id: post_inc(&mut self.next_selection_id),
|
||||
start: self
|
||||
.display_map
|
||||
.anchor_before(start, Bias::Left, ctx.as_ref())?,
|
||||
.anchor_before(start, Bias::Left, ctx.as_ref()),
|
||||
end: self
|
||||
.display_map
|
||||
.anchor_before(end, Bias::Left, ctx.as_ref())?,
|
||||
.anchor_before(end, Bias::Left, ctx.as_ref()),
|
||||
reversed,
|
||||
goal: SelectionGoal::None,
|
||||
});
|
||||
@@ -700,16 +693,12 @@ impl BufferView {
|
||||
if range.start == range.end {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::left(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Left,
|
||||
ctx.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
.to_display_point(&self.display_map, ctx.as_ref());
|
||||
let cursor = self.display_map.anchor_before(
|
||||
movement::left(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Left,
|
||||
ctx.as_ref(),
|
||||
);
|
||||
selection.set_head(&buffer, cursor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -731,16 +720,12 @@ impl BufferView {
|
||||
if range.start == range.end {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::right(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Right,
|
||||
ctx.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
.to_display_point(&self.display_map, ctx.as_ref());
|
||||
let cursor = self.display_map.anchor_before(
|
||||
movement::right(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Right,
|
||||
ctx.as_ref(),
|
||||
);
|
||||
selection.set_head(&buffer, cursor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -768,7 +753,6 @@ impl BufferView {
|
||||
let goal_display_column = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap()
|
||||
.column();
|
||||
|
||||
// Accumulate contiguous regions of rows that we want to delete.
|
||||
@@ -799,19 +783,16 @@ impl BufferView {
|
||||
cursor_buffer_row = rows.start.saturating_sub(1);
|
||||
}
|
||||
|
||||
let mut cursor = Point::new(cursor_buffer_row, 0)
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let mut cursor =
|
||||
Point::new(cursor_buffer_row, 0).to_display_point(&self.display_map, app);
|
||||
*cursor.column_mut() = cmp::min(
|
||||
goal_display_column,
|
||||
self.display_map.line_len(cursor.row(), app).unwrap(),
|
||||
self.display_map.line_len(cursor.row(), app),
|
||||
);
|
||||
|
||||
new_cursors.push((
|
||||
selection.id,
|
||||
cursor
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap(),
|
||||
cursor.to_buffer_point(&self.display_map, Bias::Left, app),
|
||||
));
|
||||
edit_ranges.push(edit_start..edit_end);
|
||||
}
|
||||
@@ -937,9 +918,8 @@ impl BufferView {
|
||||
.to_offset(buffer);
|
||||
|
||||
let prev_row_display_start = DisplayPoint::new(display_rows.start - 1, 0);
|
||||
let prev_row_start = prev_row_display_start
|
||||
.to_buffer_offset(&self.display_map, Bias::Left, app)
|
||||
.unwrap();
|
||||
let prev_row_start =
|
||||
prev_row_display_start.to_buffer_offset(&self.display_map, Bias::Left, app);
|
||||
|
||||
let mut text = String::new();
|
||||
text.extend(buffer.text_for_range(start..end));
|
||||
@@ -950,7 +930,6 @@ impl BufferView {
|
||||
let row_delta = buffer_rows.start
|
||||
- prev_row_display_start
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap()
|
||||
.row;
|
||||
|
||||
// Move selections up.
|
||||
@@ -961,7 +940,7 @@ impl BufferView {
|
||||
|
||||
// Move folds up.
|
||||
old_folds.push(start..end);
|
||||
for fold in self.display_map.folds_in_range(start..end, app).unwrap() {
|
||||
for fold in self.display_map.folds_in_range(start..end, app) {
|
||||
let mut start = fold.start.to_point(buffer);
|
||||
let mut end = fold.end.to_point(buffer);
|
||||
start.row -= row_delta;
|
||||
@@ -1024,11 +1003,10 @@ impl BufferView {
|
||||
|
||||
let next_row_display_end = DisplayPoint::new(
|
||||
display_rows.end,
|
||||
self.display_map.line_len(display_rows.end, app).unwrap(),
|
||||
self.display_map.line_len(display_rows.end, app),
|
||||
);
|
||||
let next_row_end = next_row_display_end
|
||||
.to_buffer_offset(&self.display_map, Bias::Right, app)
|
||||
.unwrap();
|
||||
let next_row_end =
|
||||
next_row_display_end.to_buffer_offset(&self.display_map, Bias::Right, app);
|
||||
|
||||
let mut text = String::new();
|
||||
text.push('\n');
|
||||
@@ -1038,7 +1016,6 @@ impl BufferView {
|
||||
|
||||
let row_delta = next_row_display_end
|
||||
.to_buffer_point(&self.display_map, Bias::Right, app)
|
||||
.unwrap()
|
||||
.row
|
||||
- buffer_rows.end
|
||||
+ 1;
|
||||
@@ -1051,7 +1028,7 @@ impl BufferView {
|
||||
|
||||
// Move folds down.
|
||||
old_folds.push(start..end);
|
||||
for fold in self.display_map.folds_in_range(start..end, app).unwrap() {
|
||||
for fold in self.display_map.folds_in_range(start..end, app) {
|
||||
let mut start = fold.start.to_point(buffer);
|
||||
let mut end = fold.end.to_point(buffer);
|
||||
start.row += row_delta;
|
||||
@@ -1217,26 +1194,17 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
for selection in &mut selections {
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let start = selection.start.to_display_point(&self.display_map, app);
|
||||
let end = selection.end.to_display_point(&self.display_map, app);
|
||||
|
||||
if start != end {
|
||||
selection.end = selection.start.clone();
|
||||
} else {
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::left(&self.display_map, start, app).unwrap(),
|
||||
Bias::Left,
|
||||
app,
|
||||
)
|
||||
.unwrap();
|
||||
let cursor = self.display_map.anchor_before(
|
||||
movement::left(&self.display_map, start, app).unwrap(),
|
||||
Bias::Left,
|
||||
app,
|
||||
);
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
}
|
||||
@@ -1254,16 +1222,12 @@ impl BufferView {
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::left(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Left,
|
||||
ctx.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
.to_display_point(&self.display_map, ctx.as_ref());
|
||||
let cursor = self.display_map.anchor_before(
|
||||
movement::left(&self.display_map, head, ctx.as_ref()).unwrap(),
|
||||
Bias::Left,
|
||||
ctx.as_ref(),
|
||||
);
|
||||
selection.set_head(&buffer, cursor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -1276,26 +1240,17 @@ impl BufferView {
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
for selection in &mut selections {
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let start = selection.start.to_display_point(&self.display_map, app);
|
||||
let end = selection.end.to_display_point(&self.display_map, app);
|
||||
|
||||
if start != end {
|
||||
selection.start = selection.end.clone();
|
||||
} else {
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::right(&self.display_map, end, app).unwrap(),
|
||||
Bias::Right,
|
||||
app,
|
||||
)
|
||||
.unwrap();
|
||||
let cursor = self.display_map.anchor_before(
|
||||
movement::right(&self.display_map, end, app).unwrap(),
|
||||
Bias::Right,
|
||||
app,
|
||||
);
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
}
|
||||
@@ -1314,16 +1269,12 @@ impl BufferView {
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, ctx.as_ref())
|
||||
.unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(
|
||||
movement::right(&self.display_map, head, app).unwrap(),
|
||||
Bias::Right,
|
||||
app,
|
||||
)
|
||||
.unwrap();
|
||||
.to_display_point(&self.display_map, ctx.as_ref());
|
||||
let cursor = self.display_map.anchor_before(
|
||||
movement::right(&self.display_map, head, app).unwrap(),
|
||||
Bias::Right,
|
||||
app,
|
||||
);
|
||||
selection.set_head(&buffer, cursor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -1339,24 +1290,15 @@ impl BufferView {
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
for selection in &mut selections {
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let start = selection.start.to_display_point(&self.display_map, app);
|
||||
let end = selection.end.to_display_point(&self.display_map, app);
|
||||
if start != end {
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
|
||||
let (start, goal) =
|
||||
movement::up(&self.display_map, start, selection.goal, app).unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(start, Bias::Left, app)
|
||||
.unwrap();
|
||||
let cursor = self.display_map.anchor_before(start, Bias::Left, app);
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
selection.goal = goal;
|
||||
@@ -1373,17 +1315,12 @@ impl BufferView {
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
let (head, goal) =
|
||||
movement::up(&self.display_map, head, selection.goal, app).unwrap();
|
||||
selection.set_head(
|
||||
&buffer,
|
||||
self.display_map
|
||||
.anchor_before(head, Bias::Left, app)
|
||||
.unwrap(),
|
||||
self.display_map.anchor_before(head, Bias::Left, app),
|
||||
);
|
||||
selection.goal = goal;
|
||||
}
|
||||
@@ -1399,24 +1336,15 @@ impl BufferView {
|
||||
{
|
||||
let app = ctx.as_ref();
|
||||
for selection in &mut selections {
|
||||
let start = selection
|
||||
.start
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let end = selection
|
||||
.end
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let start = selection.start.to_display_point(&self.display_map, app);
|
||||
let end = selection.end.to_display_point(&self.display_map, app);
|
||||
if start != end {
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
|
||||
let (start, goal) =
|
||||
movement::down(&self.display_map, end, selection.goal, app).unwrap();
|
||||
let cursor = self
|
||||
.display_map
|
||||
.anchor_before(start, Bias::Right, app)
|
||||
.unwrap();
|
||||
let cursor = self.display_map.anchor_before(start, Bias::Right, app);
|
||||
selection.start = cursor.clone();
|
||||
selection.end = cursor;
|
||||
selection.goal = goal;
|
||||
@@ -1433,17 +1361,12 @@ impl BufferView {
|
||||
let app = ctx.as_ref();
|
||||
let buffer = self.buffer.read(app);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
let (head, goal) =
|
||||
movement::down(&self.display_map, head, selection.goal, app).unwrap();
|
||||
selection.set_head(
|
||||
&buffer,
|
||||
self.display_map
|
||||
.anchor_before(head, Bias::Right, app)
|
||||
.unwrap(),
|
||||
self.display_map.anchor_before(head, Bias::Right, app),
|
||||
);
|
||||
selection.goal = goal;
|
||||
}
|
||||
@@ -1456,15 +1379,9 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1480,15 +1397,9 @@ impl BufferView {
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -1508,15 +1419,9 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1532,15 +1437,9 @@ impl BufferView {
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -1560,16 +1459,10 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1589,16 +1482,10 @@ impl BufferView {
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -1618,15 +1505,9 @@ impl BufferView {
|
||||
let mut selections = self.selections(app).to_vec();
|
||||
{
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.start = anchor.clone();
|
||||
selection.end = anchor;
|
||||
selection.reversed = false;
|
||||
@@ -1642,15 +1523,9 @@ impl BufferView {
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
for selection in &mut selections {
|
||||
let head = selection
|
||||
.head()
|
||||
.to_display_point(&self.display_map, app)
|
||||
.unwrap();
|
||||
let head = selection.head().to_display_point(&self.display_map, app);
|
||||
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();
|
||||
let anchor = self.display_map.anchor_before(new_head, Bias::Left, app);
|
||||
selection.set_head(buffer, anchor);
|
||||
selection.goal = SelectionGoal::None;
|
||||
}
|
||||
@@ -1878,20 +1753,14 @@ impl BufferView {
|
||||
ctx: &AppContext,
|
||||
) -> Option<Selection> {
|
||||
let is_empty = columns.start == columns.end;
|
||||
let line_len = self.display_map.line_len(row, ctx).unwrap();
|
||||
let line_len = self.display_map.line_len(row, ctx);
|
||||
if columns.start < line_len || (is_empty && columns.start == line_len) {
|
||||
let start = DisplayPoint::new(row, columns.start);
|
||||
let end = DisplayPoint::new(row, cmp::min(columns.end, line_len));
|
||||
Some(Selection {
|
||||
id: post_inc(&mut self.next_selection_id),
|
||||
start: self
|
||||
.display_map
|
||||
.anchor_before(start, Bias::Left, ctx)
|
||||
.unwrap(),
|
||||
end: self
|
||||
.display_map
|
||||
.anchor_before(end, Bias::Left, ctx)
|
||||
.unwrap(),
|
||||
start: self.display_map.anchor_before(start, Bias::Left, ctx),
|
||||
end: self.display_map.anchor_before(end, Bias::Left, ctx),
|
||||
reversed,
|
||||
goal: SelectionGoal::ColumnRange {
|
||||
start: columns.start,
|
||||
@@ -1908,10 +1777,7 @@ impl BufferView {
|
||||
range: Range<DisplayPoint>,
|
||||
app: &'a AppContext,
|
||||
) -> impl 'a + Iterator<Item = Range<DisplayPoint>> {
|
||||
let start = self
|
||||
.display_map
|
||||
.anchor_before(range.start, Bias::Left, app)
|
||||
.unwrap();
|
||||
let start = self.display_map.anchor_before(range.start, Bias::Left, app);
|
||||
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(&self.display_map, app);
|
||||
@@ -2030,14 +1896,13 @@ impl BufferView {
|
||||
let buffer_start_row = range
|
||||
.start
|
||||
.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)
|
||||
&& !self.display_map.is_line_folded(row, ctx.as_ref())
|
||||
{
|
||||
let fold_range = self.foldable_range_for_line(row, app).unwrap();
|
||||
let fold_range = self.foldable_range_for_line(row, app);
|
||||
if fold_range.end.row >= buffer_start_row {
|
||||
fold_ranges.push(fold_range);
|
||||
if row <= range.start.row() {
|
||||
@@ -2063,12 +1928,10 @@ impl BufferView {
|
||||
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();
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app);
|
||||
let mut end = range
|
||||
.end
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app)
|
||||
.unwrap();
|
||||
.to_buffer_point(&self.display_map, Bias::Left, app);
|
||||
start.column = 0;
|
||||
end.column = buffer.line_len(end.row);
|
||||
start..end
|
||||
@@ -2082,13 +1945,12 @@ impl BufferView {
|
||||
if display_row >= max_point.row() {
|
||||
false
|
||||
} else {
|
||||
let (start_indent, is_blank) = self.display_map.line_indent(display_row, app).unwrap();
|
||||
let (start_indent, is_blank) = self.display_map.line_indent(display_row, app);
|
||||
if is_blank {
|
||||
false
|
||||
} else {
|
||||
for display_row in display_row + 1..=max_point.row() {
|
||||
let (indent, is_blank) =
|
||||
self.display_map.line_indent(display_row, app).unwrap();
|
||||
let (indent, is_blank) = self.display_map.line_indent(display_row, app);
|
||||
if !is_blank {
|
||||
return indent > start_indent;
|
||||
}
|
||||
@@ -2098,23 +1960,23 @@ impl BufferView {
|
||||
}
|
||||
}
|
||||
|
||||
fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Result<Range<Point>> {
|
||||
fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Range<Point> {
|
||||
let max_point = self.max_point(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 (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) = self.display_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)?));
|
||||
end = Some(DisplayPoint::new(row - 1, self.line_len(row - 1, app)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let end = end.unwrap_or(max_point);
|
||||
return Ok(start.to_buffer_point(&self.display_map, Bias::Left, app)?
|
||||
..end.to_buffer_point(&self.display_map, Bias::Left, app)?);
|
||||
return 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>) {
|
||||
@@ -2131,7 +1993,7 @@ impl BufferView {
|
||||
|
||||
fn fold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, ctx: &mut ViewContext<Self>) {
|
||||
if !ranges.is_empty() {
|
||||
self.display_map.fold(ranges, ctx.as_ref()).unwrap();
|
||||
self.display_map.fold(ranges, ctx.as_ref());
|
||||
*self.autoscroll_requested.lock() = true;
|
||||
ctx.notify();
|
||||
}
|
||||
@@ -2139,17 +2001,17 @@ impl BufferView {
|
||||
|
||||
fn unfold_ranges<T: ToOffset>(&mut self, ranges: Vec<Range<T>>, ctx: &mut ViewContext<Self>) {
|
||||
if !ranges.is_empty() {
|
||||
self.display_map.unfold(ranges, ctx.as_ref()).unwrap();
|
||||
self.display_map.unfold(ranges, ctx.as_ref());
|
||||
*self.autoscroll_requested.lock() = true;
|
||||
ctx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result<String> {
|
||||
pub fn line(&self, display_row: u32, ctx: &AppContext) -> String {
|
||||
self.display_map.line(display_row, ctx)
|
||||
}
|
||||
|
||||
pub fn line_len(&self, display_row: u32, ctx: &AppContext) -> Result<u32> {
|
||||
pub fn line_len(&self, display_row: u32, ctx: &AppContext) -> u32 {
|
||||
self.display_map.line_len(display_row, ctx)
|
||||
}
|
||||
|
||||
@@ -2244,7 +2106,7 @@ impl BufferView {
|
||||
for buffer_row in self
|
||||
.display_map
|
||||
.snapshot(ctx)
|
||||
.buffer_rows(start_row as u32)?
|
||||
.buffer_rows(start_row as u32)
|
||||
.take(line_count)
|
||||
{
|
||||
line_number.clear();
|
||||
@@ -2281,9 +2143,7 @@ impl BufferView {
|
||||
let mut line_len = 0;
|
||||
let mut row = rows.start;
|
||||
let snapshot = self.display_map.snapshot(ctx);
|
||||
let chars = snapshot
|
||||
.chars_at(DisplayPoint::new(rows.start, 0), ctx)
|
||||
.unwrap();
|
||||
let chars = snapshot.chars_at(DisplayPoint::new(rows.start, 0), ctx);
|
||||
for char in chars.chain(Some('\n')) {
|
||||
if char == '\n' {
|
||||
layouts.push(layout_cache.layout_str(&line, font_size, &[(0..line_len, font_id)]));
|
||||
@@ -2313,12 +2173,12 @@ impl BufferView {
|
||||
let font_id =
|
||||
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
||||
|
||||
let line = self.line(row, app)?;
|
||||
let line = self.line(row, app);
|
||||
|
||||
Ok(layout_cache.layout_str(
|
||||
&line,
|
||||
settings.buffer_font_size,
|
||||
&[(0..self.line_len(row, app)? as usize, font_id)],
|
||||
&[(0..self.line_len(row, app) as usize, font_id)],
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ use crate::{
|
||||
sum_tree::{self, Cursor, FilterCursor, SeekBias, SumTree},
|
||||
time,
|
||||
};
|
||||
use anyhow::{anyhow, Result};
|
||||
use gpui::{AppContext, ModelHandle};
|
||||
use parking_lot::{Mutex, MutexGuard};
|
||||
use std::{
|
||||
@@ -55,17 +54,14 @@ impl FoldMap {
|
||||
self.sync(ctx).summary().display.bytes
|
||||
}
|
||||
|
||||
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
|
||||
let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx)?.0;
|
||||
pub fn line_len(&self, row: u32, ctx: &AppContext) -> u32 {
|
||||
let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx).0;
|
||||
let line_end = if row >= self.max_point(ctx).row() {
|
||||
self.len(ctx)
|
||||
} else {
|
||||
self.to_display_offset(DisplayPoint::new(row + 1, 0), ctx)?
|
||||
.0
|
||||
- 1
|
||||
self.to_display_offset(DisplayPoint::new(row + 1, 0), ctx).0 - 1
|
||||
};
|
||||
|
||||
Ok((line_end - line_start) as u32)
|
||||
(line_end - line_start) as u32
|
||||
}
|
||||
|
||||
pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
@@ -80,18 +76,18 @@ impl FoldMap {
|
||||
&'a self,
|
||||
range: Range<T>,
|
||||
ctx: &'a AppContext,
|
||||
) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
|
||||
) -> impl Iterator<Item = &'a Range<Anchor>>
|
||||
where
|
||||
T: ToOffset,
|
||||
{
|
||||
Ok(self.intersecting_folds(range, ctx)?.map(|f| &f.0))
|
||||
self.intersecting_folds(range, ctx).map(|f| &f.0)
|
||||
}
|
||||
|
||||
pub fn fold<T: ToOffset>(
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
) {
|
||||
let _ = self.sync(ctx);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
@@ -128,14 +124,13 @@ impl FoldMap {
|
||||
new_tree
|
||||
};
|
||||
self.apply_edits(edits, ctx);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unfold<T: ToOffset>(
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
) {
|
||||
let _ = self.sync(ctx);
|
||||
|
||||
let buffer = self.buffer.read(ctx);
|
||||
@@ -144,7 +139,7 @@ impl FoldMap {
|
||||
let mut fold_ixs_to_delete = Vec::new();
|
||||
for range in ranges.into_iter() {
|
||||
// Remove intersecting folds and add their ranges to edits that are passed to apply_edits.
|
||||
let mut folds_cursor = self.intersecting_folds(range, ctx)?;
|
||||
let mut folds_cursor = self.intersecting_folds(range, ctx);
|
||||
while let Some(fold) = folds_cursor.item() {
|
||||
let offset_range = fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer);
|
||||
edits.push(Edit {
|
||||
@@ -176,24 +171,23 @@ impl FoldMap {
|
||||
folds
|
||||
};
|
||||
self.apply_edits(edits, ctx);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn intersecting_folds<'a, T>(
|
||||
&self,
|
||||
range: Range<T>,
|
||||
ctx: &'a AppContext,
|
||||
) -> Result<FilterCursor<impl 'a + Fn(&FoldSummary) -> bool, Fold, usize>>
|
||||
) -> FilterCursor<impl 'a + Fn(&FoldSummary) -> bool, Fold, usize>
|
||||
where
|
||||
T: ToOffset,
|
||||
{
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let start = buffer.anchor_before(range.start.to_offset(buffer));
|
||||
let end = buffer.anchor_after(range.end.to_offset(buffer));
|
||||
Ok(self.folds.filter::<_, usize>(move |summary| {
|
||||
self.folds.filter::<_, usize>(move |summary| {
|
||||
start.cmp(&summary.max_end, buffer).unwrap() == Ordering::Less
|
||||
&& end.cmp(&summary.min_start, buffer).unwrap() == Ordering::Greater
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_line_folded(&self, display_row: u32, ctx: &AppContext) -> bool {
|
||||
@@ -221,11 +215,7 @@ impl FoldMap {
|
||||
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx))
|
||||
}
|
||||
|
||||
pub fn to_display_offset(
|
||||
&self,
|
||||
point: DisplayPoint,
|
||||
ctx: &AppContext,
|
||||
) -> Result<DisplayOffset> {
|
||||
pub fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
|
||||
self.snapshot(ctx).to_display_offset(point, ctx)
|
||||
}
|
||||
|
||||
@@ -405,48 +395,46 @@ pub struct FoldMapSnapshot {
|
||||
}
|
||||
|
||||
impl FoldMapSnapshot {
|
||||
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
|
||||
pub fn buffer_rows(&self, start_row: u32) -> BufferRows {
|
||||
if start_row > self.transforms.summary().display.lines.row {
|
||||
return Err(anyhow!("invalid display row {}", start_row));
|
||||
panic!("invalid display row {}", start_row);
|
||||
}
|
||||
|
||||
let display_point = Point::new(start_row, 0);
|
||||
let mut cursor = self.transforms.cursor();
|
||||
cursor.seek(&DisplayPoint(display_point), SeekBias::Left, &());
|
||||
|
||||
Ok(BufferRows {
|
||||
BufferRows {
|
||||
display_point,
|
||||
cursor,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Result<Chars<'a>> {
|
||||
let offset = self.to_display_offset(point, ctx)?;
|
||||
pub fn chars_at<'a>(&'a self, point: DisplayPoint, ctx: &'a AppContext) -> Chars<'a> {
|
||||
let offset = self.to_display_offset(point, ctx);
|
||||
let mut cursor = self.transforms.cursor();
|
||||
cursor.seek(&offset, SeekBias::Right, &());
|
||||
Ok(Chars {
|
||||
Chars {
|
||||
cursor,
|
||||
offset: offset.0,
|
||||
buffer: self.buffer.read(ctx),
|
||||
buffer_chars: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> Result<DisplayOffset> {
|
||||
fn to_display_offset(&self, point: DisplayPoint, ctx: &AppContext) -> DisplayOffset {
|
||||
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
|
||||
cursor.seek(&point, SeekBias::Right, &());
|
||||
let overshoot = point.0 - cursor.start().display.lines;
|
||||
let mut offset = cursor.start().display.bytes;
|
||||
if !overshoot.is_zero() {
|
||||
let transform = cursor
|
||||
.item()
|
||||
.ok_or_else(|| anyhow!("display point {:?} is out of range", point))?;
|
||||
let transform = cursor.item().expect("display point out of range");
|
||||
assert!(transform.display_text.is_none());
|
||||
let end_buffer_offset =
|
||||
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.read(ctx));
|
||||
offset += end_buffer_offset - cursor.start().buffer.bytes;
|
||||
}
|
||||
Ok(DisplayOffset(offset))
|
||||
DisplayOffset(offset)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -680,8 +668,7 @@ mod tests {
|
||||
Point::new(2, 4)..Point::new(4, 1),
|
||||
],
|
||||
app.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
);
|
||||
assert_eq!(map.text(app.as_ref()), "aa…cc…eeeee");
|
||||
|
||||
buffer.update(app, |buffer, ctx| {
|
||||
@@ -707,8 +694,7 @@ mod tests {
|
||||
});
|
||||
assert_eq!(map.text(app.as_ref()), "123a…c123456eee");
|
||||
|
||||
map.unfold(Some(Point::new(0, 4)..Point::new(0, 5)), app.as_ref())
|
||||
.unwrap();
|
||||
map.unfold(Some(Point::new(0, 4)..Point::new(0, 5)), app.as_ref());
|
||||
assert_eq!(map.text(app.as_ref()), "123aaaaa\nbbbbbb\nccc123456eee");
|
||||
}
|
||||
|
||||
@@ -719,17 +705,17 @@ mod tests {
|
||||
{
|
||||
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
|
||||
|
||||
map.fold(vec![5..8], app.as_ref()).unwrap();
|
||||
map.fold(vec![5..8], app.as_ref());
|
||||
map.check_invariants(app.as_ref());
|
||||
assert_eq!(map.text(app.as_ref()), "abcde…ijkl");
|
||||
|
||||
// Create an fold adjacent to the start of the first fold.
|
||||
map.fold(vec![0..1, 2..5], app.as_ref()).unwrap();
|
||||
map.fold(vec![0..1, 2..5], app.as_ref());
|
||||
map.check_invariants(app.as_ref());
|
||||
assert_eq!(map.text(app.as_ref()), "…b…ijkl");
|
||||
|
||||
// Create an fold adjacent to the end of the first fold.
|
||||
map.fold(vec![11..11, 8..10], app.as_ref()).unwrap();
|
||||
map.fold(vec![11..11, 8..10], app.as_ref());
|
||||
map.check_invariants(app.as_ref());
|
||||
assert_eq!(map.text(app.as_ref()), "…b…kl");
|
||||
}
|
||||
@@ -738,7 +724,7 @@ mod tests {
|
||||
let mut map = FoldMap::new(buffer.clone(), app.as_ref());
|
||||
|
||||
// Create two adjacent folds.
|
||||
map.fold(vec![0..2, 2..5], app.as_ref()).unwrap();
|
||||
map.fold(vec![0..2, 2..5], app.as_ref());
|
||||
map.check_invariants(app.as_ref());
|
||||
assert_eq!(map.text(app.as_ref()), "…fghijkl");
|
||||
|
||||
@@ -765,8 +751,7 @@ mod tests {
|
||||
Point::new(3, 1)..Point::new(4, 1),
|
||||
],
|
||||
app.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
);
|
||||
assert_eq!(map.text(app.as_ref()), "aa…eeeee");
|
||||
}
|
||||
|
||||
@@ -781,8 +766,7 @@ mod tests {
|
||||
Point::new(3, 1)..Point::new(4, 1),
|
||||
],
|
||||
app.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
);
|
||||
assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee");
|
||||
|
||||
buffer.update(app, |buffer, ctx| {
|
||||
@@ -807,11 +791,9 @@ mod tests {
|
||||
Point::new(3, 1)..Point::new(4, 1),
|
||||
],
|
||||
app.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
);
|
||||
let fold_ranges = map
|
||||
.folds_in_range(Point::new(1, 0)..Point::new(1, 3), app.as_ref())
|
||||
.unwrap()
|
||||
.map(|fold| fold.start.to_point(buffer)..fold.end.to_point(buffer))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
@@ -866,7 +848,7 @@ mod tests {
|
||||
to_fold.push(start..end);
|
||||
}
|
||||
log::info!("folding {:?}", to_fold);
|
||||
map.fold(to_fold, app.as_ref()).unwrap();
|
||||
map.fold(to_fold, app.as_ref());
|
||||
}
|
||||
35..=59 if !map.folds.is_empty() => {
|
||||
let buffer = buffer.read(app);
|
||||
@@ -877,7 +859,7 @@ mod tests {
|
||||
to_unfold.push(start..end);
|
||||
}
|
||||
log::info!("unfolding {:?}", to_unfold);
|
||||
map.unfold(to_unfold, app.as_ref()).unwrap();
|
||||
map.unfold(to_unfold, app.as_ref());
|
||||
}
|
||||
_ => {
|
||||
let edits = buffer.update(app, |buffer, ctx| {
|
||||
@@ -909,7 +891,7 @@ mod tests {
|
||||
assert_eq!(map.text(app.as_ref()), expected_text);
|
||||
|
||||
for (display_row, line) in expected_text.lines().enumerate() {
|
||||
let line_len = map.line_len(display_row as u32, app.as_ref()).unwrap();
|
||||
let line_len = map.line_len(display_row as u32, app.as_ref());
|
||||
assert_eq!(line_len, line.chars().count() as u32);
|
||||
}
|
||||
|
||||
@@ -928,7 +910,7 @@ mod tests {
|
||||
buffer_offset
|
||||
);
|
||||
assert_eq!(
|
||||
map.to_display_offset(display_point, app.as_ref()).unwrap(),
|
||||
map.to_display_offset(display_point, app.as_ref()),
|
||||
display_offset
|
||||
);
|
||||
|
||||
@@ -949,14 +931,13 @@ mod tests {
|
||||
|
||||
for _ in 0..5 {
|
||||
let row = rng.gen_range(0..=map.max_point(app.as_ref()).row());
|
||||
let column = rng.gen_range(0..=map.line_len(row, app.as_ref()).unwrap());
|
||||
let column = rng.gen_range(0..=map.line_len(row, app.as_ref()));
|
||||
let point = DisplayPoint::new(row, column);
|
||||
let offset = map.to_display_offset(point, app.as_ref()).unwrap().0;
|
||||
let offset = map.to_display_offset(point, app.as_ref()).0;
|
||||
let len = rng.gen_range(0..=map.len(app.as_ref()) - offset);
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.chars_at(point, app.as_ref())
|
||||
.unwrap()
|
||||
.take(len)
|
||||
.collect::<String>(),
|
||||
expected_text
|
||||
@@ -974,7 +955,6 @@ mod tests {
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.buffer_rows(display_row)
|
||||
.unwrap()
|
||||
.collect::<Vec<_>>(),
|
||||
expected_buffer_rows[idx..],
|
||||
);
|
||||
@@ -1004,7 +984,6 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
map.folds_in_range(start..end, app.as_ref())
|
||||
.unwrap()
|
||||
.cloned()
|
||||
.collect::<Vec<_>>(),
|
||||
expected_folds
|
||||
@@ -1027,21 +1006,18 @@ mod tests {
|
||||
Point::new(3, 1)..Point::new(4, 1),
|
||||
],
|
||||
app.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
);
|
||||
|
||||
assert_eq!(map.text(app.as_ref()), "aa…cccc\nd…eeeee\nffffff\n");
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.buffer_rows(0)
|
||||
.unwrap()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![0, 3, 5, 6]
|
||||
);
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.buffer_rows(3)
|
||||
.unwrap()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![6]
|
||||
);
|
||||
@@ -1051,7 +1027,6 @@ mod tests {
|
||||
fn text(&self, app: &AppContext) -> String {
|
||||
self.snapshot(app)
|
||||
.chars_at(DisplayPoint(Point::zero()), app)
|
||||
.unwrap()
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
mod fold_map;
|
||||
|
||||
use super::{buffer, Anchor, Buffer, Edit, Point, ToOffset, ToPoint};
|
||||
use anyhow::Result;
|
||||
pub use fold_map::BufferRows;
|
||||
use fold_map::{FoldMap, FoldMapSnapshot};
|
||||
use gpui::{AppContext, ModelHandle};
|
||||
@@ -39,7 +38,7 @@ impl DisplayMap {
|
||||
&'a self,
|
||||
range: Range<T>,
|
||||
app: &'a AppContext,
|
||||
) -> Result<impl Iterator<Item = &'a Range<Anchor>>>
|
||||
) -> impl Iterator<Item = &'a Range<Anchor>>
|
||||
where
|
||||
T: ToOffset,
|
||||
{
|
||||
@@ -50,7 +49,7 @@ impl DisplayMap {
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
) {
|
||||
self.fold_map.fold(ranges, ctx)
|
||||
}
|
||||
|
||||
@@ -58,7 +57,7 @@ impl DisplayMap {
|
||||
&mut self,
|
||||
ranges: impl IntoIterator<Item = Range<T>>,
|
||||
ctx: &AppContext,
|
||||
) -> Result<()> {
|
||||
) {
|
||||
self.fold_map.unfold(ranges, ctx)
|
||||
}
|
||||
|
||||
@@ -69,24 +68,22 @@ impl DisplayMap {
|
||||
pub fn text(&self, ctx: &AppContext) -> String {
|
||||
self.snapshot(ctx)
|
||||
.chars_at(DisplayPoint::zero(), ctx)
|
||||
.unwrap()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn line(&self, display_row: u32, ctx: &AppContext) -> Result<String> {
|
||||
Ok(self
|
||||
.snapshot(ctx)
|
||||
.chars_at(DisplayPoint::new(display_row, 0), ctx)?
|
||||
pub fn line(&self, display_row: u32, ctx: &AppContext) -> String {
|
||||
self.snapshot(ctx)
|
||||
.chars_at(DisplayPoint::new(display_row, 0), ctx)
|
||||
.take_while(|c| *c != '\n')
|
||||
.collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> Result<(u32, bool)> {
|
||||
pub fn line_indent(&self, display_row: u32, ctx: &AppContext) -> (u32, bool) {
|
||||
let mut indent = 0;
|
||||
let mut is_blank = true;
|
||||
for c in self
|
||||
.snapshot(ctx)
|
||||
.chars_at(DisplayPoint::new(display_row, 0), ctx)?
|
||||
.chars_at(DisplayPoint::new(display_row, 0), ctx)
|
||||
{
|
||||
if c == ' ' {
|
||||
indent += 1;
|
||||
@@ -95,45 +92,33 @@ impl DisplayMap {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok((indent, is_blank))
|
||||
(indent, is_blank)
|
||||
}
|
||||
|
||||
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
|
||||
DisplayPoint::new(row, self.fold_map.line_len(row, ctx)?)
|
||||
pub fn line_len(&self, row: u32, ctx: &AppContext) -> u32 {
|
||||
DisplayPoint::new(row, self.fold_map.line_len(row, ctx))
|
||||
.expand_tabs(self, ctx)
|
||||
.map(|point| point.column())
|
||||
.column()
|
||||
}
|
||||
|
||||
pub fn max_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
self.fold_map.max_point(ctx).expand_tabs(self, ctx).unwrap()
|
||||
self.fold_map.max_point(ctx).expand_tabs(self, ctx)
|
||||
}
|
||||
|
||||
pub fn rightmost_point(&self, ctx: &AppContext) -> DisplayPoint {
|
||||
self.fold_map.rightmost_point(ctx)
|
||||
}
|
||||
|
||||
pub fn anchor_before(
|
||||
&self,
|
||||
point: DisplayPoint,
|
||||
bias: Bias,
|
||||
app: &AppContext,
|
||||
) -> Result<Anchor> {
|
||||
Ok(self
|
||||
.buffer
|
||||
pub fn anchor_before(&self, point: DisplayPoint, bias: Bias, app: &AppContext) -> Anchor {
|
||||
self.buffer
|
||||
.read(app)
|
||||
.anchor_before(point.to_buffer_point(self, bias, app)?))
|
||||
.anchor_before(point.to_buffer_point(self, bias, app))
|
||||
}
|
||||
|
||||
pub fn anchor_after(
|
||||
&self,
|
||||
point: DisplayPoint,
|
||||
bias: Bias,
|
||||
app: &AppContext,
|
||||
) -> Result<Anchor> {
|
||||
Ok(self
|
||||
.buffer
|
||||
pub fn anchor_after(&self, point: DisplayPoint, bias: Bias, app: &AppContext) -> Anchor {
|
||||
self.buffer
|
||||
.read(app)
|
||||
.anchor_after(point.to_buffer_point(self, bias, app)?))
|
||||
.anchor_after(point.to_buffer_point(self, bias, app))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,33 +128,32 @@ pub struct DisplayMapSnapshot {
|
||||
}
|
||||
|
||||
impl DisplayMapSnapshot {
|
||||
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
|
||||
pub fn buffer_rows(&self, start_row: u32) -> BufferRows {
|
||||
self.folds_snapshot.buffer_rows(start_row)
|
||||
}
|
||||
|
||||
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
|
||||
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Chars<'a> {
|
||||
let column = point.column() as usize;
|
||||
let (point, to_next_stop) = self.collapse_tabs(point, Bias::Left, app)?;
|
||||
let mut fold_chars = self.folds_snapshot.chars_at(point, app)?;
|
||||
let (point, to_next_stop) = self.collapse_tabs(point, Bias::Left, app);
|
||||
let mut fold_chars = self.folds_snapshot.chars_at(point, app);
|
||||
if to_next_stop > 0 {
|
||||
fold_chars.next();
|
||||
}
|
||||
|
||||
Ok(Chars {
|
||||
Chars {
|
||||
fold_chars,
|
||||
column,
|
||||
to_next_stop,
|
||||
tab_size: self.tab_size,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> Result<DisplayPoint> {
|
||||
fn expand_tabs(&self, mut point: DisplayPoint, ctx: &AppContext) -> DisplayPoint {
|
||||
let chars = self
|
||||
.folds_snapshot
|
||||
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx)?;
|
||||
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx);
|
||||
let expanded = expand_tabs(chars, point.column() as usize, self.tab_size);
|
||||
*point.column_mut() = expanded as u32;
|
||||
Ok(point)
|
||||
point
|
||||
}
|
||||
|
||||
fn collapse_tabs(
|
||||
@@ -177,15 +161,15 @@ impl DisplayMapSnapshot {
|
||||
mut point: DisplayPoint,
|
||||
bias: Bias,
|
||||
ctx: &AppContext,
|
||||
) -> Result<(DisplayPoint, usize)> {
|
||||
) -> (DisplayPoint, usize) {
|
||||
let chars = self
|
||||
.folds_snapshot
|
||||
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx)?;
|
||||
.chars_at(DisplayPoint(Point::new(point.row(), 0)), ctx);
|
||||
let expanded = point.column() as usize;
|
||||
let (collapsed, to_next_stop) = collapse_tabs(chars, expanded, bias, self.tab_size);
|
||||
*point.column_mut() = collapsed as u32;
|
||||
|
||||
Ok((point, to_next_stop))
|
||||
(point, to_next_stop)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,45 +201,38 @@ impl DisplayPoint {
|
||||
&mut self.0.column
|
||||
}
|
||||
|
||||
pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result<Point> {
|
||||
Ok(map
|
||||
.fold_map
|
||||
.to_buffer_point(self.collapse_tabs(map, bias, ctx)?.0, ctx))
|
||||
pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Point {
|
||||
map.fold_map
|
||||
.to_buffer_point(self.collapse_tabs(map, bias, ctx).0, ctx)
|
||||
}
|
||||
|
||||
pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> Result<usize> {
|
||||
Ok(map
|
||||
.fold_map
|
||||
.to_buffer_offset(self.collapse_tabs(&map, bias, ctx)?.0, ctx))
|
||||
pub fn to_buffer_offset(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> usize {
|
||||
map.fold_map
|
||||
.to_buffer_offset(self.collapse_tabs(&map, bias, ctx).0, ctx)
|
||||
}
|
||||
|
||||
fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Result<Self> {
|
||||
fn expand_tabs(self, map: &DisplayMap, ctx: &AppContext) -> Self {
|
||||
map.snapshot(ctx).expand_tabs(self, ctx)
|
||||
}
|
||||
|
||||
fn collapse_tabs(
|
||||
self,
|
||||
map: &DisplayMap,
|
||||
bias: Bias,
|
||||
ctx: &AppContext,
|
||||
) -> Result<(Self, usize)> {
|
||||
fn collapse_tabs(self, map: &DisplayMap, bias: Bias, ctx: &AppContext) -> (Self, usize) {
|
||||
map.snapshot(ctx).collapse_tabs(self, bias, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> Result<DisplayPoint> {
|
||||
pub fn to_display_point(self, map: &DisplayMap, ctx: &AppContext) -> DisplayPoint {
|
||||
let mut display_point = map.fold_map.to_display_point(self, ctx);
|
||||
let snapshot = map.fold_map.snapshot(ctx);
|
||||
let chars = snapshot.chars_at(DisplayPoint::new(display_point.row(), 0), ctx)?;
|
||||
let chars = snapshot.chars_at(DisplayPoint::new(display_point.row(), 0), ctx);
|
||||
*display_point.column_mut() =
|
||||
expand_tabs(chars, display_point.column() as usize, map.tab_size) as u32;
|
||||
Ok(display_point)
|
||||
display_point
|
||||
}
|
||||
}
|
||||
|
||||
impl Anchor {
|
||||
pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> Result<DisplayPoint> {
|
||||
pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> DisplayPoint {
|
||||
self.to_point(map.buffer.read(app))
|
||||
.to_display_point(map, app)
|
||||
}
|
||||
@@ -365,7 +342,6 @@ mod tests {
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.chars_at(DisplayPoint::new(1, 0), app.as_ref())
|
||||
.unwrap()
|
||||
.take(10)
|
||||
.collect::<String>(),
|
||||
" b bb"
|
||||
@@ -373,7 +349,6 @@ mod tests {
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.chars_at(DisplayPoint::new(1, 2), app.as_ref())
|
||||
.unwrap()
|
||||
.take(10)
|
||||
.collect::<String>(),
|
||||
" b bbbb"
|
||||
@@ -381,7 +356,6 @@ mod tests {
|
||||
assert_eq!(
|
||||
map.snapshot(app.as_ref())
|
||||
.chars_at(DisplayPoint::new(1, 6), app.as_ref())
|
||||
.unwrap()
|
||||
.take(13)
|
||||
.collect::<String>(),
|
||||
" bbbbb\nc c"
|
||||
|
||||
@@ -8,13 +8,13 @@ pub fn left(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Resu
|
||||
*point.column_mut() -= 1;
|
||||
} else if point.row() > 0 {
|
||||
*point.row_mut() -= 1;
|
||||
*point.column_mut() = map.line_len(point.row(), app)?;
|
||||
*point.column_mut() = map.line_len(point.row(), app);
|
||||
}
|
||||
Ok(point)
|
||||
}
|
||||
|
||||
pub fn right(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
|
||||
let max_column = map.line_len(point.row(), app).unwrap();
|
||||
let max_column = map.line_len(point.row(), app);
|
||||
if point.column() < max_column {
|
||||
*point.column_mut() += 1;
|
||||
} else if point.row() < map.max_point(app).row() {
|
||||
@@ -37,7 +37,7 @@ pub fn up(
|
||||
};
|
||||
if point.row() > 0 {
|
||||
*point.row_mut() -= 1;
|
||||
*point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)?);
|
||||
*point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app));
|
||||
} else {
|
||||
point = DisplayPoint::new(0, 0);
|
||||
}
|
||||
@@ -59,7 +59,7 @@ pub fn down(
|
||||
let max_point = map.max_point(app);
|
||||
if point.row() < max_point.row() {
|
||||
*point.row_mut() += 1;
|
||||
*point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app)?)
|
||||
*point.column_mut() = cmp::min(goal_column, map.line_len(point.row(), app))
|
||||
} else {
|
||||
point = max_point;
|
||||
}
|
||||
@@ -73,7 +73,7 @@ pub fn line_beginning(
|
||||
toggle_indent: bool,
|
||||
app: &AppContext,
|
||||
) -> Result<DisplayPoint> {
|
||||
let (indent, is_blank) = map.line_indent(point.row(), app)?;
|
||||
let (indent, is_blank) = map.line_indent(point.row(), app);
|
||||
if toggle_indent && !is_blank && point.column() != indent {
|
||||
Ok(DisplayPoint::new(point.row(), indent))
|
||||
} else {
|
||||
@@ -84,7 +84,7 @@ pub fn line_beginning(
|
||||
pub fn line_end(map: &DisplayMap, point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
|
||||
Ok(DisplayPoint::new(
|
||||
point.row(),
|
||||
map.line_len(point.row(), app)?,
|
||||
map.line_len(point.row(), app),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -98,13 +98,13 @@ pub fn prev_word_boundary(
|
||||
Ok(DisplayPoint::new(0, 0))
|
||||
} else {
|
||||
let row = point.row() - 1;
|
||||
Ok(DisplayPoint::new(row, map.line_len(row, app)?))
|
||||
Ok(DisplayPoint::new(row, map.line_len(row, app)))
|
||||
}
|
||||
} else {
|
||||
let mut boundary = DisplayPoint::new(point.row(), 0);
|
||||
let mut column = 0;
|
||||
let mut prev_c = None;
|
||||
for c in map.snapshot(app).chars_at(boundary, app)? {
|
||||
for c in map.snapshot(app).chars_at(boundary, app) {
|
||||
if column >= point.column() {
|
||||
break;
|
||||
}
|
||||
@@ -126,7 +126,7 @@ pub fn next_word_boundary(
|
||||
app: &AppContext,
|
||||
) -> Result<DisplayPoint> {
|
||||
let mut prev_c = None;
|
||||
for c in map.snapshot(app).chars_at(point, app)? {
|
||||
for c in map.snapshot(app).chars_at(point, app) {
|
||||
if prev_c.is_some() && (c == '\n' || char_kind(prev_c.unwrap()) != char_kind(c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user