Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into johan/half-line-hig…
Browse files Browse the repository at this point in the history
…hlights
  • Loading branch information
walles committed Oct 19, 2024
2 parents ea583eb + d3ffecd commit e384d22
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 18 deletions.
38 changes: 25 additions & 13 deletions src/conflicts_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub(crate) struct ConflictsHighlighter {
/// Always ends with a newline.
base: String,

/// Prefixes of the base section, one per line.
base_line_prefixes: vec::Vec<String>,

/// `=======`, followed by `c2`
c2_header: String,

Expand Down Expand Up @@ -90,17 +93,17 @@ impl LinesHighlighter for ConflictsHighlighter {
// All header lines handled, this is a content line
//

let destination = if !self.c2_header.is_empty() {
&mut self.c2
let (prefix_destination, destination) = if !self.c2_header.is_empty() {
(None, &mut self.c2)
} else if !self.base_header.is_empty() {
&mut self.base
(Some(&mut self.base_line_prefixes), &mut self.base)
} else {
&mut self.c1
(None, &mut self.c1)
};

let prefixes = if self.c1_header.starts_with("++") {
// Possible content line prefixes when doing "git diff"
vec!["+ ", "++", " +"]
vec!["+ ", "++", " +", " -", "- ", " "]
} else {
vec![""]
};
Expand All @@ -110,6 +113,11 @@ impl LinesHighlighter for ConflictsHighlighter {
// Handle the context line
destination.push_str(line);
destination.push('\n');

if let Some(prefix_destination) = prefix_destination {
prefix_destination.push(prefix.to_string());
}

return Ok(Response {
line_accepted: LineAcceptance::AcceptedWantMore,
highlighted: vec![],
Expand Down Expand Up @@ -149,6 +157,7 @@ impl ConflictsHighlighter {
footer: String::new(),
c1: String::new(),
base: String::new(),
base_line_prefixes: Vec::new(),
c2: String::new(),
});
}
Expand Down Expand Up @@ -240,18 +249,18 @@ impl ConflictsHighlighter {
/// vs C1 or vs C2.
/// * In section C2, we highlight additions compared to base
fn render_diff3(&self, thread_pool: &ThreadPool) -> StringFuture {
let (header_prefix, c1_prefix, base_prefix, c2_prefix, reset) =
if self.c1_header.starts_with("++") {
(INVERSE_VIDEO, " +", "++", "+ ", NORMAL)
} else {
(INVERSE_VIDEO, "", "", "", "")
};
let (header_prefix, c1_prefix, c2_prefix, reset) = if self.c1_header.starts_with("++") {
(INVERSE_VIDEO, " +", "+ ", NORMAL)
} else {
(INVERSE_VIDEO, "", "", "")
};

assert!(!self.base.is_empty());
let c1_header = self.c1_header.clone();
let c1 = self.c1.clone();
let base_header = self.base_header.clone();
let base = self.base.clone();
let base_line_prefixes = self.base_line_prefixes.clone();
let c2_header = self.c2_header.clone();
let c2 = self.c2.clone();
let footer = self.footer.clone();
Expand Down Expand Up @@ -296,8 +305,11 @@ impl ConflictsHighlighter {
}
}

let highlighted_base =
token_collector::render(&LINE_STYLE_CONFLICT_BASE, base_prefix, &base_tokens);
let highlighted_base = token_collector::render_multiprefix(
&LINE_STYLE_CONFLICT_BASE,
&base_line_prefixes,
&base_tokens,
);

let mut rendered = String::new();
rendered.push_str(header_prefix);
Expand Down
68 changes: 64 additions & 4 deletions src/token_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ impl StyledToken {
}

#[must_use]
fn render_row(line_style: &LineStyle, prefix: &str, row: &[StyledToken]) -> String {
fn render_row(
line_style: &LineStyle,
prefix: &str,
row: &[StyledToken],
force_faint: bool,
) -> String {
let mut rendered = String::new();

let mut current_style = ANSI_STYLE_NORMAL;
Expand All @@ -128,7 +133,7 @@ fn render_row(line_style: &LineStyle, prefix: &str, row: &[StyledToken]) -> Stri

// Render tokens
for token in row {
let new_style = match token.style {
let mut new_style = match token.style {
Style::Context => ANSI_STYLE_NORMAL,
Style::Lowlighted => ANSI_STYLE_NORMAL.with_weight(Weight::Faint),
Style::Bright => ANSI_STYLE_NORMAL.with_weight(Weight::Bold),
Expand All @@ -139,6 +144,10 @@ fn render_row(line_style: &LineStyle, prefix: &str, row: &[StyledToken]) -> Stri
Style::Error => ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
};

if force_faint {
new_style = new_style.with_weight(Weight::Faint);
}

rendered.push_str(&new_style.from(&current_style));
current_style = new_style;
rendered.push_str(&token.token);
Expand All @@ -158,7 +167,8 @@ pub fn render(line_style: &LineStyle, prefix: &str, tokens: &[StyledToken]) -> S
let mut current_row_start = 0;
for (i, token) in tokens.iter().enumerate() {
if token.token == "\n" {
let rendered_row = &render_row(line_style, prefix, &tokens[current_row_start..i]);
let rendered_row =
&render_row(line_style, prefix, &tokens[current_row_start..i], false);
rendered.push_str(rendered_row);
rendered.push('\n');
current_row_start = i + 1;
Expand All @@ -167,7 +177,57 @@ pub fn render(line_style: &LineStyle, prefix: &str, tokens: &[StyledToken]) -> S
}

if current_row_start < tokens.len() {
let rendered_row = &render_row(line_style, prefix, &tokens[current_row_start..]);
let rendered_row = &render_row(line_style, prefix, &tokens[current_row_start..], false);
rendered.push_str(rendered_row);
}

return rendered;
}

/// Render all the tokens into a (most of the time multiline) string. Each line
/// is prefixed by a prefix from `line_prefixes`.
#[must_use]
pub fn render_multiprefix(
line_style: &LineStyle,
line_prefixes: &[String],
tokens: &[StyledToken],
) -> String {
let mut rendered = String::new();

let mut current_row_start = 0;
let mut line_number_zero_based = 0;
for (i, token) in tokens.iter().enumerate() {
if token.token != "\n" {
continue;
}

let prefix = &line_prefixes[line_number_zero_based];
let force_faint = prefix.chars().any(|c| c == '-');

let rendered_row = &render_row(
line_style,
prefix,
&tokens[current_row_start..i],
force_faint,
);
rendered.push_str(rendered_row);
rendered.push('\n');
current_row_start = i + 1;

line_number_zero_based += 1;
}

if current_row_start < tokens.len() {
// Render the last row
let prefix = &line_prefixes[line_number_zero_based];
let force_faint = prefix.chars().any(|c| c == '-');

let rendered_row = &render_row(
line_style,
prefix,
&tokens[current_row_start..],
force_faint,
);
rendered.push_str(rendered_row);
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/conflict-with-context.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[36m@@@ -10,45 -6,9 +10,79 @@@ [1mimport [0m
"github.com/walles/moar/m/linenumbers"
)

[7m++<<<<<<< HEAD[0m
[7m +[0mfunc (p *Pager) scrollToSearchHits() {
[7m +[0m if p.searchPattern == nil {
Expand Down
120 changes: 120 additions & 0 deletions testdata/git-conflict-with-base-minus.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
diff --cc src/token_collector.rs
index b0b23a5,d8d6da8..0000000
--- src/token_collector.rs
+++ src/token_collector.rs
@@@ -34,47 -35,62 +34,95 @@@ pub(crate) struct LineStyle
}

pub(crate) const LINE_STYLE_OLD: LineStyle = {
+ LineStyle {
+ prefix_style: ANSI_STYLE_NORMAL.with_color(Red),
+ plain_style: ANSI_STYLE_NORMAL.with_color(Red),
+ highlighted_style: ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
+ }
+};
+
++<<<<<<< HEAD
+pub(crate) const LINE_STYLE_OLD_FAINT: LineStyle = {
+ LineStyle {
+ prefix_style: ANSI_STYLE_NORMAL.with_color(Red).with_weight(Weight::Faint),
+ plain_style: ANSI_STYLE_NORMAL.with_color(Red).with_weight(Weight::Faint),
+ highlighted_style: ANSI_STYLE_NORMAL
+ .with_color(Red)
+ .with_weight(Weight::Faint)
+ .with_inverse(true),
+ }
+};
+
++||||||| parent of 980b661 (Remove the adds-only special highlighting feature)
++pub(crate) const LINE_STYLE_OLD_FAINT: LineStyle = {
+ LineStyle {
+ prefix_style: AnsiStyle {
+ inverse: false,
- weight: Weight::Normal,
++ weight: Weight::Faint,
+ color: Red,
+ },
+ plain_style: AnsiStyle {
+ inverse: false,
- weight: Weight::Normal,
++ weight: Weight::Faint,
+ color: Red,
+ },
+ highlighted_style: AnsiStyle {
+ inverse: true,
- weight: Weight::Normal,
++ weight: Weight::Faint,
+ color: Red,
+ },
+ }
+ };
+
++=======
++>>>>>>> 980b661 (Remove the adds-only special highlighting feature)
pub(crate) const LINE_STYLE_NEW: LineStyle = {
+ LineStyle {
+ prefix_style: ANSI_STYLE_NORMAL.with_color(Green),
+ plain_style: ANSI_STYLE_NORMAL.with_color(Green),
+ highlighted_style: ANSI_STYLE_NORMAL.with_color(Green).with_inverse(true),
+ }
+};
+
++<<<<<<< HEAD
+pub(crate) const LINE_STYLE_ADDS_ONLY: LineStyle = {
+ LineStyle {
+ prefix_style: ANSI_STYLE_NORMAL
+ .with_color(Green)
+ .with_weight(Weight::Faint),
+ plain_style: ANSI_STYLE_NORMAL,
+ highlighted_style: ANSI_STYLE_NORMAL.with_color(Green).with_inverse(true),
+ }
+};
+
++||||||| parent of 980b661 (Remove the adds-only special highlighting feature)
++pub(crate) const LINE_STYLE_ADDS_ONLY: LineStyle = {
+ LineStyle {
+ prefix_style: AnsiStyle {
+ inverse: false,
- weight: Weight::Normal,
++ weight: Weight::Faint,
+ color: Green,
+ },
+ plain_style: AnsiStyle {
+ inverse: false,
+ weight: Weight::Normal,
- color: Green,
++ color: Default,
+ },
+ highlighted_style: AnsiStyle {
+ inverse: true,
+ weight: Weight::Normal,
+ color: Green,
+ },
+ }
+ };
+
++=======
++>>>>>>> 980b661 (Remove the adds-only special highlighting feature)
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: Red,
- },
- highlighted_style: AnsiStyle {
- inverse: true,
- weight: Weight::Normal,
- color: Red,
- },
+ prefix_style: ANSI_STYLE_NORMAL.with_inverse(true),
+ plain_style: ANSI_STYLE_NORMAL.with_color(Red),
+ highlighted_style: ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
}
};

Loading

0 comments on commit e384d22

Please sign in to comment.