mirror of
https://github.com/wavetermdev/inshellisense.git
synced 2026-08-05 13:43:30 -07:00
feat: enable partial command suggestions for subcommand (w/ broken ui tabbing)
Signed-off-by: cpendery <cpendery@vt.edu>
This commit is contained in:
+15
-44
@@ -49,45 +49,6 @@ func argsAreOptional(args []model.Arg) bool {
|
||||
return allOptional
|
||||
}
|
||||
|
||||
/* <has a> <no a>
|
||||
cmd --flag arg --flag
|
||||
|
||||
thus load suggestions for the base command, which would be any template, suggestions, options, subcommand, or an arg
|
||||
|
||||
rules:
|
||||
if there is an arg, just show the description of that arg
|
||||
else, show suggestions, subcommand, template, options
|
||||
in that org
|
||||
*/
|
||||
|
||||
/*
|
||||
generally, we want to make a recommendation based on what we finish on, so that is what matters, we can tree down until we get there
|
||||
|
||||
subcommand
|
||||
if isOption -> option
|
||||
else
|
||||
if match subcommand -> subcommand
|
||||
else -> arg
|
||||
|
||||
if _ -> recommend based self + rest
|
||||
|
||||
option
|
||||
if hasArgs
|
||||
if args optional
|
||||
if next is subcommand || option -> go there
|
||||
if args variadic ---> arg
|
||||
else -> arg
|
||||
|
||||
if _ ->
|
||||
if args optional -> recommend on parent subcommand + rest + args
|
||||
else if has args -> recommend based on args
|
||||
else -> recommend based on parent subcommand + rest
|
||||
|
||||
arg
|
||||
<NEEDS_WORK>
|
||||
if _ -> recommend based on self
|
||||
*/
|
||||
|
||||
func getLongName(names []string) string {
|
||||
longestName := ""
|
||||
for _, name := range names {
|
||||
@@ -98,8 +59,17 @@ func getLongName(names []string) string {
|
||||
return longestName
|
||||
}
|
||||
|
||||
func getSubcommandDrivenRecommendation(spec model.Subcommand, persistentOptions []model.Option) []Suggestion {
|
||||
func getSubcommandDrivenRecommendation(spec model.Subcommand, persistentOptions []model.Option, partialCmd *commandToken) []Suggestion {
|
||||
suggestions := []Suggestion{}
|
||||
if partialCmd != nil {
|
||||
switch spec.FilterStrategy {
|
||||
case model.FilterStrategyFuzzy:
|
||||
return fuzzyMatchSubcommands(partialCmd.token, spec.Subcommands)
|
||||
case model.FilterStrategyPrefix, model.FilterStrategyEmpty:
|
||||
return prefixMatchSubcommands(partialCmd.token, spec.Subcommands)
|
||||
}
|
||||
}
|
||||
|
||||
for _, sub := range spec.Subcommands {
|
||||
suggestions = append(suggestions, Suggestion{
|
||||
Name: getLongName(sub.Name),
|
||||
@@ -121,7 +91,9 @@ func getArgDrivenRecommendation(args []model.Arg, spec model.Subcommand, persist
|
||||
|
||||
func handleSubcommand(tokens []commandToken, spec model.Subcommand, persistentOptions []model.Option) (suggestions []Suggestion) {
|
||||
if len(tokens) == 0 {
|
||||
return getSubcommandDrivenRecommendation(spec, persistentOptions)
|
||||
return getSubcommandDrivenRecommendation(spec, persistentOptions, nil)
|
||||
} else if !tokens[0].complete {
|
||||
return getSubcommandDrivenRecommendation(spec, persistentOptions, &tokens[0])
|
||||
}
|
||||
for _, option := range spec.Options {
|
||||
if option.IsPersistent {
|
||||
@@ -156,6 +128,8 @@ func handleOption(tokens []commandToken, option model.Option, spec model.Subcomm
|
||||
func handleArg(tokens []commandToken, args []model.Arg, spec model.Subcommand, persistentOptions []model.Option) (suggestions []Suggestion) {
|
||||
if len(tokens) == 0 {
|
||||
return getArgDrivenRecommendation(args, spec, persistentOptions)
|
||||
} else if !tokens[0].complete {
|
||||
return getArgDrivenRecommendation(args, spec, persistentOptions)
|
||||
} else if len(args) == 0 {
|
||||
return handleSubcommand(tokens, spec, persistentOptions)
|
||||
}
|
||||
@@ -199,9 +173,6 @@ func loadSuggestions(cmd string) (suggestions []Suggestion) {
|
||||
if !rootToken.complete {
|
||||
return
|
||||
}
|
||||
if !activeCmd[len(activeCmd)-1].complete {
|
||||
return
|
||||
}
|
||||
if spec, ok := specs.Specs[rootToken.token]; ok {
|
||||
return handleSubcommand(activeCmd[1:], spec, []model.Option{})
|
||||
}
|
||||
|
||||
@@ -127,6 +127,19 @@ const generateTemplate = (template: Fig.Template): string => {
|
||||
throw Error("unknown template value");
|
||||
};
|
||||
|
||||
const generateFilterStrategy = (
|
||||
filterStrategy: "fuzzy" | "prefix" | "default"
|
||||
): string => {
|
||||
switch (filterStrategy) {
|
||||
case "fuzzy":
|
||||
return "model.FilterStrategyFuzzy";
|
||||
case "prefix":
|
||||
return "model.FilterStrategyPrefix";
|
||||
case "default":
|
||||
return "model.FilterStrategyPrefix";
|
||||
}
|
||||
};
|
||||
|
||||
const genName = (name: Fig.SingleOrArray<string> | undefined): string => {
|
||||
if (name == null) return "";
|
||||
return Array.isArray(name)
|
||||
@@ -186,6 +199,14 @@ const genIsPersistent = (persistent: boolean): string => {
|
||||
return persistent === true ? "IsPersistent: true," : "";
|
||||
};
|
||||
|
||||
const genFilterStrategy = (
|
||||
filterStrategy: "fuzzy" | "prefix" | "default" | undefined
|
||||
): string => {
|
||||
return filterStrategy != null
|
||||
? `FilterStrategy: ${generateFilterStrategy(filterStrategy)},`
|
||||
: "";
|
||||
};
|
||||
|
||||
const genExclusiveOn = (exclusiveOn: string[] | undefined): string => {
|
||||
if (exclusiveOn == null) return "";
|
||||
return `ExclusiveOn: []string{${exclusiveOn
|
||||
@@ -193,6 +214,26 @@ const genExclusiveOn = (exclusiveOn: string[] | undefined): string => {
|
||||
.join(",")}},`;
|
||||
};
|
||||
|
||||
const genGenerators = (
|
||||
generators: Fig.SingleOrArray<Fig.Generator> | undefined
|
||||
) => {
|
||||
return generators != null
|
||||
? `Generator: nil, // TODO: port over generator`
|
||||
: "";
|
||||
};
|
||||
|
||||
const genIsOptional = (isOptional: boolean | undefined) => {
|
||||
return isOptional === true ? "IsOptional: true," : "";
|
||||
};
|
||||
|
||||
const genIsCommand = (isCommand: boolean | undefined) => {
|
||||
return isCommand === true ? "IsCommand: true," : "";
|
||||
};
|
||||
|
||||
const genIsVariadic = (isVariadic: boolean | undefined) => {
|
||||
return isVariadic === true ? "IsVariadic: true," : "";
|
||||
};
|
||||
|
||||
const generateSuggestions = (
|
||||
suggestions: (string | Fig.Suggestion)[]
|
||||
): string => {
|
||||
@@ -232,6 +273,11 @@ const generateArgs = (args: Fig.SingleOrArray<Fig.Arg>): string => {
|
||||
${genSingleName(arg.name)}
|
||||
${genDescription(arg.description)}
|
||||
${genSuggestions(arg.suggestions)}
|
||||
${genFilterStrategy(arg.filterStrategy)}
|
||||
${genGenerators(arg.generators)}
|
||||
${genIsOptional(arg.isOptional)}
|
||||
${genIsCommand(arg.isCommand)}
|
||||
${genIsVariadic(arg.isVariadic)}
|
||||
}`;
|
||||
})
|
||||
.join(",");
|
||||
@@ -262,7 +308,8 @@ const generateSubcommand = (
|
||||
${genArgs(subcommand.args)}
|
||||
${genOptions(subcommand.options)}
|
||||
${genSubcommands(subcommand.subcommands)}
|
||||
}`;
|
||||
${genFilterStrategy(subcommand.filterStrategy)}
|
||||
}`.replaceAll(/(\n\s+\n)+/g, "\n");
|
||||
};
|
||||
|
||||
const generateGolang = (
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package autocomplete
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/cpendery/clac/autocomplete/model"
|
||||
"github.com/lithammer/fuzzysearch/fuzzy"
|
||||
)
|
||||
|
||||
func fuzzyMatchSubcommands(input string, subcommands []model.Subcommand) []Suggestion {
|
||||
type match struct {
|
||||
name string
|
||||
rank int
|
||||
subcommand model.Subcommand
|
||||
}
|
||||
matchers := []match{}
|
||||
for _, sub := range subcommands {
|
||||
bestName := ""
|
||||
bestNameRank := -1
|
||||
for _, n := range sub.Name {
|
||||
rank := fuzzy.RankMatch(input, n)
|
||||
if rank > bestNameRank {
|
||||
bestName = n
|
||||
bestNameRank = rank
|
||||
}
|
||||
}
|
||||
if bestNameRank != -1 {
|
||||
matchers = append(matchers, match{
|
||||
name: bestName,
|
||||
subcommand: sub,
|
||||
})
|
||||
}
|
||||
}
|
||||
sort.Slice(matchers, func(i, j int) bool {
|
||||
return matchers[i].rank > matchers[j].rank
|
||||
})
|
||||
results := []Suggestion{}
|
||||
for _, m := range matchers {
|
||||
results = append(results, Suggestion{
|
||||
Name: m.name,
|
||||
Description: m.subcommand.Description,
|
||||
})
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func prefixMatchSubcommands(input string, subcommands []model.Subcommand) []Suggestion {
|
||||
results := []Suggestion{}
|
||||
for _, sub := range subcommands {
|
||||
for _, n := range sub.Name {
|
||||
if strings.HasPrefix(n, input) {
|
||||
results = append(results, Suggestion{
|
||||
Name: n,
|
||||
Description: sub.Description,
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
+30
-12
@@ -1,11 +1,12 @@
|
||||
package model
|
||||
|
||||
type Subcommand struct {
|
||||
Name []string //single or array string, required
|
||||
Description string
|
||||
Args []Arg
|
||||
Options []Option
|
||||
Subcommands []Subcommand
|
||||
Name []string //single or array string, required
|
||||
Description string
|
||||
Args []Arg
|
||||
Options []Option
|
||||
Subcommands []Subcommand
|
||||
FilterStrategy FilterStrategy
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
@@ -17,13 +18,15 @@ type Option struct {
|
||||
}
|
||||
|
||||
type Arg struct {
|
||||
Name string //single, optional
|
||||
Description string //single, optional
|
||||
Templates []Template
|
||||
Suggestions []Suggestion
|
||||
IsVariadic bool
|
||||
IsOptional bool
|
||||
IsCommand bool
|
||||
Name string //single, optional
|
||||
Description string //single, optional
|
||||
Templates []Template
|
||||
Suggestions []Suggestion
|
||||
FilterStrategy FilterStrategy
|
||||
Generator *Generator
|
||||
IsVariadic bool
|
||||
IsOptional bool
|
||||
IsCommand bool
|
||||
}
|
||||
|
||||
type Suggestion struct {
|
||||
@@ -31,6 +34,13 @@ type Suggestion struct {
|
||||
Description string
|
||||
}
|
||||
|
||||
type Generator struct {
|
||||
Script string
|
||||
Function func() string
|
||||
PostProcess func(string) []Suggestion
|
||||
Template []Template
|
||||
}
|
||||
|
||||
type Template string
|
||||
|
||||
const (
|
||||
@@ -40,6 +50,14 @@ const (
|
||||
TemplateHelp Template = "help"
|
||||
)
|
||||
|
||||
type FilterStrategy string
|
||||
|
||||
const (
|
||||
FilterStrategyPrefix FilterStrategy = "prefix"
|
||||
FilterStrategyFuzzy FilterStrategy = "fuzzy"
|
||||
FilterStrategyEmpty FilterStrategy = ""
|
||||
)
|
||||
|
||||
var (
|
||||
Templates = []Template{TemplateFilepaths, TemplateFolders, TemplateHistory}
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ require (
|
||||
github.com/charmbracelet/bubbles v0.16.1
|
||||
github.com/charmbracelet/bubbletea v0.24.2
|
||||
github.com/charmbracelet/lipgloss v0.8.0
|
||||
github.com/lithammer/fuzzysearch v1.1.8
|
||||
github.com/mattn/go-runewidth v0.0.14
|
||||
github.com/muesli/reflow v0.3.0
|
||||
github.com/stretchr/testify v1.8.4
|
||||
@@ -27,6 +28,6 @@ require (
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.7.0 // indirect
|
||||
golang.org/x/text v0.3.8 // indirect
|
||||
golang.org/x/text v0.9.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -12,6 +12,8 @@ github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2
|
||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
|
||||
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
|
||||
@@ -36,16 +38,45 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
|
||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
Reference in New Issue
Block a user