From 53f828df7d56ed97cdb0eb85615897fe79f53492 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 23 Jul 2024 19:03:15 +0200 Subject: [PATCH] Avoid inserting extra newlines when evaluating code (#15018) When the evaluation range ends at the start of a line, back it up to the end of the previous line. This avoids inserting extra newlines below the evaluation range when they already exist. Release Notes: - N/A Co-authored-by: Nathan --- crates/repl/src/repl_editor.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/crates/repl/src/repl_editor.rs b/crates/repl/src/repl_editor.rs index 0dde4a3203..90838bf10a 100644 --- a/crates/repl/src/repl_editor.rs +++ b/crates/repl/src/repl_editor.rs @@ -143,25 +143,22 @@ fn snippet( let buffer = editor.buffer().read(cx).snapshot(cx); - let selection = editor.selections.newest::(cx); + let selection = editor.selections.newest::(cx); let multi_buffer_snapshot = editor.buffer().read(cx).snapshot(cx); let range = if selection.is_empty() { - let cursor = selection.head(); - - let cursor_row = multi_buffer_snapshot.offset_to_point(cursor).row; - let start_offset = multi_buffer_snapshot.point_to_offset(Point::new(cursor_row, 0)); - - let end_point = Point::new( - cursor_row, - multi_buffer_snapshot.line_len(MultiBufferRow(cursor_row)), - ); - let end_offset = start_offset.saturating_add(end_point.column as usize); - - // Create a range from the start to the end of the line - start_offset..end_offset + Point::new(selection.start.row, 0) + ..Point::new( + selection.start.row, + multi_buffer_snapshot.line_len(MultiBufferRow(selection.start.row)), + ) } else { - selection.range() + let mut range = selection.range(); + if range.end.column == 0 { + range.end.row -= 1; + range.end.column = multi_buffer_snapshot.line_len(MultiBufferRow(range.end.row)); + } + range }; let anchor_range = range.to_anchors(&multi_buffer_snapshot);