VIM plugin: Improve commands, and help document. (#113)

* VIM plugin: Add help and improve commands.

* Improve help command and command completion.
add-tracing
Zhiming Ma 2023-04-21 13:06:16 +08:00 committed by GitHub
parent 3dedd49afc
commit 300fe8c2b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 187 additions and 26 deletions

1
clients/vim/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/doc/tags

View File

@ -12,12 +12,15 @@
" Make sure that the filetype plugin has been enabled. " Make sure that the filetype plugin has been enabled.
filetype plugin on filetype plugin on
" Assume using vim-plug as plugin manager
Plug 'TabbyML/tabby', {'rtp': 'clients/vim'} Plug 'TabbyML/tabby', {'rtp': 'clients/vim'}
" Set URL of Tabby server
let g:tabby_server_url = 'http://127.0.0.1:5000' let g:tabby_server_url = 'http://127.0.0.1:5000'
``` ```
## Usage ## Usage
1. In insert mode, Tabby will show code suggestion when you stop typing. Press `<Tab>` to accpet the current suggestion, `<M-]>` to see the next suggestion, `<M-[>` to see previous suggestion, or `<C-]>` to dismiss. 1. In insert mode, Tabby will show code suggestion when you stop typing. Press `<Tab>` to accpet the current suggestion, `<M-]>` to see the next suggestion, `<M-[>` to see previous suggestion, or `<C-]>` to dismiss.
2. Use command `:Tabby enable` to enable, `:Tabby disable` to disable Tabby, and `:Tabby status` to check status. 2. Use command `:Tabby enable` to enable, `:Tabby disable` to disable Tabby, and `:Tabby status` to check status.
3. Use command `:help Tabby` for more information.

View File

@ -7,27 +7,77 @@ let g:autoloaded_tabby = 1
let s:commands = {} let s:commands = {}
function! s:commands.status(...)
call tabby#Status()
endfunction
function! s:commands.enable(...)
call tabby#Enable()
call tabby#Status()
endfunction
function! s:commands.disable(...)
call tabby#Disable()
call tabby#Status()
endfunction
function! s:commands.toggle(...)
call tabby#Toggle()
endfunction
function! s:commands.help(...)
let args = get(a:, 1, [])
if len(args) < 1
execute 'help Tabby'
return
endif
try
execute 'help Tabby-' . join(args, '-')
return
catch
endtry
try
execute 'help tabby_' . join(args, '_')
return
catch
endtry
execute 'help Tabby'
endfunction
function! tabby#CompleteCommands(arglead, cmd, pos)
let words = split(a:cmd[0:a:pos].'#', ' ')
if len(words) > 3
return []
endif
if len(words) == 3
if words[1] == 'help'
let candidates = ['compatibility', 'commands', 'options', 'keybindings']
else
return []
endif
else
let candidates = keys(s:commands)
endif
let end_index = len(a:arglead) - 1
if end_index < 0
return candidates
else
return filter(candidates, { idx, val ->
\ val[0:end_index] ==# a:arglead
\})
endif
endfunction
function! tabby#Command(args) function! tabby#Command(args)
let args = split(a:args, ' ') let args = split(a:args, ' ')
if len(args) < 1 if len(args) < 1
call tabby#Status() call tabby#Status()
echo 'Use :help Tabby to see available commands.'
return return
endif endif
if args[0] == 'enable' if has_key(s:commands, args[0])
call tabby#Enable() call s:commands[args[0]](args[1:])
call tabby#Status()
elseif args[0] == 'disable'
call tabby#Disable()
call tabby#Status()
elseif args[0] == 'server'
if len(args) < 2
echo 'Usage: Tabby server <url>'
return
endif
call tabby#SetServerUrl(args[1])
echo 'Tabby server URL set to ' . args[1]
elseif args[0] == 'status'
call tabby#Status()
else else
echo 'Unknown command' echo 'Unknown command'
endif endif
@ -39,6 +89,10 @@ if !exists('g:tabby_enabled')
let g:tabby_enabled = v:true let g:tabby_enabled = v:true
endif endif
if !exists('g:tabby_suggestion_delay')
let g:tabby_suggestion_delay = 150
endif
if !exists('g:tabby_filetype_to_languages') if !exists('g:tabby_filetype_to_languages')
" From: vim filetype https://github.com/vim/vim/blob/master/runtime/filetype.vim " From: vim filetype https://github.com/vim/vim/blob/master/runtime/filetype.vim
" To: vscode language identifier https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers " To: vscode language identifier https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers
@ -51,6 +105,13 @@ if !exists('g:tabby_filetype_to_languages')
\ } \ }
endif endif
function! tabby#SetServerUrl(url)
let g:tabby_server_url = a:url
call s:UpdateServerUrl()
endfunction
" Node job control
function! tabby#Enable() function! tabby#Enable()
let g:tabby_enabled = v:true let g:tabby_enabled = v:true
if !tabby#Running() if !tabby#Running()
@ -73,13 +134,6 @@ function! tabby#Toggle()
endif endif
endfunction endfunction
function! tabby#SetServerUrl(url)
let g:tabby_server_url = a:url
call s:UpdateServerUrl()
endfunction
" Node job control
function! tabby#Start() function! tabby#Start()
if !g:tabby_enabled || tabby#Running() if !g:tabby_enabled || tabby#Running()
return return
@ -233,8 +287,7 @@ function! tabby#Schedule()
return return
endif endif
call tabby#Clear() call tabby#Clear()
let delay = 150 let s:scheduled = timer_start(g:tabby_suggestion_delay, function('tabby#Trigger'))
let s:scheduled = timer_start(delay, function('tabby#Trigger'))
endfunction endfunction
function! tabby#Trigger(timer) function! tabby#Trigger(timer)

