spacevim/bundle/defx-git/ftplugin/defx.vim
JIe 2bb7059579
Some checks failed
Detach Plugins / check (FlyGrep.vim) (push) Has been cancelled
Detach Plugins / check (GitHub.vim) (push) Has been cancelled
Detach Plugins / check (JavaUnit.vim) (push) Has been cancelled
Detach Plugins / check (SourceCounter.vim) (push) Has been cancelled
Detach Plugins / check (cpicker.nvim) (push) Has been cancelled
Detach Plugins / check (dein-ui.vim) (push) Has been cancelled
Detach Plugins / check (git.vim) (push) Has been cancelled
Detach Plugins / check (iedit.vim) (push) Has been cancelled
Detach Plugins / check (scrollbar.vim) (push) Has been cancelled
Detach Plugins / check (vim-chat) (push) Has been cancelled
Detach Plugins / check (vim-cheat) (push) Has been cancelled
Detach Plugins / check (vim-todo) (push) Has been cancelled
Detach Plugins / check (xmake.vim) (push) Has been cancelled
test / Linux (nvim, nightly) (push) Has been cancelled
test / Linux (nvim, v0.3.8) (push) Has been cancelled
test / Linux (nvim, v0.4.0) (push) Has been cancelled
test / Linux (nvim, v0.4.2) (push) Has been cancelled
test / Linux (nvim, v0.4.3) (push) Has been cancelled
test / Linux (nvim, v0.4.4) (push) Has been cancelled
test / Linux (nvim, v0.5.0) (push) Has been cancelled
test / Linux (nvim, v0.5.1) (push) Has been cancelled
test / Linux (nvim, v0.6.0) (push) Has been cancelled
test / Linux (nvim, v0.6.1) (push) Has been cancelled
test / Linux (nvim, v0.7.0) (push) Has been cancelled
test / Linux (nvim, v0.7.2) (push) Has been cancelled
test / Linux (nvim, v0.8.0) (push) Has been cancelled
test / Linux (nvim, v0.8.1) (push) Has been cancelled
test / Linux (nvim, v0.8.2) (push) Has been cancelled
test / Linux (nvim, v0.8.3) (push) Has been cancelled
test / Linux (nvim, v0.9.0) (push) Has been cancelled
test / Linux (nvim, v0.9.1) (push) Has been cancelled
test / Linux (true, vim, v7.4.052) (push) Has been cancelled
test / Linux (true, vim, v7.4.1689) (push) Has been cancelled
test / Linux (true, vim, v7.4.629) (push) Has been cancelled
test / Linux (true, vim, v8.0.0027) (push) Has been cancelled
test / Linux (true, vim, v8.0.0183) (push) Has been cancelled
test / Linux (vim, nightly) (push) Has been cancelled
test / Linux (vim, v8.0.0184) (push) Has been cancelled
test / Linux (vim, v8.0.1453) (push) Has been cancelled
test / Linux (vim, v8.1.2269) (push) Has been cancelled
test / Linux (vim, v8.2.2434) (push) Has been cancelled
test / Linux (vim, v8.2.3995) (push) Has been cancelled
test / Windows (nvim, nightly) (push) Has been cancelled
test / Windows (nvim, v0.3.8) (push) Has been cancelled
test / Windows (nvim, v0.4.2) (push) Has been cancelled
test / Windows (nvim, v0.4.3) (push) Has been cancelled
test / Windows (nvim, v0.4.4) (push) Has been cancelled
test / Windows (nvim, v0.5.0) (push) Has been cancelled
test / Windows (nvim, v0.5.1) (push) Has been cancelled
test / Windows (nvim, v0.6.0) (push) Has been cancelled
test / Windows (nvim, v0.6.1) (push) Has been cancelled
test / Windows (nvim, v0.7.0) (push) Has been cancelled
test / Windows (nvim, v0.7.2) (push) Has been cancelled
test / Windows (nvim, v0.8.0) (push) Has been cancelled
test / Windows (nvim, v0.8.1) (push) Has been cancelled
test / Windows (nvim, v0.8.2) (push) Has been cancelled
test / Windows (nvim, v0.8.3) (push) Has been cancelled
test / Windows (nvim, v0.9.0) (push) Has been cancelled
test / Windows (nvim, v0.9.1) (push) Has been cancelled
test / Windows (vim, nightly) (push) Has been cancelled
test / Windows (vim, v7.4.1185) (push) Has been cancelled
test / Windows (vim, v7.4.1689) (push) Has been cancelled
test / Windows (vim, v8.0.0027) (push) Has been cancelled
test / Windows (vim, v8.0.1453) (push) Has been cancelled
test / Windows (vim, v8.1.2269) (push) Has been cancelled
test / Windows (vim, v8.2.2434) (push) Has been cancelled
test / Windows (vim, v8.2.3995) (push) Has been cancelled
docker / docker (push) Has been cancelled
mirror / check (coding) (push) Has been cancelled
mirror / check (gitee) (push) Has been cancelled
mirror / check (gitlab) (push) Has been cancelled
init
2024-08-21 14:17:26 +08:00

