Repository: rsteube/carapace
Pull request #906 (2023-08-27 15:12:34)

MultiPartsP: fix suffix


Patch:
diff --git a/action.go b/action.go
index 365009b..7f6364d 100644
--- a/action.go
+++ b/action.go
@@ -188,25 +188,31 @@ func (a Action) MultiPartsP(delimiter string, pattern string, f func(placeholder
 				if len(segments) == (len(c.Parts) + 1) {
 					matchedSegments[segments[len(c.Parts)]] = invoked.rawValues[index]
 				} else {
-					if segment := segments[len(c.Parts)]; rPlaceholder.MatchString(segment) {
-						matchedSegments[segment] = common.RawValue{}
-					} else {
-						matchedSegments[segment+delimiter] = common.RawValue{}
-					}
+					matchedSegments[segments[len(c.Parts)]+delimiter] = common.RawValue{}
 				}
 			}
 
 			actions := make([]Action, 0, len(matchedSegments))
 			for key, value := range matchedSegments {
-				if rPlaceholder.MatchString(key) {
-					actions = append(actions, f(key, matchedData))
+				if trimmedKey := strings.TrimSuffix(key, delimiter); rPlaceholder.MatchString(trimmedKey) {
+					suffix := ""
+					if strings.HasSuffix(key, delimiter) {
+						suffix = delimiter
+					}
+					actions = append(actions, ActionCallback(func(c Context) Action {
+						invoked := f(trimmedKey, matchedData).Invoke(c).Suffix(suffix)
+						for index := range invoked.rawValues {
+							invoked.rawValues[index].Display += suffix
+						}
+						return invoked.ToA()
+					}))
 				} else {
 					actions = append(actions, ActionStyledValuesDescribed(key, value.Description, value.Style)) // TODO tag,..
 				}
 			}
 
 			// TODO verify
-			a := Batch(actions...).ToA().NoSpace()
+			a := Batch(actions...).ToA()
 			a.meta.Merge(invoked.meta)
 			return a
 		})
diff --git a/example/cmd/modifier.go b/example/cmd/modifier.go
index 8efe3b1..7b04bd0 100644
--- a/example/cmd/modifier.go
+++ b/example/cmd/modifier.go
@@ -134,6 +134,7 @@ func init() {
 			"dir/subdir2/fileC.txt",
 		).MultiParts("/"),
 		"multipartsp": carapace.ActionStyledValuesDescribed(
+			"keys/<key>", "key example", style.Default,
 			"keys/<key>/<value>", "key/value example", style.Default,
 			"styles/custom", "custom style", style.Of(style.Blue, style.Blink),
 			"styles", "list", style.Yellow,


Test patch:
diff --git a/example/cmd/modifier_test.go b/example/cmd/modifier_test.go
index 34e1972..d536f36 100644
--- a/example/cmd/modifier_test.go
+++ b/example/cmd/modifier_test.go
@@ -259,15 +259,17 @@ func TestMultiPartsP(t *testing.T) {
 				"keys/", "", style.Default,
 				"styles", "list", style.Yellow,
 				"styles/", "", style.Default,
-			).NoSpace().
+			).NoSpace('/').
 				Usage("MultiPartsP()"))
 
 		s.Run("modifier", "--multipartsp", "keys/").
 			Expect(carapace.ActionValues(
 				"key1",
+				"key1/",
 				"key2",
+				"key2/",
 			).Prefix("keys/").
-				NoSpace().
+				NoSpace('/').
 				Usage("MultiPartsP()"))
 
 		s.Run("modifier", "--multipartsp", "keys/key1/").
@@ -275,7 +277,7 @@ func TestMultiPartsP(t *testing.T) {
 				"val1",
 				"val2",
 			).Prefix("keys/key1/").
-				NoSpace().
+				NoSpace('/').
 				Usage("MultiPartsP()"))
 
 		s.Run("modifier", "--multipartsp", "keys/key2/").
@@ -283,7 +285,7 @@ func TestMultiPartsP(t *testing.T) {
 				"val3",
 				"val4",
 			).Prefix("keys/key2/").
-				NoSpace().
+				NoSpace('/').
 				Usage("MultiPartsP()"))
 
 		s.Run("modifier", "--multipartsp", "styles/c").
@@ -297,7 +299,7 @@ func TestMultiPartsP(t *testing.T) {
 				),
 			).ToA().
 				Prefix("styles/").
-				NoSpace().
+				NoSpace('/', 'r').
 				Usage("MultiPartsP()"))
 	})
 }