Improve PlugDiff: 'X' key to revert the update

This commit is contained in:
Junegunn Choi 2014-08-10 16:32:46 +09:00
parent 6272f5e289
commit f7ebba7b9e
3 changed files with 76 additions and 29 deletions

View File

@ -96,6 +96,10 @@ Reload .vimrc and `:PlugInstall` to install plugins.
- `S` - `PlugStatus` - `S` - `PlugStatus`
- `R` - Retry failed update or installation tasks - `R` - Retry failed update or installation tasks
- `q` - Close the window - `q` - Close the window
- `:PlugStatus`
- `L` - Load plugin
- `:PlugDiff`
- `X` - Revert the update
### Example: A small [sensible](https://github.com/tpope/vim-sensible) Vim configuration ### Example: A small [sensible](https://github.com/tpope/vim-sensible) Vim configuration

View File

@ -69,7 +69,7 @@ let s:cpo_save = &cpo
set cpo&vim set cpo&vim
let s:plug_source = 'https://raw.github.com/junegunn/vim-plug/master/plug.vim' let s:plug_source = 'https://raw.github.com/junegunn/vim-plug/master/plug.vim'
let s:plug_buf = -1 let s:plug_buf = get(s:, 'plug_buf', -1)
let s:mac_gui = has('gui_macvim') && has('gui_running') let s:mac_gui = has('gui_macvim') && has('gui_running')
let s:is_win = has('win32') || has('win64') let s:is_win = has('win32') || has('win64')
let s:me = expand('<sfile>:p') let s:me = expand('<sfile>:p')
@ -468,6 +468,7 @@ function! s:prepare()
else else
execute winnr . 'wincmd w' execute winnr . 'wincmd w'
endif endif
setlocal modifiable
silent %d _ silent %d _
else else
vertical topleft new vertical topleft new
@ -483,7 +484,8 @@ function! s:prepare()
endif endif
silent! unmap <buffer> <cr> silent! unmap <buffer> <cr>
silent! unmap <buffer> L silent! unmap <buffer> L
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap cursorline silent! unmap <buffer> X
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap cursorline modifiable
setf vim-plug setf vim-plug
call s:syntax() call s:syntax()
endfunction endfunction
@ -1112,6 +1114,7 @@ function! s:status()
endfor endfor
call setline(1, 'Finished. '.ecnt.' error(s).') call setline(1, 'Finished. '.ecnt.' error(s).')
normal! gg normal! gg
setlocal nomodifiable
if unloaded if unloaded
echo "Press 'L' on each line to load plugin" echo "Press 'L' on each line to load plugin"
nnoremap <silent> <buffer> L :call <SID>status_load(line('.'))<cr> nnoremap <silent> <buffer> L :call <SID>status_load(line('.'))<cr>
@ -1125,7 +1128,9 @@ function! s:status_load(lnum)
if !empty(matches) if !empty(matches)
let name = matches[1] let name = matches[1]
call plug#load(name) call plug#load(name)
setlocal modifiable
call setline(a:lnum, substitute(line, ' (not loaded)$', '', '')) call setline(a:lnum, substitute(line, ' (not loaded)$', '', ''))
setlocal nomodifiable
endif endif
endfunction endfunction
@ -1138,22 +1143,36 @@ function! s:is_preview_window_open()
return 0 return 0
endfunction endfunction
function! s:find_name(lnum)
for lnum in reverse(range(1, a:lnum))
let line = getline(lnum)
if empty(line)
return ''
endif
let name = matchstr(line, '\(^- \)\@<=[^:]\+')
if !empty(name)
return name
endif
endfor
return ''
endfunction
function! s:preview_commit() function! s:preview_commit()
if b:plug_preview < 0 if b:plug_preview < 0
let b:plug_preview = !s:is_preview_window_open() let b:plug_preview = !s:is_preview_window_open()
endif endif
let sha = matchstr(getline('.'), '\(^ \)\@<=[0-9a-z]\{7}') let sha = matchstr(getline('.'), '\(^ \)\@<=[0-9a-z]\{7}')
if !empty(sha) if empty(sha)
let lnum = line('.') return
while lnum > 1 endif
let lnum -= 1
let line = getline(lnum) let name = s:find_name(line('.'))
let name = matchstr(line, '\(^- \)\@<=[^:]\+') if empty(name) || !has_key(g:plugs, name) || !isdirectory(g:plugs[name].dir)
if !empty(name) return
let dir = g:plugs[name].dir endif
if isdirectory(dir)
execute 'cd '.s:esc(dir) execute 'cd '.s:esc(g:plugs[name].dir)
execute 'pedit '.sha execute 'pedit '.sha
wincmd P wincmd P
setlocal filetype=git buftype=nofile nobuflisted setlocal filetype=git buftype=nofile nobuflisted
@ -1161,11 +1180,6 @@ function! s:preview_commit()
normal! ggdd normal! ggdd
wincmd p wincmd p
cd - cd -
endif
break
endif
endwhile
endif
endfunction endfunction
function! s:section(flags) function! s:section(flags)
@ -1199,7 +1213,28 @@ function! s:diff()
call setline(1, cnt == 0 ? 'No updates.' : 'Last update:') call setline(1, cnt == 0 ? 'No updates.' : 'Last update:')
nnoremap <silent> <buffer> <cr> :silent! call <SID>preview_commit()<cr> nnoremap <silent> <buffer> <cr> :silent! call <SID>preview_commit()<cr>
nnoremap <silent> <buffer> X :call <SID>revert()<cr>
normal! gg normal! gg
setlocal nomodifiable
if cnt > 0
echo "Press 'X' on each block to revert the update"
endif
endfunction
function! s:revert()
let name = s:find_name(line('.'))
if empty(name) || !has_key(g:plugs, name) ||
\ input(printf('Revert the update of %s? (Y/N) ', name)) !~? '^y'
return
endif
execute 'cd '.s:esc(g:plugs[name].dir)
call system('git reset --hard HEAD@{1} && git checkout '.s:esc(g:plugs[name].branch))
cd -
setlocal modifiable
normal! dap
setlocal nomodifiable
echo 'Reverted.'
endfunction endfunction
let s:first_rtp = s:esc(get(split(&rtp, ','), 0, '')) let s:first_rtp = s:esc(get(split(&rtp, ','), 0, ''))

