Add the p mapping to preview a change.

This commit is contained in:
Steve Losh 2010-11-09 21:13:21 -05:00
parent 5cd18132a7
commit 09fcef2178
3 changed files with 73 additions and 3 deletions

View File

@ -96,6 +96,10 @@ of the change that state made.
Pressing enter on a state (or double clicking on it) will revert the contents Pressing enter on a state (or double clicking on it) will revert the contents
of the file to match that state. of the file to match that state.
You can use p on a state to make the preview window show the diff between
your current state and the selected state, instead of a preview of what the
selected state changed.
Pressing P while on a state will initiate "play to" mode targeted at that Pressing P while on a state will initiate "play to" mode targeted at that
state. This will replay all the changes between your current state and the state. This will replay all the changes between your current state and the
target, with a slight pause after each change. It's mostly useless, but can be target, with a slight pause after each change. It's mostly useless, but can be
@ -192,6 +196,8 @@ Development version
* Fix movement commands with counts in the graph. * Fix movement commands with counts in the graph.
* Make GundoToggle close the Gundo windows if they're visible but not the * Make GundoToggle close the Gundo windows if they're visible but not the
current window, instead of moving to them. current window, instead of moving to them.
* Add the 'p' mapping to preview the result of reverting to the selected
state.
v1.0.0 v1.0.0
* Initial stable release. * Initial stable release.

View File

@ -55,6 +55,8 @@ if !exists('g:gundo_right')"{{{
let g:gundo_right = 0 let g:gundo_right = 0
endif"}}} endif"}}}
let s:inline_help_length = 6
"}}} "}}}
"{{{ Mercurial's graphlog code "{{{ Mercurial's graphlog code
@ -427,7 +429,8 @@ def _undo_to(n):
INLINE_HELP = '''\ INLINE_HELP = '''\
" Gundo for %s [%d] " Gundo for %s [%d]
" j/k - move between undo states " j/k - move between undo states
" <cr> - revert to that state " p - preview diff of selected and current states
" <cr> - revert to selected state
''' '''
ENDPYTHON ENDPYTHON
@ -518,6 +521,7 @@ function! s:GundoMapGraph()"{{{
nnoremap <script> <silent> <buffer> k :call <sid>GundoMove(-1)<CR> nnoremap <script> <silent> <buffer> k :call <sid>GundoMove(-1)<CR>
nnoremap <script> <silent> <buffer> gg gg:call <sid>GundoMove(1)<CR> nnoremap <script> <silent> <buffer> gg gg:call <sid>GundoMove(1)<CR>
nnoremap <script> <silent> <buffer> P :call <sid>GundoPlayTo()<CR> nnoremap <script> <silent> <buffer> P :call <sid>GundoPlayTo()<CR>
nnoremap <script> <silent> <buffer> p :call <sid>GundoRenderChangePreview()<CR>
nnoremap <script> <silent> <buffer> q :call <sid>GundoClose()<CR> nnoremap <script> <silent> <buffer> q :call <sid>GundoClose()<CR>
cabbrev <script> <silent> <buffer> q call <sid>GundoClose() cabbrev <script> <silent> <buffer> q call <sid>GundoClose()
cabbrev <script> <silent> <buffer> quit call <sid>GundoClose() cabbrev <script> <silent> <buffer> quit call <sid>GundoClose()
@ -719,8 +723,8 @@ function! s:GundoMove(direction) range"{{{
let target_n = line('.') + (distance * a:direction) let target_n = line('.') + (distance * a:direction)
" Bound the movement to the graph. " Bound the movement to the graph.
if target_n <= 4 if target_n <= s:inline_help_length - 1
call cursor(5, 0) call cursor(s:inline_help_length, 0)
else else
call cursor(target_n, 0) call cursor(target_n, 0)
endif endif
@ -794,6 +798,26 @@ def _generate_preview_diff(current, node_before, node_after):
_undo_to(current) _undo_to(current)
return list(difflib.unified_diff(before_lines, after_lines,
before_name, after_name,
before_time, after_time))
def _generate_change_preview_diff(current, node_before, node_after):
_goto_window_for_buffer(vim.eval('g:gundo_target_n'))
_undo_to(node_before.n)
before_lines = vim.current.buffer[:]
_undo_to(node_after.n)
after_lines = vim.current.buffer[:]
before_name = node_before.n or 'Original'
before_time = node_before.time and _fmt_time(node_before.time) or ''
after_name = node_after.n or 'Original'
after_time = node_after.time and _fmt_time(node_after.time) or ''
_undo_to(current)
return list(difflib.unified_diff(before_lines, after_lines, return list(difflib.unified_diff(before_lines, after_lines,
before_name, after_name, before_name, after_name,
before_time, after_time)) before_time, after_time))
@ -880,6 +904,40 @@ GundoRenderPreview()
ENDPYTHON ENDPYTHON
endfunction"}}} endfunction"}}}
function! s:GundoRenderChangePreview()"{{{
python << ENDPYTHON
def GundoRenderChangePreview():
if not _check_sanity():
return
target_state = vim.eval('s:GundoGetTargetState()')
# Check that there's an undo state. There may not be if we're talking about
# a buffer with no changes yet.
if target_state == None:
_goto_window_for_buffer_name('__Gundo__')
return
else:
target_state = int(target_state)
_goto_window_for_buffer(vim.eval('g:gundo_target_n'))
nodes, nmap = make_nodes()
current = changenr(nodes)
node_after = nmap[target_state]
node_before = nmap[current]
print node_after, node_before
vim.command('call s:GundoOpenPreview()')
_output_preview_text(_generate_change_preview_diff(current, node_before, node_after))
_goto_window_for_buffer_name('__Gundo__')
GundoRenderChangePreview()
ENDPYTHON
endfunction"}}}
"}}} "}}}
"{{{ Gundo undo/redo "{{{ Gundo undo/redo

View File

@ -202,6 +202,12 @@
of the file to match that state. of the file to match that state.
</p> </p>
<p>
You can use p on a state to make the preview window show the diff between
your current state and the selected state, instead of a preview of what the
selected state changed.
</p>
<p> <p>
Pressing <code>P</code> while on a state will initiate "play to" mode targeted at that Pressing <code>P</code> while on a state will initiate "play to" mode targeted at that
state. This will replay all the changes between your current state and the state. This will replay all the changes between your current state and the