From 60a9431ca1a9b43bab8c235e0f2ef18ec0278d09 Mon Sep 17 00:00:00 2001 From: Martin Verzilli Date: Wed, 18 Oct 2023 14:48:36 +0200 Subject: [PATCH] Extract some auxiliaries for printing --- tooling/debugger/src/lib.rs | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/tooling/debugger/src/lib.rs b/tooling/debugger/src/lib.rs index 36ffbac6153..206d72dc8de 100644 --- a/tooling/debugger/src/lib.rs +++ b/tooling/debugger/src/lib.rs @@ -99,18 +99,20 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { for loc in locations { self.print_location_path(loc); - let line_index = debug_artifact.location_line_index(loc).unwrap(); + let loc_line_index = debug_artifact.location_line_index(loc).unwrap(); + + // How many lines before or after the location's line we + // print + let context_lines = 5; + + let first_line_to_print = + if loc_line_index < context_lines { 0 } else { loc_line_index - context_lines }; + let last_line_index = debug_artifact.last_line_index(loc).unwrap(); - let print_context_size = 5; - let first_line_to_print = if line_index < print_context_size { - 0 - } else { - line_index - print_context_size - }; - let last_line_to_print = if line_index + print_context_size > last_line_index { + let last_line_to_print = if loc_line_index + context_lines > last_line_index { last_line_index } else { - line_index + print_context_size + loc_line_index + context_lines }; let source = debug_artifact.location_source_code(loc).unwrap(); @@ -122,17 +124,18 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { continue; } else if current_line_index == first_line_to_print && current_line_index > 0 { // Denote that there's more lines before but we're not showing them - println!("{:>3} {}", current_line_index.dimmed(), "...".dimmed()); + print_ellipsized_line(current_line_index); } if current_line_index > last_line_to_print { // Denote that there's more lines after but we're not showing them, // and stop printing - println!("{:>3} {}", current_line_number.dimmed(), "...".dimmed()); + print_ellipsized_line(current_line_number); break; } - if current_line_index == line_index { + if current_line_index == loc_line_index { + // Highlight current location let Range { start: loc_start, end: loc_end } = debug_artifact.location_in_line(loc).unwrap(); println!( @@ -144,12 +147,7 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { &line[loc_end..].to_string().dimmed() ); } else { - println!( - "{:>3} {:2} {}", - current_line_number.dimmed(), - "".dimmed(), - line.dimmed() - ); + print_dimmed_line(current_line_number, line); } } } @@ -171,6 +169,14 @@ impl<'backend, B: BlackBoxFunctionSolver> DebugContext<'backend, B> { } } +fn print_ellipsized_line(line_number: usize) { + println!("{}", format!("{:>3} {}", line_number, "...").dimmed()); +} + +fn print_dimmed_line(line_number: usize, line: &str) { + println!("{}", format!("{:>3} {:2} {}", line_number, "", line).dimmed()); +} + fn map_command_status(result: SolveResult) -> CommandStatus { match result { SolveResult::Ok => CommandStatus::Done,