Repository: rsteube/carapace
Pull request #609 (2022-11-23 21:45:57)

Action: added tags


Patch:
diff --git a/action.go b/action.go
index ca7ee42..8e33baf 100644
--- a/action.go
+++ b/action.go
@@ -26,7 +26,6 @@ type CompletionCallback func(c Context) Action
 
 // Cache cashes values of a CompletionCallback for given duration and keys
 func (a Action) Cache(timeout time.Duration, keys ...pkgcache.Key) Action {
-	// TODO static actions are using callback now as well (for performance) - probably best to add a `static` bool to Action for this and check that here
 	if a.callback != nil { // only relevant for callback actions
 		cachedCallback := a.callback
 		_, file, line, _ := runtime.Caller(1) // generate uid from wherever Cache() was called
diff --git a/defaultActions.go b/defaultActions.go
index 17ba723..a97b542 100644
--- a/defaultActions.go
+++ b/defaultActions.go
@@ -183,9 +183,12 @@ func ActionMessage(msg string, a ...interface{}) Action {
 		if len(a) > 0 {
 			msg = fmt.Sprintf(msg, a...)
 		}
-		return ActionStyledValuesDescribed("_", "", style.Default, "ERR", msg, style.Carapace.Error).
-			Invoke(c).Prefix(c.CallbackValue).ToA(). // needs to be prefixed with current callback value to not be filtered out
-			noSpace("*").skipCache(true)
+		m := ActionStyledValuesDescribed("_", "", style.Default, "ERR", msg, style.Carapace.Error).Invoke(c)
+		for index := range m.rawValues {
+			m.rawValues[index].Tag = "messages"
+		}
+		return m.Prefix(c.CallbackValue).ToA(). // needs to be prefixed with current callback value to not be filtered out
+							noSpace("*").skipCache(true)
 	})
 }
 
@@ -363,5 +366,5 @@ func ActionStyles(styles ...string) Action {
 		))
 
 		return batch.ToA()
-	})
+	}).Tag("styles")
 }
diff --git a/internal/common/value.go b/internal/common/value.go
index 57c7a22..62767cd 100644
--- a/internal/common/value.go
+++ b/internal/common/value.go
@@ -18,7 +18,7 @@ type RawValue struct {
 
 // IsMessage checks if the value is a message (ActionMessage)
 func (r RawValue) IsMessage() bool {
-	return r.Value == "ERR" || r.Value == "_"
+	return r.Tag == "messages"
 }
 
 // TrimmedDescription returns the trimmed description
diff --git a/internalActions.go b/internalActions.go
index 2ed2046..7768495 100644
--- a/internalActions.go
+++ b/internalActions.go
@@ -67,7 +67,7 @@ func actionPath(fileSuffixes []string, dirOnly bool) Action {
 			return ActionStyledValues(vals...).Invoke(Context{}).Prefix("./").ToA()
 		}
 		return ActionStyledValues(vals...)
-	})
+	}).Tag("files")
 }
 
 func actionFlags(cmd *cobra.Command) Action {
@@ -120,20 +120,22 @@ func actionFlags(cmd *cobra.Command) Action {
 			}
 		}
 		return ActionValuesDescribed(vals...)
-	})
+	}).Tag("flags")
 }
 
 func actionSubcommands(cmd *cobra.Command) Action {
-	vals := make([]string, 0)
-	for _, subcommand := range cmd.Commands() {
-		if !subcommand.Hidden && subcommand.Deprecated == "" {
-			vals = append(vals, subcommand.Name(), subcommand.Short)
-			for _, alias := range subcommand.Aliases {
-				vals = append(vals, alias, subcommand.Short)
+	return ActionCallback(func(c Context) Action {
+		vals := make([]string, 0)
+		for _, subcommand := range cmd.Commands() {
+			if !subcommand.Hidden && subcommand.Deprecated == "" {
+				vals = append(vals, subcommand.Name(), subcommand.Short)
+				for _, alias := range subcommand.Aliases {
+					vals = append(vals, alias, subcommand.Short)
+				}
 			}
 		}
-	}
-	return ActionValuesDescribed(vals...)
+		return ActionValuesDescribed(vals...)
+	}).Tag("commands")
 }
 
 func actionRawValues(rawValues ...common.RawValue) Action {


Test patch:
diff --git a/action_test.go b/action_test.go
index 6c86a8c..b8d9b72 100644
--- a/action_test.go
+++ b/action_test.go
@@ -204,8 +204,12 @@ func TestActionFilesChdir(t *testing.T) {
 }
 
 func TestActionMessage(t *testing.T) {
+	expected := ActionStyledValuesDescribed("_", "", style.Default, "ERR", "example message", style.Carapace.Error).noSpace("*").skipCache(true).Invoke(Context{}).Prefix("docs/")
+	for index := range expected.rawValues {
+		expected.rawValues[index].Tag = "messages"
+	}
 	assertEqual(t,
-		ActionStyledValuesDescribed("_", "", style.Default, "ERR", "example message", style.Carapace.Error).noSpace("*").skipCache(true).Invoke(Context{}).Prefix("docs/"),
+		expected,
 		ActionMessage("example message").Invoke(Context{CallbackValue: "docs/"}),
 	)
 }
diff --git a/defaultActions_test.go b/defaultActions_test.go
index 77538e4..9ffff9f 100644
--- a/defaultActions_test.go
+++ b/defaultActions_test.go
@@ -39,7 +39,7 @@ func TestActionFlags(t *testing.T) {
 
 	cmd.Flag("alpha").Changed = true
 	a := actionFlags(cmd).Invoke(Context{CallbackValue: "-a"})
-	assertEqual(t, ActionValuesDescribed("b", "").NoSpace().Invoke(Context{}).Prefix("-a"), a)
+	assertEqual(t, ActionValuesDescribed("b", "").Tag("flags").NoSpace().Invoke(Context{}).Prefix("-a"), a)
 }
 
 func TestActionExecCommandEnv(t *testing.T) {