Skip to content

Commit

Permalink
Add support for clink
Browse files Browse the repository at this point in the history
  • Loading branch information
sago35 committed Sep 6, 2020
1 parent 34400ba commit 43b69a9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
78 changes: 78 additions & 0 deletions autocmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,83 @@ func handleCompletionScriptZsh(listPath string) {
return
}

const completionScriptClinkStr = `
local parser = clink.arg.new_parser
local tinygo_targets_parser = parser({
%s,
})
-- function getTargets(dir)
-- local handle = io.popen("dir /b "..dir.."\\*.json", "r")
-- local content = handle:read("*all")
-- handle:close()
--
-- local t = {}
-- i = 1
--
-- for s in string.gmatch(content, "([^\n]+).json") do
-- t[i] = s
-- i = i + 1
-- end
--
-- return t
-- end
-- local tinygo_targets_parser = parser(getTargets("C:\\tinygo\\targets"))
local tinygo_flag_parser = parser(
%s
)
local tinygo_parser = parser({
"build"..tinygo_flag_parser,
"run"..tinygo_flag_parser,
"test"..tinygo_flag_parser,
"flash"..tinygo_flag_parser,
"gdb"..tinygo_flag_parser,
"env"..tinygo_flag_parser,
"list"..tinygo_flag_parser,
"clean"..tinygo_flag_parser,
"help"..tinygo_flag_parser,
})
clink.arg.register_parser("tinygo", tinygo_parser)
`

func handleCompletionScriptClink(listPath string) {
targets := []string{}
for _, t := range validTargets {
targets = append(targets, fmt.Sprintf("%q", t))
}

flags := []string{}
for _, f := range getFlagCompletion() {
m, ok := flagCompleteMap[f]
if !ok {
panic("panic")
}

if 0 < len(m) {
p := []string{}
for _, c := range m {
p = append(p, fmt.Sprintf("%q", c))
}

flags = append(flags, fmt.Sprintf(`"-%s"..parser({%s})`, f, strings.Join(p, ", ")))
} else {
flags = append(flags, fmt.Sprintf(`"-%s"`, f))
}
}

fmt.Printf(completionScriptClinkStr,
strings.Join(targets, ", "),
strings.Join(flags, ",\n "),
)
return
}

// handleCompletionBash returns a string to be used in bash's compgen.
// Typically, the input will look like this
// args := []string{"flash", "-target"}
Expand Down Expand Up @@ -171,5 +248,6 @@ func getFlagCompletion() []string {
for k := range flagCompleteMap {
ret = append(ret, k)
}
ret.Sort()
return ret
}
12 changes: 9 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ type cli struct {
// Run ...
func (c *cli) Run(args []string) error {
var (
completionScriptBash = flag.Bool("completion-script-bash", false, "print completion-script-bash")
completionScriptZsh = flag.Bool("completion-script-zsh", false, "print completion-script-zsh")
targetsListPath = flag.String("targets", "", "targets list file")
completionScriptBash = flag.Bool("completion-script-bash", false, "print completion-script-bash")
completionScriptZsh = flag.Bool("completion-script-zsh", false, "print completion-script-zsh")
completionScriptClink = flag.Bool("completion-script-clink", false, "print completion-script-clink")
targetsListPath = flag.String("targets", "", "targets list file")
)

flag.Parse()
Expand All @@ -38,6 +39,11 @@ func (c *cli) Run(args []string) error {
return nil
}

if *completionScriptClink {
handleCompletionScriptClink(*targetsListPath)
return nil
}

if *targetsListPath != "" {
r, err := os.Open(*targetsListPath)
if err != nil {
Expand Down

0 comments on commit 43b69a9

Please sign in to comment.