on master help sign in
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.

commit0851bbbb28107c41219168ffce0ba2c3863dde4b authorMartin Nordholts <enselic@gmail.com> dateThu 5 May 2022, 07:00 (5 years ago) parent05ebf5e
1 file changed+13−10
src/printer.rs+13−10
src/printer.rs+13−10
@@ -445,18 +445,21 @@ impl<'a> Printer for InteractivePrinter<'a> {
445445 return Ok(());
446446 }
447447 };
448+
448449 // skip syntax highlighting on long lines
449− if line.len() > 1024 * 16 {
450− let mut empty = highlighter_from_set
451− .highlighter
452− .highlight(&"\n", highlighter_from_set.syntax_set);
453− empty[0].1 = line.as_ref();
454− empty
455− } else {
456− highlighter_from_set
457− .highlighter
458− .highlight(&line, highlighter_from_set.syntax_set)
450+ let too_long = line.len() > 1024 * 16;
451+
452+ let for_highlighting: &str = if too_long { "\n" } else { &line };
453+
454+ let mut highlighted_line = highlighter_from_set
455+ .highlighter
456+ .highlight(for_highlighting, highlighter_from_set.syntax_set);
457+
458+ if too_long {
459+ highlighted_line[0].1 = &line;
459460 }
461+
462+ highlighted_line
460463 };
461464
462465 if out_of_range {