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

Method NavigateToLine

pkg/integration/components/view_driver.go:484–550  ·  view source on GitHub ↗

this will look for a list item in the current panel and if it finds it, it will enter the keypresses required to navigate to it. The test will fail if: - the user is not in a list item - no list item is found containing the given text - multiple list items are found containing the given text in the

(matcher *TextMatcher)

Source from the content-addressed store, hash-verified

482// - no list item is found containing the given text
483// - multiple list items are found containing the given text in the initial page of items
484func (self *ViewDriver) NavigateToLine(matcher *TextMatcher) *ViewDriver {
485 self.IsFocused()
486
487 view := self.getView()
488 lines := view.BufferLines()
489
490 matchIndex := -1
491
492 self.t.assertWithRetries(func() (bool, string) {
493 var matches []string
494 // first we look for a duplicate on the current screen. We won't bother looking beyond that though.
495 for i, line := range lines {
496 ok, _ := matcher.test(line)
497 if ok {
498 matches = append(matches, line)
499 matchIndex = i
500 }
501 }
502 if len(matches) > 1 {
503 return false, fmt.Sprintf("Found %d matches for `%s`, expected only a single match. Matching lines:\n%s", len(matches), matcher.name(), strings.Join(matches, "\n"))
504 }
505 return true, ""
506 })
507
508 // If no match was found, it could be that this is a view that renders only
509 // the visible lines. In that case, we jump to the top and then press
510 // down-arrow until we found the match. We simply return the first match we
511 // find, so we have no way to assert that there are no duplicates.
512 if matchIndex == -1 {
513 self.GotoTop()
514 matchIndex = len(lines)
515 }
516
517 selectedLineIdx := self.getSelectedLineIdx()
518 if selectedLineIdx == matchIndex {
519 return self.SelectedLine(matcher)
520 }
521
522 // At this point we can't just take the difference of selected and matched
523 // index and press up or down arrow this many times. The reason is that
524 // there might be section headers between those lines, and these will be
525 // skipped when pressing up or down arrow. So we must keep pressing the
526 // arrow key in a loop, and check after each one whether we now reached the
527 // target line.
528 var maxNumKeyPresses int
529 var keyPress func()
530 if selectedLineIdx < matchIndex {
531 maxNumKeyPresses = matchIndex - selectedLineIdx
532 keyPress = func() { self.SelectNextItem() }
533 } else {
534 maxNumKeyPresses = selectedLineIdx - matchIndex
535 keyPress = func() { self.SelectPreviousItem() }
536 }
537
538 for range maxNumKeyPresses {
539 keyPress()
540 idx := self.getSelectedLineIdx()
541 // It is important to use view.BufferLines() here and not lines, because it

Calls 12

IsFocusedMethod · 0.95
GotoTopMethod · 0.95
getSelectedLineIdxMethod · 0.95
SelectedLineMethod · 0.95
SelectNextItemMethod · 0.95
SelectPreviousItemMethod · 0.95
BufferLinesMethod · 0.80
assertWithRetriesMethod · 0.80
testMethod · 0.80
nameMethod · 0.80
failMethod · 0.80
SprintfMethod · 0.65

Tested by

no test coverage detected