Avoid inserting extra newlines when evaluating code (#15018)

Antonio Scandurra and Nathan created

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 <nathan@zed.dev>

Change summary

crates/repl/src/repl_editor.rs | 27 ++++++++++++---------------
1 file changed, 12 insertions(+), 15 deletions(-)

Detailed changes

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::<usize>(cx);
+    let selection = editor.selections.newest::<Point>(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);