| 1 | // A module for constructing and writing the hunk header. |
| 2 | // |
| 3 | // The structure of the hunk header output by delta is |
| 4 | // ``` |
| 5 | // (file):(line-number): (code-fragment) |
| 6 | // ``` |
| 7 | // |
| 8 | // The code fragment and line number derive from a line of git/diff output that looks like |
| 9 | // ``` |
| 10 | // @@ -119,12 +119,7 @@ fn write_to_output_buffer( |
| 11 | // ``` |
| 12 | // |
| 13 | // Whether or not file and line-number are included is controlled by the presence of the special |
| 14 | // style attributes 'file' and 'line-number' in the hunk-header-style string. For example, delta |
| 15 | // might output the above hunk header as |
| 16 | // ``` |
| 17 | // ───────────────────────────────────────────────────┐ |
| 18 | // src/hunk_header.rs:119: fn write_to_output_buffer( │ |
| 19 | // ───────────────────────────────────────────────────┘ |
| 20 | // ``` |
| 21 | use std::convert::TryInto; |
| 22 | use std::fmt::Write as FmtWrite; |
| 23 | |
| 24 | use super::draw; |
| 25 | use crate::config::{ |
| 26 | Config, HunkHeaderIncludeCodeFragment, HunkHeaderIncludeFilePath, HunkHeaderIncludeLineNumber, |
| 27 | }; |
| 28 | use crate::delta::{self, DiffType, InMergeConflict, MergeParents, State, StateMachine}; |
| 29 | use crate::paint::{self, BgShouldFill, Painter, StyleSectionSpecifier}; |
| 30 | use crate::style::{DecorationStyle, Style}; |
| 31 | use lazy_static::lazy_static; |
| 32 | use regex::Regex; |
| 33 | |
| 34 | #[derive(Clone, Default, Debug, PartialEq, Eq)] |
| 35 | pub struct ParsedHunkHeader { |
| 36 | code_fragment: String, |
| 37 | line_numbers_and_hunk_lengths: Vec<(usize, usize)>, |
| 38 | } |
| 39 | |
| 40 | pub enum HunkHeaderIncludeHunkLabel { |
| 41 | Yes, |
| 42 | No, |
| 43 | } |
| 44 | |
| 45 | // The output of `diff -u file1 file2` does not contain a header before the |
| 46 | // `--- old.lua` / `+++ new.lua` block, and the hunk can of course contain lines |
| 47 | // starting with '-- '. To avoid interpreting '--- lua comment' as a new header, |
| 48 | // count the minus lines in a hunk (provided by the '@@ -1,4 +1,3 @@' header). |
| 49 | // `diff` itself can not generate two diffs in this ambiguous format, so a second header |
| 50 | // could just be ignored. But concatenated diffs exist and are accepted by `patch`. |
| 51 | #[derive(Debug)] |
| 52 | pub struct AmbiguousDiffMinusCounter(isize); |
| 53 | |
| 54 | impl AmbiguousDiffMinusCounter { |
| 55 | // The internal isize representation avoids calling `if let Some(..)` on every line. For |
| 56 | // nearly all input the counter is not needed, in this case it is decremented but ignored. |
| 57 | // [min, COUNTER_RELEVANT_IF_GT] unambiguous diff |
| 58 | // (COUNTER_RELEVANT_IF_GT, 0] handle next '--- ' like a header, and set counter in next @@ block |
| 59 | // [1, max] counting minus lines in ambiguous header |
| 60 | const COUNTER_RELEVANT_IF_GREATER_THAN: isize = -4096; // -1 works too, but be defensive |
| 61 | const EXPECT_DIFF_3DASH_HEADER: isize = 0; |
| 62 | |
| 63 | pub fn not_needed() -> Self { |
| 64 | Self(Self::COUNTER_RELEVANT_IF_GREATER_THAN) |
| 65 | } |
| 66 | pub fn prepare_to_count() -> Self { |
| 67 | Self(Self::EXPECT_DIFF_3DASH_HEADER) |
| 68 | } |
| 69 | pub fn three_dashes_expected(&self) -> bool { |
| 70 | if self.0 > Self::COUNTER_RELEVANT_IF_GREATER_THAN { |
| 71 | self.0 <= Self::EXPECT_DIFF_3DASH_HEADER |
| 72 | } else { |
| 73 | true |
| 74 | } |
| 75 | } |
| 76 | pub fn count_line(&mut self) { |
| 77 | self.0 -= 1; |
| 78 | } |
| 79 | fn count_from(lines: usize) -> Self { |
| 80 | Self( |
| 81 | lines |
| 82 | .try_into() |
| 83 | .unwrap_or(Self::COUNTER_RELEVANT_IF_GREATER_THAN), |
| 84 | ) |
| 85 | } |
| 86 | #[allow(clippy::needless_bool)] |
| 87 | fn must_count(&mut self) -> bool { |
| 88 | let relevant = self.0 > Self::COUNTER_RELEVANT_IF_GREATER_THAN; |
| 89 | if relevant { |
| 90 | true |
| 91 | } else { |
| 92 | #[cfg(target_pointer_width = "32")] |
| 93 | { |
| 94 | self.0 = Self::COUNTER_RELEVANT_IF_GREATER_THAN; |
| 95 | } |
| 96 | false |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl StateMachine<'_> { |
| 102 | #[inline] |
| 103 | fn test_hunk_header_line(&self) -> bool { |
| 104 | self.line.starts_with("@@") && |
| 105 | // A hunk header can occur within a merge conflict region, but we don't attempt to handle |
| 106 | // that. See #822. |
| 107 | !matches!(self.state, State::MergeConflict(_, _)) |
| 108 | } |
| 109 | |
| 110 | pub fn handle_hunk_header_line(&mut self) -> std::io::Result<bool> { |
| 111 | use DiffType::*; |
| 112 | use State::*; |
| 113 | if !self.test_hunk_header_line() { |
| 114 | return Ok(false); |
| 115 | } |
| 116 | let mut handled_line = false; |
| 117 | if let Some(parsed_hunk_header) = parse_hunk_header(&self.line) { |
| 118 | let diff_type = match &self.state { |
| 119 | DiffHeader(Combined(MergeParents::Unknown, InMergeConflict::No)) => { |
| 120 | // https://git-scm.com/docs/git-diff#_combined_diff_format |
| 121 | let n_parents = self.line.chars().take_while(|c| c == &'@').count() - 1; |
| 122 | Combined(MergeParents::Number(n_parents), InMergeConflict::No) |
| 123 | } |
| 124 | DiffHeader(diff_type) |
| 125 | | HunkMinus(diff_type, _) |
| 126 | | HunkZero(diff_type, _) |
| 127 | | HunkPlus(diff_type, _) => diff_type.clone(), |
| 128 | _ => Unified, |
| 129 | }; |
| 130 | |
| 131 | if self.minus_line_counter.must_count() { |
| 132 | if let &[(_, minus_lines), (_, _plus_lines), ..] = |
| 133 | parsed_hunk_header.line_numbers_and_hunk_lengths.as_slice() |
| 134 | { |
| 135 | self.minus_line_counter = AmbiguousDiffMinusCounter::count_from(minus_lines); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | self.state = HunkHeader( |
| 140 | diff_type, |
| 141 | parsed_hunk_header, |
| 142 | self.line.clone(), |
| 143 | self.raw_line.clone(), |
| 144 | ); |
| 145 | handled_line = true; |
| 146 | } |
| 147 | Ok(handled_line) |
| 148 | } |
| 149 | |
| 150 | /// Emit the hunk header, with any requested decoration. |
| 151 | pub fn emit_hunk_header_line( |
| 152 | &mut self, |
| 153 | parsed_hunk_header: &ParsedHunkHeader, |
| 154 | line: &str, |
| 155 | raw_line: &str, |
| 156 | ) -> std::io::Result<bool> { |
| 157 | self.painter.paint_buffered_minus_and_plus_lines(); |
| 158 | self.painter.set_highlighter(); |
| 159 | self.painter.emit()?; |
| 160 | |
| 161 | let ParsedHunkHeader { |
| 162 | code_fragment, |
| 163 | line_numbers_and_hunk_lengths, |
| 164 | } = parsed_hunk_header; |
| 165 | |
| 166 | if self.config.line_numbers { |
| 167 | self.painter |
| 168 | .line_numbers_data |
| 169 | .as_mut() |
| 170 | .unwrap() |
| 171 | .initialize_hunk(line_numbers_and_hunk_lengths, self.plus_file.to_string()); |
| 172 | } |
| 173 | |
| 174 | if self.config.hunk_header_style.is_raw { |
| 175 | write_hunk_header_raw(&mut self.painter, line, raw_line, self.config)?; |
| 176 | } else if self.config.hunk_header_style.is_omitted { |
| 177 | writeln!(self.painter.writer)?; |
| 178 | } else { |
| 179 | // Add a blank line below the hunk-header-line for readability, unless |
| 180 | // color_only mode is active. |
| 181 | if !self.config.color_only { |
| 182 | writeln!(self.painter.writer)?; |
| 183 | } |
| 184 | |
| 185 | write_line_of_code_with_optional_path_and_line_number( |
| 186 | code_fragment, |
| 187 | line_numbers_and_hunk_lengths, |
| 188 | None, |
| 189 | &mut self.painter, |
| 190 | line, |
| 191 | if self.plus_file == "/dev/null" { |
| 192 | &self.minus_file |
| 193 | } else { |
| 194 | &self.plus_file |
| 195 | }, |
| 196 | self.config.hunk_header_style.decoration_style, |
| 197 | &self.config.hunk_header_file_style, |
| 198 | &self.config.hunk_header_line_number_style, |
| 199 | &self.config.hunk_header_style_include_file_path, |
| 200 | &self.config.hunk_header_style_include_line_number, |
| 201 | &HunkHeaderIncludeHunkLabel::Yes, |
| 202 | &self.config.hunk_header_style_include_code_fragment, |
| 203 | ":", |
| 204 | self.config, |
| 205 | )?; |
| 206 | }; |
| 207 | self.painter.set_highlighter(); |
| 208 | Ok(true) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | lazy_static! { |
| 213 | static ref HUNK_HEADER_REGEX: Regex = Regex::new(r"@+ ([^@]+)@+(.*\s?)").unwrap(); |
| 214 | } |
| 215 | |
| 216 | // Parse unified diff hunk header format. See |
| 217 | // https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html |
| 218 | // https://www.artima.com/weblogs/viewpost.jsp?thread=164293 |
| 219 | lazy_static! { |
| 220 | static ref HUNK_HEADER_FILE_COORDINATE_REGEX: Regex = Regex::new( |
| 221 | r"(?x) |
| 222 | [-+] |
| 223 | (\d+) # 1. Hunk start line number |
| 224 | (?: # Start optional hunk length section (non-capturing) |
| 225 | , # Literal comma |
| 226 | (\d+) # 2. Optional hunk length (defaults to 1) |
| 227 | )?" |
| 228 | ) |
| 229 | .unwrap(); |
| 230 | } |
| 231 | |
| 232 | /// Given input like |
| 233 | /// "@@ -74,15 +74,14 @@ pub fn delta(" |
| 234 | /// Return " pub fn delta(" and a vector of (line_number, hunk_length) tuples. |
| 235 | fn parse_hunk_header(line: &str) -> Option<ParsedHunkHeader> { |
| 236 | if let Some(caps) = HUNK_HEADER_REGEX.captures(line) { |
| 237 | let file_coordinates = &caps[1]; |
| 238 | let line_numbers_and_hunk_lengths: Vec<(usize, usize)> = HUNK_HEADER_FILE_COORDINATE_REGEX |
| 239 | .captures_iter(file_coordinates) |
| 240 | .map(|caps| { |
| 241 | ( |
| 242 | caps[1].parse::<usize>().unwrap(), |
| 243 | caps.get(2) |
| 244 | .map(|m| m.as_str()) |
| 245 | // Per the specs linked above, if the hunk length is absent then it is 1. |
| 246 | .unwrap_or("1") |
| 247 | .parse::<usize>() |
| 248 | .unwrap(), |
| 249 | ) |
| 250 | }) |
| 251 | .collect(); |
| 252 | if line_numbers_and_hunk_lengths.is_empty() { |
| 253 | None |
| 254 | } else { |
| 255 | let code_fragment = caps[2].to_string(); |
| 256 | Some(ParsedHunkHeader { |
| 257 | code_fragment, |
| 258 | line_numbers_and_hunk_lengths, |
| 259 | }) |
| 260 | } |
| 261 | } else { |
| 262 | None |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | fn write_hunk_header_raw( |
| 267 | painter: &mut Painter, |
| 268 | line: &str, |
| 269 | raw_line: &str, |
| 270 | config: &Config, |
| 271 | ) -> std::io::Result<()> { |
| 272 | let (mut draw_fn, pad, decoration_ansi_term_style) = |
| 273 | draw::get_draw_function(config.hunk_header_style.decoration_style); |
| 274 | if config.hunk_header_style.decoration_style != DecorationStyle::NoDecoration { |
| 275 | writeln!(painter.writer)?; |
| 276 | } |
| 277 | draw_fn( |
| 278 | painter.writer, |
| 279 | &format!("{}{}", line, if pad { " " } else { "" }), |
| 280 | &format!("{}{}", raw_line, if pad { " " } else { "" }), |
| 281 | "", |
| 282 | &config.decorations_width, |
| 283 | config.hunk_header_style, |
| 284 | decoration_ansi_term_style, |
| 285 | )?; |
| 286 | Ok(()) |
| 287 | } |
| 288 | |
| 289 | #[allow(clippy::too_many_arguments)] |
| 290 | pub fn write_line_of_code_with_optional_path_and_line_number( |
| 291 | code_fragment: &str, |
| 292 | line_numbers_and_hunk_lengths: &[(usize, usize)], |
| 293 | style_sections: Option<StyleSectionSpecifier>, |
| 294 | painter: &mut Painter, |
| 295 | line: &str, |
| 296 | plus_file: &str, |
| 297 | decoration_style: DecorationStyle, |
| 298 | file_style: &Style, |
| 299 | line_number_style: &Style, |
| 300 | include_file_path: &HunkHeaderIncludeFilePath, |
| 301 | include_line_number: &HunkHeaderIncludeLineNumber, |
| 302 | include_hunk_label: &HunkHeaderIncludeHunkLabel, |
| 303 | include_code_fragment: &HunkHeaderIncludeCodeFragment, |
| 304 | file_path_separator: &str, |
| 305 | config: &Config, |
| 306 | ) -> std::io::Result<()> { |
| 307 | let (mut draw_fn, _, decoration_ansi_term_style) = draw::get_draw_function(decoration_style); |
| 308 | let line = match (config.color_only, include_code_fragment) { |
| 309 | (true, _) => line.to_string(), |
| 310 | // When called from a ripgrep json context: No " " added, as this would prevent |
| 311 | // identifying that a newline already exists, and so another would be added. |
| 312 | (_, HunkHeaderIncludeCodeFragment::YesNoSpace) => code_fragment.to_string(), |
| 313 | (_, HunkHeaderIncludeCodeFragment::Yes) if !code_fragment.is_empty() => { |
| 314 | format!("{code_fragment} ") |
| 315 | } |
| 316 | _ => "".to_string(), |
| 317 | }; |
| 318 | |
| 319 | let plus_line_number = line_numbers_and_hunk_lengths[line_numbers_and_hunk_lengths.len() - 1].0; |
| 320 | let file_with_line_number = paint_file_path_with_line_number( |
| 321 | Some(plus_line_number), |
| 322 | plus_file, |
| 323 | file_style, |
| 324 | line_number_style, |
| 325 | include_file_path, |
| 326 | include_line_number, |
| 327 | file_path_separator, |
| 328 | config, |
| 329 | ); |
| 330 | |
| 331 | if !line.is_empty() || !file_with_line_number.is_empty() { |
| 332 | write_to_output_buffer( |
| 333 | &file_with_line_number, |
| 334 | file_path_separator, |
| 335 | line, |
| 336 | style_sections, |
| 337 | include_hunk_label, |
| 338 | painter, |
| 339 | config, |
| 340 | ); |
| 341 | draw_fn( |
| 342 | painter.writer, |
| 343 | &painter.output_buffer, |
| 344 | &painter.output_buffer, |
| 345 | "", |
| 346 | &config.decorations_width, |
| 347 | config.null_style, |
| 348 | decoration_ansi_term_style, |
| 349 | )?; |
| 350 | painter.output_buffer.clear(); |
| 351 | } |
| 352 | |
| 353 | Ok(()) |
| 354 | } |
| 355 | |
| 356 | #[allow(clippy::too_many_arguments)] |
| 357 | fn paint_file_path_with_line_number( |
| 358 | line_number: Option<usize>, |
| 359 | plus_file: &str, |
| 360 | file_style: &Style, |
| 361 | line_number_style: &Style, |
| 362 | include_file_path: &HunkHeaderIncludeFilePath, |
| 363 | include_line_number: &HunkHeaderIncludeLineNumber, |
| 364 | separator: &str, |
| 365 | config: &Config, |
| 366 | ) -> String { |
| 367 | let file_style = match include_file_path { |
| 368 | HunkHeaderIncludeFilePath::Yes => Some(*file_style), |
| 369 | HunkHeaderIncludeFilePath::No => None, |
| 370 | }; |
| 371 | let line_number_style = if matches!(include_line_number, HunkHeaderIncludeLineNumber::Yes) |
| 372 | && line_number.is_some() |
| 373 | && !config.hunk_header_style.is_raw |
| 374 | && !config.color_only |
| 375 | { |
| 376 | Some(*line_number_style) |
| 377 | } else { |
| 378 | None |
| 379 | }; |
| 380 | |
| 381 | paint::paint_file_path_with_line_number( |
| 382 | line_number, |
| 383 | plus_file, |
| 384 | false, |
| 385 | separator, |
| 386 | false, |
| 387 | file_style, |
| 388 | line_number_style, |
| 389 | config, |
| 390 | ) |
| 391 | } |
| 392 | |
| 393 | fn write_to_output_buffer( |
| 394 | file_with_line_number: &str, |
| 395 | file_path_separator: &str, |
| 396 | line: String, |
| 397 | style_sections: Option<StyleSectionSpecifier>, |
| 398 | include_hunk_label: &HunkHeaderIncludeHunkLabel, |
| 399 | painter: &mut Painter, |
| 400 | config: &Config, |
| 401 | ) { |
| 402 | if matches!(include_hunk_label, HunkHeaderIncludeHunkLabel::Yes) |
| 403 | && !config.hunk_label.is_empty() |
| 404 | { |
| 405 | let _ = write!( |
| 406 | &mut painter.output_buffer, |
| 407 | "{} ", |
| 408 | config.hunk_header_file_style.paint(&config.hunk_label) |
| 409 | ); |
| 410 | } |
| 411 | if !file_with_line_number.is_empty() { |
| 412 | // The code fragment in "line" adds whitespace, but if only a line number is printed |
| 413 | // then the trailing space must be added. |
| 414 | let space = if line.is_empty() { " " } else { "" }; |
| 415 | let _ = write!( |
| 416 | &mut painter.output_buffer, |
| 417 | "{file_with_line_number}{file_path_separator}{space}", |
| 418 | ); |
| 419 | } |
| 420 | if !line.is_empty() { |
| 421 | painter.syntax_highlight_and_paint_line( |
| 422 | &line, |
| 423 | style_sections.unwrap_or(StyleSectionSpecifier::Style(config.hunk_header_style)), |
| 424 | delta::State::HunkHeader( |
| 425 | DiffType::Unified, |
| 426 | ParsedHunkHeader::default(), |
| 427 | "".to_owned(), |
| 428 | "".to_owned(), |
| 429 | ), |
| 430 | BgShouldFill::No, |
| 431 | ); |
| 432 | painter.output_buffer.pop(); // trim newline |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | #[cfg(test)] |
| 437 | pub mod tests { |
| 438 | use super::*; |
| 439 | use crate::ansi::strip_ansi_codes; |
| 440 | use crate::tests::integration_test_utils; |
| 441 | |
| 442 | #[test] |
| 443 | fn test_parse_hunk_header() { |
| 444 | let ParsedHunkHeader { |
| 445 | code_fragment, |
| 446 | line_numbers_and_hunk_lengths, |
| 447 | } = parse_hunk_header("@@ -74,15 +75,14 @@ pub fn delta(\n").unwrap(); |
| 448 | assert_eq!(code_fragment, " pub fn delta(\n"); |
| 449 | assert_eq!(line_numbers_and_hunk_lengths[0], (74, 15),); |
| 450 | assert_eq!(line_numbers_and_hunk_lengths[1], (75, 14),); |
| 451 | } |
| 452 | |
| 453 | #[test] |
| 454 | fn test_parse_hunk_header_with_omitted_hunk_lengths() { |
| 455 | let ParsedHunkHeader { |
| 456 | code_fragment, |
| 457 | line_numbers_and_hunk_lengths, |
| 458 | } = parse_hunk_header("@@ -74 +75,2 @@ pub fn delta(\n").unwrap(); |
| 459 | assert_eq!(code_fragment, " pub fn delta(\n"); |
| 460 | assert_eq!(line_numbers_and_hunk_lengths[0], (74, 1),); |
| 461 | assert_eq!(line_numbers_and_hunk_lengths[1], (75, 2),); |
| 462 | } |
| 463 | |
| 464 | #[test] |
| 465 | fn test_parse_hunk_header_with_no_hunk_lengths() { |
| 466 | let result = parse_hunk_header("@@ @@\n"); |
| 467 | assert_eq!(result, None); |
| 468 | } |
| 469 | |
| 470 | #[test] |
| 471 | fn test_parse_hunk_header_added_file() { |
| 472 | let ParsedHunkHeader { |
| 473 | code_fragment, |
| 474 | line_numbers_and_hunk_lengths, |
| 475 | } = parse_hunk_header("@@ -1,22 +0,0 @@").unwrap(); |
| 476 | assert_eq!(code_fragment, "",); |
| 477 | assert_eq!(line_numbers_and_hunk_lengths[0], (1, 22),); |
| 478 | assert_eq!(line_numbers_and_hunk_lengths[1], (0, 0),); |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn test_parse_hunk_header_deleted_file() { |
| 483 | let ParsedHunkHeader { |
| 484 | code_fragment, |
| 485 | line_numbers_and_hunk_lengths, |
| 486 | } = parse_hunk_header("@@ -0,0 +1,3 @@").unwrap(); |
| 487 | assert_eq!(code_fragment, "",); |
| 488 | assert_eq!(line_numbers_and_hunk_lengths[0], (0, 0),); |
| 489 | assert_eq!(line_numbers_and_hunk_lengths[1], (1, 3),); |
| 490 | } |
| 491 | |
| 492 | #[test] |
| 493 | fn test_parse_hunk_header_merge() { |
| 494 | let ParsedHunkHeader { |
| 495 | code_fragment, |
| 496 | line_numbers_and_hunk_lengths, |
| 497 | } = parse_hunk_header("@@@ -293,11 -358,15 +358,16 @@@ dependencies =").unwrap(); |
| 498 | assert_eq!(code_fragment, " dependencies ="); |
| 499 | assert_eq!(line_numbers_and_hunk_lengths[0], (293, 11),); |
| 500 | assert_eq!(line_numbers_and_hunk_lengths[1], (358, 15),); |
| 501 | assert_eq!(line_numbers_and_hunk_lengths[2], (358, 16),); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn test_parse_hunk_header_cthulhu() { |
| 506 | let ParsedHunkHeader { |
| 507 | code_fragment, |
| 508 | line_numbers_and_hunk_lengths, |
| 509 | } = parse_hunk_header("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -444,17 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 -446,6 +444,17 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ int snd_soc_jack_add_gpios(struct snd_s").unwrap(); |
| 510 | assert_eq!(code_fragment, " int snd_soc_jack_add_gpios(struct snd_s"); |
| 511 | assert_eq!(line_numbers_and_hunk_lengths[0], (446, 6),); |
| 512 | assert_eq!(line_numbers_and_hunk_lengths[1], (446, 6),); |
| 513 | assert_eq!(line_numbers_and_hunk_lengths[2], (446, 6),); |
| 514 | assert_eq!(line_numbers_and_hunk_lengths[65], (446, 6),); |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn test_paint_file_path_with_line_number_default() { |
| 519 | // hunk-header-style (by default) includes 'line-number' but not 'file'. |
| 520 | // This test confirms that `paint_file_path_with_line_number` returns a painted line number. |
| 521 | let config = integration_test_utils::make_config_from_args(&[]); |
| 522 | |
| 523 | let result = paint_file_path_with_line_number( |
| 524 | Some(3), |
| 525 | "some-file", |
| 526 | &config.hunk_header_style, |
| 527 | &config.hunk_header_line_number_style, |
| 528 | &config.hunk_header_style_include_file_path, |
| 529 | &config.hunk_header_style_include_line_number, |
| 530 | ":", |
| 531 | &config, |
| 532 | ); |
| 533 | |
| 534 | assert_eq!(result, "\u{1b}[34m3\u{1b}[0m"); |
| 535 | } |
| 536 | |
| 537 | #[test] |
| 538 | fn test_paint_file_path_with_line_number_hyperlinks() { |
| 539 | use std::{iter::FromIterator, path::PathBuf}; |
| 540 | |
| 541 | use crate::utils; |
| 542 | |
| 543 | // hunk-header-style (by default) includes 'line-number' but not 'file'. |
| 544 | // Normally, `paint_file_path_with_line_number` would return a painted line number. |
| 545 | // But in this test hyperlinks are activated, and the test ensures that delta.__workdir__ is |
| 546 | // present in git_config_entries. |
| 547 | // This test confirms that, under those circumstances, `paint_file_path_with_line_number` |
| 548 | // returns a hyperlinked file path with line number. |
| 549 | |
| 550 | let config = integration_test_utils::make_config_from_args(&["--features", "hyperlinks"]); |
| 551 | let relative_path = PathBuf::from_iter(["some-dir", "some-file"]); |
| 552 | |
| 553 | let result = paint_file_path_with_line_number( |
| 554 | Some(3), |
| 555 | &relative_path.to_string_lossy(), |
| 556 | &config.hunk_header_style, |
| 557 | &config.hunk_header_line_number_style, |
| 558 | &config.hunk_header_style_include_file_path, |
| 559 | &config.hunk_header_style_include_line_number, |
| 560 | ":", |
| 561 | &config, |
| 562 | ); |
| 563 | |
| 564 | assert_eq!( |
| 565 | result, |
| 566 | format!( |
| 567 | "\u{1b}]8;;file://{}\u{1b}\\\u{1b}[34m3\u{1b}[0m\u{1b}]8;;\u{1b}\\", |
| 568 | utils::path::fake_delta_cwd_for_tests() |
| 569 | .join(relative_path) |
| 570 | .to_string_lossy() |
| 571 | ) |
| 572 | ); |
| 573 | } |
| 574 | |
| 575 | #[test] |
| 576 | fn test_paint_file_path_with_line_number_empty() { |
| 577 | // hunk-header-style includes neither 'file' nor 'line-number'. |
| 578 | // This causes `paint_file_path_with_line_number` to return empty string. |
| 579 | let config = integration_test_utils::make_config_from_args(&[ |
| 580 | "--hunk-header-style", |
| 581 | "syntax bold", |
| 582 | "--hunk-header-decoration-style", |
| 583 | "omit", |
| 584 | ]); |
| 585 | |
| 586 | let result = paint_file_path_with_line_number( |
| 587 | Some(3), |
| 588 | "some-file", |
| 589 | &config.hunk_header_style, |
| 590 | &config.hunk_header_line_number_style, |
| 591 | &config.hunk_header_style_include_file_path, |
| 592 | &config.hunk_header_style_include_line_number, |
| 593 | ":", |
| 594 | &config, |
| 595 | ); |
| 596 | |
| 597 | // result is |
| 598 | // "\u{1b}[1msome-file\u{1b}[0m:\u{1b}[34m3\u{1b}[0m" |
| 599 | assert_eq!(result, ""); |
| 600 | } |
| 601 | |
| 602 | #[test] |
| 603 | fn test_paint_file_path_with_line_number_empty_hyperlinks() { |
| 604 | // hunk-header-style includes neither 'file' nor 'line-number'. |
| 605 | // This causes `paint_file_path_with_line_number` to return empty string. |
| 606 | // This test confirms that this remains true even when we are requesting hyperlinks. |
| 607 | |
| 608 | let config = integration_test_utils::make_config_from_args(&[ |
| 609 | "--hunk-header-style", |
| 610 | "syntax bold", |
| 611 | "--hunk-header-decoration-style", |
| 612 | "omit", |
| 613 | "--features", |
| 614 | "hyperlinks", |
| 615 | ]); |
| 616 | |
| 617 | let result = paint_file_path_with_line_number( |
| 618 | Some(3), |
| 619 | "some-file", |
| 620 | &config.hunk_header_style, |
| 621 | &config.hunk_header_line_number_style, |
| 622 | &config.hunk_header_style_include_file_path, |
| 623 | &config.hunk_header_style_include_line_number, |
| 624 | ":", |
| 625 | &config, |
| 626 | ); |
| 627 | |
| 628 | assert_eq!(result, ""); |
| 629 | } |
| 630 | |
| 631 | #[test] |
| 632 | fn test_paint_file_path_with_line_number_empty_navigate() { |
| 633 | let config = integration_test_utils::make_config_from_args(&[ |
| 634 | "--hunk-header-style", |
| 635 | "syntax bold", |
| 636 | "--hunk-header-decoration-style", |
| 637 | "omit", |
| 638 | "--navigate", |
| 639 | ]); |
| 640 | |
| 641 | let result = paint_file_path_with_line_number( |
| 642 | Some(3), |
| 643 | "δ some-file", |
| 644 | &config.hunk_header_style, |
| 645 | &config.hunk_header_line_number_style, |
| 646 | &config.hunk_header_style_include_file_path, |
| 647 | &config.hunk_header_style_include_line_number, |
| 648 | ":", |
| 649 | &config, |
| 650 | ); |
| 651 | |
| 652 | // result is |
| 653 | // "\u{1b}[1mδ some-file\u{1b}[0m:\u{1b}[34m3\u{1b}[0m" |
| 654 | assert_eq!(result, ""); |
| 655 | } |
| 656 | |
| 657 | #[test] |
| 658 | fn test_not_a_hunk_header_is_handled_gracefully() { |
| 659 | let config = integration_test_utils::make_config_from_args(&[]); |
| 660 | let output = |
| 661 | integration_test_utils::run_delta(GIT_LOG_OUTPUT_WITH_NOT_A_HUNK_HEADER, &config); |
| 662 | let output = strip_ansi_codes(&output); |
| 663 | assert!(output.contains("@@@2021-12-05")); |
| 664 | } |
| 665 | |
| 666 | const GIT_LOG_OUTPUT_WITH_NOT_A_HUNK_HEADER: &str = "\ |
| 667 | @@@2021-12-05 |
| 668 | |
| 669 | src/config.rs | 2 +- |
| 670 | src/delta.rs | 3 ++- |
| 671 | src/handlers/hunk.rs | 12 ++++++------ |
| 672 | src/handlers/hunk_header.rs | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ |
| 673 | src/handlers/merge_conflict.rs | 2 +- |
| 674 | src/handlers/submodule.rs | 4 ++-- |
| 675 | src/paint.rs | 2 +- |
| 676 | 7 files changed, 90 insertions(+), 54 deletions(-) |
| 677 | "; |
| 678 | } |