1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v complete: fix completion of v fmt cmd/tools/vcomp<Tab>

This commit is contained in:
Delyan Angelov 2020-11-20 21:29:59 +02:00
parent de0cfdd323
commit e53b451d60

View File

@ -28,6 +28,7 @@ import os
const ( const (
auto_complete_shells = ['bash', 'fish', 'zsh', 'powershell'] // list of supported shells auto_complete_shells = ['bash', 'fish', 'zsh', 'powershell'] // list of supported shells
vexe = os.getenv('VEXE')
) )
// Snooped from cmd/v/v.v, vlib/v/pref/pref.v // Snooped from cmd/v/v.v, vlib/v/pref/pref.v
@ -202,16 +203,16 @@ fn auto_complete(args []string) {
shell := sub_args[1] shell := sub_args[1]
mut setup := '' mut setup := ''
match shell { match shell {
'bash' { setup = r' 'bash' { setup = '
_v_completions() { _v_completions() {
local src local src
local limit local limit
# Send all words up to the word the cursor is currently on # Send all words up to the word the cursor is currently on
let limit=1+$COMP_CWORD let limit=1+\$COMP_CWORD
src=$(v complete bash $(printf "%s\n" ${COMP_WORDS[@]: 0:$limit})) src=\$($vexe complete bash \$(printf "%s\\n" \${COMP_WORDS[@]: 0:\$limit}))
if [[ $? == 0 ]]; then if [[ \$? == 0 ]]; then
eval ${src} eval \${src}
#echo ${src} #echo \${src}
fi fi
} }
@ -328,10 +329,12 @@ fn auto_complete_request(args []string) []string {
mut ls_path := '.' mut ls_path := '.'
mut collect_all := part in auto_complete_commands mut collect_all := part in auto_complete_commands
mut path_complete := false mut path_complete := false
if part.ends_with(os.path_separator) || part == '.' || part == '..' { // 'v <command>(.*/$|.|..)<tab>' -> output full directory list if part.ends_with(os.path_separator) || part == '.' || part == '..' {
// 'v <command>(.*/$|.|..)<tab>' -> output full directory list
ls_path = '.' + os.path_separator + part ls_path = '.' + os.path_separator + part
collect_all = true collect_all = true
} 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 } 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
ls_path = os.dir(part) ls_path = os.dir(part)
path_complete = true path_complete = true
} }
@ -343,13 +346,13 @@ fn auto_complete_request(args []string) []string {
path := part.all_before_last(os.path_separator) path := part.all_before_last(os.path_separator)
for entry in entries { for entry in entries {
if entry.starts_with(last) { if entry.starts_with(last) {
list << entry list << os.join_path(path, entry)
} }
} }
// If only one possible file - send full path to completion system. // If only one possible file - send full path to completion system.
// Please note that this might be bash specific - needs more testing. // Please note that this might be bash specific - needs more testing.
if list.len == 1 { if list.len == 1 {
list = [os.join_path(path, list[0])] list = [list[0]]
} }
} else { } else {
for entry in entries { for entry in entries {