Skip to content

Commit f7ebba7

Browse files
committed
Improve PlugDiff: 'X' key to revert the update
1 parent 6272f5e commit f7ebba7

File tree

3 files changed

+76
-29
lines changed

3 files changed

+76
-29
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Reload .vimrc and `:PlugInstall` to install plugins.
9696
- `S` - `PlugStatus`
9797
- `R` - Retry failed update or installation tasks
9898
- `q` - Close the window
99+
- `:PlugStatus`
100+
- `L` - Load plugin
101+
- `:PlugDiff`
102+
- `X` - Revert the update
99103

100104
### Example: A small [sensible](https://github.com/tpope/vim-sensible) Vim configuration
101105

plug.vim

+58-23
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ let s:cpo_save = &cpo
6969
set cpo&vim
7070

7171
let s:plug_source = 'https://raw.github.com/junegunn/vim-plug/master/plug.vim'
72-
let s:plug_buf = -1
72+
let s:plug_buf = get(s:, 'plug_buf', -1)
7373
let s:mac_gui = has('gui_macvim') && has('gui_running')
7474
let s:is_win = has('win32') || has('win64')
7575
let s:me = expand('<sfile>:p')
@@ -468,6 +468,7 @@ function! s:prepare()
468468
else
469469
execute winnr . 'wincmd w'
470470
endif
471+
setlocal modifiable
471472
silent %d _
472473
else
473474
vertical topleft new
@@ -483,7 +484,8 @@ function! s:prepare()
483484
endif
484485
silent! unmap <buffer> <cr>
485486
silent! unmap <buffer> L
486-
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap cursorline
487+
silent! unmap <buffer> X
488+
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap cursorline modifiable
487489
setf vim-plug
488490
call s:syntax()
489491
endfunction
@@ -1112,6 +1114,7 @@ function! s:status()
11121114
endfor
11131115
call setline(1, 'Finished. '.ecnt.' error(s).')
11141116
normal! gg
1117+
setlocal nomodifiable
11151118
if unloaded
11161119
echo "Press 'L' on each line to load plugin"
11171120
nnoremap <silent> <buffer> L :call <SID>status_load(line('.'))<cr>
@@ -1125,7 +1128,9 @@ function! s:status_load(lnum)
11251128
if !empty(matches)
11261129
let name = matches[1]
11271130
call plug#load(name)
1131+
setlocal modifiable
11281132
call setline(a:lnum, substitute(line, ' (not loaded)$', '', ''))
1133+
setlocal nomodifiable
11291134
endif
11301135
endfunction
11311136

@@ -1138,34 +1143,43 @@ function! s:is_preview_window_open()
11381143
return 0
11391144
endfunction
11401145

1146+
function! s:find_name(lnum)
1147+
for lnum in reverse(range(1, a:lnum))
1148+
let line = getline(lnum)
1149+
if empty(line)
1150+
return ''
1151+
endif
1152+
let name = matchstr(line, '\(^- \)\@<=[^:]\+')
1153+
if !empty(name)
1154+
return name
1155+
endif
1156+
endfor
1157+
return ''
1158+
endfunction
1159+
11411160
function! s:preview_commit()
11421161
if b:plug_preview < 0
11431162
let b:plug_preview = !s:is_preview_window_open()
11441163
endif
11451164

