Skip to content

Commit 2ba8df9

Browse files
authored
Merge pull request #3084 from carapace-sh/jj-alias-completion
jj: add alias completion
2 parents 55356cc + 1b2b5e6 commit 2ba8df9

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

completers/common/jj_completer/cmd/describe.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
)
88

99
var describeCmd = &cobra.Command{
10-
Use: "describe [OPTIONS] [REVISION]",
11-
Short: "Update the change description or other metadata",
12-
Aliases: []string{"desc"},
13-
Run: func(cmd *cobra.Command, args []string) {},
10+
Use: "describe [OPTIONS] [REVISION]",
11+
Short: "Update the change description or other metadata",
12+
Run: func(cmd *cobra.Command, args []string) {},
1413
}
1514

1615
func init() {

completers/common/jj_completer/cmd/root.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package cmd
22

33
import (
4+
"encoding/json"
5+
"strings"
6+
47
"github.com/carapace-sh/carapace"
58
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/jj"
9+
"github.com/carapace-sh/carapace-bridge/pkg/actions/bridge"
10+
shlex "github.com/carapace-sh/carapace-shlex"
611
"github.com/carapace-sh/carapace/pkg/style"
712
"github.com/carapace-sh/carapace/pkg/traverse"
13+
"github.com/carapace-sh/carapace/third_party/golang.org/x/sys/execabs"
814
"github.com/spf13/cobra"
915
"github.com/spf13/pflag"
1016
)
@@ -48,4 +54,40 @@ func init() {
4854
carapace.Gen(rootCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action {
4955
return action.ChdirF(traverse.Flag(rootCmd.Flag("repository")))
5056
})
57+
58+
carapace.Gen(rootCmd).PreRun(func(cmd *cobra.Command, args []string) {
59+
output, err := execabs.Command("jj", "config", "get", "aliases").Output()
60+
if err != nil {
61+
carapace.LOG.Println(err.Error())
62+
return
63+
}
64+
s := string(output)
65+
s = strings.TrimLeft(s, "{ ")
66+
s = strings.TrimRight(s, " }\n")
67+
for _, alias := range strings.Split(s, ", ") {
68+
if name, value, ok := strings.Cut(alias, " = "); ok {
69+
var args []string
70+
if err := json.Unmarshal([]byte(value), &args); err != nil {
71+
carapace.LOG.Println(err.Error())
72+
continue
73+
}
74+
75+
aliasCmd := &cobra.Command{
76+
Use: name,
77+
Short: shlex.Join(args),
78+
GroupID: "alias",
79+
DisableFlagParsing: true,
80+
Run: func(cmd *cobra.Command, args []string) {},
81+
}
82+
cmd.Root().AddCommand(aliasCmd)
83+
84+
carapace.Gen(aliasCmd).PositionalAnyCompletion(
85+
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
86+
c.Args = append(args, c.Args...)
87+
return bridge.ActionCarapaceBin("jj").Invoke(c).ToA()
88+
}),
89+
)
90+
}
91+
}
92+
})
5193
}

0 commit comments

Comments
 (0)