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

Set custom prefix filter on predictor #100

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

WillAbides
Copy link

This replaces #99. I didn't like that you had to change the filter for all arguments or nothing there. This allows the custom prefix filter to be set in the predictor.

It adds the PrefixFilter interface. When a predictor implements PrefixFilter, it will use the supplied filter instead of the default strings.HasPrefix. A new type PrefixFilteringPredictor is also introduced. It wraps both a Predictor and a PrefixFilter.

@codecov
Copy link

codecov bot commented Jul 6, 2019

Codecov Report

Merging #100 into master will increase coverage by 0.23%.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #100      +/-   ##
========================================
+ Coverage   89.76%    90%   +0.23%     
========================================
  Files          12     13       +1     
  Lines         879    900      +21     
========================================
+ Hits          789    810      +21     
  Misses         79     79              
  Partials       11     11
Impacted Files Coverage Δ
complete.go 67.64% <100%> (-4.15%) ⬇️
prefixfilter.go 100% <100%> (ø)
command.go 100% <100%> (ø) ⬆️
predict.go 90.9% <100%> (+7.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2f2ff27...0aa33eb. Read the comment docs.

Copy link
Owner

@posener posener left a comment

Choose a reason for hiding this comment

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

Hey!
Thanks for this PR!
I generally like the idea, and I see why it is needed. The proposed solution is very elegant - much better solution than the previous one.
It looks good, I have some minor comments.

predict.go Outdated Show resolved Hide resolved
prefixfilter.go Outdated Show resolved Hide resolved
prefixfilter.go Outdated Show resolved Hide resolved
}

//PrefixFilteringPredictor is a Predictor that also implements PrefixFilter
type PrefixFilteringPredictor struct {
Copy link
Owner

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?

Copy link
Author

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:

&complete.PrefixFilteringPredictor{
	PrefixFilterFunc: complete.CaseInsensitivePrefixFilter,
	Predictor:        complete.PredictSet("foo", "bar", "baz", )
}

Without it, I end up implementing PrefixFilteringPredictor in my own codebase. Admittedly it's pretty simple to implement though.

Copy link
Owner

Choose a reason for hiding this comment

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

}

//PermissivePrefixFilter always returns true
func PermissivePrefixFilter(_, _ string) bool {
Copy link
Owner

Choose a reason for hiding this comment

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

MatchAny?
Maybe this is also not needed?

}

//CaseInsensitivePrefixFilter ignores case differences between the prefix and tested string
func CaseInsensitivePrefixFilter(s, prefix string) bool {
Copy link
Owner

Choose a reason for hiding this comment

The 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 Predictor and returns a MatchPredictor: it attributes it with case insensitivity.
WDYT?

Copy link
Author

Choose a reason for hiding this comment

The 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 MatchPredictor would be an interface that includes both Predictor and PrefixFilter (that brings up a naming question which I'm ignoring in this comment to stay focused on this part).

I'll work on an API change that looks like that.

Copy link
Owner

Choose a reason for hiding this comment

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

I think you understood correctly.
This will also enables us dropping PrefixFilteringPredictor.

gocomplete/go.sum Outdated Show resolved Hide resolved
@WillAbides
Copy link
Author

Thanks for the comments. I am away at a company event this week, so I can’t make any updates immediately. I should be able to address the comments within a week.

I just wanted to let you know so you don’t think this is an abandoned drive-by PR.

@WillAbides
Copy link
Author

@posener, Do you have a suggestion for a better name than PrefixFilter? It was the best name I could think of at the time without reusing any terms that are already in use in the project. I half expected your first bit of feedback to be "That name is awful, you should call it X".

@posener
Copy link
Owner

posener commented Jul 14, 2019

@posener, Do you have a suggestion for a better name than PrefixFilter? It was the best name I could think of at the time without reusing any terms that are already in use in the project. I half expected your first bit of feedback to be "That name is awful, you should call it X".

:-)

I would go with Matcher, as it allows you to do different sorts of matching between the completed word and the completion option.

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