Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor the movement into a single function. Feels good man.

  • Loading branch information...
commit 5c1bc7dfe9db98989add759013eba5f63700e3c3 1 parent d64027b
Steve Losh authored

Showing 1 changed file with 16 additions and 27 deletions. Show diff stats Hide diff stats

  1. +16 27 plugin/gundo.vim
43 plugin/gundo.vim
@@ -22,38 +22,23 @@ endif
22 22 "}}}
23 23
24 24 "{{{ Movement Mappings
25   -function! s:GundoMoveUp()
  25 +function! s:GundoMove(direction)
26 26 let start_line = getline('.')
27   - if line('.') - 2 <= 4
28   - call cursor(5, 0)
29   - elseif stridx(start_line, '[') == -1
30   - call cursor(line('.') - 1, 0)
31   - else
32   - call cursor(line('.') - 2, 0)
33   - endif
34 27
35   - let line = getline('.')
36   - let idx1 = stridx(line, '@')
37   - let idx2 = stridx(line, 'o')
38   - if idx1 != -1
39   - call cursor(0, idx1 + 1)
  28 + if stridx(start_line, '[') == -1
  29 + let distance = 1
40 30 else
41   - call cursor(0, idx2 + 1)
  31 + let distance = 2
42 32 endif
43 33
44   - let target_line = matchstr(getline("."), '\v\[[0-9]+\]')
45   - let target_num = matchstr(target_line, '\v[0-9]+')
46   - call s:GundoRenderPreview(target_num)
47   -endfunction
  34 + let target_n = line('.') + (distance * a:direction)
48 35
49   -function! s:GundoMoveDown()
50   - let start_line = getline('.')
51   - if line('.') + 2 >= line('$')
  36 + if target_n <= 4
  37 + call cursor(5, 0)
  38 + elseif target_n >= line('$')
52 39 call cursor(line('$') - 1, 0)
53   - elseif stridx(start_line, '[') == -1
54   - call cursor(line('.') + 1, 0)
55 40 else
56   - call cursor(line('.') + 2, 0)
  41 + call cursor(target_n, 0)
57 42 endif
58 43
59 44 let line = getline('.')
@@ -88,8 +73,10 @@ function! s:GundoOpenBuffer()
88 73 wincmd H
89 74 call s:GundoResizeBuffers(winnr())
90 75 nnoremap <script> <silent> <buffer> <CR> :call <sid>GundoRevert()<CR>
91   - nnoremap <script> <silent> <buffer> j :call <sid>GundoMoveDown()<CR>
92   - nnoremap <script> <silent> <buffer> k :call <sid>GundoMoveUp()<CR>
  76 + nnoremap <script> <silent> <buffer> j :call <sid>GundoMove(1)<CR>
  77 + nnoremap <script> <silent> <buffer> k :call <sid>GundoMove(-1)<CR>
  78 + nnoremap <script> <silent> <buffer> gg gg:call <sid>GundoMove(1)<CR>
  79 + nnoremap <script> <silent> <buffer> G G:call <sid>GundoMove(-1)<CR>
93 80 else
94 81 let existing_gundo_window = bufwinnr(existing_gundo_buffer)
95 82
@@ -608,7 +595,6 @@ def GundoRender():
608 595
609 596 vim.command('GundoOpenBuffer')
610 597 vim.command('setlocal modifiable')
611   - vim.command('normal ggdG')
612 598 vim.current.buffer[:] = (header + result)
613 599 vim.command('setlocal nomodifiable')
614 600
@@ -636,6 +622,9 @@ import difflib
636 622 def GundoRenderPreview():
637 623 _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
638 624
  625 + ut = vim.eval('undotree()')
  626 + entries = ut['entries']
  627 +
639 628 root, nodes = make_nodes(entries)
640 629 current = changenr(nodes)
641 630

0 comments on commit 5c1bc7d

Please sign in to comment.
Something went wrong with that request. Please try again.