Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed root.searcher is race condition #642

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ func (root *Root) watchControl() {

// searchGo will go to the line with the matching term after searching.
// Jump by section if JumpTargetSection is true.
func (root *Root) searchGo(ctx context.Context, lN int) {
func (root *Root) searchGo(ctx context.Context, lN int, searcher Searcher) {
root.resetSelect()
root.Doc.lastSearchLN = lN
x := root.searchXPos(lN)
x := root.searchXPos(lN, searcher)
if root.Doc.jumpTargetSection {
root.Doc.searchGoSection(ctx, lN, x)
return
Expand Down
2 changes: 1 addition & 1 deletion oviewer/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ func TestRoot_searchGo(t *testing.T) {
root.Doc.jumpTargetSection = tt.fields.jumpTargetSection
root.setSectionDelimiter(tt.fields.sectionDelimiter)
root.Doc.SectionHeader = true
root.searchGo(ctx, tt.args.lN)
root.searchGo(ctx, tt.args.lN, root.searcher)
if root.Doc.topLN != tt.want {
t.Errorf("searchGo() = %v, want %v", root.Doc.topLN, tt.want)
}
Expand Down
2 changes: 1 addition & 1 deletion oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (root *Root) event(ctx context.Context, ev tcell.Event) bool {
case *eventNextBackSearch:
root.backSearch(ctx, ev.str, -1)
case *eventSearchMove:
root.searchGo(ctx, ev.value)
root.searchGo(ctx, ev.ln, ev.searcher)
case *eventGoto:
root.goLine(ev.value)
case *eventHeader:
Expand Down
16 changes: 9 additions & 7 deletions oviewer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ func (root *Root) searchPosition(str string) [][]int {
}

// searchXPos returns the x position of the first match.
func (root *Root) searchXPos(lineNum int) int {
func (root *Root) searchXPos(lineNum int, searcher Searcher) int {
line := root.Doc.getLineC(lineNum)
indexes := root.searcher.FindAll(line.str)
indexes := searcher.FindAll(line.str)
if len(indexes) == 0 {
return 0
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func (root *Root) searchMove(ctx context.Context, forward bool, lineNum int, sea
if err != nil {
return fmt.Errorf("search:%w:%v", err, word)
}
root.sendSearchMove(n)
root.sendSearchMove(n, searcher)
return nil
})

Expand Down Expand Up @@ -523,13 +523,15 @@ func (root *Root) sendSearchQuit() {
// eventSearchMove represents the move input mode.
type eventSearchMove struct {
tcell.EventTime
value int
ln int
searcher Searcher
}

func (root *Root) sendSearchMove(lineNum int) {
func (root *Root) sendSearchMove(lineNum int, searcher Searcher) {
ev := &eventSearchMove{}
ev.SetEventNow()
ev.value = lineNum
ev.ln = lineNum
ev.searcher = searcher
root.postEvent(ev)
}

Expand Down Expand Up @@ -566,7 +568,7 @@ func (root *Root) incSearch(ctx context.Context, forward bool, lineNum int) {
root.debugMessage(fmt.Sprintf("incSearch: %s", err))
return
}
root.sendSearchMove(n)
root.sendSearchMove(n, searcher)
}()
}

Expand Down