Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 25, 2023
1 parent 8713895 commit 6800db4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
4 changes: 2 additions & 2 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c Carapace) PositionalCompletion(action ...Action) {

// PositionalAnyCompletion defines completion for any positional arguments not already defined.
func (c Carapace) PositionalAnyCompletion(action Action) {
storage.get(c.cmd).positionalAny = action
storage.get(c.cmd).positionalAny = &action
}

// DashCompletion defines completion for positional arguments after dash (`--`) using a list of Actions.
Expand All @@ -67,7 +67,7 @@ func (c Carapace) DashCompletion(action ...Action) {

// DashAnyCompletion defines completion for any positional arguments after dash (`--`) not already defined.
func (c Carapace) DashAnyCompletion(action Action) {
storage.get(c.cmd).dashAny = action
storage.get(c.cmd).dashAny = &action
}

// FlagCompletion defines completion for flags using a map consisting of name and Action.
Expand Down
6 changes: 5 additions & 1 deletion compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
func registerValidArgsFunction(cmd *cobra.Command) {
if cmd.ValidArgsFunction == nil {
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
action := storage.getPositional(cmd, len(args)).Invoke(Context{Args: args, Value: toComplete})
// TODO check storage.hasPositional to prevent loop
action := Action{}.Invoke(Context{Args: args, Value: toComplete}) // TODO just IvokedAction{} ok?
if storage.hasPositional(cmd, len(args)) {
action = storage.getPositional(cmd, len(args)).Invoke(Context{Args: args, Value: toComplete})
}
return cobraValuesFor(action), cobraDirectiveFor(action)
}
}
Expand Down
6 changes: 5 additions & 1 deletion defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,11 @@ func ActionPositional(cmd *cobra.Command) Action {
c.Args = cmd.Flags().Args()
entry := storage.get(cmd)

a := entry.positionalAny
var a Action
if entry.positionalAny != nil {
a = *entry.positionalAny
}

if index := len(c.Args); index < len(entry.positional) {
a = entry.positional[len(c.Args)]
}
Expand Down
38 changes: 30 additions & 8 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type entry struct {
flag ActionMap
flagMutex sync.RWMutex
positional []Action
positionalAny Action
positionalAny *Action
dash []Action
dashAny Action
dashAny *Action
preinvoke func(cmd *cobra.Command, flag *pflag.Flag, action Action) Action
prerun func(cmd *cobra.Command, args []string)
bridged bool
Expand Down Expand Up @@ -60,8 +60,7 @@ func (s _storage) bridge(cmd *cobra.Command) {
defer bridgeMutex.Unlock()

if !entry.initialized {
// TODO only if completion is defined in carapace
// registerValidArgsFunction(cmd)
registerValidArgsFunction(cmd)
registerFlagCompletion(cmd)
entry.initialized = true
}
Expand Down Expand Up @@ -133,6 +132,24 @@ func (s _storage) preinvoke(cmd *cobra.Command, flag *pflag.Flag, action Action)
return a
}

func (s _storage) hasPositional(cmd *cobra.Command, index int) bool {
entry := s.get(cmd)
isDash := common.IsDash(cmd)

// TODO fallback to cobra defined completion if exists

switch {
case !isDash && len(entry.positional) > index:
return true
case !isDash:
return entry.positionalAny != nil
case len(entry.dash) > index:
return true
default:
return entry.dashAny != nil
}
}

func (s _storage) getPositional(cmd *cobra.Command, index int) Action {
entry := s.get(cmd)
isDash := common.IsDash(cmd)
Expand All @@ -142,14 +159,19 @@ func (s _storage) getPositional(cmd *cobra.Command, index int) Action {
var a Action
switch {
case !isDash && len(entry.positional) > index:
a = s.preinvoke(cmd, nil, entry.positional[index])
a = entry.positional[index]
case !isDash:
a = s.preinvoke(cmd, nil, entry.positionalAny)
if entry.positionalAny != nil {
a = *entry.positionalAny
}
case len(entry.dash) > index:
a = s.preinvoke(cmd, nil, entry.dash[index])
a = entry.dash[index]
default:
a = s.preinvoke(cmd, nil, entry.dashAny)
if entry.dashAny != nil {
a = *entry.dashAny
}
}
a = s.preinvoke(cmd, nil, a)

return ActionCallback(func(c Context) Action {
invoked := a.Invoke(c)
Expand Down

0 comments on commit 6800db4

Please sign in to comment.