328f098fb33e

Refactor changenr()
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sat, 09 Oct 2010 11:13:06 -0400
parents 725085a34b62
children 717091909bd6
branches/tags (none)
files plugin/gundo.vim

Changes

--- a/plugin/gundo.vim	Sat Oct 09 11:09:26 2010 -0400
+++ b/plugin/gundo.vim	Sat Oct 09 11:13:06 2010 -0400
@@ -449,8 +449,10 @@
 ENDPYTHON
 "}}}
 
-"{{{ Mercurial utility functions
+"{{{ Mercurial age function
 python << ENDPYTHON
+import time
+
 agescales = [("year", 3600 * 24 * 365),
              ("month", 3600 * 24 * 30),
              ("week", 3600 * 24 * 7),
@@ -498,12 +500,13 @@
 def _goto_window_for_buffer_name(bn):
     b = vim.eval('bufnr("%s")' % bn)
     _goto_window_for_buffer(b)
-
 ENDPYTHON
 "}}}
 
 "{{{ Python undo tree data structures and functions
 python << ENDPYTHON
+import itertools
+
 class Buffer(object):
     def __init__(self):
         self.b = ''
@@ -536,13 +539,19 @@
     _make_nodes(entries, nodes, root)
     return (root, nodes)
 
+def changenr(nodes):
+    _curhead_l = list(itertools.dropwhile(lambda n: not n.curhead, nodes))
+    if _curhead_l:
+        current = _curhead_l[0].parent.n
+    else:
+        current = int(vim.eval('changenr()'))
+    return current
 ENDPYTHON
 "}}}
 
 "{{{ Graph rendering
 function! s:GundoRender()
 python << ENDPYTHON
-import itertools, time
 
 ut = vim.eval('undotree()')
 entries = ut['entries']
@@ -559,12 +568,7 @@
         yield(node, [node.parent] if node.parent else [])
 
 dag = sorted(nodes, key=lambda n: int(n.n), reverse=True) + [root]
-
-_curhead_l = list(itertools.dropwhile(lambda n: not n.curhead, dag))
-if _curhead_l:
-    current = _curhead_l[0].parent.n
-else:
-    current = int(vim.eval('changenr()'))
+current = changenr(nodes)
 
 result = generate(walk_nodes(dag), asciiedges, current).splitlines()
 result = [' ' + l for l in result]
@@ -606,12 +610,7 @@
 _goto_window_for_buffer(vim.eval('g:gundo_target_n'))
 
 root, nodes = make_nodes(entries)
-
-_curhead_l = list(itertools.dropwhile(lambda n: not n.curhead, nodes))
-if _curhead_l:
-    current = _curhead_l[0].parent.n
-else:
-    current = int(vim.eval('changenr()'))
+current = changenr(nodes)
 
 print current
 ENDPYTHON