102
clients/vim/doc/tabby.txt Normal file
View File

@ -0,0 +1,102 @@
tabby.txt Tabby
*Tabby* *tabby* *Tabby-doc*
Tabby is a self-hosted AI coding assistant that can suggest multi-line code or
full functions in real-time. For more information, please check out our
{Website}{1} and {Github}{2}. If you encounter any problem or have any
suggestion, please open an {issue}{3}.
{1} https://www.tabbyml.com/
{2} https://github.com/TabbyML/tabby
{3} https://github.com/TabbyML/tabby/issues/new
*Tabby-compatibility* *Tabby-neovim*
Compatibility~
This plugin is compatible with VIM 9.0+ with `+job` and `+textprop` features
enabled, or NeoVIM 0.6.0+.
*Tabby-commands*
Commands~
*:Tabby*
:Tabby Same as |:Tabby-status|.
*:Tabby-enable*
:Tabby enable Start Tabby if not currently running in current VIM
process.
*:Tabby-disable*
:Tabby disable Stop Tabby in current VIM process. To disable Tabby
globally, set |g:tabby_enable| to v:false.
*:Tabby-toggle*
:Tabby toggle Toggle enable or disable Tabby, same as use command
|:Tabby-enable| or |:Tabby-disable|.
*:Tabby-status*
:Tabby status Check whether Tabby is enabled or not, and the
reachabilty to the Tabby server. Also report errors
if any compatibility problems exist.
*:Tabby-help*
:Tabby help [subject] Search for help information in this document using
VIM command `:help`.
*Tabby-options*
Options~
*g:tabby_enable*
g:tabby_enable Controls Tabby whether auto-starts along with VIM or
not. Modifying this value do not start or stop Tabby
at the same time. You can use |:Tabby-enable| or
|:Tabby-disable| to start or stop Tabby manually in
current VIM process.
>
let g:tabby_enable = v:true
<
*g:tabby_server_url*
g:tabby_server_url Specify the Tabby server URL. You always need this
setting in your vimrc file, unless you are using the
default value: "http://localhost:5000".
>
let g:tabby_server_url = 'http://localhost:5000'
<
*g:tabby_suggestion_delay*
g:tabby_suggestion_delay
Controls the delay after which the suggestion request
is sent to server. If you want suggestion to show up
more quickly or slowly, try to tune this value.
Default value is 150 milliseconds.
>
let g:tabby_suggestion_delay = 150
<
*g:tabby_filetype_to_languages*
g:tabby_filetype_to_languages
This option is a dictionary that map from the VIM
`:filetype` to {VSCode-Language-Identifier}{1}. Not
listed filetype will be used as language identifier
directly.
A correct language identifier is required for the
Tabby server to generate suggestion. If your filetype
need converting to language identifier but not listed,
add it in this dictionary.
You can also map a filetype to "unknow" to prevent
Tabby giving suggestion for specified filetype.
{1} https://code.visualstudio.com/docs/languages/identifiers
>
let g:tabby_filetype_to_languages = {
\ "bash": "shellscript",
\ "cs": "csharp",
\ "objc": "objective-c",
\ "objcpp": "objective-cpp",
\ }
<
*Tabby-keybindings* *Tabby-maps*
Keybindings~
<Tab> Accept the current suggestion, fallback to normal
`<TAB>` if no suggestion is shown.
<C-]> Dismiss the current suggestion. Fallback to normal
`<C-]>` if no suggestion is shown.
<M-]> Show the next suggestion. There is a empty suggestion
after the last, before return to first one.
<M-[> Show the previous suggestion. There is a empty
suggestion before the first, before return to last
one.
vim:tw=78:ts=8:noet:ft=help:norl:

View File

@ -5,7 +5,7 @@ let g:loaded_tabby = 1
call tabby#Start() call tabby#Start()
command! -nargs=* Tabby exe tabby#Command(<q-args>) command! -nargs=* -complete=customlist,tabby#CompleteCommands Tabby call tabby#Command(<q-args>)
imap <script><silent><nowait><expr> <Tab> tabby#Accept(pumvisible() ? "\<C-N>" : "\t") imap <script><silent><nowait><expr> <Tab> tabby#Accept(pumvisible() ? "\<C-N>" : "\t")
imap <script><silent><nowait><expr> <C-]> tabby#Dismiss("\<C-]>") imap <script><silent><nowait><expr> <C-]> tabby#Dismiss("\<C-]>")
@ -25,3 +25,5 @@ augroup tabby
autocmd BufLeave * call tabby#Clear() autocmd BufLeave * call tabby#Clear()
autocmd InsertLeave * call tabby#Clear() autocmd InsertLeave * call tabby#Clear()
augroup END augroup END
silent! execute 'helptags' fnameescape(expand('<sfile>:h:h') . '/doc')