Skip to content

Commit

Permalink
Make hunk header code fragment display optional (#1568)
Browse files Browse the repository at this point in the history
Fixes: #1032
  • Loading branch information
zmc committed Nov 21, 2023
1 parent 49a9918 commit 06b9530
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion manual/src/full---help-output.md
Expand Up @@ -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"]
Expand Down
16 changes: 16 additions & 0 deletions src/config.rs
Expand Up @@ -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>,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/handlers/grep.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -168,6 +169,7 @@ impl<'a> StateMachine<'a> {
&HunkHeaderIncludeFilePath::Yes,
&HunkHeaderIncludeLineNumber::No,
&HunkHeaderIncludeHunkLabel::Yes,
&HunkHeaderIncludeCodeFragment::Yes,
"",
self.config,
)?
Expand Down Expand Up @@ -230,6 +232,7 @@ impl<'a> StateMachine<'a> {
&HunkHeaderIncludeLineNumber::No
},
&HunkHeaderIncludeHunkLabel::No,
&HunkHeaderIncludeCodeFragment::Yes,
grep_line.line_type.file_path_separator(),
self.config,
)
Expand All @@ -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,
)?
Expand Down
10 changes: 8 additions & 2 deletions src/handlers/hunk_header.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
)?;
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/parse_style.rs
Expand Up @@ -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" {
Expand Down
15 changes: 15 additions & 0 deletions src/tests/test_example_diffs.rs
Expand Up @@ -1091,6 +1091,21 @@ src/delta.rs:1: │
assert!(output.contains("commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e"));
}

#[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(&[
Expand Down

0 comments on commit 06b9530

Please sign in to comment.