-
Notifications
You must be signed in to change notification settings - Fork 72
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
Set custom prefix filter on predictor #100
base: master
Are you sure you want to change the base?
Changes from all commits
5b02316
f4f0aa2
f51d684
0d8a15e
0aa33eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package complete | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// PrefixFilter filters a predictor's options based on the prefix | ||
type PrefixFilter interface { | ||
FilterPrefix(str, prefix string) bool | ||
} | ||
|
||
// PrefixFilteringPredictor is a Predictor that also implements PrefixFilter | ||
type PrefixFilteringPredictor struct { | ||
Predictor Predictor | ||
PrefixFilterFunc func(s, prefix string) bool | ||
} | ||
|
||
func (p *PrefixFilteringPredictor) Predict(a Args) []string { | ||
if p.Predictor == nil { | ||
return []string{} | ||
} | ||
return p.Predictor.Predict(a) | ||
} | ||
|
||
func (p *PrefixFilteringPredictor) FilterPrefix(str, prefix string) bool { | ||
if p.PrefixFilterFunc == nil { | ||
return defaultPrefixFilter(str, prefix) | ||
} | ||
return p.PrefixFilterFunc(str, prefix) | ||
} | ||
|
||
// defaultPrefixFilter is the PrefixFilter used when none is set | ||
func defaultPrefixFilter(s, prefix string) bool { | ||
return strings.HasPrefix(s, prefix) | ||
} | ||
|
||
// PermissivePrefixFilter always returns true | ||
func PermissivePrefixFilter(_, _ string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return true | ||
} | ||
|
||
// CaseInsensitivePrefixFilter ignores case differences between the prefix and tested string | ||
func CaseInsensitivePrefixFilter(s, prefix string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Consider a different API where this is a factory method that accepts a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I like that suggestion if I understand it correctly. I'm assuming that I'll work on an API change that looks like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you understood correctly. |
||
if len(prefix) > len(s) { | ||
return false | ||
} | ||
return strings.EqualFold(prefix, s[:len(prefix)]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this struct definition is not necessary?
If anyone wants to use the new interface, They will just make sure that the predictor also implements the new interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will be useful in instances where somebody wants to use an existing Predictor with a non-default PrefixFilter.
An example would be if I want to use a SetPredictor with a case insensitive matcher I would do something like:
Without it, I end up implementing PrefixFilteringPredictor in my own codebase. Admittedly it's pretty simple to implement though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following https://github.com/posener/complete/pull/100/files#r301385506, this won't be needed.