11461165
let sha = matchstr(getline('.'), '\(^ \)\@<=[0-9a-z]\{7}')
1147-
if !empty(sha)
1148-
let lnum = line('.')
1149-
while lnum > 1
1150-
let lnum -= 1
1151-
let line = getline(lnum)
1152-
let name = matchstr(line, '\(^- \)\@<=[^:]\+')
1153-
if !empty(name)
1154-
let dir = g:plugs[name].dir
1155-
if isdirectory(dir)
1156-
execute 'cd '.s:esc(dir)
1157-
execute 'pedit '.sha
1158-
wincmd P
1159-
setlocal filetype=git buftype=nofile nobuflisted
1160-
execute 'silent read !git show '.sha
1161-
normal! ggdd
1162-
wincmd p
1163-
cd -
1164-
endif
1165-
break
1166-
endif
1167-
endwhile
1166+
if empty(sha)
1167+
return
11681168
endif
1169+
1170+
let name = s:find_name(line('.'))
1171+
if empty(name) || !has_key(g:plugs, name) || !isdirectory(g:plugs[name].dir)
1172+
return
1173+
endif
1174+
1175+
execute 'cd '.s:esc(g:plugs[name].dir)
1176+
execute 'pedit '.sha
1177+
wincmd P
1178+
setlocal filetype=git buftype=nofile nobuflisted
1179+
execute 'silent read !git show '.sha
1180+
normal! ggdd
1181+
wincmd p
1182+
cd -
11691183
endfunction
11701184

11711185
function! s:section(flags)
@@ -1199,7 +1213,28 @@ function! s:diff()
11991213

12001214
call setline(1, cnt == 0 ? 'No updates.' : 'Last update:')
12011215
nnoremap <silent> <buffer> <cr> :silent! call <SID>preview_commit()<cr>
1216+
nnoremap <silent> <buffer> X :call <SID>revert()<cr>
12021217
normal! gg
1218+
setlocal nomodifiable
1219+
if cnt > 0
1220+
echo "Press 'X' on each block to revert the update"
1221+
endif
1222+
endfunction
1223+
1224+
function! s:revert()
1225+
let name = s:find_name(line('.'))
1226+
if empty(name) || !has_key(g:plugs, name) ||
1227+
\ input(printf('Revert the update of %s? (Y/N) ', name)) !~? '^y'
1228+
return
1229+
endif
1230+
1231+
execute 'cd '.s:esc(g:plugs[name].dir)
1232+
call system('git reset --hard HEAD@{1} && git checkout '.s:esc(g:plugs[name].branch))
1233+
cd -
1234+
setlocal modifiable
1235+
normal! dap
1236+
setlocal nomodifiable
1237+
echo 'Reverted.'
12031238
endfunction
12041239

12051240
let s:first_rtp = s:esc(get(split(&rtp, ','), 0, ''))

test/workflow.vader

+14-6
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ Execute (Rollback recent updates, PlugUpdate, then PlugDiff):
307307
AssertEqual lnum, line('.')
308308
AssertEqual 3, col('.')
309309

310+
" X key to revert the update
311+
AssertExpect '^- ', 2
312+
execute "normal Xn\<cr>"
313+
AssertExpect '^- ', 2
314+
execute "normal Xy\<cr>"
315+
AssertExpect '^- ', 1
316+
310317
" q will close preview window as well
311318
normal q
312319

@@ -317,6 +324,7 @@ Execute (Rollback recent updates, PlugUpdate, then PlugDiff):
317324
" q should not close preview window if it's already open
318325
pedit
319326
PlugDiff
327+
AssertExpect '^- ', 1
320328
execute "normal ]]j\<cr>"
321329
normal q
322330

@@ -330,8 +338,8 @@ Execute (Plug window in a new tab):
330338
set buftype=nofile
331339
PlugUpdate
332340
normal D
333-
AssertEqual 'No updates.', getline(1)
334-
q
341+
AssertExpect '^- ', 1
342+
normal q
335343
AssertEqual 'new-tab', expand('%')
336344
q
337345
q
@@ -798,12 +806,12 @@ Execute (PlugStatus reports (not loaded)):
798806
q
799807

800808
Execute (plug#load to load it):
801-
setf xxx
802-
f test.rs
803-
Log &filetype
804-
809+
tabnew test.rs
810+
" Vader will switch tab to [Vader-workbench] after Log
811+
" Log &filetype
805812
AssertEqual 1, plug#load('rust.vim')
806813
AssertEqual 'rust', &filetype
814+
q
807815

808816
Execute (PlugStatus should not contain (not loaded)):
809817
PlugStatus

0 commit comments

Comments
 (0)