MCPcopy Index your code
hub / github.com/jesseduffield/lazygit / LineNumberOfLine

Method LineNumberOfLine

pkg/commands/patch/patch.go:92–115  ·  view source on GitHub ↗

Takes a line index in the patch and returns the line number in the new file. If the line is a header line, returns 1. If the line is a hunk header line, returns the first file line number in that hunk. If the line is out of range below, returns the last file line number in the last hunk.

(idx int)

Source from the content-addressed store, hash-verified

90// If the line is a hunk header line, returns the first file line number in that hunk.
91// If the line is out of range below, returns the last file line number in the last hunk.
92func (self *Patch) LineNumberOfLine(idx int) int {
93 if idx < len(self.header) || len(self.hunks) == 0 {
94 return 1
95 }
96
97 hunkIdx := self.HunkContainingLine(idx)
98 // cursor out of range, return last file line number
99 if hunkIdx == -1 {
100 lastHunk := self.hunks[len(self.hunks)-1]
101 return lastHunk.newStart + lastHunk.newLength() - 1
102 }
103
104 hunk := self.hunks[hunkIdx]
105 hunkStartIdx := self.HunkStartIdx(hunkIdx)
106 idxInHunk := idx - hunkStartIdx
107
108 if idxInHunk == 0 {
109 return hunk.newStart
110 }
111
112 lines := hunk.bodyLines[:idxInHunk-1]
113 offset := nLinesWithKind(lines, []PatchLineKind{ADDITION, CONTEXT})
114 return hunk.newStart + offset
115}
116
117// Returns hunk index containing the line at the given patch line index
118func (self *Patch) HunkContainingLine(idx int) int {

Callers 2

TestLineNumberOfLineFunction · 0.80
CurrentLineNumberMethod · 0.80

Calls 4

HunkContainingLineMethod · 0.95
HunkStartIdxMethod · 0.95
nLinesWithKindFunction · 0.85
newLengthMethod · 0.80

Tested by 1

TestLineNumberOfLineFunction · 0.64