From 0851bbbb28107c41219168ffce0ba2c3863dde4b Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Thu, 5 May 2022 07:00:03 +0200 Subject: [PATCH] printer.rs: De-duplicate code to highlight long lines We do this to only have one invocation of `highlighter.highlight(...)` so we don't need to change to `highlighter.highlight_line(...)` in two places in #2181. --- src/printer.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 2ad41f0..d85120b 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -445,18 +445,21 @@ impl<'a> Printer for InteractivePrinter<'a> { return Ok(()); } }; + // skip syntax highlighting on long lines - if line.len() > 1024 * 16 { - let mut empty = highlighter_from_set - .highlighter - .highlight(&"\n", highlighter_from_set.syntax_set); - empty[0].1 = line.as_ref(); - empty - } else { - highlighter_from_set - .highlighter - .highlight(&line, highlighter_from_set.syntax_set) + let too_long = line.len() > 1024 * 16; + + let for_highlighting: &str = if too_long { "\n" } else { &line }; + + let mut highlighted_line = highlighter_from_set + .highlighter + .highlight(for_highlighting, highlighter_from_set.syntax_set); + + if too_long { + highlighted_line[0].1 = &line; } + + highlighted_line }; if out_of_range {