Repository: rsteube/carapace
Pull request #660 (2023-01-02 01:25:45)

separate tags for files and directories


Patch:
diff --git a/defaultActions.go b/defaultActions.go
index 9da79bc..2d2bc6d 100644
--- a/defaultActions.go
+++ b/defaultActions.go
@@ -106,7 +106,7 @@ func ActionDirectories() Action {
 			}
 			return ""
 		})
-	})
+	}).Tag("directories")
 }
 
 // ActionFiles completes files with optional suffix filtering.
@@ -118,7 +118,7 @@ func ActionFiles(suffix ...string) Action {
 			}
 			return ""
 		})
-	})
+	}).Tag("files")
 }
 
 // ActionValues completes arbitrary keywords (values).
diff --git a/internalActions.go b/internalActions.go
index b6c2aa3..9f79edf 100644
--- a/internalActions.go
+++ b/internalActions.go
@@ -66,7 +66,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 {


Test patch:
diff --git a/action_test.go b/action_test.go
index ae2cc95..593438b 100644
--- a/action_test.go
+++ b/action_test.go
@@ -121,7 +121,7 @@ func TestActionDirectories(t *testing.T) {
 			"internal/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
-		).NoSpace('/').Invoke(Context{}),
+		).NoSpace('/').Tag("directories").Invoke(Context{}),
 		ActionDirectories().Invoke(Context{CallbackValue: ""}).Filter([]string{"vendor/"}),
 	)
 
@@ -132,7 +132,7 @@ func TestActionDirectories(t *testing.T) {
 			"internal/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
-		).NoSpace('/').Invoke(Context{}).Prefix("./"),
+		).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("./"),
 		ActionDirectories().Invoke(Context{CallbackValue: "./"}).Filter([]string{"./vendor/"}),
 	)
 
@@ -140,14 +140,14 @@ func TestActionDirectories(t *testing.T) {
 		ActionStyledValues(
 			"_test/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
-		).NoSpace('/').Invoke(Context{}).Prefix("example/"),
+		).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("example/"),
 		ActionDirectories().Invoke(Context{CallbackValue: "example/"}),
 	)
 
 	assertEqual(t,
 		ActionStyledValues(
 			"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
-		).NoSpace('/').Invoke(Context{}).Prefix("example/"),
+		).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("example/"),
 		ActionDirectories().Invoke(Context{CallbackValue: "example/cm"}),
 	)
 }
@@ -161,7 +161,7 @@ func TestActionFiles(t *testing.T) {
 			"internal/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
-		).NoSpace('/').Invoke(Context{}),
+		).NoSpace('/').Tag("files").Invoke(Context{}),
 		ActionFiles(".md").Invoke(Context{CallbackValue: ""}).Filter([]string{"vendor/"}),
 	)
 
@@ -172,7 +172,7 @@ func TestActionFiles(t *testing.T) {
 			"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"main.go", style.Of("fg-default", "bg-default"),
 			"main_test.go", style.Of("fg-default", "bg-default"),
-		).NoSpace('/').Invoke(Context{}).Prefix("example/"),
+		).NoSpace('/').Tag("files").Invoke(Context{}).Prefix("example/"),
 		ActionFiles().Invoke(Context{CallbackValue: "example/"}).Filter([]string{"example/example"}),
 	)
 }
@@ -194,7 +194,7 @@ func TestActionFilesChdir(t *testing.T) {
 		ActionStyledValues(
 			"action.go", style.Of("fg-default", "bg-default"),
 			"snippet.go", style.Of("fg-default", "bg-default"),
-		).NoSpace('/').Invoke(Context{}).Prefix("elvish/"),
+		).NoSpace('/').Tag("files").Invoke(Context{}).Prefix("elvish/"),
 		ActionFiles().Chdir("internal/shell").Invoke(Context{CallbackValue: "elvish/"}),
 	)
 
diff --git a/example/cmd/action_test.go b/example/cmd/action_test.go
index cfade44..e27e82e 100644
--- a/example/cmd/action_test.go
+++ b/example/cmd/action_test.go
@@ -22,12 +22,12 @@ func TestAction(t *testing.T) {
 
 		s.Run("action", "--callback", "").Expect(carapace.ActionMessage("values flag is not set"))
 		s.Run("action", "--values", "first", "--callback", "").Expect(carapace.ActionMessage("values flag is set to: 'first'"))
-		s.Run("action", "--directories", "").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath))
-		s.Run("action", "--directories", "dirB/").Expect(carapace.ActionValues("dirC/").Prefix("dirB/").StyleF(style.ForPath))
+		s.Run("action", "--directories", "").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("directories").StyleF(style.ForPath))
+		s.Run("action", "--directories", "dirB/").Expect(carapace.ActionValues("dirC/").Prefix("dirB/").Tag("directories").StyleF(style.ForPath))
 		s.Run("action", "--exec-command", "").Expect(carapace.ActionValues("origin", "fork"))
