Skip to content

Commit

Permalink
Ensure line wrapping uses the same length on both sides
Browse files Browse the repository at this point in the history
Closes #421
  • Loading branch information
Wilfred committed Nov 13, 2022
1 parent 0603b9f commit 2e7c90c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ distinct from file content.
Fixed an issue with inline display discarding newlines when color is
disabled, leading to broken display.

Two column display now ensures that both columns have the same width,
so line wrapping is at the same point on both sides.

## 0.37 (released 14th October 2022)

### Manual
Expand Down
12 changes: 6 additions & 6 deletions sample_files/compare.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sample_files/b2_math_before.h sample_files/b2_math_after.h
ec0854e630def8d7e6ce04cbcf1358f3 -
ccbd784fa7df5d317fce588ac477956a -

sample_files/bad_combine_before.rs sample_files/bad_combine_after.rs
affa06f407f8e07bc025c24e96d01298 -
Expand Down Expand Up @@ -68,7 +68,7 @@ sample_files/html_simple_before.html sample_files/html_simple_after.html
ce3bfa12bc21d0eb5528766e18387e86 -

sample_files/huge_cpp_before.cpp sample_files/huge_cpp_after.cpp
d948c1835eac1cdf4019edb6eed6bcaa -
94a49575858e6400ac95b80e5bc52dfe -

sample_files/identical_before.scala sample_files/identical_after.scala
9c7319f61833e46a0a8cb6c01cc997c9 -
Expand Down Expand Up @@ -104,7 +104,7 @@ sample_files/lua_before.lua sample_files/lua_after.lua
9886d61f459cdf566be9c42f7fa61a12 -

sample_files/metadata_before.clj sample_files/metadata_after.clj
a23d575010ed8d902740e7fa9ef6d15b -
8464059b3e1bb3b7109020386fc2985b -

sample_files/modules_before.ml sample_files/modules_after.ml
7e578048e01b2c9eccf26c055c0eb9fd -
Expand All @@ -116,7 +116,7 @@ sample_files/multiline_string_before.ml sample_files/multiline_string_after.ml
ba135b1451962f563ce8c2f449a904bf -

sample_files/nest_before.rs sample_files/nest_after.rs
e51eac2420b5bd0a6e156dc0bae28e4c -
a8d0454af5275807d425cdfa2d08dce8 -

sample_files/nested_slider_before.rs sample_files/nested_slider_after.rs
5c3dc3d870cdf182658da6a29650d911 -
Expand Down Expand Up @@ -155,7 +155,7 @@ sample_files/scala_before.scala sample_files/scala_after.scala
dc36b671167f2b16c7800808d07197ee -

sample_files/Session_before.kt sample_files/Session_after.kt
75db3872d74834ab70358bc50c50b072 -
9def134da4bbc423a1fde93001618776 -

sample_files/simple_before.js sample_files/simple_after.js
43feeac1f3afe2c0c0b862009770f27a -
Expand All @@ -170,7 +170,7 @@ sample_files/slider_before.rs sample_files/slider_after.rs
52e0f2e690d11ef6f38704b8cee84d36 -

sample_files/slow_before.rs sample_files/slow_after.rs
0dba9e3e733237733a050435f0d741fc -
f67aeacbc7164dd076a9a6d6fe0413be -

sample_files/small_before.js sample_files/small_after.js
b4300bfc0203acd8f2603b504b859dc8 -
Expand Down
32 changes: 23 additions & 9 deletions src/display/side_by_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use owo_colors::{OwoColorize, Style};
use rustc_hash::FxHashMap;
use std::{cmp::max, collections::HashSet};
use std::{
cmp::{max, min},
collections::HashSet,
};

use crate::{
constants::Side,
Expand Down Expand Up @@ -147,11 +150,18 @@ fn display_line_nums(
// Sizes used when displaying a hunk.
#[derive(Debug)]
struct SourceDimensions {
lhs_content_width: usize,
rhs_content_width: usize,
/// The number of characters used to display source lines. Any
/// line that exceeds this length will be wrapped.
content_width: usize,
/// The number of characters required to display line numbers on
/// the LHS.
lhs_line_nums_width: usize,
/// The number of characters required to display line numbers on
/// the RHS.
rhs_line_nums_width: usize,
/// The highest line number in the LHS source.
lhs_max_line: LineNumber,
/// The highest line number in the RHS source.
rhs_max_line: LineNumber,
}

Expand Down Expand Up @@ -206,9 +216,13 @@ impl SourceDimensions {
- rhs_line_nums_width as isize,
) as usize;

// We want the content width to be the same on both
// sides. This ensures that line wrapping splits lines at the
// same point on both sides.
let content_width = min(lhs_content_width, rhs_content_width);

Self {
lhs_content_width,
rhs_content_width,
content_width,
lhs_line_nums_width,
rhs_line_nums_width,
lhs_max_line,
Expand Down Expand Up @@ -493,17 +507,17 @@ pub fn print(
let lhs_line = match lhs_line_num {
Some(lhs_line_num) => split_and_apply(
lhs_lines[lhs_line_num.as_usize()],
source_dims.lhs_content_width,
source_dims.content_width,
display_options.use_color,
lhs_highlights.get(lhs_line_num).unwrap_or(&vec![]),
Side::Left,
),
None => vec![" ".repeat(source_dims.lhs_content_width)],
None => vec![" ".repeat(source_dims.content_width)],
};
let rhs_line = match rhs_line_num {
Some(rhs_line_num) => split_and_apply(
rhs_lines[rhs_line_num.as_usize()],
source_dims.rhs_content_width,
source_dims.content_width,
display_options.use_color,
rhs_highlights.get(rhs_line_num).unwrap_or(&vec![]),
Side::Right,
Expand All @@ -516,7 +530,7 @@ pub fn print(
.enumerate()
{
let lhs_line =
lhs_line.unwrap_or_else(|| " ".repeat(source_dims.lhs_content_width));
lhs_line.unwrap_or_else(|| " ".repeat(source_dims.content_width));
let rhs_line = rhs_line.unwrap_or_else(|| "".into());
let lhs_num: String = if i == 0 {
display_lhs_line_num.clone()
Expand Down

0 comments on commit 2e7c90c

Please sign in to comment.