84 lines
2.5 KiB
VimL

if exists('*defx#redraw')
augroup defx_git
autocmd!
autocmd BufWritePost * call defx#redraw()
augroup END
endif
scriptencoding utf-8
if exists('b:defx_git_loaded')
finish
endif
let b:defx_git_loaded = 1
function! s:search(dir) abort
let l:icons = get(g:, 'defx_git_indicators', {})
let l:icons_pattern = join(values(l:icons), '\|')
if !empty(l:icons_pattern)
let l:direction = a:dir > 0 ? 'w' : 'bw'
return search(printf('\(%s\)', l:icons_pattern), l:direction)
endif
endfunction
function! s:git_cmd(cmd) abort
let l:actions = {
\ 'stage': '!git add',
\ 'reset': '!git reset',
\ 'discard': '!git checkout --'
\ }
let l:candidate = defx#get_candidate()
let l:path = get(l:candidate, 'action__path')
let l:word = get(l:candidate, 'word')
let l:is_dir = get(l:candidate, 'is_directory')
if empty(l:path)
return
endif
let l:cmd = l:actions[a:cmd].' '.l:path
if a:cmd !=? 'discard'
call execute(l:cmd)
return defx#call_action('redraw')
endif
let l:choice = confirm('Are you sure you want to discard all changes to '.l:word.'? ', "&Yes\n&No")
if l:choice !=? 1
return
endif
let l:status = system('git status --porcelain '.l:path)
" File must be unstaged before discarding
if !empty(l:status[0])
call execute(l:actions['reset'].' '.l:path)
endif
call execute(l:cmd)
return defx#call_action('redraw')
endfunction
nnoremap <buffer><silent><Plug>(defx-git-next) :<C-u>call <sid>search(1)<CR>
nnoremap <buffer><silent><Plug>(defx-git-prev) :<C-u>call <sid>search(-1)<CR>
nnoremap <buffer><silent><Plug>(defx-git-stage) :<C-u>call <sid>git_cmd('stage')<CR>
nnoremap <buffer><silent><Plug>(defx-git-reset) :<C-u>call <sid>git_cmd('reset')<CR>
nnoremap <buffer><silent><Plug>(defx-git-discard) :<C-u>call <sid>git_cmd('discard')<CR>
if !hasmapto('<Plug>(defx-git-prev)') && maparg('[c', 'n') ==? ''
silent! nmap <buffer><unique><silent> [c <Plug>(defx-git-prev)
endif
if !hasmapto('<Plug>(defx-git-next)') && maparg(']c', 'n') ==? ''
silent! nmap <buffer><unique><silent> ]c <Plug>(defx-git-next)
endif
if !hasmapto('<Plug>(defx-git-stage)') && maparg(']a', 'n') ==? ''
silent! nmap <buffer><unique><silent> ]a <Plug>(defx-git-stage)
endif
if !hasmapto('<Plug>(defx-git-reset)') && maparg(']r', 'n') ==? ''
silent! nmap <buffer><unique><silent> ]r <Plug>(defx-git-reset)
endif
if !hasmapto('<Plug>(defx-git-discard)') && maparg(']d', 'n') ==? ''
silent! nmap <buffer><unique><silent> ]d <Plug>(defx-git-discard)
endif