Skip to content

Commit

Permalink
Refactor: get_painted_file_with_line_number function
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Nov 15, 2021
1 parent fe835ff commit 4ed9688
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 41 deletions.
75 changes: 35 additions & 40 deletions src/handlers/hunk_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use regex::Regex;
use super::draw;
use crate::config::Config;
use crate::delta::{self, State, StateMachine};
use crate::features;
use crate::paint::{BgShouldFill, Painter, StyleSectionSpecifier};
use crate::paint::{self, BgShouldFill, Painter, StyleSectionSpecifier};
use crate::style::DecorationStyle;

impl<'a> StateMachine<'a> {
Expand Down Expand Up @@ -173,7 +172,9 @@ fn write_hunk_header(
"".to_string()
};

let file_with_line_number = get_painted_file_with_line_number(line_numbers, plus_file, config);
let plus_line_number = line_numbers[line_numbers.len() - 1].0;
let file_with_line_number =
paint_file_path_with_line_number(Some(plus_line_number), plus_file, config);

if !line.is_empty() || !file_with_line_number.is_empty() {
write_to_output_buffer(&file_with_line_number, line, painter, config);
Expand All @@ -191,41 +192,35 @@ fn write_hunk_header(
Ok(())
}

fn get_painted_file_with_line_number(
line_numbers: &[(usize, usize)],
fn paint_file_path_with_line_number(
line_number: Option<usize>,
plus_file: &str,
config: &Config,
) -> String {
let mut file_with_line_number = Vec::new();
let plus_line_number = line_numbers[line_numbers.len() - 1].0;
if config.hunk_header_style_include_file_path {
file_with_line_number.push(config.hunk_header_file_style.paint(plus_file))
let file_style = if config.hunk_header_style_include_file_path {
Some(config.hunk_header_file_style)
} else {
None
};
if config.hunk_header_style_include_line_number
let line_number_style = if config.hunk_header_style_include_line_number
&& !config.hunk_header_style.is_raw
&& !config.color_only
&& line_number.is_some()
{
if !file_with_line_number.is_empty() {
file_with_line_number.push(ansi_term::ANSIString::from(":"));
}
file_with_line_number.push(
config
.hunk_header_line_number_style
.paint(format!("{}", plus_line_number)),
)
}
let file_with_line_number = ansi_term::ANSIStrings(&file_with_line_number).to_string();
if config.hyperlinks && !file_with_line_number.is_empty() {
features::hyperlinks::format_osc8_file_hyperlink(
plus_file,
Some(plus_line_number),
&file_with_line_number,
config,
)
.into()
Some(config.hunk_header_line_number_style)
} else {
file_with_line_number
}
None
};

paint::paint_file_path_with_line_number(
line_number,
plus_file,
false,
false,
file_style,
line_number_style,
config,
)
}

fn write_to_output_buffer(
Expand Down Expand Up @@ -318,39 +313,39 @@ pub mod tests {
assert_eq!(line_numbers_and_hunk_lengths[2], (358, 16),);
}
#[test]
fn test_get_painted_file_with_line_number_default() {
fn test_paint_file_path_with_line_number_default() {
let cfg = integration_test_utils::make_config_from_args(&[]);

let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg);
let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);

assert_eq!(result, "\u{1b}[34m3\u{1b}[0m");
}

#[test]
fn test_get_painted_file_with_line_number_hyperlinks() {
fn test_paint_file_path_with_line_number_hyperlinks() {
let cfg = integration_test_utils::make_config_from_args(&["--features", "hyperlinks"]);

let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg);
let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);

assert_eq!(result, "some-file");
}

#[test]
fn test_get_painted_file_with_line_number_empty() {
fn test_paint_file_path_with_line_number_empty() {
let cfg = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",
"syntax bold",
"--hunk-header-decoration-style",
"omit",
]);

let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg);
let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);

assert_eq!(result, "");
}

#[test]
fn test_get_painted_file_with_line_number_empty_hyperlinks() {
fn test_paint_file_path_with_line_number_empty_hyperlinks() {
let cfg = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",
"syntax bold",
Expand All @@ -360,13 +355,13 @@ pub mod tests {
"hyperlinks",
]);

let result = get_painted_file_with_line_number(&vec![(3, 4)], "some-file", &cfg);
let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);

assert_eq!(result, "");
}

#[test]
fn test_get_painted_file_with_line_number_empty_navigate() {
fn test_paint_file_path_with_line_number_empty_navigate() {
let cfg = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",
"syntax bold",
Expand All @@ -375,7 +370,7 @@ pub mod tests {
"--navigate",
]);

let result = get_painted_file_with_line_number(&vec![(3, 4)], "δ some-file", &cfg);
let result = paint_file_path_with_line_number(Some(3), "δ some-file", &cfg);

assert_eq!(result, "");
}
Expand Down
57 changes: 56 additions & 1 deletion src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use syntect::parsing::{SyntaxReference, SyntaxSet};
use unicode_segmentation::UnicodeSegmentation;

use crate::ansi;
use crate::config::{self, delta_unreachable};
use crate::config::{self, delta_unreachable, Config};
use crate::delta::State;
use crate::edits;
use crate::features::hyperlinks;
use crate::features::line_numbers;
use crate::features::side_by_side::ansifill;
use crate::features::side_by_side::{self, available_line_width, LineSegments, PanelSide};
Expand Down Expand Up @@ -749,6 +750,60 @@ impl<'p> Painter<'p> {
}
}

pub fn paint_file_path_with_line_number(
line_number: Option<usize>,
plus_file: &str,
pad_line_number: bool,
terminate_with_colon: bool,
file_style: Option<Style>, // None means do not include file path
line_number_style: Option<Style>, // None means do not include line number
config: &Config,
) -> String {
let mut file_with_line_number = Vec::new();
if let Some(file_style) = file_style {
file_with_line_number.push(file_style.paint(plus_file))
};
if let Some(line_number) = line_number {
if let Some(line_number_style) = line_number_style {
if !file_with_line_number.is_empty() {
file_with_line_number.push(ansi_term::ANSIString::from(":"));
}
file_with_line_number.push(line_number_style.paint(format!("{}", line_number)))
}
}
if terminate_with_colon {
file_with_line_number.push(ansi_term::ANSIGenericString::from(":"));
}
if pad_line_number {
// If requested we pad line numbers to a width of at least
// 3, so that we do not see any misalignment up to line
// number 999. However, see
// https://github.com/BurntSushi/ripgrep/issues/795 for
// discussion about aligning grep output.
match line_number {
Some(n) if n < 10 => {
file_with_line_number.push(ansi_term::ANSIGenericString::from(" "))
}
Some(n) if n < 100 => {
file_with_line_number.push(ansi_term::ANSIGenericString::from(" "))
}
_ => {}
}
}
let file_with_line_number = ansi_term::ANSIStrings(&file_with_line_number).to_string();
if config.hyperlinks && !file_with_line_number.is_empty() {
hyperlinks::format_osc8_file_hyperlink(
plus_file,
line_number,
&file_with_line_number,
config,
)
.into()
} else {
file_with_line_number
}
}

// edits::annotate doesn't return "coalesced" annotations (see comment there), so we can't assume
// that `sections.len() > 1 <=> (multiple styles)`.
fn style_sections_contain_more_than_one_style(sections: &[(Style, &str)]) -> bool {
Expand Down

0 comments on commit 4ed9688

Please sign in to comment.