mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
Adjust line number for working copy when editing a line
There are two ways to jump to the editor on a specific line: pressing `e` in the staging or patch building panels, or clicking on a hyperlink in a delta diff. In both cases, this works perfectly in the unstaged changes view, but in other views (either staged changes, or an older commit) it can often jump to the wrong line; this happens when there are further changes to the file being viewed in later commits or in unstaged changes. This commit fixes this so that you end up on the right line in these cases.
This commit is contained in:
@ -154,3 +154,24 @@ func (self *Patch) LineCount() int {
|
||||
func (self *Patch) HunkCount() int {
|
||||
return len(self.hunks)
|
||||
}
|
||||
|
||||
// Adjust the given line number (one-based) according to the current patch. The
|
||||
// patch is supposed to be a diff of an old file state against the working
|
||||
// directory; the line number is a line number in that old file, and the
|
||||
// function returns the corresponding line number in the working directory file.
|
||||
func (self *Patch) AdjustLineNumber(lineNumber int) int {
|
||||
adjustedLineNumber := lineNumber
|
||||
for _, hunk := range self.hunks {
|
||||
if hunk.oldStart >= lineNumber {
|
||||
break
|
||||
}
|
||||
|
||||
if hunk.oldStart+hunk.oldLength() > lineNumber {
|
||||
return hunk.newStart
|
||||
}
|
||||
|
||||
adjustedLineNumber += hunk.newLength() - hunk.oldLength()
|
||||
}
|
||||
|
||||
return adjustedLineNumber
|
||||
}
|
||||
|
@ -639,3 +639,59 @@ func TestGetNextStageableLineIndex(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdjustLineNumber(t *testing.T) {
|
||||
type scenario struct {
|
||||
oldLineNumbers []int
|
||||
expectedResults []int
|
||||
}
|
||||
scenarios := []scenario{
|
||||
{
|
||||
oldLineNumbers: []int{1, 2, 3, 4, 5, 6, 7},
|
||||
expectedResults: []int{1, 2, 2, 3, 4, 7, 8},
|
||||
},
|
||||
}
|
||||
|
||||
// The following diff was generated from old.txt:
|
||||
// 1
|
||||
// 2a
|
||||
// 2b
|
||||
// 3
|
||||
// 4
|
||||
// 7
|
||||
// 8
|
||||
// against new.txt:
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
// 4
|
||||
// 5
|
||||
// 6
|
||||
// 7
|
||||
// 8
|
||||
|
||||
// This test setup makes the test easy to understand, because the resulting
|
||||
// adjusted line numbers are the same as the content of the lines in new.txt.
|
||||
|
||||
diff := `--- old.txt 2024-12-16 18:04:29
|
||||
+++ new.txt 2024-12-16 18:04:27
|
||||
@@ -2,2 +2 @@
|
||||
-2a
|
||||
-2b
|
||||
+2
|
||||
@@ -5,0 +5,2 @@
|
||||
+5
|
||||
+6
|
||||
`
|
||||
|
||||
patch := Parse(diff)
|
||||
|
||||
for _, s := range scenarios {
|
||||
t.Run("TestAdjustLineNumber", func(t *testing.T) {
|
||||
for idx, oldLineNumber := range s.oldLineNumbers {
|
||||
result := patch.AdjustLineNumber(oldLineNumber)
|
||||
assert.Equal(t, s.expectedResults[idx], result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user