Skip to content

Commit

Permalink
Merge pull request #229 from noborus/fix-jumptarget
Browse files Browse the repository at this point in the history
Fixed search start position
  • Loading branch information
noborus authored Dec 21, 2022
2 parents f56cd1a + 1dfecfc commit a62927d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: Install Go
if: success()
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Check out code into the Go module directory
uses: actions/checkout@v3
cache: true

- name: Run tests
run: make test
12 changes: 6 additions & 6 deletions oviewer/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ func (m *Document) firstLine() int {
}

// SearchLine searches the document and returns the matching line.
func (m *Document) SearchLine(ctx context.Context, searcher Searcher, num int) (int, error) {
num = max(num, 0)
func (m *Document) SearchLine(ctx context.Context, searcher Searcher, lN int) (int, error) {
lN = max(lN, 0)

for n := num; n < m.BufEndNum(); n++ {
for n := lN; n < m.BufEndNum(); n++ {
if searcher.Match(m.GetLine(n)) {
return n, nil
}
Expand All @@ -298,10 +298,10 @@ func (m *Document) SearchLine(ctx context.Context, searcher Searcher, num int) (
}

// BackSearchLine does a backward search on the document and returns a matching line.
func (m *Document) BackSearchLine(ctx context.Context, searcher Searcher, num int) (int, error) {
num = min(num, m.BufEndNum()-1)
func (m *Document) BackSearchLine(ctx context.Context, searcher Searcher, lN int) (int, error) {
lN = min(lN, m.BufEndNum()-1)

for n := num; n >= 0; n-- {
for n := lN; n >= 0; n-- {
if searcher.Match(m.GetLine(n)) {
return n, nil
}
Expand Down
9 changes: 7 additions & 2 deletions oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ func (root *Root) main(ctx context.Context, quitChan chan<- struct{}) {
root.setViewMode(ev.value)
case *searchInput:
searcher := root.setSearcher(root.input.value, root.CaseSensitive)
root.searchMove(ctx, true, root.Doc.topLN+root.Doc.firstLine(), searcher)
l := root.lineNumber(root.headerLen + root.Doc.JumpTarget)
if l.line-root.Doc.topLN > root.Doc.topLN {
l.line = 0
}
root.searchMove(ctx, true, l.line, searcher)
case *backSearchInput:
searcher := root.setSearcher(root.input.value, root.CaseSensitive)
root.searchMove(ctx, false, root.Doc.topLN+root.Doc.firstLine(), searcher)
l := root.lineNumber(root.headerLen)
root.searchMove(ctx, false, l.line, searcher)
case *gotoInput:
root.goLine(ev.value)
case *headerInput:
Expand Down
8 changes: 6 additions & 2 deletions oviewer/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ func (root *Root) incrementalSearch(ctx context.Context) {
return
}

l := root.lineNumber(root.headerLen + root.Doc.JumpTarget)
if l.line-root.Doc.topLN > root.Doc.topLN {
l.line = 0
}
switch root.input.mode {
case Search:
root.incSearch(ctx, true)
root.incSearch(ctx, true, l.line)
case Backsearch:
root.incSearch(ctx, false)
root.incSearch(ctx, false, l.line)
}
}

Expand Down
4 changes: 1 addition & 3 deletions oviewer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (root *Root) searchMove(ctx context.Context, forward bool, lN int, searcher
}

// incSearch implements incremental forward/back search.
func (root *Root) incSearch(ctx context.Context, forward bool) {
func (root *Root) incSearch(ctx context.Context, forward bool, lN int) {
root.Doc.topLN = root.returnStartPosition()

searcher := root.setSearcher(root.input.value, root.CaseSensitive)
Expand All @@ -270,9 +270,7 @@ func (root *Root) incSearch(ctx context.Context, forward bool) {
}

ctx = root.cancelRestart(ctx)

go func() {
lN := root.Doc.topLN + root.Doc.firstLine()
var err error
if forward {
lN, err = root.Doc.SearchLine(ctx, searcher, lN)
Expand Down

0 comments on commit a62927d

Please sign in to comment.