Skip to content

Commit

Permalink
meta: added Merge function
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 9, 2022
1 parent 6dcbbee commit 707d446
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
8 changes: 2 additions & 6 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (a Action) Cache(timeout time.Duration, keys ...pkgcache.Key) Action {
a.callback = func(c Context) Action {
if cacheFile, err := cache.File(file, line, keys...); err == nil {
if rawValues, err := cache.Load(cacheFile, timeout); err == nil {
return actionRawValues(rawValues...)
return Action{rawValues: rawValues}
}
invokedAction := (Action{callback: cachedCallback}).Invoke(c)
if invokedAction.meta.Messages.IsEmpty() {
Expand Down Expand Up @@ -66,11 +66,7 @@ func (a Action) nestedAction(c Context, maxDepth int) Action {
}
if a.rawValues == nil && a.callback != nil {
result := a.callback(c).nestedAction(c, maxDepth-1)
if usage := a.meta.Usage; usage != "" {
result.meta.Usage = usage
}
result.meta.Nospace.Merge(a.meta.Nospace)
result.meta.Messages.Merge(a.meta.Messages)
result.meta.Merge(a.meta)
return result
}
return a
Expand Down
17 changes: 8 additions & 9 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ func ActionImport(output []byte) Action {
if err := json.Unmarshal(output, &e); err != nil {
return ActionMessage(err.Error())
}
a := actionRawValues(e.RawValues...)
a.meta.Nospace = e.Nospace
a.meta.Usage = e.Usage
a.meta.Messages = e.Messages
return a
return Action{
rawValues: e.RawValues,
meta: e.Meta,
}
})
}

Expand Down Expand Up @@ -130,7 +129,7 @@ func ActionValues(values ...string) Action {
for _, val := range values {
vals = append(vals, common.RawValue{Value: val, Display: val, Description: "", Style: style.Default})
}
return actionRawValues(vals...)
return Action{rawValues: vals}
})
}

Expand All @@ -145,7 +144,7 @@ func ActionStyledValues(values ...string) Action {
for i := 0; i < len(values); i += 2 {
vals = append(vals, common.RawValue{Value: values[i], Display: values[i], Description: "", Style: values[i+1]})
}
return actionRawValues(vals...)
return Action{rawValues: vals}
})
}

Expand All @@ -160,7 +159,7 @@ func ActionValuesDescribed(values ...string) Action {
for i := 0; i < len(values); i += 2 {
vals = append(vals, common.RawValue{Value: values[i], Display: values[i], Description: values[i+1], Style: style.Default})
}
return actionRawValues(vals...)
return Action{rawValues: vals}
})
}

Expand All @@ -175,7 +174,7 @@ func ActionStyledValuesDescribed(values ...string) Action {
for i := 0; i < len(values); i += 3 {
vals = append(vals, common.RawValue{Value: values[i], Display: values[i], Description: values[i+1], Style: values[i+2]})
}
return actionRawValues(vals...)
return Action{rawValues: vals}
})
}

Expand Down
8 changes: 8 additions & 0 deletions internal/common/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ type Meta struct {
Nospace SuffixMatcher
Usage string
}

func (m *Meta) Merge(other Meta) {
if usage := other.Usage; usage != "" {
m.Usage = usage
}
m.Nospace.Merge(other.Nospace)
m.Messages.Merge(other.Messages)
}
10 changes: 3 additions & 7 deletions internal/shell/export/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ import (
)

type Export struct {
Version string
Usage string
Messages common.Messages
Nospace common.SuffixMatcher
Version string
common.Meta
RawValues common.RawValues
}

func ActionRawValues(currentWord string, meta common.Meta, values common.RawValues) string {
sort.Sort(common.ByValue(values))
m, _ := json.Marshal(Export{
Version: version(),
Usage: meta.Usage,
Messages: meta.Messages,
Nospace: meta.Nospace,
Meta: meta,
RawValues: values,
})
return string(m)
Expand Down
7 changes: 0 additions & 7 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"regexp"
"strings"

"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/internal/pflagfork"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -144,9 +143,3 @@ func actionSubcommands(cmd *cobra.Command) Action {
return batch.ToA()
})
}

func actionRawValues(rawValues ...common.RawValue) Action {
return Action{
rawValues: rawValues,
}
}
18 changes: 5 additions & 13 deletions invokedAction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,21 @@ func (a InvokedAction) Filter(values []string) InvokedAction {
// c := a.Merge(b) // ["A", "B", "C"]
func (a InvokedAction) Merge(others ...InvokedAction) InvokedAction {
uniqueRawValues := make(map[string]common.RawValue)
nospace := a.meta.Nospace
usage := a.meta.Usage
messages := a.meta.Messages
var meta common.Meta
for _, other := range append([]InvokedAction{a}, others...) {
for _, c := range other.rawValues {
uniqueRawValues[c.Value] = c
}
nospace.Merge(other.meta.Nospace)
if other.meta.Usage != "" {
usage = other.meta.Usage
}
messages.Merge(other.meta.Messages)
meta.Merge(other.meta)
}

rawValues := make([]common.RawValue, 0, len(uniqueRawValues))
for _, c := range uniqueRawValues {
rawValues = append(rawValues, c)
}

invoked := InvokedAction{actionRawValues(rawValues...)}
invoked.meta.Usage = usage
invoked.meta.Nospace.Merge(nospace)
invoked.meta.Messages = messages // TODO verify & optimize
invoked := InvokedAction{Action{rawValues: rawValues}}
invoked.meta.Merge(meta)
return invoked
}

Expand Down Expand Up @@ -144,7 +136,7 @@ func (a InvokedAction) ToMultiPartsA(dividers ...string) Action {
vals = append(vals, val)
}

a := actionRawValues(vals...)
a := Action{rawValues: vals}
for _, divider := range dividers {
if runes := []rune(divider); len(runes) == 0 {
a.meta.Nospace.Add('*')
Expand Down

0 comments on commit 707d446

Please sign in to comment.