mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
fix out of range error
This commit is contained in:
@ -137,12 +137,17 @@ func ModifiedPatchForLines(log *logrus.Entry, filename string, diffText string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// I want to know, given a hunk, what line a given index is on
|
// I want to know, given a hunk, what line a given index is on
|
||||||
func (hunk *PatchHunk) LineNumberOfLine(idx int) int {
|
func (hunk *PatchHunk) LineNumberOfLine(idx int) (int, error) {
|
||||||
lines := hunk.bodyLines[0 : idx-hunk.FirstLineIdx-1]
|
n := idx - hunk.FirstLineIdx - 1
|
||||||
|
if n < 0 || len(hunk.bodyLines) <= n {
|
||||||
|
return -1, fmt.Errorf("line index out of range")
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := hunk.bodyLines[0:n]
|
||||||
|
|
||||||
offset := nLinesWithPrefix(lines, []string{"+", " "})
|
offset := nLinesWithPrefix(lines, []string{"+", " "})
|
||||||
|
|
||||||
return hunk.newStart + offset
|
return hunk.newStart + offset, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func nLinesWithPrefix(lines []string, chars []string) int {
|
func nLinesWithPrefix(lines []string, chars []string) int {
|
||||||
|
@ -539,7 +539,7 @@ func TestLineNumberOfLine(t *testing.T) {
|
|||||||
|
|
||||||
for _, s := range scenarios {
|
for _, s := range scenarios {
|
||||||
t.Run(s.testName, func(t *testing.T) {
|
t.Run(s.testName, func(t *testing.T) {
|
||||||
result := s.hunk.LineNumberOfLine(s.idx)
|
result, _ := s.hunk.LineNumberOfLine(s.idx)
|
||||||
if !assert.Equal(t, s.expected, result) {
|
if !assert.Equal(t, s.expected, result) {
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
}
|
}
|
||||||
|
@ -167,7 +167,7 @@ func (s *State) SelectedRange() (int, int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) CurrentLineNumber() int {
|
func (s *State) CurrentLineNumber() (int, error) {
|
||||||
return s.CurrentHunk().LineNumberOfLine(s.selectedLineIdx)
|
return s.CurrentHunk().LineNumberOfLine(s.selectedLineIdx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,7 +208,11 @@ func (gui *Gui) handleOpenFileAtLine() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// need to look at current index, then work out what my hunk's header information is, and see how far my line is away from the hunk header
|
// need to look at current index, then work out what my hunk's header information is, and see how far my line is away from the hunk header
|
||||||
lineNumber := state.CurrentLineNumber()
|
lineNumber, err := state.CurrentLineNumber()
|
||||||
|
if err != nil {
|
||||||
|
lineNumber = 1
|
||||||
|
}
|
||||||
|
|
||||||
filenameWithLineNum := fmt.Sprintf("%s:%d", filename, lineNumber)
|
filenameWithLineNum := fmt.Sprintf("%s:%d", filename, lineNumber)
|
||||||
if err := gui.OSCommand.OpenFile(filenameWithLineNum); err != nil {
|
if err := gui.OSCommand.OpenFile(filenameWithLineNum); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -279,7 +283,10 @@ func (gui *Gui) handleLineByLineEdit() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
lineNumber := gui.State.Panels.LineByLine.CurrentLineNumber()
|
lineNumber, err := gui.State.Panels.LineByLine.CurrentLineNumber()
|
||||||
|
if err != nil {
|
||||||
|
lineNumber = 1
|
||||||
|
}
|
||||||
|
|
||||||
return gui.editFileAtLine(file.Name, lineNumber)
|
return gui.editFileAtLine(file.Name, lineNumber)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user