Skip to content

Commit

Permalink
Fix jumping to the correct line from the staging view (#2919)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Aug 10, 2023
2 parents f2e8d54 + 73b6892 commit 08624b8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/commands/patch/patch.go
Expand Up @@ -97,12 +97,12 @@ func (self *Patch) LineNumberOfLine(idx int) int {
idxInHunk := idx - hunkStartIdx

if idxInHunk == 0 {
return hunk.oldStart
return hunk.newStart
}

lines := hunk.bodyLines[:idxInHunk-1]
offset := nLinesWithKind(lines, []PatchLineKind{ADDITION, CONTEXT})
return hunk.oldStart + offset
return hunk.newStart + offset
}

// Returns hunk index containing the line at the given patch line index
Expand Down
29 changes: 29 additions & 0 deletions pkg/commands/patch/patch_test.go
Expand Up @@ -67,6 +67,29 @@ index e48a11c..b2ab81b 100644
...
`

const twoHunksWithMoreAdditionsThanRemovals = `diff --git a/filename b/filename
index bac359d75..6e5b89f36 100644
--- a/filename
+++ b/filename
@@ -1,5 +1,6 @@
apple
-grape
+orange
+kiwi
...
...
...
@@ -8,6 +9,8 @@ grape
...
...
...
+pear
+lemon
...
...
...
`

const twoChangesInOneHunk = `diff --git a/filename b/filename
index 9320895..6d79956 100644
--- a/filename
Expand Down Expand Up @@ -572,6 +595,12 @@ func TestLineNumberOfLine(t *testing.T) {
indexes: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 1000},
expecteds: []int{1, 1, 1, 1, 1, 1, 2, 2, 3, 4, 5, 8, 8, 9, 10, 11, 12, 13, 14, 15, 15, 15, 15, 15, 15},
},
{
testName: "twoHunksWithMoreAdditionsThanRemovals",
patchStr: twoHunksWithMoreAdditionsThanRemovals,
indexes: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 1000},
expecteds: []int{1, 1, 1, 1, 1, 1, 2, 2, 3, 4, 5, 6, 9, 9, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16},
},
}

for _, s := range scenarios {
Expand Down

0 comments on commit 08624b8

Please sign in to comment.