summaryrefslogtreecommitdiffstats
path: root/src/paint.rs
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-11-29 20:54:48 -0500
committerDan Davison <dandavison7@gmail.com>2021-12-05 11:25:05 -0500
commit6745f42ddadeccfa30628c70d39b8f9abbff35f0 (patch)
tree19cdd504eba5632d6bb0d6a72559b1ee1774c057 /src/paint.rs
parente7294060ef3b8af0d2307eea4359123232f85646 (diff)
Display merge conflicts
Diffstat (limited to 'src/paint.rs')
-rw-r--r--src/paint.rs63
1 files changed, 34 insertions, 29 deletions
diff --git a/src/paint.rs b/src/paint.rs
index b14cdd86..9c69203c 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -9,12 +9,13 @@ use syntect::parsing::{SyntaxReference, SyntaxSet};
use unicode_segmentation::UnicodeSegmentation;
use crate::config::{self, delta_unreachable, Config};
-use crate::delta::State;
+use crate::delta::{DiffType, MergeParents, State};
use crate::edits;
use crate::features::hyperlinks;
use crate::features::line_numbers::{self, LineNumbersData};
use crate::features::side_by_side::ansifill;
use crate::features::side_by_side::{self, PanelSide};
+use crate::handlers::merge_conflict;
use crate::minusplus::*;
use crate::paint::superimpose_style_sections::superimpose_style_sections;
use crate::style::Style;
@@ -34,6 +35,8 @@ pub struct Painter<'p> {
// In side-by-side mode it is always Some (but possibly an empty one), even
// if config.line_numbers is false. See `UseFullPanelWidth` as well.
pub line_numbers_data: Option<line_numbers::LineNumbersData<'p>>,
+ pub merge_conflict_lines: merge_conflict::MergeConflictLines,
+ pub merge_conflict_commit_names: merge_conflict::MergeConflictCommitNames,
}
// How the background of a line is filled up to the end
@@ -95,6 +98,8 @@ impl<'p> Painter<'p> {
writer,
config,
line_numbers_data,
+ merge_conflict_lines: merge_conflict::MergeConflictLines::new(),
+ merge_conflict_commit_names: merge_conflict::MergeConflictCommitNames::new(),
}
}
@@ -123,18 +128,12 @@ impl<'p> Painter<'p> {
// Terminating with newline character is necessary for many of the sublime syntax definitions to
// highlight correctly.
// See https://docs.rs/syntect/3.2.0/syntect/parsing/struct.SyntaxSetBuilder.html#method.add_from_folder
- pub fn prepare(&self, line: &str, prefix: Option<&str>) -> String {
+ pub fn prepare(&self, line: &str, prefix_length: usize) -> String {
if !line.is_empty() {
- let mut line = line.graphemes(true);
-
- // The initial columns contain -/+/space characters, added by git. Remove them now so
- // they are not present during syntax highlighting or wrapping. If
- // --keep-plus-minus-markers is in effect this prefix is re-inserted in
- // Painter::paint_line.
- let prefix_length = prefix.map(|s| s.len()).unwrap_or(1);
- for _ in 0..prefix_length {
- line.next();
- }
+ // The prefix contains -/+/space characters, added by git. We removes them now so they
+ // are not present during syntax highlighting or wrapping. If --keep-plus-minus-markers
+ // is in effect the prefix is re-inserted in Painter::paint_line.
+ let line = line.graphemes(true).skip(prefix_length);
format!("{}\n", self.expand_tabs(line))
} else {
"\n".to_string()
@@ -143,13 +142,10 @@ impl<'p> Painter<'p> {
// Remove initial -/+ characters, expand tabs as spaces, retaining ANSI sequences. Terminate with
// newline character.
- pub fn prepare_raw_line(&self, raw_line: &str, prefix: Option<&str>) -> String {
+ pub fn prepare_raw_line(&self, raw_line: &str, prefix_length: usize) -> String {
format!(
"{}\n",
- ansi::ansi_preserving_slice(
- &self.expand_tabs(raw_line.graphemes(true)),
- prefix.map(|s| s.len()).unwrap_or(1)
- ),
+ ansi::ansi_preserving_slice(&self.expand_tabs(raw_line.graphemes(true)), prefix_length),
)
}
@@ -180,9 +176,9 @@ impl<'p> Painter<'p> {
self.plus_lines.clear();
}
- pub fn paint_zero_line(&mut self, line: &str, prefix: Option<String>) {
- let line = self.prepare(line, prefix.as_deref());
- let state = State::HunkZero(prefix);
+ pub fn paint_zero_line(&mut self, line: &str, diff_type: DiffType) {
+ let line = self.prepare(line, diff_type.n_parents());
+ let state = State::HunkZero(diff_type);
let lines = vec![(line, state.clone())];
let syntax_style_sections =
get_syntax_style_sections_for_lines(&lines, self.highlighter.as_mut(), self.config);
@@ -498,6 +494,7 @@ impl<'p> Painter<'p> {
| State::HunkMinusWrapped
| State::HunkZeroWrapped
| State::HunkPlusWrapped
+ | State::MergeConflict(_, _)
| State::SubmoduleLog
| State::SubmoduleShort(_) => {
panic!(
@@ -722,18 +719,26 @@ fn get_diff_style_sections<'a>(
}
fn painted_prefix(state: State, config: &config::Config) -> Option<ANSIString> {
+ use DiffType::*;
+ use State::*;
match (state, config.keep_plus_minus_markers) {
- // If there is Some(prefix) then this is a combined diff. In this case we do not honor
- // keep_plus_minus_markers -- i.e. always emit the prefix -- because there is currently no
- // way to distinguish, say, a '+ ' line from a ' +' line, by styles alone.
- (State::HunkMinus(Some(prefix), _), _) => Some(config.minus_style.paint(prefix)),
- (State::HunkZero(Some(prefix)), _) => Some(config.zero_style.paint(prefix)),
- (State::HunkPlus(Some(prefix), _), _) => Some(config.plus_style.paint(prefix)),
+ // For a combined diff we do not honor keep_plus_minus_markers -- i.e. always emit the
+ // prefix -- because there is currently no way to distinguish, say, a '+ ' line from a ' +'
+ // line, by styles alone.
+ (HunkMinus(Combined(MergeParents::Prefix(prefix)), _), _) => {
+ Some(config.minus_style.paint(prefix))
+ }
+ (HunkZero(Combined(MergeParents::Prefix(prefix))), _) => {
+ Some(config.zero_style.paint(prefix))
+ }
+ (HunkPlus(Combined(MergeParents::Prefix(prefix)), _), _) => {
+ Some(config.plus_style.paint(prefix))
+ }
// But if there is no prefix we honor keep_plus_minus_markers.
(_, false) => None,
- (State::HunkMinus(None, _), true) => Some(config.minus_style.paint("-".to_string())),
- (State::HunkZero(None), true) => Some(config.zero_style.paint(" ".to_string())),
- (State::HunkPlus(None, _), true) => Some(config.plus_style.paint("+".to_string())),
+ (HunkMinus(Unified, _), true) => Some(config.minus_style.paint("-".to_string())),
+ (HunkZero(Unified), true) => Some(config.zero_style.paint(" ".to_string())),
+ (HunkPlus(Unified, _), true) => Some(config.plus_style.paint("+".to_string())),
_ => None,
}
}