2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-11-20 17:17:31 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
//
|
|
|
|
// Utility functions helping integrate with various shell auto-completion systems.
|
|
|
|
// The install process and communication is inspired from that of [kitty](https://sw.kovidgoyal.net/kitty/#completion-for-kitty)
|
|
|
|
// This method avoids writing and maintaining external files on the user's file system.
|
|
|
|
// The user will be responsible for adding a small line to their .*rc - that will ensure *live* (i.e. not-static)
|
|
|
|
// auto-completion features.
|
|
|
|
//
|
|
|
|
// # bash
|
2020-11-23 15:53:32 +03:00
|
|
|
// To install auto-completion for V in bash, simply add this code to your `~/.bashrc`:
|
2020-11-20 17:17:31 +03:00
|
|
|
// `source /dev/stdin <<<"$(v complete setup bash)"`
|
|
|
|
// On more recent versions of bash (>3.2) this should suffice:
|
|
|
|
// `source <(v complete setup bash)`
|
|
|
|
//
|
2020-11-23 15:53:32 +03:00
|
|
|
// # fish
|
|
|
|
// For versions of fish <3.0.0, add the following to your `~/.config/fish/config.fish`
|
|
|
|
// `v complete setup fish | source`
|
|
|
|
// Later versions of fish source completions by default.
|
2020-11-20 17:17:31 +03:00
|
|
|
//
|
|
|
|
// # zsh
|
2020-11-23 15:53:32 +03:00
|
|
|
// To install auto-completion for V in zsh - please add the following to your `~/.zshrc`:
|
2020-11-22 22:12:34 +03:00
|
|
|
// ```
|
|
|
|
// autoload -Uz compinit
|
|
|
|
// compinit
|
|
|
|
// # Completion for v
|
|
|
|
// v complete setup zsh | source /dev/stdin
|
|
|
|
// ```
|
2020-11-23 15:53:32 +03:00
|
|
|
// Please note that you should let v load the zsh completions after the call to compinit
|
2020-11-22 22:12:34 +03:00
|
|
|
//
|
2021-03-04 01:57:08 +03:00
|
|
|
// # powershell
|
|
|
|
// To install auto-complete for V in PowerShell, simply do this
|
|
|
|
// `v complete setup powershell >> $PROFILE`
|
|
|
|
// and reload profile
|
|
|
|
// `& $PROFILE`
|
|
|
|
// If `$PROFILE` didn't exist yet, create it before
|
|
|
|
// `New-Item -Type File -Force $PROFILE`
|
2020-11-20 17:17:31 +03:00
|
|
|
//
|
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
const (
|
2020-11-20 21:50:01 +03:00
|
|
|
auto_complete_shells = ['bash', 'fish', 'zsh', 'powershell'] // list of supported shells
|
2020-11-20 22:29:59 +03:00
|
|
|
vexe = os.getenv('VEXE')
|
2022-01-09 17:09:10 +03:00
|
|
|
help_text = "Usage:
|
|
|
|
v complete [options] [SUBCMD] QUERY...
|
|
|
|
|
|
|
|
Description:
|
|
|
|
Tool for bridging auto completion between various shells and v
|
|
|
|
|
|
|
|
Supported shells:
|
|
|
|
bash, fish, zsh, powershell
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Echo auto-detected shell install script to STDOUT
|
|
|
|
v complete
|
|
|
|
Echo specific shell install script to STDOUT
|
|
|
|
v complete setup bash
|
|
|
|
Auto complete input `v tes`*USER PUSHES TAB* (in Bash compatible format).
|
|
|
|
This is not meant for manual invocation - it's called by the relevant
|
|
|
|
shell via the script installed with `v complete` or `v complete setup SHELL`.
|
|
|
|
v complete bash v tes
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help Show this help text.
|
|
|
|
|
|
|
|
SUBCMD:
|
|
|
|
setup : setup [SHELL] - returns the code for completion setup for SHELL
|
|
|
|
bash : [QUERY] - returns Bash compatible completion code with completions computed from QUERY
|
|
|
|
fish : [QUERY] - returns Fish compatible completion code with completions computed from QUERY
|
|
|
|
zsh : [QUERY] - returns ZSH compatible completion code with completions computed from QUERY
|
|
|
|
powershell: [QUERY] - returns PowerShell compatible completion code with completions computed from QUERY"
|
2020-11-20 17:17:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Snooped from cmd/v/v.v, vlib/v/pref/pref.v
|
|
|
|
const (
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_commands = [
|
2021-03-22 17:45:29 +03:00
|
|
|
// simple_cmd
|
2021-12-08 23:27:07 +03:00
|
|
|
'ast',
|
|
|
|
'doc',
|
2020-11-20 17:17:31 +03:00
|
|
|
'vet',
|
2021-12-08 23:27:07 +03:00
|
|
|
// tools in one .v file
|
|
|
|
'bin2v',
|
|
|
|
'bug',
|
|
|
|
'build-examples',
|
|
|
|
'build-tools',
|
|
|
|
'build-vbinaries',
|
2021-12-11 23:17:01 +03:00
|
|
|
'bump',
|
2021-12-08 23:27:07 +03:00
|
|
|
'check-md',
|
|
|
|
'complete',
|
|
|
|
'compress',
|
|
|
|
'create',
|
|
|
|
'doctor',
|
|
|
|
'fmt',
|
2021-12-09 22:31:55 +03:00
|
|
|
'gret',
|
2022-07-10 20:27:50 +03:00
|
|
|
'ls',
|
2021-12-08 23:27:07 +03:00
|
|
|
'repl',
|
2020-11-20 17:17:31 +03:00
|
|
|
'self',
|
2021-12-08 23:27:07 +03:00
|
|
|
'setup-freetype',
|
2021-10-16 22:10:48 +03:00
|
|
|
'shader',
|
2020-11-20 17:17:31 +03:00
|
|
|
'symlink',
|
2021-12-08 23:27:07 +03:00
|
|
|
'test-all',
|
|
|
|
'test-cleancode',
|
2020-11-20 17:17:31 +03:00
|
|
|
'test-fmt',
|
2021-12-08 23:27:07 +03:00
|
|
|
'test-parser',
|
2021-01-19 11:41:51 +03:00
|
|
|
'test-self',
|
2021-12-08 23:27:07 +03:00
|
|
|
'test',
|
|
|
|
'tracev',
|
|
|
|
'up',
|
|
|
|
'watch',
|
|
|
|
'wipe-cache',
|
2021-03-22 17:45:29 +03:00
|
|
|
// commands
|
2020-11-20 17:17:31 +03:00
|
|
|
'help',
|
|
|
|
'new',
|
|
|
|
'init',
|
|
|
|
'translate',
|
|
|
|
'self',
|
|
|
|
'search',
|
|
|
|
'install',
|
|
|
|
'update',
|
|
|
|
'upgrade',
|
|
|
|
'outdated',
|
|
|
|
'list',
|
|
|
|
'remove',
|
|
|
|
'vlib-docs',
|
|
|
|
'get',
|
|
|
|
'version',
|
|
|
|
'run',
|
|
|
|
'build',
|
|
|
|
'build-module',
|
2022-05-18 08:50:32 +03:00
|
|
|
'missdoc',
|
2020-11-20 17:17:31 +03:00
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
// Entries in the flag arrays below should be entered as is:
|
|
|
|
// * Short flags, e.g.: "-v", should be entered: '-v'
|
|
|
|
// * Long flags, e.g.: "--version", should be entered: '--version'
|
|
|
|
// * Single-dash flags, e.g.: "-version", should be entered: '-version'
|
|
|
|
auto_complete_flags = [
|
2020-11-20 17:17:31 +03:00
|
|
|
'-apk',
|
|
|
|
'-show-timings',
|
|
|
|
'-check-syntax',
|
2021-09-07 07:17:53 +03:00
|
|
|
'-check',
|
2020-11-20 17:17:31 +03:00
|
|
|
'-v',
|
|
|
|
'-progress',
|
|
|
|
'-silent',
|
|
|
|
'-g',
|
|
|
|
'-cg',
|
|
|
|
'-repl',
|
|
|
|
'-live',
|
|
|
|
'-sharedlive',
|
|
|
|
'-shared',
|
|
|
|
'--enable-globals',
|
|
|
|
'-enable-globals',
|
|
|
|
'-autofree',
|
|
|
|
'-compress',
|
|
|
|
'-freestanding',
|
2022-06-10 11:57:12 +03:00
|
|
|
'-no-parallel',
|
2020-11-20 17:17:31 +03:00
|
|
|
'-no-preludes',
|
|
|
|
'-prof',
|
|
|
|
'-profile',
|
|
|
|
'-profile-no-inline',
|
|
|
|
'-prod',
|
|
|
|
'-simulator',
|
|
|
|
'-stats',
|
|
|
|
'-obfuscate',
|
|
|
|
'-translated',
|
|
|
|
'-color',
|
|
|
|
'-nocolor',
|
|
|
|
'-showcc',
|
|
|
|
'-show-c-output',
|
|
|
|
'-experimental',
|
|
|
|
'-usecache',
|
|
|
|
'-prealloc',
|
|
|
|
'-parallel',
|
2021-04-26 16:39:38 +03:00
|
|
|
'-native',
|
2020-11-20 17:17:31 +03:00
|
|
|
'-W',
|
2020-11-27 22:16:24 +03:00
|
|
|
'-keepc',
|
2020-11-20 17:17:31 +03:00
|
|
|
'-w',
|
2021-03-04 15:38:05 +03:00
|
|
|
'-print-v-files',
|
2020-11-20 17:17:31 +03:00
|
|
|
'-error-limit',
|
2021-08-15 13:46:50 +03:00
|
|
|
'-message-limit',
|
2020-11-20 17:17:31 +03:00
|
|
|
'-os',
|
|
|
|
'-printfn',
|
|
|
|
'-cflags',
|
|
|
|
'-define',
|
|
|
|
'-d',
|
|
|
|
'-cc',
|
|
|
|
'-o',
|
|
|
|
'-b',
|
|
|
|
'-path',
|
|
|
|
'-custom-prelude',
|
|
|
|
'-name',
|
|
|
|
'-bundle',
|
|
|
|
'-V',
|
|
|
|
'-version',
|
|
|
|
'--version',
|
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_flags_doc = [
|
2020-11-20 21:50:01 +03:00
|
|
|
'-all',
|
|
|
|
'-f',
|
|
|
|
'-h',
|
|
|
|
'-help',
|
|
|
|
'-m',
|
|
|
|
'-o',
|
|
|
|
'-readme',
|
|
|
|
'-v',
|
|
|
|
'-filename',
|
|
|
|
'-pos',
|
|
|
|
'-no-timestamp',
|
|
|
|
'-inline-assets',
|
2022-02-21 13:08:46 +03:00
|
|
|
'-theme-dir',
|
2020-11-20 21:50:01 +03:00
|
|
|
'-open',
|
|
|
|
'-p',
|
|
|
|
'-s',
|
|
|
|
'-l',
|
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_flags_fmt = [
|
2020-11-20 21:50:01 +03:00
|
|
|
'-c',
|
|
|
|
'-diff',
|
|
|
|
'-l',
|
|
|
|
'-w',
|
|
|
|
'-debug',
|
|
|
|
'-verify',
|
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_flags_bin2v = [
|
2020-11-20 21:50:01 +03:00
|
|
|
'-h',
|
|
|
|
'--help',
|
|
|
|
'-m',
|
|
|
|
'--module',
|
|
|
|
'-p',
|
|
|
|
'--prefix',
|
|
|
|
'-w',
|
|
|
|
'--write',
|
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_flags_shader = [
|
|
|
|
'--help',
|
|
|
|
'-h',
|
|
|
|
'--force-update',
|
|
|
|
'-u',
|
|
|
|
'--verbose',
|
|
|
|
'-v',
|
|
|
|
'--slang',
|
|
|
|
'-l',
|
|
|
|
'--output',
|
|
|
|
'-o',
|
2021-10-16 22:10:48 +03:00
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_flags_missdoc = [
|
|
|
|
'--help',
|
|
|
|
'-h',
|
|
|
|
'--tags',
|
|
|
|
'-t',
|
|
|
|
'--deprecated',
|
|
|
|
'-d',
|
|
|
|
'--private',
|
|
|
|
'-p',
|
|
|
|
'--no-line-numbers',
|
|
|
|
'-n',
|
|
|
|
'--exclude',
|
|
|
|
'-e',
|
|
|
|
'--relative-paths',
|
|
|
|
'-r',
|
|
|
|
'--js',
|
2022-05-27 18:19:06 +03:00
|
|
|
'--verify',
|
|
|
|
'--diff',
|
2022-05-18 08:50:32 +03:00
|
|
|
]
|
2022-06-21 15:11:42 +03:00
|
|
|
auto_complete_flags_bump = [
|
|
|
|
'--patch',
|
|
|
|
'--minor',
|
|
|
|
'--major',
|
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_flags_self = [
|
2020-11-20 21:50:01 +03:00
|
|
|
'-prod',
|
|
|
|
]
|
2022-05-18 08:50:32 +03:00
|
|
|
auto_complete_compilers = [
|
2020-11-20 17:17:31 +03:00
|
|
|
'cc',
|
|
|
|
'gcc',
|
|
|
|
'tcc',
|
|
|
|
'tinyc',
|
|
|
|
'clang',
|
|
|
|
'mingw',
|
|
|
|
'msvc',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-12-21 08:03:33 +03:00
|
|
|
// auto_complete prints auto completion results back to the calling shell's completion system.
|
|
|
|
// auto_complete acts as communication bridge between the calling shell and V's completions.
|
2020-11-20 17:17:31 +03:00
|
|
|
fn auto_complete(args []string) {
|
|
|
|
if args.len <= 1 || args[0] != 'complete' {
|
|
|
|
if args.len == 1 {
|
2022-01-08 00:36:42 +03:00
|
|
|
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)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('Unknown shell ${shell_name}. Supported shells are: ${auto_complete_shells}')
|
2022-01-08 00:36:42 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
eprintln('auto completion require arguments to work.')
|
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('auto completion failed for "${args}".')
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
sub := args[1]
|
|
|
|
sub_args := args[1..]
|
|
|
|
match sub {
|
|
|
|
'setup' {
|
|
|
|
if sub_args.len <= 1 || sub_args[1] !in auto_complete_shells {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('please specify a shell to setup auto completion for (${auto_complete_shells}).')
|
2020-11-20 17:17:31 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
shell := sub_args[1]
|
2022-01-08 00:36:42 +03:00
|
|
|
println(setup_for_shell(shell))
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
|
|
|
'bash' {
|
|
|
|
if sub_args.len <= 1 {
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
mut lines := []string{}
|
|
|
|
list := auto_complete_request(sub_args[1..])
|
|
|
|
for entry in list {
|
2022-11-15 16:53:13 +03:00
|
|
|
lines << "COMPREPLY+=('${entry}')"
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
|
|
|
println(lines.join('\n'))
|
|
|
|
}
|
2021-03-04 01:57:08 +03:00
|
|
|
'fish', 'powershell' {
|
2020-11-23 15:53:32 +03:00
|
|
|
if sub_args.len <= 1 {
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
mut lines := []string{}
|
|
|
|
list := auto_complete_request(sub_args[1..])
|
|
|
|
for entry in list {
|
2022-11-15 16:53:13 +03:00
|
|
|
lines << '${entry}'
|
2020-11-23 15:53:32 +03:00
|
|
|
}
|
|
|
|
println(lines.join('\n'))
|
|
|
|
}
|
2020-11-22 22:12:34 +03:00
|
|
|
'zsh' {
|
|
|
|
if sub_args.len <= 1 {
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
mut lines := []string{}
|
|
|
|
list := auto_complete_request(sub_args[1..])
|
|
|
|
for entry in list {
|
2022-11-15 16:53:13 +03:00
|
|
|
lines << 'compadd -U -S' + '""' + ' -- ' + "'${entry}';"
|
2020-11-22 22:12:34 +03:00
|
|
|
}
|
|
|
|
println(lines.join('\n'))
|
|
|
|
}
|
2022-01-09 17:09:10 +03:00
|
|
|
'-h', '--help' {
|
|
|
|
println(help_text)
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:03:33 +03:00
|
|
|
// append_separator_if_dir is a utility function.that returns the input `path` appended an
|
|
|
|
// OS dependant path separator if the `path` is a directory.
|
|
|
|
fn append_separator_if_dir(path string) string {
|
2021-03-22 17:45:29 +03:00
|
|
|
if os.is_dir(path) && !path.ends_with(os.path_separator) {
|
|
|
|
return path + os.path_separator
|
2020-12-21 08:03:33 +03:00
|
|
|
}
|
2021-03-22 17:45:29 +03:00
|
|
|
return path
|
2020-12-21 08:03:33 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:39 +03:00
|
|
|
// nearest_path_or_root returns the nearest valid path searching
|
|
|
|
// backwards from `path`.
|
|
|
|
fn nearest_path_or_root(path string) string {
|
|
|
|
mut fixed_path := path
|
|
|
|
if !os.is_dir(fixed_path) {
|
|
|
|
fixed_path = path.all_before_last(os.path_separator)
|
|
|
|
if fixed_path == '' {
|
|
|
|
fixed_path = '/'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fixed_path
|
|
|
|
}
|
|
|
|
|
2020-12-21 08:03:33 +03:00
|
|
|
// auto_complete_request retuns a list of completions resolved from a full argument list.
|
2020-11-20 17:17:31 +03:00
|
|
|
fn auto_complete_request(args []string) []string {
|
|
|
|
// Using space will ensure a uniform input in cases where the shell
|
2020-11-20 21:50:01 +03:00
|
|
|
// returns the completion input as a string (['v','run'] vs. ['v run']).
|
2020-11-20 17:17:31 +03:00
|
|
|
split_by := ' '
|
|
|
|
request := args.join(split_by)
|
2022-01-10 22:50:39 +03:00
|
|
|
mut do_home_expand := false
|
2020-11-20 17:17:31 +03:00
|
|
|
mut list := []string{}
|
|
|
|
// new_part := request.ends_with('\n\n')
|
|
|
|
mut parts := request.trim_right(' ').split(split_by)
|
|
|
|
if parts.len <= 1 { // 'v <tab>' -> top level commands.
|
|
|
|
for command in auto_complete_commands {
|
|
|
|
list << command
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-10 22:50:39 +03:00
|
|
|
mut part := parts.last().trim(' ')
|
2020-11-20 21:50:01 +03:00
|
|
|
mut parent_command := ''
|
|
|
|
for i := parts.len - 1; i >= 0; i-- {
|
|
|
|
if parts[i].starts_with('-') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parent_command = parts[i]
|
|
|
|
break
|
|
|
|
}
|
2022-05-18 08:50:32 +03:00
|
|
|
if part.starts_with('-') { // 'v [subcmd] -<tab>' or 'v [subcmd] --<tab>'-> flags.
|
|
|
|
get_flags := fn (base []string, flag string) []string {
|
|
|
|
mut results := []string{}
|
|
|
|
for entry in base {
|
|
|
|
if entry.starts_with(flag) {
|
|
|
|
results << entry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2020-11-20 21:50:01 +03:00
|
|
|
match parent_command {
|
|
|
|
'bin2v' { // 'v bin2v -<tab>'
|
|
|
|
list = get_flags(auto_complete_flags_bin2v, part)
|
|
|
|
}
|
|
|
|
'build' { // 'v build -<tab>' -> flags.
|
|
|
|
list = get_flags(auto_complete_flags, part)
|
|
|
|
}
|
|
|
|
'doc' { // 'v doc -<tab>' -> flags.
|
|
|
|
list = get_flags(auto_complete_flags_doc, part)
|
|
|
|
}
|
|
|
|
'fmt' { // 'v fmt -<tab>' -> flags.
|
|
|
|
list = get_flags(auto_complete_flags_fmt, part)
|
|
|
|
}
|
|
|
|
'self' { // 'v self -<tab>' -> flags.
|
|
|
|
list = get_flags(auto_complete_flags_self, part)
|
|
|
|
}
|
2021-10-16 22:10:48 +03:00
|
|
|
'shader' { // 'v shader -<tab>' -> flags.
|
|
|
|
list = get_flags(auto_complete_flags_shader, part)
|
|
|
|
}
|
2022-05-18 08:50:32 +03:00
|
|
|
'missdoc' { // 'v missdoc -<tab>' -> flags.
|
|
|
|
list = get_flags(auto_complete_flags_missdoc, part)
|
|
|
|
}
|
2022-06-21 15:11:42 +03:00
|
|
|
'bump' { // 'v bump -<tab>' -> flags.
|
|
|
|
list = get_flags(auto_complete_flags_bump, part)
|
|
|
|
}
|
2020-11-20 21:50:01 +03:00
|
|
|
else {
|
|
|
|
for flag in auto_complete_flags {
|
|
|
|
if flag == part {
|
|
|
|
if flag == '-cc' { // 'v -cc <tab>' -> list of available compilers.
|
|
|
|
for compiler in auto_complete_compilers {
|
2020-12-21 08:03:33 +03:00
|
|
|
path := os.find_abs_path_of_executable(compiler) or { '' }
|
2020-11-20 21:50:01 +03:00
|
|
|
if path != '' {
|
|
|
|
list << compiler
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
2020-11-20 21:50:01 +03:00
|
|
|
} else if flag.starts_with(part) { // 'v -<char(s)><tab>' -> flags matching "<char(s)>".
|
|
|
|
list << flag
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 08:50:32 +03:00
|
|
|
// Clear the list if the result is identical to the part examined
|
|
|
|
// (the flag must have already been completed)
|
|
|
|
if list.len == 1 && part == list[0] {
|
|
|
|
list.clear()
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
} else {
|
2020-11-20 21:50:01 +03:00
|
|
|
match part {
|
|
|
|
'help' { // 'v help <tab>' -> top level commands except "help".
|
|
|
|
list = auto_complete_commands.filter(it != part && it != 'complete')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// 'v <char(s)><tab>' -> commands matching "<char(s)>".
|
|
|
|
// Don't include if part matches a full command - instead go to path completion below.
|
|
|
|
for command in auto_complete_commands {
|
|
|
|
if part != command && command.starts_with(part) {
|
|
|
|
list << command
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Nothing of value was found.
|
|
|
|
// Mimic shell dir and file completion
|
|
|
|
if list.len == 0 {
|
|
|
|
mut ls_path := '.'
|
|
|
|
mut collect_all := part in auto_complete_commands
|
|
|
|
mut path_complete := false
|
2022-01-10 22:50:39 +03:00
|
|
|
do_home_expand = part.starts_with('~')
|
|
|
|
if do_home_expand {
|
|
|
|
add_sep := if part == '~' { os.path_separator } else { '' }
|
|
|
|
part = part.replace_once('~', os.home_dir().trim_right(os.path_separator)) + add_sep
|
|
|
|
}
|
|
|
|
is_abs_path := part.starts_with(os.path_separator) // TODO Windows support for drive prefixes
|
2020-11-20 22:29:59 +03:00
|
|
|
if part.ends_with(os.path_separator) || part == '.' || part == '..' {
|
|
|
|
// 'v <command>(.*/$|.|..)<tab>' -> output full directory list
|
2020-11-20 17:17:31 +03:00
|
|
|
ls_path = '.' + os.path_separator + part
|
2022-01-10 22:50:39 +03:00
|
|
|
if is_abs_path {
|
|
|
|
ls_path = nearest_path_or_root(part)
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
collect_all = true
|
2020-11-20 22:29:59 +03:00
|
|
|
} else if !collect_all && part.contains(os.path_separator) && os.is_dir(os.dir(part)) {
|
|
|
|
// 'v <command>(.*/.* && os.is_dir)<tab>' -> output completion friendly directory list
|
2022-01-10 22:50:39 +03:00
|
|
|
if is_abs_path {
|
|
|
|
ls_path = nearest_path_or_root(part)
|
|
|
|
} else {
|
|
|
|
ls_path = os.dir(part)
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
path_complete = true
|
|
|
|
}
|
2022-01-10 22:50:39 +03:00
|
|
|
|
2020-12-21 08:03:33 +03:00
|
|
|
entries := os.ls(ls_path) or { return list }
|
2022-01-10 22:50:39 +03:00
|
|
|
mut last := part.all_after_last(os.path_separator)
|
|
|
|
if is_abs_path && os.is_dir(part) {
|
|
|
|
last = ''
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
if path_complete {
|
|
|
|
path := part.all_before_last(os.path_separator)
|
|
|
|
for entry in entries {
|
|
|
|
if entry.starts_with(last) {
|
2020-12-21 08:03:33 +03:00
|
|
|
list << append_separator_if_dir(os.join_path(path, entry))
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for entry in entries {
|
2022-01-10 22:50:39 +03:00
|
|
|
if collect_all || entry.starts_with(last) {
|
2020-12-21 08:03:33 +03:00
|
|
|
list << append_separator_if_dir(entry)
|
2020-11-20 17:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 22:50:39 +03:00
|
|
|
if do_home_expand {
|
|
|
|
return list.map(it.replace_once(os.home_dir().trim_right(os.path_separator), '~'))
|
|
|
|
}
|
2020-11-20 17:17:31 +03:00
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:36:42 +03:00
|
|
|
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
|
2022-11-15 16:53:13 +03:00
|
|
|
src=\$(${vexe} complete bash \$(printf "%s\\n" \${COMP_WORDS[@]: 0:\$limit}))
|
2022-01-08 00:36:42 +03:00
|
|
|
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
|
2022-11-15 16:53:13 +03:00
|
|
|
${vexe} complete fish (commandline -cop)
|
2022-01-08 00:36:42 +03:00
|
|
|
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
|
2022-11-15 16:53:13 +03:00
|
|
|
src=\$(${vexe} complete zsh \$(printf "%s\\n" \${(@)words[1,\$CURRENT]}))
|
2022-01-08 00:36:42 +03:00
|
|
|
if [[ \$? == 0 ]]; then
|
|
|
|
eval \${src}
|
|
|
|
#echo \${src}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
compdef _v v
|
|
|
|
'
|
|
|
|
}
|
|
|
|
'powershell' {
|
|
|
|
setup = '
|
|
|
|
Register-ArgumentCompleter -Native -CommandName v -ScriptBlock {
|
|
|
|
param(\$commandName, \$wordToComplete, \$cursorPosition)
|
2022-11-15 16:53:13 +03:00
|
|
|
${vexe} complete powershell "\$wordToComplete" | ForEach-Object {
|
2022-01-08 00:36:42 +03:00
|
|
|
[System.Management.Automation.CompletionResult]::new(\$_, \$_, \'ParameterValue\', \$_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
return setup
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:17:31 +03:00
|
|
|
fn main() {
|
|
|
|
args := os.args[1..]
|
2020-11-20 21:50:01 +03:00
|
|
|
// println('"$args"')
|
2020-11-20 17:17:31 +03:00
|
|
|
auto_complete(args)
|
|
|
|
}
|