templater: add hook point to populate additional mapping items
authorYuya Nishihara <yuya@tcha.org>
Thu, 15 Mar 2018 21:49:33 +0900
changeset 37102 638a241202a3
parent 37101 656ac240f392
child 37103 be3f33f5e232
templater: add hook point to populate additional mapping items The 'revcache' dict will be inserted by this hook.
mercurial/formatter.py
mercurial/templater.py
--- a/mercurial/formatter.py	Sat Mar 24 01:30:50 2018 -0400
+++ b/mercurial/formatter.py	Thu Mar 15 21:49:33 2018 +0900
@@ -517,6 +517,9 @@
             return None
         return get(self, context, mapping, key)
 
+    def populatemap(self, context, origmapping, newmapping):
+        return {}
+
     def _getsome(self, context, mapping, key):
         v = mapping.get(key)
         if v is not None:
--- a/mercurial/templater.py	Sat Mar 24 01:30:50 2018 -0400
+++ b/mercurial/templater.py	Thu Mar 15 21:49:33 2018 +0900
@@ -577,6 +577,11 @@
     def lookup(self, context, mapping, key):
         """Return a resource for the key if available; otherwise None"""
 
+    @abc.abstractmethod
+    def populatemap(self, context, origmapping, newmapping):
+        """Return a dict of additional mapping items which should be paired
+        with the given new mapping"""
+
 class nullresourcemapper(resourcemapper):
     def availablekeys(self, context, mapping):
         return set()
@@ -587,6 +592,9 @@
     def lookup(self, context, mapping, key):
         return None
 
+    def populatemap(self, context, origmapping, newmapping):
+        return {}
+
 class engine(object):
     '''template expansion engine.
 
@@ -634,6 +642,8 @@
                    if (k in knownres  # not a symbol per self.symbol()
                        or newres.isdisjoint(self._defaultrequires(k)))}
         mapping.update(newmapping)
+        mapping.update(
+            self._resources.populatemap(self, origmapping, newmapping))
         return mapping
 
     def _defaultrequires(self, key):
@@ -689,6 +699,13 @@
         mapping contains added elements for use during expansion. Is a
         generator.'''
         func, data = self._load(t)
+        # populate additional items only if they don't exist in the given
+        # mapping. this is slightly different from overlaymap() because the
+        # initial 'revcache' may contain pre-computed items.
+        extramapping = self._resources.populatemap(self, {}, mapping)
+        if extramapping:
+            extramapping.update(mapping)
+            mapping = extramapping
         return _flatten(func(self, mapping, data))
 
 engines = {'default': engine}