From f5201e8fead7426589cb54bb035b0aebfbc1fc3f Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Wed, 20 Dec 2023 11:23:11 -0500 Subject: [PATCH] Fixed project / alias autocomplete functions --- cmd/project_open.go | 3 +-- cmd/util_completion.go | 5 +++-- internal/projects/fuzz.go | 2 +- internal/projects/projects.go | 20 ++++++++++++-------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/project_open.go b/cmd/project_open.go index 3b56860..6980a8e 100644 --- a/cmd/project_open.go +++ b/cmd/project_open.go @@ -15,8 +15,7 @@ var projectOpenCmd = &cobra.Command{ Use: "open [fuzzy alias search]", Short: "Open project in your IDE", Aliases: []string{"goto", "cd"}, - Args: cobra.MaximumNArgs(1), - ArgAliases: []string{"project"}, + Args: cobra.OnlyValidArgs, ValidArgsFunction: validAliasesFunc, Long: projOpenCmdLong, Run: projectOpenCmdRun, diff --git a/cmd/util_completion.go b/cmd/util_completion.go index 2514b17..74f0f74 100644 --- a/cmd/util_completion.go +++ b/cmd/util_completion.go @@ -4,12 +4,13 @@ import "github.com/spf13/cobra" func validProjectsFunc(cmd *cobra.Command, args []string, toComplete string) ( []string, cobra.ShellCompDirective) { - return cache.ProjectStrings(), cobra.ShellCompDirectiveDefault + return cache.ProjectStrings(toComplete), cobra.ShellCompDirectiveNoFileComp } func validAliasesFunc(cmd *cobra.Command, args []string, toComplete string) ( []string, cobra.ShellCompDirective) { - return cache.AliasStrings(), cobra.ShellCompDirectiveDefault + initProjectCache(cmd, args) + return cache.AliasStrings(toComplete), cobra.ShellCompDirectiveNoFileComp } func validProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete string) ( diff --git a/internal/projects/fuzz.go b/internal/projects/fuzz.go index 8ccd1f5..49b538e 100644 --- a/internal/projects/fuzz.go +++ b/internal/projects/fuzz.go @@ -10,7 +10,7 @@ import ( // matched based on its Levenshtein distance, along with an integer // indicating number of matches found func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias { - ranks := fuzzy.RankFindFold(name, c.AliasStrings()) + ranks := fuzzy.RankFindFold(name, c.AliasStrings("")) if ranks.Len() == 1 { c.log.Debug("Fuzzy found alias result", c.log.Args( diff --git a/internal/projects/projects.go b/internal/projects/projects.go index 440c67a..e8bfcf7 100644 --- a/internal/projects/projects.go +++ b/internal/projects/projects.go @@ -65,18 +65,22 @@ func (c *Cache) ProjectString(p *gitlab.Project) string { return info.String() } -func (c *Cache) ProjectStrings() []string { - projects := make([]string, len(c.Projects)) - for i, p := range c.Projects { - projects[i] = p.NameWithNamespace +func (c *Cache) ProjectStrings(prefix string) []string { + projects := make([]string, 0, len(c.Projects)) + for _, p := range c.Projects { + if strings.HasPrefix(p.NameWithNamespace, prefix) { + projects = append(projects, p.NameWithNamespace) + } } return projects } -func (c *Cache) AliasStrings() []string { - aliases := make([]string, len(c.Aliases)) - for i, a := range c.Aliases { - aliases[i] = a.Alias +func (c *Cache) AliasStrings(prefix string) []string { + aliases := make([]string, 0, len(c.Aliases)) + for _, a := range c.Aliases { + if strings.HasPrefix(a.Alias, prefix) { + aliases = append(aliases, a.Alias) + } } return aliases }