summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-08-28 20:14:50 -0400
committerDan Davison <dandavison7@gmail.com>2021-08-28 21:36:57 -0400
commit2f0f3ebeef6f4c3d641a28c09fade811541768de (patch)
tree153c898ee062c57fcd242e0c859fb6c49da8c65a /src
parent23e88e55676ec094098bdb5dfbb2bb167c08f994 (diff)
Refactor: syntax_highlight_and_paint_line function
Diffstat (limited to 'src')
-rw-r--r--src/hunk_header.rs36
-rw-r--r--src/paint.rs29
2 files changed, 41 insertions, 24 deletions
diff --git a/src/hunk_header.rs b/src/hunk_header.rs
index b3c596cf..7743e8e4 100644
--- a/src/hunk_header.rs
+++ b/src/hunk_header.rs
@@ -21,14 +21,12 @@
use std::fmt::Write as FmtWrite;
-use unicode_segmentation::UnicodeSegmentation;
-
use crate::config::Config;
use crate::delta;
use crate::draw;
use crate::features;
use crate::paint::Painter;
-use crate::style::DecorationStyle;
+use crate::style::{DecorationStyle, Style};
pub fn write_hunk_header_raw(
painter: &mut Painter,
@@ -73,7 +71,12 @@ pub fn write_hunk_header(
let file_with_line_number = get_painted_file_with_line_number(line_numbers, plus_file, config);
if !line.is_empty() || !file_with_line_number.is_empty() {
- write_to_output_buffer(&file_with_line_number, line, painter, config);
+ write_to_output_buffer(
+ &file_with_line_number,
+ line,
+ config.hunk_header_style,
+ painter,
+ );
draw_fn(
painter.writer,
&painter.output_buffer,
@@ -133,33 +136,18 @@ fn get_painted_file_with_line_number(
fn write_to_output_buffer(
file_with_line_number: &str,
line: String,
+ style: Style,
painter: &mut Painter,
- config: &Config,
) {
if !file_with_line_number.is_empty() {
let _ = write!(&mut painter.output_buffer, "{}: ", file_with_line_number);
}
if !line.is_empty() {
- let lines = vec![(
- painter.expand_tabs(line.graphemes(true)),
+ painter.syntax_highlight_and_paint_line(
+ &line,
+ style,
delta::State::HunkHeader("".to_owned(), "".to_owned()),
- )];
- let syntax_style_sections = Painter::get_syntax_style_sections_for_lines(
- &lines,
- &delta::State::HunkHeader("".to_owned(), "".to_owned()),
- painter.highlighter.as_mut(),
- painter.config,
- );
- Painter::paint_lines(
- syntax_style_sections,
- vec![vec![(config.hunk_header_style, &lines[0].0)]], // TODO: compute style from state
- [delta::State::HunkHeader("".to_owned(), "".to_owned())].iter(),
- &mut painter.output_buffer,
- config,
- &mut None,
- None,
- None,
- Some(false),
+ false,
);
painter.output_buffer.pop(); // trim newline
}
diff --git a/src/paint.rs b/src/paint.rs
index 2236ec2d..f807146a 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -291,6 +291,35 @@ impl<'a> Painter<'a> {
}
}
+ /// Write painted line to the output buffer, with syntax-highlighting and `style` superimposed.
+ pub fn syntax_highlight_and_paint_line(
+ &mut self,
+ line: &str,
+ style: Style,
+ state: State,
+ background_color_extends_to_terminal_width: bool,
+ ) {
+ let lines = vec![(self.expand_tabs(line.graphemes(true)), state.clone())];
+ let syntax_style_sections = Painter::get_syntax_style_sections_for_lines(
+ &lines,
+ &state,
+ self.highlighter.as_mut(),
+ self.config,
+ );
+ let diff_style_sections = vec![vec![(style, lines[0].0.as_str())]]; // TODO: compute style from state
+ Painter::paint_lines(
+ syntax_style_sections,
+ diff_style_sections,
+ [state].iter(),
+ &mut self.output_buffer,
+ self.config,
+ &mut None,
+ None,
+ None,
+ Some(background_color_extends_to_terminal_width),
+ );
+ }
+
/// Determine whether the terminal should fill the line rightwards with a background color, and
/// the style for doing so.
pub fn get_should_right_fill_background_color_and_fill_style(