Patchwork [4,of,5] parser: extract closure of prettyformat() to a top-level function

login
register
mail settings
Submitter Yuya Nishihara
Date May 22, 2015, 2:11 p.m.
Message ID <e8998439901c3ed2ffe9.1432303907@mimosa>
Download mbox | patch
Permalink /patch/9238/
State Accepted
Headers show

Comments

Yuya Nishihara - May 22, 2015, 2:11 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1430875061 -32400
#      Wed May 06 10:17:41 2015 +0900
# Node ID e8998439901c3ed2ffe9cf22022f1f1f0d984be9
# Parent  a903a8ec32f1191b94077de850384f2eb8771f07
parser: extract closure of prettyformat() to a top-level function

There was no capture until I added 'leafnodes' argument.

Patch

diff --git a/mercurial/parser.py b/mercurial/parser.py
--- a/mercurial/parser.py
+++ b/mercurial/parser.py
@@ -94,17 +94,17 @@  class parser(object):
             return self.eval(t)
         return t
 
+def _prettyformat(tree, leafnodes, level, lines):
+    if not isinstance(tree, tuple) or tree[0] in leafnodes:
+        lines.append((level, str(tree)))
+    else:
+        lines.append((level, '(%s' % tree[0]))
+        for s in tree[1:]:
+            _prettyformat(s, leafnodes, level + 1, lines)
+        lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')]
+
 def prettyformat(tree, leafnodes):
-    def _prettyformat(tree, level, lines):
-        if not isinstance(tree, tuple) or tree[0] in leafnodes:
-            lines.append((level, str(tree)))
-        else:
-            lines.append((level, '(%s' % tree[0]))
-            for s in tree[1:]:
-                _prettyformat(s, level + 1, lines)
-            lines[-1:] = [(lines[-1][0], lines[-1][1] + ')')]
-
     lines = []
-    _prettyformat(tree, 0, lines)
+    _prettyformat(tree, leafnodes, 0, lines)
     output = '\n'.join(('  ' * l + s) for l, s in lines)
     return output