Skip to content

Commit

Permalink
nix: simplify action signature
Browse files Browse the repository at this point in the history
- fix cache
- fix macro compability
  • Loading branch information
rsteube committed Dec 22, 2024
1 parent 937bd7d commit c229ea7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
23 changes: 11 additions & 12 deletions completers/nix_completer/cmd/action/flake.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ import (
// in the flake registry.
// It takes in the subcommand being completed
//
// nixpkgs#hello
// .#foo
func ActionFlakeRefs(fullCmd []string) carapace.Action {
// nixpkgs#hello
// .#foo
func ActionFlakeRefs() carapace.Action {
return carapace.ActionMultiPartsN("#", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
// TODO add directory completion externally
return nix.ActionFlakes().Suffix("#")
default:
return ActionFlakeAttributes(fullCmd, c.Parts[0], c.Value)
return ActionFlakeAttributes(c.Parts[0])
}
})
}
Expand All @@ -33,16 +34,15 @@ func ActionFlakeRefs(fullCmd []string) carapace.Action {
// Completions are only supplied for local flakes or flakes
// in the registry.
//
// hello
// packages.x86_64-linux.hello
func ActionFlakeAttributes(fullCmd []string, flake, flakePath string) carapace.Action {
// hello
// packages.x86_64-linux.hello
func ActionFlakeAttributes(flake string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// Get completions from the flake registry
c.Setenv("NIX_GET_COMPLETIONS", fmt.Sprint(len(fullCmd)))
completionArgs := append(fullCmd[1:], flake)
c.Setenv("NIX_GET_COMPLETIONS", "2")

var stdout, stderr bytes.Buffer
cmd := c.Command("nix", completionArgs...)
cmd := c.Command("nix", "build", flake)
cmd.Stdout = &stdout
cmd.Stderr = &stderr

Expand All @@ -68,8 +68,7 @@ func ActionFlakeAttributes(fullCmd []string, flake, flakePath string) carapace.A
stdout.Reset()
stderr.Reset()

completionArgs = append(fullCmd[1:], flake+"#"+flakePath)
cmd = c.Command("nix", completionArgs...)
cmd = c.Command("nix", "build", fmt.Sprintf("%v#%v", flake, c.Value))
cmd.Stdout = &stdout
cmd.Stderr = &stderr

Expand Down
5 changes: 4 additions & 1 deletion completers/nix_completer/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ func init() {
"profile": carapace.ActionFiles(),
"reference-lock-file": carapace.ActionFiles("lock"),
})
carapace.Gen(buildCmd).PositionalCompletion(action.ActionFlakeRefs([]string{"nix", "build"}))

carapace.Gen(buildCmd).PositionalCompletion(
action.ActionFlakeRefs(),
)
}
5 changes: 4 additions & 1 deletion completers/nix_completer/cmd/develop.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ func init() {
"reference-lock-file": carapace.ActionFiles("lock"),
"unset": os.ActionEnvironmentVariables(),
})
carapace.Gen(developCmd).PositionalCompletion(action.ActionFlakeRefs([]string{"nix", "develop"}))

carapace.Gen(developCmd).PositionalCompletion(
action.ActionFlakeRefs(),
)
}
2 changes: 1 addition & 1 deletion pkg/actions/tools/nix/develop.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func getCurrentSystem(c carapace.Context) (string, error) {
}

// Completes <system>.<devshell>
func actionDevShellsAndSystem(c carapace.Context, devShells *devShellsSchema) carapace.Action {
func actionDevShellsAndSystem(_ carapace.Context, devShells *devShellsSchema) carapace.Action {
return carapace.ActionMultiPartsN(".", 2, func(c carapace.Context) carapace.Action {
if len(c.Parts) == 0 {
currentSystem, err := getCurrentSystem(c)
Expand Down
9 changes: 3 additions & 6 deletions pkg/actions/tools/nix/flake.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ func ActionFlakes() carapace.Action {
vals = append(vals, name[1], fields[2], styleForRegistry(fields[0]))
}
}
return carapace.Batch(
carapace.ActionValues(".", "..").StyleF(style.ForPath),
carapace.ActionDirectories(),
carapace.ActionStyledValuesDescribed(vals...),
).ToA()
}).Cache(time.Minute)
// TODO add directory completion externally
return carapace.ActionStyledValuesDescribed(vals...)
}).Cache(time.Minute).Tag("flakes")
}

func styleForRegistry(s string) string {
Expand Down

0 comments on commit c229ea7

Please sign in to comment.