From 08ff3c0d478b9bbb21079a44ac44379a60b44a19 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 25 Mar 2024 23:34:20 +0800 Subject: [PATCH] Fix empty lines bug --- src/bin/rhai-dbg.rs | 3 ++- src/bin/rhai-repl.rs | 3 ++- src/bin/rhai-run.rs | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bin/rhai-dbg.rs b/src/bin/rhai-dbg.rs index cecb2ad5c..72ffe8eab 100644 --- a/src/bin/rhai-dbg.rs +++ b/src/bin/rhai-dbg.rs @@ -84,7 +84,8 @@ fn print_current_source( /// Pretty-print error. fn print_error(input: &str, mut err: EvalAltResult) { - let lines: Vec<_> = input.trim().lines().collect(); + // Do not use `line` because it "eats" the last empty line if the script ends with a newline. + let lines: Vec<_> = input.split('\n').collect(); let pos = err.take_position(); let line_no = if lines.len() > 1 { diff --git a/src/bin/rhai-repl.rs b/src/bin/rhai-repl.rs index 65b826b7b..6a1215c7d 100644 --- a/src/bin/rhai-repl.rs +++ b/src/bin/rhai-repl.rs @@ -11,7 +11,8 @@ const HISTORY_FILE: &str = ".rhai-repl-history"; /// Pretty-print error. fn print_error(input: &str, mut err: EvalAltResult) { - let lines: Vec<_> = input.lines().collect(); + // Do not use `line` because it "eats" the last empty line if the script ends with a newline. + let lines: Vec<_> = input.split('\n').collect(); let pos = err.take_position(); let line_no = if lines.len() > 1 { diff --git a/src/bin/rhai-run.rs b/src/bin/rhai-run.rs index b8503db0f..effd568b0 100644 --- a/src/bin/rhai-run.rs +++ b/src/bin/rhai-run.rs @@ -20,7 +20,8 @@ fn eprint_error(input: &str, mut err: EvalAltResult) { eprintln!(); } - let lines: Vec<_> = input.lines().collect(); + // Do not use `line` because it "eats" the last empty line if the script ends with a newline. + let lines: Vec<_> = input.split('\n').collect(); // Print error let pos = err.take_position();