Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add inshellisense #150

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
})
}