summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZack Cerza <zack@cerza.org>2023-11-20 20:18:19 -0700
committerGitHub <noreply@github.com>2023-11-20 22:18:19 -0500
commit06b95306ac87c00b3146724976b35da7898a58eb (patch)
tree193157b31eaf92baae8558460f70becdde56b70a
parent49a99180fed618b62ba773e3aebd8968b044262d (diff)
Make hunk header code fragment display optional (#1568)
Fixes: #1032
-rw-r--r--manual/src/full---help-output.md2
-rw-r--r--src/config.rs16
-rw-r--r--src/handlers/grep.rs6
-rw-r--r--src/handlers/hunk_header.rs10
-rw-r--r--src/parse_style.rs2
-rw-r--r--src/tests/test_example_diffs.rs15
6 files changed, 46 insertions, 5 deletions
diff --git a/manual/src/full---help-output.md b/manual/src/full---help-output.md
index 58323ef1..16116a1d 100644
--- a/manual/src/full---help-output.md
+++ b/manual/src/full---help-output.md
@@ -238,7 +238,7 @@ Options:
--hunk-header-style <STYLE>
Style string for the hunk-header.
- See STYLES section. Special attributes 'file' and 'line-number' can be used to include the file path, and number of first hunk line, in the hunk header. The style 'omit' can be used to remove the hunk header section from the output.
+ See STYLES section. Special attributes 'file' and 'line-number' can be used to include the file path, and number of first hunk line, in the hunk header. The special attribute 'omit-code-fragment' can be used to remove the code fragment in the hunk header. The style 'omit' can be used to remove the entire hunk header section from the output.
[default: "line-number syntax"]
diff --git a/src/config.rs b/src/config.rs
index cab21eb7..c5c57bb2 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -78,6 +78,7 @@ pub struct Config {
pub hunk_header_line_number_style: Style,
pub hunk_header_style_include_file_path: HunkHeaderIncludeFilePath,
pub hunk_header_style_include_line_number: HunkHeaderIncludeLineNumber,
+ pub hunk_header_style_include_code_fragment: HunkHeaderIncludeCodeFragment,
pub hunk_header_style: Style,
pub hunk_label: String,
pub hyperlinks_commit_link_format: Option<String>,
@@ -151,6 +152,12 @@ pub enum HunkHeaderIncludeLineNumber {
No,
}
+#[cfg_attr(test, derive(Clone))]
+pub enum HunkHeaderIncludeCodeFragment {
+ Yes,
+ No,
+}
+
impl Config {
pub fn get_style(&self, state: &State) -> &Style {
match state {
@@ -331,6 +338,15 @@ impl From<cli::Opt> for Config {
} else {
HunkHeaderIncludeLineNumber::No
},
+ hunk_header_style_include_code_fragment: if opt
+ .hunk_header_style
+ .split(' ')
+ .any(|s| s == "omit-code-fragment")
+ {
+ HunkHeaderIncludeCodeFragment::No
+ } else {
+ HunkHeaderIncludeCodeFragment::Yes
+ },
hyperlinks: opt.hyperlinks,
hyperlinks_commit_link_format: opt.hyperlinks_commit_link_format,
hyperlinks_file_link_format: opt.hyperlinks_file_link_format,
diff --git a/src/handlers/grep.rs b/src/handlers/grep.rs
index ecfc7e2a..bbdee10c 100644
--- a/src/handlers/grep.rs
+++ b/src/handlers/grep.rs
@@ -6,7 +6,8 @@ use serde::Deserialize;
use crate::ansi;
use crate::config::{
- delta_unreachable, GrepType, HunkHeaderIncludeFilePath, HunkHeaderIncludeLineNumber,
+ delta_unreachable, GrepType, HunkHeaderIncludeCodeFragment, HunkHeaderIncludeFilePath,
+ HunkHeaderIncludeLineNumber,
};
use crate::delta::{State, StateMachine};
use crate::handlers::{self, ripgrep_json};
@@ -168,6 +169,7 @@ impl<'a> StateMachine<'a> {
&HunkHeaderIncludeFilePath::Yes,
&HunkHeaderIncludeLineNumber::No,
&HunkHeaderIncludeHunkLabel::Yes,
+ &HunkHeaderIncludeCodeFragment::Yes,
"",
self.config,
)?
@@ -230,6 +232,7 @@ impl<'a> StateMachine<'a> {
&HunkHeaderIncludeLineNumber::No
},
&HunkHeaderIncludeHunkLabel::No,
+ &HunkHeaderIncludeCodeFragment::Yes,
grep_line.line_type.file_path_separator(),
self.config,
)
@@ -255,6 +258,7 @@ impl<'a> StateMachine<'a> {
&self.config.hunk_header_style_include_file_path,
&self.config.hunk_header_style_include_line_number,
&HunkHeaderIncludeHunkLabel::Yes,
+ &HunkHeaderIncludeCodeFragment::Yes,
grep_line.line_type.file_path_separator(),
self.config,
)?
diff --git a/src/handlers/hunk_header.rs b/src/handlers/hunk_header.rs
index f06dcc52..a34035b5 100644
--- a/src/handlers/hunk_header.rs
+++ b/src/handlers/hunk_header.rs
@@ -25,7 +25,9 @@ use lazy_static::lazy_static;
use regex::Regex;
use super::draw;
-use crate::config::{Config, HunkHeaderIncludeFilePath, HunkHeaderIncludeLineNumber};
+use crate::config::{
+ Config, HunkHeaderIncludeCodeFragment, HunkHeaderIncludeFilePath, HunkHeaderIncludeLineNumber,
+};
use crate::delta::{self, DiffType, InMergeConflict, MergeParents, State, StateMachine};
use crate::paint::{self, BgShouldFill, Painter, StyleSectionSpecifier};
use crate::style::{DecorationStyle, Style};
@@ -133,6 +135,7 @@ impl<'a> StateMachine<'a> {
&self.config.hunk_header_style_include_file_path,
&self.config.hunk_header_style_include_line_number,
&HunkHeaderIncludeHunkLabel::Yes,
+ &self.config.hunk_header_style_include_code_fragment,
":",
self.config,
)?;
@@ -229,13 +232,16 @@ pub fn write_line_of_code_with_optional_path_and_line_number(
include_file_path: &HunkHeaderIncludeFilePath,
include_line_number: &HunkHeaderIncludeLineNumber,
include_hunk_label: &HunkHeaderIncludeHunkLabel,
+ include_code_fragment: &HunkHeaderIncludeCodeFragment,
file_path_separator: &str,
config: &Config,
) -> std::io::Result<()> {
let (mut draw_fn, _, decoration_ansi_term_style) = draw::get_draw_function(decoration_style);
let line = if config.color_only {
line.to_string()
- } else if !code_fragment.is_empty() {
+ } else if matches!(include_code_fragment, HunkHeaderIncludeCodeFragment::Yes)
+ && !code_fragment.is_empty()
+ {
format!("{code_fragment} ")
} else {
"".to_string()
diff --git a/src/parse_style.rs b/src/parse_style.rs
index 30106ba3..24d15ddf 100644
--- a/src/parse_style.rs
+++ b/src/parse_style.rs
@@ -186,7 +186,7 @@ fn parse_ansi_term_style(
style.is_strikethrough = true;
} else if word == "ul" || word == "underline" {
style.is_underline = true;
- } else if word == "line-number" || word == "file" {
+ } else if word == "line-number" || word == "file" || word == "omit-code-fragment" {
// Allow: these are meaningful in hunk-header-style.
} else if !seen_foreground {
if word == "syntax" {
diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs
index 9790d95a..a3df1e36 100644
--- a/src/tests/test_example_diffs.rs
+++ b/src/tests/test_example_diffs.rs
@@ -1092,6 +1092,21 @@ src/delta.rs:1: │
}
#[test]
+ fn test_hunk_header_omit_code_fragment() {
+ let config = integration_test_utils::make_config_from_args(&[
+ "--hunk-header-style",
+ "line-number omit-code-fragment",
+ "--hunk-header-decoration-style",
+ "none",
+ ]);
+ let output = strip_ansi_codes(&integration_test_utils::run_delta(
+ GIT_DIFF_SINGLE_HUNK,
+ &config,
+ ));
+ assert!(output.contains("\n71: \n"));
+ }
+
+ #[test]
fn test_hunk_header_style_colored_input_color_is_stripped_under_normal() {
let config = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",