diff --git a/cmd/tools/vcomplete.v b/cmd/tools/vcomplete.v index 9e4e061fe4..342db79e0c 100644 --- a/cmd/tools/vcomplete.v +++ b/cmd/tools/vcomplete.v @@ -229,6 +229,16 @@ const ( fn auto_complete(args []string) { if args.len <= 1 || args[0] != 'complete' { if args.len == 1 { + shell_path := os.getenv('SHELL') + if shell_path.len > 0 { + shell_name := os.file_name(shell_path).to_lower() + if shell_name in auto_complete_shells { + println(setup_for_shell(shell_name)) + exit(0) + } + eprintln('Unknown shell ${shell_name}. Supported shells are: $auto_complete_shells') + exit(1) + } eprintln('auto completion require arguments to work.') } else { eprintln('auto completion failed for "$args".') @@ -244,62 +254,7 @@ fn auto_complete(args []string) { exit(1) } shell := sub_args[1] - mut setup := '' - match shell { - 'bash' { - setup = ' -_v_completions() { - local src - local limit - # Send all words up to the word the cursor is currently on - let limit=1+\$COMP_CWORD - src=\$($vexe complete bash \$(printf "%s\\n" \${COMP_WORDS[@]: 0:\$limit})) - if [[ \$? == 0 ]]; then - eval \${src} - #echo \${src} - fi -} - -complete -o nospace -F _v_completions v -' - } - 'fish' { - setup = ' -function __v_completions - # Send all words up to the one before the cursor - $vexe complete fish (commandline -cop) -end -complete -f -c v -a "(__v_completions)" -' - } - 'zsh' { - setup = ' -#compdef v -_v() { - local src - # Send all words up to the word the cursor is currently on - src=\$($vexe complete zsh \$(printf "%s\\n" \${(@)words[1,\$CURRENT]})) - if [[ \$? == 0 ]]; then - eval \${src} - #echo \${src} - fi -} -compdef _v v -' - } - 'powershell' { - setup = ' -Register-ArgumentCompleter -Native -CommandName v -ScriptBlock { - param(\$commandName, \$wordToComplete, \$cursorPosition) - $vexe complete powershell "\$wordToComplete" | ForEach-Object { - [System.Management.Automation.CompletionResult]::new(\$_, \$_, \'ParameterValue\', \$_) - } -} -' - } - else {} - } - println(setup) + println(setup_for_shell(shell)) } 'bash' { if sub_args.len <= 1 { @@ -474,6 +429,65 @@ fn auto_complete_request(args []string) []string { return list } +fn setup_for_shell(shell string) string { + mut setup := '' + match shell { + 'bash' { + setup = ' +_v_completions() { + local src + local limit + # Send all words up to the word the cursor is currently on + let limit=1+\$COMP_CWORD + src=\$($vexe complete bash \$(printf "%s\\n" \${COMP_WORDS[@]: 0:\$limit})) + if [[ \$? == 0 ]]; then + eval \${src} + #echo \${src} + fi +} + +complete -o nospace -F _v_completions v +' + } + 'fish' { + setup = ' +function __v_completions + # Send all words up to the one before the cursor + $vexe complete fish (commandline -cop) +end +complete -f -c v -a "(__v_completions)" +' + } + 'zsh' { + setup = ' +#compdef v +_v() { + local src + # Send all words up to the word the cursor is currently on + src=\$($vexe complete zsh \$(printf "%s\\n" \${(@)words[1,\$CURRENT]})) + if [[ \$? == 0 ]]; then + eval \${src} + #echo \${src} + fi +} +compdef _v v +' + } + 'powershell' { + setup = ' +Register-ArgumentCompleter -Native -CommandName v -ScriptBlock { + param(\$commandName, \$wordToComplete, \$cursorPosition) + $vexe complete powershell "\$wordToComplete" | ForEach-Object { + [System.Management.Automation.CompletionResult]::new(\$_, \$_, \'ParameterValue\', \$_) + } +} +' + } + else {} + } + return setup +} + fn main() { args := os.args[1..] // println('"$args"')