Skip to content

Commit

Permalink
Use a formatter instance
Browse files Browse the repository at this point in the history
As we will need to be able to configure it.
  • Loading branch information
walles committed Oct 21, 2024
1 parent 315418c commit ca08235
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/plusminus_lines_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub(crate) struct PlusMinusLinesHighlighter {
prefixes: Vec<String>,

last_seen_prefix: Option<String>,

formatter: Formatter,
}

impl LinesHighlighter for PlusMinusLinesHighlighter {
Expand Down Expand Up @@ -124,6 +126,7 @@ impl PlusMinusLinesHighlighter {
texts: vec![line.to_string() + "\n"],
prefixes: vec![prefix.to_string()],
last_seen_prefix: Some(prefix.to_string()),
formatter: Formatter {},
});
}

Expand Down Expand Up @@ -202,14 +205,15 @@ impl PlusMinusLinesHighlighter {

let texts = self.texts.clone();
let prefixes = self.prefixes.clone();
let formatter = self.formatter;

self.texts.clear();
self.prefixes.clear();

let return_me = StringFuture::from_function(
move || {
let mut result = String::new();
for line in Formatter::format(
for line in formatter.format(
&prefixes.iter().map(String::as_str).collect::<Vec<&str>>(),
&texts.iter().map(String::as_str).collect::<Vec<&str>>(),
) {
Expand Down
39 changes: 24 additions & 15 deletions src/refiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use crate::line_collector::NO_EOF_NEWLINE_MARKER_HOLDER;
use crate::token_collector::*;
use crate::tokenizer;

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Formatter {}

impl Formatter {
/// Format old and new lines in OLD and NEW colors.
///
/// No intra-line refinement.
#[must_use]
fn format_simple(prefixes: &[&str], prefix_texts: &[&str]) -> Vec<String> {
fn format_simple(&self, prefixes: &[&str], prefix_texts: &[&str]) -> Vec<String> {
let mut lines: Vec<String> = Vec::new();

for (prefix, prefix_text) in prefixes.iter().zip(prefix_texts.iter()) {
Expand Down Expand Up @@ -80,18 +81,18 @@ impl Formatter {
///
/// `prefixes` are the prefixes to use for each `prefix_texts` text.
#[must_use]
pub fn format(prefixes: &[&str], prefix_texts: &[&str]) -> Vec<String> {
pub fn format(&self, prefixes: &[&str], prefix_texts: &[&str]) -> Vec<String> {
if prefixes.len() < 2 {
// Nothing to compare, we can't highlight anything
return Self::format_simple(prefixes, prefix_texts);
return self.format_simple(prefixes, prefix_texts);
}
if !prefixes.iter().any(|prefix| prefix.contains('+')) {
// Nothing added, we can't highlight anything
return Self::format_simple(prefixes, prefix_texts);
return self.format_simple(prefixes, prefix_texts);
}

if Self::too_large_to_refine(prefix_texts) {
return Self::format_simple(prefixes, prefix_texts);
if Formatter::too_large_to_refine(prefix_texts) {
return self.format_simple(prefixes, prefix_texts);
}

// This is what all old texts will be compared against
Expand Down Expand Up @@ -332,16 +333,18 @@ mod tests {

#[test]
fn test_simple_format_adds_and_removes() {
let formatter = Formatter {};

let empty: Vec<String> = Vec::new();
assert_eq!(Formatter::format_simple(&[], &[]), empty);
assert_eq!(formatter.format_simple(&[], &[]), empty);

// Test adds-only
assert_eq!(
Formatter::format_simple(&["+"], &["a\n"]),
formatter.format_simple(&["+"], &["a\n"]),
["".to_string() + GREEN + "+a" + NORMAL]
);
assert_eq!(
Formatter::format_simple(&["+"], &["a\nb\n"]),
formatter.format_simple(&["+"], &["a\nb\n"]),
[
"".to_string() + GREEN + "+a" + NORMAL,
"".to_string() + GREEN + "+b" + NORMAL,
Expand All @@ -350,11 +353,11 @@ mod tests {

// Test removes-only
assert_eq!(
Formatter::format_simple(&["-"], &["a\n"]),
formatter.format_simple(&["-"], &["a\n"]),
["".to_string() + OLD + "-a" + NORMAL]
);
assert_eq!(
Formatter::format_simple(&["-"], &["a\nb\n"]),
formatter.format_simple(&["-"], &["a\nb\n"]),
[
"".to_string() + OLD + "-a" + NORMAL,
"".to_string() + OLD + "-b" + NORMAL,
Expand All @@ -366,6 +369,8 @@ mod tests {
/// hangs, that's probably what happened again.
#[test]
fn test_format_simple_complexity() {
let formatter = Formatter {};

// Values from whan this file was added in a single commit:
// https://github.com/walles/moar/blob/59270d6f8cf454f7a79fcde36a7fcf794768ced9/sample-files/large-git-log-patch.txt
let lines = 300_000;
Expand All @@ -379,13 +384,15 @@ mod tests {
let prefixes = vec!["+"];
let texts = vec![text.as_str()];

let result = Formatter::format_simple(&prefixes, &texts);
let result = formatter.format_simple(&prefixes, &texts);
assert_eq!(text.lines().count(), result.len());
}

#[test]
fn test_quote_change() {
let result = Formatter::format(
let formatter = Formatter {};

let result = formatter.format(
&["-", "+"],
&[
"<unchanged text between quotes>\n",
Expand All @@ -407,10 +414,12 @@ mod tests {

#[test]
fn test_almost_empty_changes() {
let result = Formatter::format(&["-"], &["x\n"]);
let formatter = Formatter {};

let result = formatter.format(&["-"], &["x\n"]);
assert_eq!(result, [format!("{OLD}-x{NORMAL}"),]);

let result = Formatter::format(&["+"], &["x\n"]);
let result = formatter.format(&["+"], &["x\n"]);
assert_eq!(result, [format!("{GREEN}+x{NORMAL}"),]);
}

Expand Down

0 comments on commit ca08235

Please sign in to comment.