View File

@ -307,6 +307,13 @@ Execute (Rollback recent updates, PlugUpdate, then PlugDiff):
AssertEqual lnum, line('.') AssertEqual lnum, line('.')
AssertEqual 3, col('.') AssertEqual 3, col('.')
" X key to revert the update
AssertExpect '^- ', 2
execute "normal Xn\<cr>"
AssertExpect '^- ', 2
execute "normal Xy\<cr>"
AssertExpect '^- ', 1
" q will close preview window as well " q will close preview window as well
normal q normal q
@ -317,6 +324,7 @@ Execute (Rollback recent updates, PlugUpdate, then PlugDiff):
" q should not close preview window if it's already open " q should not close preview window if it's already open
pedit pedit
PlugDiff PlugDiff
AssertExpect '^- ', 1
execute "normal ]]j\<cr>" execute "normal ]]j\<cr>"
normal q normal q
@ -330,8 +338,8 @@ Execute (Plug window in a new tab):
set buftype=nofile set buftype=nofile
PlugUpdate PlugUpdate
normal D normal D
AssertEqual 'No updates.', getline(1) AssertExpect '^- ', 1
q normal q
AssertEqual 'new-tab', expand('%') AssertEqual 'new-tab', expand('%')
q q
q q
@ -798,12 +806,12 @@ Execute (PlugStatus reports (not loaded)):
q q
Execute (plug#load to load it): Execute (plug#load to load it):
setf xxx tabnew test.rs
f test.rs " Vader will switch tab to [Vader-workbench] after Log
Log &filetype " Log &filetype
AssertEqual 1, plug#load('rust.vim') AssertEqual 1, plug#load('rust.vim')
AssertEqual 'rust', &filetype AssertEqual 'rust', &filetype
q
Execute (PlugStatus should not contain (not loaded)): Execute (PlugStatus should not contain (not loaded)):
PlugStatus PlugStatus