diff options
author | Wilfred Hughes <me@wilfred.me.uk> | 2022-11-13 00:35:06 -0800 |
---|---|---|
committer | Wilfred Hughes <me@wilfred.me.uk> | 2022-11-13 00:35:06 -0800 |
commit | 2e7c90c472fbfe9df93d2c0e63817ba7f8491deb (patch) | |
tree | f065846890f2439bc37f17f622476d5ca7dfe1cd | |
parent | 0603b9fb23a9acab1ece26132d2b0b139bff0a9c (diff) |
Ensure line wrapping uses the same length on both sides0.38.0
Closes #421
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | sample_files/compare.expected | 12 | ||||
-rw-r--r-- | src/display/side_by_side.rs | 32 |
3 files changed, 32 insertions, 15 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 34224f32b..958cae6fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sample_files/compare.expected b/sample_files/compare.expected index 9349436e2..aa1fa93d4 100644 --- a/sample_files/compare.expected +++ b/sample_files/compare.expected @@ -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 - @@ -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 - @@ -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 - @@ -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 - @@ -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 - @@ -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 - diff --git a/src/display/side_by_side.rs b/src/display/side_by_side.rs index f8596cd02..79014c37a 100644 --- a/src/display/side_by_side.rs +++ b/src/display/side_by_side.rs @@ -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, @@ -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, } @@ -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, @@ -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, @@ -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() |