diff --git a/src/conflicts_highlighter.rs b/src/conflicts_highlighter.rs index 877965a..0753880 100644 --- a/src/conflicts_highlighter.rs +++ b/src/conflicts_highlighter.rs @@ -173,6 +173,7 @@ impl ConflictsHighlighter { /// vs C1 or vs C2. /// * In section C2, we highlight additions compared to base fn render_diff3(&self, thread_pool: &ThreadPool) -> StringFuture { + assert!(self.base.is_some()); let c1_header = self.c1_header.clone(); let c1 = self.c1.clone(); let base_header = self.base_header.clone(); @@ -183,12 +184,16 @@ impl ConflictsHighlighter { return StringFuture::from_function( move || { + let base_or_newline = if base.is_empty() { "\n" } else { &base }; + + let c1_or_newline = if c1.is_empty() { "\n" } else { &c1 }; let (base_vs_c1_tokens, c1_tokens, _, _) = - refiner::to_highlighted_tokens(&base, &c1); + refiner::to_highlighted_tokens(base_or_newline, c1_or_newline); let highlighted_c1 = token_collector::render(&LINE_STYLE_NEW, "", &c1_tokens); + let c2_or_newline = if c2.is_empty() { "\n" } else { &c2 }; let (base_vs_c2_tokens, c2_tokens, _, _) = - refiner::to_highlighted_tokens(&base, &c2); + refiner::to_highlighted_tokens(base_or_newline, c2_or_newline); let highlighted_c2 = token_collector::render(&LINE_STYLE_NEW, "", &c2_tokens); assert_eq!(base_vs_c1_tokens.len(), base_vs_c2_tokens.len()); @@ -208,15 +213,21 @@ impl ConflictsHighlighter { let mut rendered = String::new(); rendered.push_str(&c1_header); rendered.push('\n'); - rendered.push_str(&highlighted_c1); + if !c1.is_empty() { + rendered.push_str(&highlighted_c1); + } rendered.push_str(&base_header); rendered.push('\n'); - rendered.push_str(&highlighted_base); + if !base.is_empty() { + rendered.push_str(&highlighted_base); + } rendered.push_str(&c2_header); rendered.push('\n'); - rendered.push_str(&highlighted_c2); + if !c2.is_empty() { + rendered.push_str(&highlighted_c2); + } rendered.push_str(&footer); rendered.push('\n'); diff --git a/testdata/conflict-markers-diff3-removed.txt b/testdata/conflict-markers-diff3-removed.txt new file mode 100644 index 0000000..c4b2c23 --- /dev/null +++ b/testdata/conflict-markers-diff3-removed.txt @@ -0,0 +1,8 @@ +This is an example of git conflict markers. + +<<<<<<< HEAD +This line is changed on the main branch. +||||||| 07ffb9b +This line is from the initial commit on the main branch. +======= +>>>>>>> branch diff --git a/testdata/conflict-markers-diff3-removed.txt.riff-output b/testdata/conflict-markers-diff3-removed.txt.riff-output new file mode 100644 index 0000000..d507cb0 --- /dev/null +++ b/testdata/conflict-markers-diff3-removed.txt.riff-output @@ -0,0 +1,8 @@ +This is an example of git conflict markers. + +<<<<<<< HEAD +This line is changed on the main branch. +||||||| 07ffb9b +This line is from the initial commit on the main branch. +======= +>>>>>>> branch