Fixed project / alias autocomplete functions

This commit is contained in:
Ryan McGuire 2023-12-20 11:23:11 -05:00
parent 2082287bdd
commit f5201e8fea
4 changed files with 17 additions and 13 deletions

View File

@ -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,

View File

@ -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) (

View File

@ -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(

View File

@ -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
}