File tree Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -47,19 +47,29 @@ protected function renderHunkHeader(Differ $differ, array $hunk): string
47
47
{
48
48
$ lastBlockIdx = \count ($ hunk ) - 1 ;
49
49
50
+ // note that these line number variables are 0-based
50
51
$ i1 = $ hunk [0 ][1 ];
51
52
$ i2 = $ hunk [$ lastBlockIdx ][2 ];
52
53
$ j1 = $ hunk [0 ][3 ];
53
54
$ j2 = $ hunk [$ lastBlockIdx ][4 ];
54
55
55
- if ($ i1 === 0 && $ i2 === 0 ) {
56
- $ i1 = $ i2 = -1 ; // trick
57
- }
56
+ $ oldLinesCount = $ i2 - $ i1 ;
57
+ $ newLinesCount = $ j2 - $ j1 ;
58
58
59
59
return
60
60
'@@ ' .
61
- '- ' . ($ i1 + 1 ) . ', ' . ($ i2 - $ i1 ) . ' ' .
62
- '+ ' . ($ j1 + 1 ) . ', ' . ($ j2 - $ j1 ) . ' ' .
61
+ '- ' .
62
+ // the line number in GNU diff is 1-based, so we add 1
63
+ // a special case is when a hunk has only changed blocks,
64
+ // i.e., context is set to 0, we do not need the adding
65
+ ($ i1 === $ i2 ? $ i1 : $ i1 + 1 ) .
66
+ // if the line counts is 1, it can (and mostly) be omitted
67
+ ($ oldLinesCount === 1 ? '' : ", {$ oldLinesCount }" ) .
68
+ ' ' .
69
+ '+ ' .
70
+ ($ j1 === $ j2 ? $ j1 : $ j1 + 1 ) .
71
+ ($ newLinesCount === 1 ? '' : ", {$ newLinesCount }" ) .
72
+ ' ' .
63
73
"@@ \n" ;
64
74
}
65
75
You can’t perform that action at this time.
0 commit comments