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)
| 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. |
| 92 | func (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 |
| 118 | func (self *Patch) HunkContainingLine(idx int) int { |