summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/delta.rs26
-rw-r--r--src/edits.rs57
2 files changed, 36 insertions, 47 deletions
diff --git a/src/delta.rs b/src/delta.rs
index e51aa111..c7cafa44 100644
--- a/src/delta.rs
+++ b/src/delta.rs
@@ -2,7 +2,6 @@ use std::io::Write;
use ansi_term::Colour::{Blue, Yellow};
use console::strip_ansi_codes;
-use unicode_segmentation::UnicodeSegmentation;
use crate::bat::assets::HighlightingAssets;
use crate::cli;
@@ -220,16 +219,16 @@ fn handle_hunk_line(painter: &mut Painter, line: &str, state: State, config: &Co
if state == State::HunkPlus {
painter.paint_buffered_lines();
}
- painter.minus_lines.push(prepare(&line, config));
+ painter.minus_lines.push(prepare(&line));
State::HunkMinus
}
Some('+') => {
- painter.plus_lines.push(prepare(&line, config));
+ painter.plus_lines.push(prepare(&line));
State::HunkPlus
}
_ => {
painter.paint_buffered_lines();
- let line = prepare(&line, config);
+ let line = prepare(&line);
let syntax_style_sections = Painter::get_line_syntax_style_sections(
&line,
&mut painter.highlighter,
@@ -246,19 +245,12 @@ fn handle_hunk_line(painter: &mut Painter, line: &str, state: State, config: &Co
}
}
-/// Replace initial -/+ character with ' ', pad to width, and terminate with newline character.
-fn prepare(_line: &str, config: &Config) -> String {
- let mut line = String::new();
- if _line.len() > 0 {
- line.push_str(" ");
- line.push_str(&_line[1..]);
- }
- let line_length = line.graphemes(true).count();
- match config.width {
- Some(width) if width > line_length => {
- format!("{}{}\n", line, " ".repeat(width - line_length))
- }
- _ => format!("{}\n", line),
+/// Replace initial -/+ character with ' '
+fn prepare(line: &str) -> String {
+ if line.len() > 0 {
+ format!(" {}", &line[1..])
+ } else {
+ line.to_string()
}
}
diff --git a/src/edits.rs b/src/edits.rs
index 67933688..64180299 100644
--- a/src/edits.rs
+++ b/src/edits.rs
@@ -29,17 +29,14 @@ where
'minus_lines_loop: for minus_line in minus_lines {
let mut considered = 0; // plus lines considered so far as match for minus_line
- let minus_line = minus_line.trim_end();
for plus_line in &plus_lines[emitted..] {
- let plus_line = plus_line.trim_end();
-
let alignment = align::Alignment::new(tokenize(minus_line), tokenize(plus_line));
if alignment.distance() <= max_line_distance {
// minus_line and plus_line are inferred to be a homologous pair.
// Emit as unpaired the plus lines already considered and rejected
for plus_line in &plus_lines[emitted..(emitted + considered)] {
- annotated_plus_lines.push(vec![(non_insertion, plus_line.trim_end())]);
+ annotated_plus_lines.push(vec![(non_insertion, plus_line)]);
}
emitted += considered;
@@ -69,11 +66,11 @@ where
}
}
// No homolog was found for minus i; emit as unpaired.
- annotated_minus_lines.push(vec![(non_deletion, minus_line.trim_end())]);
+ annotated_minus_lines.push(vec![(non_deletion, minus_line)]);
}
// Emit any remaining plus lines
for plus_line in &plus_lines[emitted..] {
- annotated_plus_lines.push(vec![(non_insertion, plus_line.trim_end())]);
+ annotated_plus_lines.push(vec![(non_insertion, plus_line)]);
}
(annotated_minus_lines, annotated_plus_lines)
@@ -274,8 +271,8 @@ mod tests {
#[test]
fn test_infer_edits_1() {
assert_paired_edits(
- vec!["aaa\n"],
- vec!["aba\n"],
+ vec!["aaa"],
+ vec!["aba"],
(
vec![vec![(Deletion, "aaa")]],
vec![vec![(Insertion, "aba")]],
@@ -286,8 +283,8 @@ mod tests {
#[test]
fn test_infer_edits_1_2() {
assert_paired_edits(
- vec!["aaa ccc\n"],
- vec!["aba ccc\n"],
+ vec!["aaa ccc"],
+ vec!["aba ccc"],
(
vec![vec![(Deletion, "aaa "), (MinusNoop, "ccc")]],
vec![vec![(Insertion, "aba "), (PlusNoop, "ccc")]],
@@ -298,8 +295,8 @@ mod tests {
#[test]
fn test_infer_edits_2() {
assert_paired_edits(
- vec!["áaa\n"],
- vec!["ááb\n"],
+ vec!["áaa"],
+ vec!["ááb"],
(
vec![vec![(Deletion, "áaa")]],
vec![vec![(Insertion, "ááb")]],
@@ -310,8 +307,8 @@ mod tests {
#[test]
fn test_infer_edits_3() {
assert_paired_edits(
- vec!["d.iteritems()\n"],
- vec!["d.items()\n"],
+ vec!["d.iteritems()"],
+ vec!["d.items()"],
(
vec![vec![
(MinusNoop, "d."),
@@ -326,8 +323,8 @@ mod tests {
#[test]
fn test_infer_edits_4() {
assert_edits(
- vec!["áaaáaaáaa\n", "áábáábááb\n"],
- vec!["áábáácááb\n"],
+ vec!["áaaáaaáaa", "áábáábááb"],
+ vec!["áábáácááb"],
(
vec![
vec![(MinusNoop, "áaaáaaáaa")],
@@ -350,8 +347,8 @@ mod tests {
#[test]
fn test_infer_edits_5() {
assert_edits(
- vec!["aaaaaaaa\n", "bbbbbbbb\n", "cccccccc\n"],
- vec!["bbbb!bbb\n", "dddddddd\n", "cccc!ccc\n"],
+ vec!["aaaaaaaa", "bbbbbbbb", "cccccccc"],
+ vec!["bbbb!bbb", "dddddddd", "cccc!ccc"],
(
vec![
vec![(MinusNoop, "aaaaaaaa")],
@@ -372,20 +369,20 @@ mod tests {
fn test_infer_edits_6() {
assert_no_edits(
vec![
- " let mut i = 0;\n",
- " for ((_, c0), (_, c1)) in s0.zip(s1) {\n",
- " if c0 != c1 {\n",
- " break;\n",
- " } else {\n",
- " i += c0.len();\n",
- " }\n",
- " }\n",
- " i\n",
+ " let mut i = 0;",
+ " for ((_, c0), (_, c1)) in s0.zip(s1) {",
+ " if c0 != c1 {",
+ " break;",
+ " } else {",
+ " i += c0.len();",
+ " }",
+ " }",
+ " i",
],
vec![
- " s0.zip(s1)\n",
- " .take_while(|((_, c0), (_, c1))| c0 == c1) // TODO: Don't consume one-past-the-end!\n",
- " .fold(0, |offset, ((_, c0), (_, _))| offset + c0.len())\n"
+ " s0.zip(s1)",
+ " .take_while(|((_, c0), (_, c1))| c0 == c1) // TODO: Don't consume one-past-the-end!",
+ " .fold(0, |offset, ((_, c0), (_, _))| offset + c0.len())"
], 0.66)
}