Patchwork [4,of,9] templatekw: add default implementation of _hybrid.gen

login
register
mail settings
Submitter Yuya Nishihara
Date April 12, 2017, 3:53 p.m.
Message ID <e7257a6a6fd5cc5565fb.1492012400@mimosa>
Download mbox | patch
Permalink /patch/20135/
State Accepted
Headers show

Comments

Yuya Nishihara - April 12, 2017, 3:53 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1491999047 -32400
#      Wed Apr 12 21:10:47 2017 +0900
# Node ID e7257a6a6fd5cc5565fb7e4bfe0b0a8a780c1285
# Parent  a773ef212277598db714833be5c21297021a26ab
templatekw: add default implementation of _hybrid.gen

This is convenient for new template keyword, which doesn't need to support
the legacy list hack (provided by _showlist()), but still wants to have
a string representation.

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -32,10 +32,20 @@  class _hybrid(object):
     """
 
     def __init__(self, gen, values, makemap, joinfmt):
-        self.gen = gen
+        if gen is not None:
+            self.gen = gen
         self._values = values
         self._makemap = makemap
         self.joinfmt = joinfmt
+    @util.propertycache
+    def gen(self):
+        return self._defaultgen()
+    def _defaultgen(self):
+        """Generator to stringify this as {join(self, ' ')}"""
+        for i, d in enumerate(self.itermaps()):
+            if i > 0:
+                yield ' '
+            yield self.joinfmt(d)
     def itermaps(self):
         makemap = self._makemap
         for x in self._values: