1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 11:30:06 +08:00

feat(git): lua completion for sub command

This commit is contained in:
Eric Wong 2024-12-17 17:50:28 +08:00
parent 3ffa25addc
commit bde1252f93
6 changed files with 167 additions and 109 deletions

View File

@ -1,37 +1,50 @@
let s:JOB = SpaceVim#api#import('job')
"=============================================================================
" reset.vim --- git reset command
" Copyright (c) 2016-2019 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
if has('nvim-0.9.0')
function! git#reset#complete(ArgLead, CmdLine, CursorPos) abort
return luaeval('require("git.command.reset").complete(vim.api.nvim_eval("a:ArgLead"), vim.api.nvim_eval("a:CmdLine"), vim.api.nvim_eval("a:CursorPos"))')
endfunction
else
let s:JOB = SpaceVim#api#import('job')
function! git#reset#run(args)
function! git#reset#run(args)
if len(a:args) == 1 && a:args[0] ==# '%'
let cmd = ['git', 'reset', 'HEAD', expand('%')]
let cmd = ['git', 'reset', 'HEAD', expand('%')]
else
let cmd = ['git', 'reset'] + a:args
let cmd = ['git', 'reset'] + a:args
endif
call git#logger#debug('git-reset cmd:' . string(cmd))
call s:JOB.start(cmd,
\ {
\ 'on_exit' : function('s:on_exit'),
\ }
\ )
\ {
\ 'on_exit' : function('s:on_exit'),
\ }
\ )
endfunction
endfunction
function! s:on_exit(id, data, event) abort
function! s:on_exit(id, data, event) abort
call git#logger#debug('git-reset exit data:' . string(a:data))
if a:data ==# 0
if exists(':GitGutter')
GitGutter
endif
echo 'done!'
if exists(':GitGutter')
GitGutter
endif
echo 'done!'
else
echo 'failed!'
echo 'failed!'
endif
endfunction
endfunction
function! git#reset#complete(ArgLead, CmdLine, CursorPos)
function! git#reset#complete(ArgLead, CmdLine, CursorPos)
return "%\n" . join(getcompletion(a:ArgLead, 'file'), "\n")
endfunction
endfunction
endif

View File

@ -7,38 +7,44 @@
" :Git rm %
" <
let s:JOB = SpaceVim#api#import('job')
if has('nvim-0.9.0')
function! git#rm#complete(ArgLead, CmdLine, CursorPos) abort
return luaeval('require("git.command.rm").complete(vim.api.nvim_eval("a:ArgLead"), vim.api.nvim_eval("a:CmdLine"), vim.api.nvim_eval("a:CursorPos"))')
endfunction
else
let s:JOB = SpaceVim#api#import('job')
function! git#rm#run(files) abort
function! git#rm#run(files) abort
if len(a:files) == 1 && a:files[0] ==# '%'
let cmd = ['git', 'rm', expand('%')]
let cmd = ['git', 'rm', expand('%')]
else
let cmd = ['git', 'rm'] + a:files
let cmd = ['git', 'rm'] + a:files
endif
call git#logger#debug('git-rm cmd:' . string(cmd))
call s:JOB.start(cmd,
\ {
\ 'on_exit' : function('s:on_exit'),
\ }
\ )
\ {
\ 'on_exit' : function('s:on_exit'),
\ }
\ )
endfunction
endfunction
function! s:on_exit(id, data, event) abort
function! s:on_exit(id, data, event) abort
call git#logger#debug('git-rm exit data:' . string(a:data))
if a:data ==# 0
if exists(':GitGutter')
GitGutter
endif
echo 'done!'
if exists(':GitGutter')
GitGutter
endif
echo 'done!'
else
echo 'failed!'
echo 'failed!'
endif
endfunction
endfunction
function! git#rm#complete(ArgLead, CmdLine, CursorPos) abort
function! git#rm#complete(ArgLead, CmdLine, CursorPos) abort
return "%\n" . join(getcompletion(a:ArgLead, 'file'), "\n")
endfunction
endfunction
endif

View File

@ -6,104 +6,109 @@
" :Git stash list
" <
let s:JOB = SpaceVim#api#import('job')
let s:NOTI = SpaceVim#api#import('notify')
let s:BUFFER = SpaceVim#api#import('vim#buffer')
if has('nvim-0.9.0')
function! git#stash#complete(ArgLead, CmdLine, CursorPos) abort
return luaeval('require("git.command.stash").complete(vim.api.nvim_eval("a:ArgLead"), vim.api.nvim_eval("a:CmdLine"), vim.api.nvim_eval("a:CursorPos"))')
endfunction
else
let s:JOB = SpaceVim#api#import('job')
let s:NOTI = SpaceVim#api#import('notify')
let s:BUFFER = SpaceVim#api#import('vim#buffer')
let s:stash_show_bufnr = -1
" @todo rewrite Git stash in lua
function! git#stash#run(args) abort
let s:stash_show_bufnr = -1
" @todo rewrite Git stash in lua
function! git#stash#run(args) abort
let cmd = ['git', 'stash'] + a:args
let subcmd = get(a:args, 0, '')
if !empty(subcmd) && index(['drop', 'show'], subcmd) > -1
let subcmd = subcmd . '_'
let subcmd = subcmd . '_'
else
let subcmd = ''
let subcmd = ''
endif
call git#logger#debug('git-stash cmd:' . string(cmd))
let s:lines = []
call s:JOB.start(cmd,
\ {
\ 'on_stderr' : function('s:on_' . subcmd . 'stderr'),
\ 'on_stdout' : function('s:on_' . subcmd . 'stdout'),
\ 'on_exit' : function('s:on_' . subcmd . 'exit'),
\ }
\ )
endfunction
\ {
\ 'on_stderr' : function('s:on_' . subcmd . 'stderr'),
\ 'on_stdout' : function('s:on_' . subcmd . 'stdout'),
\ 'on_exit' : function('s:on_' . subcmd . 'exit'),
\ }
\ )
endfunction
function! s:on_stdout(id, data, event) abort
function! s:on_stdout(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'Normal')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'Normal')
endfor
endfunction
endfunction
function! s:on_stderr(id, data, event) abort
function! s:on_stderr(id, data, event) abort
for data in a:data
call git#logger#debug('git-stash stderr:' . data)
call git#logger#debug('git-stash stderr:' . data)
endfor
endfunction
endfunction
function! s:on_exit(id, data, event) abort
function! s:on_exit(id, data, event) abort
call git#logger#debug('git-stash exit data:' . string(a:data))
if a:data ==# 0
" echo 'done!'
" echo 'done!'
else
" echo 'failed!'
" echo 'failed!'
endif
endfunction
endfunction
function! s:on_drop_stdout(id, data, event) abort
function! s:on_drop_stdout(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'Normal')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'Normal')
endfor
endfunction
endfunction
function! s:on_drop_stderr(id, data, event) abort
function! s:on_drop_stderr(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'WarningMsg')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'WarningMsg')
endfor
endfunction
endfunction
function! s:on_drop_exit(id, data, event) abort
function! s:on_drop_exit(id, data, event) abort
call git#logger#debug('git-stash exit data:' . string(a:data))
if a:data ==# 0
" echo 'done!'
" echo 'done!'
else
" echo 'failed!'
" echo 'failed!'
endif
endfunction
endfunction
function! s:on_show_stdout(id, data, event) abort
function! s:on_show_stdout(id, data, event) abort
for data in a:data
call git#logger#debug('git-stash stdout:' . data)
call git#logger#debug('git-stash stdout:' . data)
endfor
let s:lines += a:data
endfunction
endfunction
function! s:on_show_stderr(id, data, event) abort
function! s:on_show_stderr(id, data, event) abort
for line in filter(a:data, '!empty(v:val)')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'WarningMsg')
call git#logger#debug('git-stash stdout:' . line)
call s:NOTI.notify(line, 'WarningMsg')
endfor
endfunction
endfunction
function! s:on_show_exit(id, data, event) abort
function! s:on_show_exit(id, data, event) abort
call git#logger#debug('git-stash exit data:' . string(a:data))
if a:data ==# 0
if !bufexists(s:stash_show_bufnr)
let s:stash_show_bufnr = s:openStashShowBuffer()
endif
call s:BUFFER.buf_set_lines(s:stash_show_bufnr, 0 , -1, 0, s:lines)
if !bufexists(s:stash_show_bufnr)
let s:stash_show_bufnr = s:openStashShowBuffer()
endif
call s:BUFFER.buf_set_lines(s:stash_show_bufnr, 0 , -1, 0, s:lines)
endif
endfunction
endfunction
function! s:openStashShowBuffer() abort
function! s:openStashShowBuffer() abort
edit git://blame
normal! "_dd
setl nobuflisted
@ -114,30 +119,31 @@ function! s:openStashShowBuffer() abort
setl syntax=diff
nnoremap <buffer><silent> q :bd!<CR>
return bufnr('%')
endfunction
endfunction
function! s:sub_commands() abort
function! s:sub_commands() abort
return join([
\ 'list',
\ 'show',
\ 'drop',
\ 'pop', 'apply',
\ 'branch',
\ 'clear',
\ 'save',
\ 'push',
\ ],
\ "\n")
endfunction
\ 'list',
\ 'show',
\ 'drop',
\ 'pop', 'apply',
\ 'branch',
\ 'clear',
\ 'save',
\ 'push',
\ ],
\ "\n")
endfunction
function! git#stash#complete(ArgLead, CmdLine, CursorPos) abort
function! git#stash#complete(ArgLead, CmdLine, CursorPos) abort
let str = a:CmdLine[:a:CursorPos-1]
if str =~# '^Git\s\+stash\s\+[a-z]\=$'
return s:sub_commands()
return s:sub_commands()
else
return ''
return ''
endif
endfunction
endfunction
endif

View File

@ -5,7 +5,7 @@
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
local m = {}
local M = {}
local job = require('spacevim.api.job')
local nt = require('spacevim.api.notify')
@ -23,7 +23,7 @@ local function on_exit(id, code, single)
end
end
function m.run(argv)
function M.run(argv)
local cmd = { 'git', 'reset' }
if #argv == 1 and argv[1] == '%' then
cmd = { 'git', 'reset', 'HEAD', vim.fn.expand('%') }
@ -38,4 +38,9 @@ function m.run(argv)
})
end
return m
function M.complete(ArgLead, CmdLine, CursorPos)
local rst = vim.fn.getcompletion(ArgLead, 'file')
return table.concat(rst, '\n')
end
return M

View File

@ -47,4 +47,9 @@ function m.run(argv)
})
end
function M.complete(ArgLead, CmdLine, CursorPos)
local rst = vim.fn.getcompletion(ArgLead, 'file')
return table.concat(rst, '\n')
end
return m

View File

@ -97,4 +97,27 @@ function M.run(argv)
end
end
local function sub_commands()
return {
'list',
'show',
'drop',
'pop',
'apply',
'branch',
'clear',
'save',
'push',
}
end
function M.complete(ArgLead, CmdLine, CursorPos)
local str = string.sub(CmdLine, 1, CursorPos)
if vim.regex([[^Git\s\+stash\s\+[a-z]\+$]]):match_str(str) then
return table.concat(sub_commands(), '\n')
else
return ''
end
end
return M