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

Enable closing of fuzzyfinder from the caller by passing context #165

Merged
merged 4 commits into from
Nov 25, 2022

Conversation

ahsan
Copy link
Contributor

@ahsan ahsan commented Nov 17, 2022

This PR adds a new option called WithContext. This enables callers of fuzzyfinder.Find to cancel the passed context and forcefully make Find return if completing it is no longer desirable.

An example use case is when Find is called with the WithHotReload option, but the caller does not have any items to put in the slice (e.g due to a failed network call).

Please let me know what you think!

Copy link
Owner

@ktr0731 ktr0731 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!
I think that it makes sense to provide an option that accepts context 👍
Almost the implementation looks good, but I suggested some trivial points. Could you work on them when you have time?

fuzzyfinder.go Outdated
@@ -94,6 +98,12 @@ func (f *finder) initFinder(items []string, matched []matching.Matched, opt opt)
if err := f.term.Init(); err != nil {
return errors.Wrap(err, "failed to initialize screen")
}

eventsChan := make(chan tcell.Event)
quitChan := make(chan struct{})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This channel is not used anywhere so we can use a nil channel.

fuzzyfinder.go Outdated
@@ -25,6 +25,7 @@ import (
var (
// ErrAbort is returned from Find* functions if there are no selections.
ErrAbort = errors.New("abort")
ErrCancel = errors.New("cancel")
Copy link
Owner

@ktr0731 ktr0731 Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we don't need a self-defined cancellation error. Context is a standard mechanism, so how about returning ctx.Err() simply?

@codecov
Copy link

codecov bot commented Nov 18, 2022

Codecov Report

Merging #165 (0af1409) into master (504da0e) will decrease coverage by 0.25%.
The diff coverage is 87.50%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #165      +/-   ##
==========================================
- Coverage   87.68%   87.42%   -0.26%     
==========================================
  Files           5        5              
  Lines         828      851      +23     
==========================================
+ Hits          726      744      +18     
- Misses         89       94       +5     
  Partials       13       13              

@ahsan ahsan requested a review from ktr0731 November 18, 2022 15:37
@ahsan ahsan requested a review from ktr0731 November 22, 2022 17:40
Copy link
Owner

@ktr0731 ktr0731 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you!

@ktr0731 ktr0731 merged commit 663fa92 into ktr0731:master Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants