jj/docs
Yuya Nishihara a83dadd5a9 diff: add option to display complex color-words diffs without inlining
In this patch, I use the number of adds<->removes alternation as a threshold,
which approximates the visual complexity of diff hunks. I don't think user can
choose the threshold intuitively, but we need a config knob to try out some.
I set `max-inline-alternation = 3` locally. 0 and 1 mean "disable inlining"
and "inline adds-only/removes-only lines" respectively.

I've added "diff.<format>" config namespace assuming "ui.diff" will be
reorganized as "ui.diff-formatter" or something. #3327

Some other metrics I've tried:
```
// Per-line alternation. This also works well, but can't measure complexity of
// changes across lines.
fn count_max_diff_alternation_per_line(diff_lines: &[DiffLine]) -> usize {
    diff_lines
        .iter()
        .map(|line| {
            let sides = line.hunks.iter().map(|&(side, _)| side);
            sides
                .filter(|&side| side != DiffLineHunkSide::Both)
                .dedup() // omit e.g. left->both->left
                .count()
        })
        .max()
        .unwrap_or(0)
}

// Per-line occupancy of changes. Large diffs don't always look complex.
fn max_diff_token_ratio_per_line(diff_lines: &[DiffLine]) -> f32 {
    diff_lines
        .iter()
        .filter_map(|line| {
            let [both_len, left_len, right_len] =
                line.hunks.iter().fold([0, 0, 0], |mut acc, (side, data)| {
                    let index = match side {
                        DiffLineHunkSide::Both => 0,
                        DiffLineHunkSide::Left => 1,
                        DiffLineHunkSide::Right => 2,
                    };
                    acc[index] += data.len();
                    acc
                });
            // left/right-only change is readable
            (left_len != 0 && right_len != 0).then(|| {
                let diff_len = left_len + right_len;
                let total_len = both_len + left_len + right_len;
                (diff_len as f32) / (total_len as f32)
            })
        })
        .reduce(f32::max)
        .unwrap_or(0.0)
}

// Total occupancy of changes. Large diffs don't always look complex.
fn total_change_ratio(diff_lines: &[DiffLine]) -> f32 {
    let (diff_len, total_len) = diff_lines
        .iter()
        .flat_map(|line| &line.hunks)
        .fold((0, 0), |(diff_len, total_len), (side, data)| {
            let l = data.len();
            match side {
                DiffLineHunkSide::Both => (diff_len, total_len + l),
                DiffLineHunkSide::Left => (diff_len + l, total_len + l),
                DiffLineHunkSide::Right => (diff_len + l, total_len + l),
            }
        });
    (diff_len as f32) / (total_len as f32)
}
```
2024-08-21 17:48:52 +09:00
..
design cleanup: leverage BoxStream/BoxFuture type aliases 2024-06-26 11:34:52 +09:00
technical
branches.md docs: minor updates to description of branch conflicts 2024-08-05 18:20:37 -07:00
cli-reference.md docs CLI Reference: make the warning in the beginning less scary 2024-06-15 20:30:40 -07:00
code-of-conduct.md docs: s/socio-economics/socioeconomics/ per codespell suggestion 2024-05-24 11:34:03 +09:00
community_tools.md Add hunk.nvim to list of community tools 2024-07-22 15:29:54 -06:00
config.md diff: add option to display complex color-words diffs without inlining 2024-08-21 17:48:52 +09:00
config.toml
conflicts.md docs: Fix example with Git conflicts markers 2024-08-02 10:36:38 -07:00
contributing.md cli: include untracked remote branches in default immutable_heads() 2024-07-15 23:41:07 +09:00
design_doc_blueprint.md docs: Add a Design Doc blueprint. 2024-06-30 15:37:40 +02:00
design_docs.md docs: Add a Design Doc blueprint. 2024-06-30 15:37:40 +02:00
FAQ.md FAQ: remove workaround for fixed bug (#2476) 2024-08-01 09:07:32 -07:00
filesets.md cli: enable fileset by default 2024-07-24 10:49:46 +09:00
git-comparison.md docs: replace jj files -> jj file list 2024-06-24 21:01:55 -07:00
git-compatibility.md docs/git-comparison.md: update the Signed_commits bullet 2024-07-17 17:12:34 +02:00
github.md
glossary.md docs: fix another missing shell quoting around root() 2024-06-04 21:19:16 +09:00
index.md
install-and-setup.md Added docs about installation under Gentoo Linux 2024-08-20 22:19:09 -07:00
operation-log.md
related-work.md
revsets.md Define builtin_immutable_heads() as a default revset alias. 2024-08-14 11:32:16 +01:00
sapling-comparison.md docs: explain that jj was started 3 years before Sapling announcement 2024-06-20 05:24:28 +09:00
templates.md templater: add commit.diff().<format>() methods 2024-07-17 18:52:49 +09:00
testimonials.md
tutorial.md tutorial: link to Steve Klabnik's tutorial, mention jj log -r :: 2024-08-06 11:27:08 -07:00
windows.md docs: update windows docs to mention minus, update minus docs 2024-06-10 11:47:34 -07:00
working-copy.md