JBlond/php-diff

Collapsed block not shown at start of end of text.

Closed this issue · 7 comments

As mentioned here...
A block that would normally collapse, but its position is at the start or end of the text, will not be shown as collapsed. Those lines are NOT rendered at all.

An indication for the block being collapsed from the start/to the end of the text should be visible or these blocks shouldn't be collapsed at all.
E.g.

LineNo LineContent
bof
... ...
10 Hello World!
... ...
eof

The sequenceMatchter is responsible for removing the blocks at the start and end of the text.

Line 554 > 562 removes the sequences at the start.

if ($opCodes['0']['0'] == 'equal') {
    $opCodes['0'] = array(
        $opCodes['0']['0'],
        max($opCodes['0']['1'], $opCodes['0']['2'] - $context),
        $opCodes['0']['2'],
        max($opCodes['0']['3'], $opCodes['0']['4'] - $context),
         $opCodes['0']['4']
    );
}

Line 564 > 574 removes the sequences at the end...

$lastItem = count($opCodes) - 1;
if ($opCodes[$lastItem]['0'] == 'equal') {
    list($tag, $i1, $i2, $j1, $j2) = $opCodes[$lastItem];
    $opCodes[$lastItem] = array(
        $tag,
        $i1,
        min($i2, $i1 + $context),
        $j1,
        min($j2, $j1 + $context)
    );
}

in combination with line 604 > 606.

if (!empty($group) && !(count($group) == 1 && $group[0][0] == 'equal')) {
    $groups[] = $group;
}

In order to show collapsed blocks at the start and end, the first 2 blocks of code should not be executed and the code at line 605 should.
Preferably (by me) create new option like trimEqual which strips equal blocks of text from the beginning and end.
TrimEqualFalse
TrimEqualTrue

The original diff on a command line diff -u a.txt b.txt looks almost the same like now the sequence matcher. So I like your idea of having trimEqual as an option.

Ok.
I will implement it as an option.

I've researched a lot of code, but many code still remains Hocus pocus. 😅
Therefor I suggest testing it well before making a new release.

I'll also re-add the long Chinese line to the same commit.

Commit 98d993e also has some test added for sequenceMatcher::getGroupedOpcodes() to test the output with different options set.

@JBlond Any chance to test the collapsing feature yet?

Yepp, for me it works great.

@DigiLive If you are fine with all your changes open another pull request.