aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-11-07 02:41:26 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-11-07 02:41:26 -0800
commitbc3c17ffc8cd56e92c4002ef923e9ae0e0e30246 (patch)
tree5b61ad364d1b1b8e0c1872745147271daa1e1864
parent3379289024dbc1660b79f631f81b4a4495ea3063 (diff)
downloadbun-bc3c17ffc8cd56e92c4002ef923e9ae0e0e30246.tar.gz
bun-bc3c17ffc8cd56e92c4002ef923e9ae0e0e30246.tar.zst
bun-bc3c17ffc8cd56e92c4002ef923e9ae0e0e30246.zip
Improve ZSH completionsbun-v0.0.46
-rw-r--r--completions/bun.zsh408
-rw-r--r--completions/spec.yaml113
-rw-r--r--src/cli.zig24
3 files changed, 484 insertions, 61 deletions
diff --git a/completions/bun.zsh b/completions/bun.zsh
index 13801931e..bd84a383c 100644
--- a/completions/bun.zsh
+++ b/completions/bun.zsh
@@ -1,66 +1,368 @@
-__bun_first_cmd() {
- echo "${words[2]}"
-}
+_bun() {
+ local program=bun
+ typeset -A opt_args
+ local curcontext="$curcontext" state line context
-__bun_first_cmd_arg() {
- echo "${words[3]}"
-}
+ # ---- Command:
+ _arguments -s \
+ '1: :->cmd' \
+ '*: :->args' &&
+ ret=0
-__bun_arg_count() {
- echo "$#words"
-}
+ case $state in
+ cmd)
+ local -a scripts_list
+ IFS=$'\n' scripts_list=($(SHELL=zsh bun getcompletes i))
+ compadd $scripts_list && ret=0
-_bun_run() {
- if [[ ("$(__bun_arg_count)" = "2") ]]; then
- local -a options
- options=(${(f)"$(SHELL=zsh bun getcompletes)"})
-
- _describe 'values' options
- elif [[ ("$(__bun_arg_count)" = "3") ]]; then
- local -a run
- run=("${(f)"$(SHELL=zsh bun getcompletes g)"}")
- compadd $run
- else
- _files
- return
- fi
+ main_commands=('bun:"Generate a bundle" create:"Create a new project" dev:"Start a dev server" help:"Show command help" run:"Run a script or package bin" upgrade:"Upgrade to the latest version of Bun"')
+ main_commands=($main_commands)
+ _alternative "args:Bun:(($main_commands))"
+ ;;
+ args)
+ case $line[1] in
+ bun)
- # Make sure we don't run default completion
- custom_completion=true
-}
+ # ---- Command: bun
+ _arguments -s -C \
+ '1: :->cmd' \
+ '*: :->file' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--use[Use a framework, e.g. "next"]:use' &&
+ ret=0
-_bun() {
+ case $state in
+ file)
+ _files
+ ;;
+ esac
- # Store custom completion status
- local custom_completion=false
+ ;;
+ create)
+
+ # ---- Command: create
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '*: :->args' &&
+ ret=0
+
+ case $state in
+ cmd2)
+ _alternative 'args:create:((next\:"Next.js app" react\:"React app"))'
+ ;;
+
+ args)
+ case $line[2] in
+ next)
+
+ # ---- Command: create next
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '3: :->file' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' &&
+ ret=0
+
+ case $state in
+ file)
+ _files
+ ;;
+ esac
+
+ ;;
+ react)
+
+ # ---- Command: create react
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '3: :->file' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' &&
+ ret=0
+
+ case $state in
+ file)
+ _files
+ ;;
+ esac
+
+ ;;
+ esac
+
+ ;;
+
+ esac
+ ;;
+ dev)
+
+ # ---- Command: dev
+ _arguments -s -C \
+ '1: :->cmd' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--bunfile[Use a specific .bun file (default: node_modules.bun)]:bunfile' \
+ '--origin[Rewrite import paths to start from a different url. Default: http://localhost:3000]:origin' \
+ '-u[Rewrite import paths to start from a different url. Default: http://localhost:3000]:u' \
+ '--server-bunfile[Use a specific .bun file for SSR in bun dev (default: node_modules.server.bun)]:server-bunfile' \
+ '--extension-order[defaults to: .tsx,.ts,.jsx,.js,.json]:extension-order' \
+ '--jsx-runtime[JSX runtime to use. Defaults to "automatic"]:jsx-runtime:("automatic" "classic")' \
+ '--main-fields[Main fields to lookup in package.json. Defaults to --platform dependent]:main-fields' \
+ '--disable-react-fast-refresh[Disable React Fast Refresh]' \
+ '--disable-hmr[Disable Hot Module Reloading]' \
+ '--jsx-factory[Changes the function called when compiling JSX elements using the classic JSX runtime]:jsx-factory' \
+ '--jsx-fragment[Changes the function called when compiling JSX fragments]:jsx-fragment' \
+ '--jsx-import-source[Declares the module specifier to be used for importing the jsx and jsxs factory functions. Default: "react"]:jsx-import-source' \
+ '--port[Port number]:port' &&
+ ret=0
+
+ ;;
+
+ help)
+
+ # ---- Command: help
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '*: :->args' &&
+ ret=0
+
+ case $state in
+ cmd2)
+ _alternative 'args:cmd3:((bun create dev run upgrade))'
+ ;;
+
+ args)
+ case $line[2] in
+ bun)
+
+ # ---- Command: help bun
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--all[]' &&
+ ret=0
+
+ ;;
+
+ create)
+
+ # ---- Command: help create
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '3: :->cmd3' \
+ '*: :->args' &&
+ ret=0
+
+ case $state in
+ cmd3)
+ _alternative 'args:create:((next react))'
+ ;;
+
+ args)
+ case $line[3] in
+ next)
+
+ # ---- Command: help create next
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '3: :->cmd3' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--all[]' &&
+ ret=0
+
+ ;;
+
+ react)
+
+ # ---- Command: help create react
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '3: :->cmd3' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--all[]' &&
+ ret=0
+
+ ;;
+
+ esac
+
+ ;;
+
+ esac
+ ;;
+ dev)
+
+ # ---- Command: help dev
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--all[]' &&
+ ret=0
+
+ ;;
+
+ run)
+
+ # ---- Command: help run
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--all[]' &&
+ ret=0
+
+ ;;
+
+ upgrade)
+
+ # ---- Command: help upgrade
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->cmd2' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--all[]' &&
+ ret=0
+
+ ;;
+
+ esac
+
+ ;;
+
+ esac
+ ;;
+ run)
+
+ # ---- Command: run
+ _arguments -s -C \
+ '1: :->cmd' \
+ '2: :->script' \
+ '*: :->other' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' \
+ '--silent[Don'"'"'t echo the command]' &&
+ ret=0
+
+ case $state in
+ script)
+ _bun_run_param_script_completion
+ ;;
+ other)
+ _files
+ ;;
+ esac
+
+ ;;
+ upgrade)
+
+ # ---- Command: upgrade
+ _arguments -s -C \
+ '1: :->cmd' \
+ '--version[Show version and exit]' \
+ '-V[Show version and exit]' \
+ '--cwd[Change directory]:cwd' \
+ '--help[Show command help]' \
+ '-h[Show command help]' &&
+ ret=0
+
+ ;;
+
+ esac
- # Load custom completion commands
- case "$(__bun_first_cmd)" in
- create)
- _files
- return;
- ;;
- dev)
- return;
- ;;
- bun)
- _files
- return;
- ;;
- upgrade)
- return;
- ;;
- discord)
- return;
- ;;
- run)
- _bun_run
- return;
;;
+
esac
- # Fall back to default completion if we haven't done a custom one
- [[ $custom_completion = false ]] && _bun_run
}
+_bun_run_param_script_completion() {
+ local -a scripts_list
+ IFS=$'\n' scripts_list=($(SHELL=zsh bun getcompletes s))
+ compadd $scripts_list && ret=0
+
+ IFS=$'\n' bunjs=($(SHELL=zsh bun getcompletes j))
+ IFS=$'\n' bins=($(SHELL=zsh bun getcompletes b))
+
+ if [ ! -z "$bunjs" -a "$bunjs" != " " ]; then
+ if [ ! -z "$bins" -a "$bins" != " " ]; then
+ compadd $bunjs && ret=0
+ _alternative "args:bin:(($bins))"
+ return 1
+ fi
+
+ _alternative "args:Bun.js:(($bunjs))"
+ fi
+
+ if [ ! -z "$bins" -a "$bins" != " " ]; then
+ _alternative "args:bin:(($bins))"
+ return 1
+ fi
+}
+
+__bun_dynamic_comp() {
+ local comp=""
+
+ for arg in scripts; do
+ local line
+ while read -r line; do
+ local name="$line"
+ local desc="$line"
+ name="${name%$'\t'*}"
+ desc="${desc/*$'\t'/}"
+ echo
+ done <<<"$arg"
+ done
+
+ return $comp
+}
+
+autoload -U compinit && compinit
compdef _bun bun
diff --git a/completions/spec.yaml b/completions/spec.yaml
new file mode 100644
index 000000000..ccab35879
--- /dev/null
+++ b/completions/spec.yaml
@@ -0,0 +1,113 @@
+---
+name: bun
+appspec: { version: "0.001" }
+plugins: [-Meta]
+title: A tool for installing and managing Python packages
+
+options:
+ - version|V --Show version and exit
+ - name: cwd
+ type: string
+ summary: "Change directory"
+
+# subcommands:
+# bun:
+
+subcommands:
+ run:
+ summary: Run a script or package bin
+ parameters:
+ - name: script
+ multiple: false
+ completion:
+ command_string: >
+ SHELL=zsh bun getcompletes r
+
+ options:
+ - silent --Don't echo the command
+
+ create:
+ summary: Create a new project
+ subcommands:
+ next:
+ summary: "Next.js app"
+ parameters:
+ - name: file
+ multiple: false
+ type: file
+ required: true
+
+ react:
+ summary: "React app"
+ parameters:
+ - name: file
+ multiple: false
+ type: file
+ required: true
+
+ bun:
+ summary: Generate a bundle
+ parameters:
+ - name: file
+ multiple: true
+ type: file
+ required: false
+ options:
+ - name: use
+ type: string
+ summary: Use a framework, e.g. "next"
+
+ upgrade:
+ summary: Upgrade to the latest version of Bun
+
+ dev:
+ summary: Start a dev server
+ options:
+ - name: bunfile
+ type: string
+ summary: "Use a specific .bun file (default: node_modules.bun)"
+
+ - name: origin
+ type: string
+ summary: "Rewrite import paths to start from a different url. Default: http://localhost:3000"
+
+ - name: u
+ type: string
+ summary: "Rewrite import paths to start from a different url. Default: http://localhost:3000"
+
+ - name: server-bunfile
+ type: string
+ summary: "Use a specific .bun file for SSR in bun dev (default: node_modules.server.bun)"
+ - name: extension-order
+ type: string
+ summary: "defaults to: .tsx,.ts,.jsx,.js,.json"
+ - name: "jsx-runtime"
+ type: string
+ enum: ["automatic", "classic"]
+ summary: 'JSX runtime to use. Defaults to "automatic"'
+ - name: main-fields
+ type: string
+ summary: Main fields to lookup in package.json. Defaults to --platform dependent
+ - disable-react-fast-refresh --Disable React Fast Refresh
+ - disable-hmr --Disable Hot Module Reloading
+ - &jsx_factory name: jsx-factory
+ type: string
+ summary: "Changes the function called when compiling JSX elements using the classic JSX runtime"
+ - &jsx_fragment name: jsx-fragment
+ type: string
+ summary: "Changes the function called when compiling JSX fragments"
+ - &jsx_import_source name: jsx-import-source
+ type: string
+ summary: 'Declares the module specifier to be used for importing the jsx and jsxs factory functions. Default: "react"'
+ - &port name: port
+ type: int
+ summary: Port number
+
+parameters:
+ - name: sasdasdds
+ completion:
+ command_string: >
+ SHELL=zsh bun getcompletes r
+
+# vim:et:sts=2:sws=2:sw=2:foldmethod=indent
+
diff --git a/src/cli.zig b/src/cli.zig
index 28fafeb55..77a110fab 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -131,7 +131,7 @@ pub const Arguments = struct {
clap.parseParam("--disable-hmr Disable Hot Module Reloading (disables fast refresh too)") catch unreachable,
clap.parseParam("--extension-order <STR>... defaults to: .tsx,.ts,.jsx,.js,.json ") catch unreachable,
clap.parseParam("--jsx-factory <STR> Changes the function called when compiling JSX elements using the classic JSX runtime") catch unreachable,
- clap.parseParam("--jsx-fragment <STR> Changes the function called when compiling JSX fragments using the classic JSX runtime") catch unreachable,
+ clap.parseParam("--jsx-fragment <STR> Changes the function called when compiling JSX fragments") catch unreachable,
clap.parseParam("--jsx-import-source <STR> Declares the module specifier to be used for importing the jsx and jsxs factory functions. Default: \"react\"") catch unreachable,
clap.parseParam("--jsx-production Use jsx instead of jsxDEV (default) for the automatic runtime") catch unreachable,
clap.parseParam("--jsx-runtime <STR> \"automatic\" (default) or \"classic\"") catch unreachable,
@@ -623,6 +623,12 @@ pub const Command = struct {
"discord",
};
+ const reject_list = default_completions_list ++ [_]string{
+ "build",
+ "completions",
+ "help",
+ };
+
pub fn start(allocator: *std.mem.Allocator, log: *logger.Log) !void {
const tag = which(allocator);
switch (tag) {
@@ -671,19 +677,21 @@ pub const Command = struct {
var completions = ShellCompletions{};
if (filter.len == 0) {
- completions = try RunCommand.completions(ctx, &default_completions_list, .all);
+ completions = try RunCommand.completions(ctx, &default_completions_list, &reject_list, .all);
} else if (strings.eqlComptime(filter[0], "s")) {
- completions = try RunCommand.completions(ctx, null, .script);
+ completions = try RunCommand.completions(ctx, null, &reject_list, .script);
+ } else if (strings.eqlComptime(filter[0], "i")) {
+ completions = try RunCommand.completions(ctx, &default_completions_list, &reject_list, .script_exclude);
} else if (strings.eqlComptime(filter[0], "b")) {
- completions = try RunCommand.completions(ctx, null, .bin);
+ completions = try RunCommand.completions(ctx, null, &reject_list, .bin);
} else if (strings.eqlComptime(filter[0], "r")) {
- completions = try RunCommand.completions(ctx, null, .all);
+ completions = try RunCommand.completions(ctx, null, &reject_list, .all);
} else if (strings.eqlComptime(filter[0], "g")) {
- completions = try RunCommand.completions(ctx, null, .all_plus_bun_js);
+ completions = try RunCommand.completions(ctx, null, &reject_list, .all_plus_bun_js);
} else if (strings.eqlComptime(filter[0], "j")) {
- completions = try RunCommand.completions(ctx, null, .bun_js);
+ completions = try RunCommand.completions(ctx, null, &reject_list, .bun_js);
} else if (strings.eqlComptime(filter[0], "z")) {
- completions = try RunCommand.completions(ctx, null, .script_and_descriptions);
+ completions = try RunCommand.completions(ctx, null, &reject_list, .script_and_descriptions);
}
completions.print();