shell completion --format: only show usable methods

In a template you cann call function that are defined on a type, however
this is only useful if they return one value. If it returns more than
one the template cannot know what value it has to display.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2022-05-16 15:10:58 +02:00
parent eeeb88a667
commit 11ff5ffd3b
2 changed files with 10 additions and 1 deletions

View File

@ -1084,7 +1084,12 @@ func getStructFields(f reflect.Value, prefix string) []string {
func getMethodNames(f reflect.Value, prefix string) []string {
suggestions := make([]string, 0, f.NumMethod())
for j := 0; j < f.NumMethod(); j++ {
fname := f.Type().Method(j).Name
method := f.Type().Method(j)
// in a template we can only run functions with one return value
if method.Func.Type().NumOut() != 1 {
continue
}
fname := method.Name
if strings.HasPrefix(fname, prefix) {
// add method name with closing braces
suggestions = append(suggestions, fname+"}}")

View File

@ -37,6 +37,10 @@ func (c Car) internal() int {
return 0
}
func (c Car) TwoOut() (string, string) {
return "", ""
}
func TestAutocompleteFormat(t *testing.T) {
testStruct := struct {
Name string