Skip to content

Commit

Permalink
add inshellisense
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 13, 2023
1 parent 5358533 commit 6d485b7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Supported frameworks:
- [cobra](https://github.com/spf13/cobra)
- [complete](https://github.com/posener/complete)
- [fish](https://fishshell.com/)
- [inshellisense](https://github.com/microsoft/inshellisense)
- [kingpin](https://github.com/alecthomas/kingpin)
- [powershell](https://microsoft.com/powershell)
- [urfavecli](https://github.com/urfave/cli)
Expand Down
1 change: 1 addition & 0 deletions cmd/carapace-bridge/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {
addSubCommand("cobra", "bridges https://github.com/spf13/cobra", bridge.ActionCobra)
addSubCommand("complete", "bridges https://github.com/posener/complete", bridge.ActionComplete)
addSubCommand("fish", "bridges completions registered in fish", bridge.ActionFish)
addSubCommand("inshellisense", "bridges https://github.com/microsoft/inshellisense", bridge.ActionInshellisense)
addSubCommand("kingpin", "bridges https://github.com/alecthomas/kingpin", bridge.ActionKingpin)
addSubCommand("powershell", "bridges completions registered in powershell", bridge.ActionPowershell)
addSubCommand("urfavecli", "bridges https://github.com/urfave/cli", bridge.ActionUrfavecli)
Expand Down
50 changes: 50 additions & 0 deletions pkg/actions/bridge/inshellisense.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package bridge

import (
"encoding/json"
"strings"

"github.com/rsteube/carapace"
)

// ActionInshellisense bridges https://github.com/microsoft/inshellisense
func ActionInshellisense(command ...string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(command) == 0 {
return carapace.ActionMessage("missing argument [ActionInshellisense]")
}

args := append(command, c.Args...)
args = append(args, c.Value)
input := strings.Join(args, " ") // TODO simple join for now as the lexer in inshellisense can't handle quotes and spaces anyway
return carapace.ActionExecCommand("inshellisense", "complete", input)(func(output []byte) carapace.Action {
var r struct {
Suggestions []struct {
Name string
Description string
}
}

if err := json.Unmarshal(output, &r); err != nil {
return carapace.ActionMessage(err.Error())
}

vals := make([]string, 0)
for _, s := range r.Suggestions {
if !strings.HasPrefix(c.Value, "-") && strings.HasPrefix(s.Name, "-") {
continue
}

if strings.HasPrefix(s.Name, c.Value) ||
(strings.HasPrefix(c.Value, "-") && strings.Contains(c.Value, "=")) {
vals = append(vals, s.Name, s.Description)
}
}
a := carapace.ActionValuesDescribed(vals...)
if strings.HasPrefix(c.Value, "-") && strings.Contains(c.Value, "=") {
a = a.Prefix(strings.SplitAfterN(c.Value, "=", 2)[0])
}
return a
})
})
}

0 comments on commit 6d485b7

Please sign in to comment.