Skip to content

Commit

Permalink
Higlight conflict markers
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Oct 10, 2024
1 parent 78f5a86 commit ae19ea5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/conflicts_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::vec;

use threadpool::ThreadPool;

use crate::constants::{GREEN, NORMAL};
use crate::constants::{GREEN, INVERSE_VIDEO, NORMAL};
use crate::lines_highlighter::{LineAcceptance, LinesHighlighter, Response};
use crate::string_future::StringFuture;
use crate::token_collector::{Style, LINE_STYLE_OLD};
use crate::token_collector::{LINE_STYLE_CONFLICT_BASE, LINE_STYLE_NEW};
use crate::token_collector::{Style, LINE_STYLE_CONFLICT_OLD};
use crate::token_collector::{LINE_STYLE_CONFLICT_BASE, LINE_STYLE_CONFLICT_NEW};
use crate::{refiner, token_collector};

const CONFLICTS_HEADER1: &str = "<<<<<<<";
Expand Down Expand Up @@ -172,7 +172,7 @@ impl ConflictsHighlighter {
}

let (header_prefix, c1_prefix, c2_prefix, reset) = if self.c1_header.starts_with("++") {
(GREEN, " +", "+ ", NORMAL)
(INVERSE_VIDEO, " +", "+ ", NORMAL)
} else {
("", "", "", "")
};
Expand All @@ -191,13 +191,13 @@ impl ConflictsHighlighter {
refiner::to_highlighted_tokens(c1_or_newline, c2_or_newline, true);

let c1_style = if base_header.is_empty() {
LINE_STYLE_OLD
LINE_STYLE_CONFLICT_OLD
} else {
LINE_STYLE_NEW
LINE_STYLE_CONFLICT_NEW
};
let highlighted_c1 = token_collector::render(&c1_style, c1_prefix, &c1_tokens);
let highlighted_c2 =
token_collector::render(&LINE_STYLE_NEW, c2_prefix, &c2_tokens);
token_collector::render(&LINE_STYLE_CONFLICT_NEW, c2_prefix, &c2_tokens);

let mut rendered = String::new();
rendered.push_str(header_prefix);
Expand Down Expand Up @@ -242,9 +242,9 @@ impl ConflictsHighlighter {
fn render_diff3(&self, thread_pool: &ThreadPool) -> StringFuture {
let (header_prefix, c1_prefix, base_prefix, c2_prefix, reset) =
if self.c1_header.starts_with("++") {
(GREEN, " +", "++", "+ ", NORMAL)
(INVERSE_VIDEO, " +", "++", "+ ", NORMAL)
} else {
("", "", "", "", "")
(INVERSE_VIDEO, "", "", "", "")
};

assert!(!self.base.is_empty());
Expand All @@ -270,7 +270,7 @@ impl ConflictsHighlighter {
});
}
let highlighted_c1 =
token_collector::render(&LINE_STYLE_NEW, c1_prefix, &c1_tokens);
token_collector::render(&LINE_STYLE_CONFLICT_NEW, c1_prefix, &c1_tokens);

let c2_or_newline = if c2.is_empty() { "\n" } else { &c2 };
let (mut base_vs_c2_tokens, c2_tokens, _, _) =
Expand All @@ -282,7 +282,7 @@ impl ConflictsHighlighter {
});
}
let highlighted_c2 =
token_collector::render(&LINE_STYLE_NEW, c2_prefix, &c2_tokens);
token_collector::render(&LINE_STYLE_CONFLICT_NEW, c2_prefix, &c2_tokens);

assert_eq!(base_vs_c1_tokens.len(), base_vs_c2_tokens.len());

Expand Down
42 changes: 41 additions & 1 deletion src/token_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,29 @@ pub(crate) const LINE_STYLE_ADDS_ONLY: LineStyle = {
pub(crate) const LINE_STYLE_CONFLICT_BASE: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Default,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Green,
color: Red,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Red,
},
}
};

pub(crate) const LINE_STYLE_CONFLICT_OLD: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Default,
},
plain_style: AnsiStyle {
inverse: false,
Expand All @@ -134,6 +154,26 @@ pub(crate) const LINE_STYLE_CONFLICT_BASE: LineStyle = {
}
};

pub(crate) const LINE_STYLE_CONFLICT_NEW: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Default,
},
plain_style: AnsiStyle {
inverse: false,
weight: Weight::Normal,
color: Green,
},
highlighted_style: AnsiStyle {
inverse: true,
weight: Weight::Normal,
color: Green,
},
}
};

pub(crate) const LINE_STYLE_OLD_FILENAME: LineStyle = {
LineStyle {
prefix_style: AnsiStyle {
Expand Down

0 comments on commit ae19ea5

Please sign in to comment.