-		s.Run("action", "--files", "").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").StyleF(style.ForPath))
-		s.Run("action", "--files-filtered", "").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath))
-		s.Run("action", "--files-filtered", "dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Prefix("dirB/").StyleF(style.ForPath))
+		s.Run("action", "--files", "").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").Tag("files").StyleF(style.ForPath))
+		s.Run("action", "--files-filtered", "").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("files").StyleF(style.ForPath))
+		s.Run("action", "--files-filtered", "dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Tag("files").Prefix("dirB/").StyleF(style.ForPath))
 		s.Run("action", "--import", "").Expect(carapace.ActionValues("first", "second", "third"))
 		s.Run("action", "--import", "s").Expect(carapace.ActionValues("second"))
 		s.Run("action", "--message", "").Expect(carapace.ActionMessage("example message"))
@@ -37,7 +37,7 @@ func TestAction(t *testing.T) {
 		s.Run("action", "--multiparts-nested", "").Expect(carapace.ActionValues("DIRECTORY", "FILE", "VALUE").Suffix("="))
 		s.Run("action", "--multiparts-nested", "VALUE=").Expect(carapace.ActionValues("one", "two", "three").Prefix("VALUE="))
 		s.Run("action", "--multiparts-nested", "VALUE=two,").Expect(carapace.ActionValues("DIRECTORY", "FILE").Prefix("VALUE=two,").Suffix("="))
-		s.Run("action", "--multiparts-nested", "VALUE=two,DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath).Prefix("VALUE=two,DIRECTORY="))
+		s.Run("action", "--multiparts-nested", "VALUE=two,DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("directories").StyleF(style.ForPath).Prefix("VALUE=two,DIRECTORY="))
 		s.Run("action", "--styled-values", "s").Expect(carapace.ActionStyledValues("second", style.Blue))
 		s.Run("action", "--styled-values-described", "t").Expect(carapace.ActionStyledValuesDescribed("third", "description of third", style.Of("#112233", style.Italic), "thirdalias", "description of third", style.BgBrightMagenta))
 		s.Run("action", "--values", "sec").Expect(carapace.ActionValues("second"))
diff --git a/example/cmd/multiparts_test.go b/example/cmd/multiparts_test.go
index 6f971c2..6a92295 100644
--- a/example/cmd/multiparts_test.go
+++ b/example/cmd/multiparts_test.go
@@ -20,12 +20,12 @@ func TestMultiparts(t *testing.T) {
 		s.Run("multiparts", "").Expect(carapace.ActionValues("DIRECTORY", "FILE", "VALUE").Suffix("="))
 		s.Run("multiparts", "D").Expect(carapace.ActionValues("DIRECTORY").Suffix("="))
 		s.Run("multiparts", "DIRECTORY").Expect(carapace.ActionValues("DIRECTORY").Suffix("="))
-		s.Run("multiparts", "DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath).Prefix("DIRECTORY="))
+		s.Run("multiparts", "DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("directories").StyleF(style.ForPath).Prefix("DIRECTORY="))
 		s.Run("multiparts", "VALUE=").Expect(carapace.ActionValues("one", "two", "three").Prefix("VALUE="))
 		s.Run("multiparts", "VALUE=o").Expect(carapace.ActionValues("one").Prefix("VALUE="))
 		s.Run("multiparts", "VALUE=one,").Expect(carapace.ActionValues("DIRECTORY", "FILE").Prefix("VALUE=one,").Suffix("="))
 		s.Run("multiparts", "VALUE=one,F").Expect(carapace.ActionValues("FILE").Prefix("VALUE=one,").Suffix("="))
-		s.Run("multiparts", "VALUE=one,FILE=").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
-		s.Run("multiparts", "VALUE=one,FILE=dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Prefix("dirB/").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
+		s.Run("multiparts", "VALUE=one,FILE=").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").Tag("files").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
+		s.Run("multiparts", "VALUE=one,FILE=dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Tag("files").Prefix("dirB/").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
 	})
 }