Commits

Pierre-Yves David committed bd05022

obsolete: add and use templatekw decorator

Comments (0)

Files changed (1)

hgext/obsolete.py

         self._extcallables = []
         self._repocallables = []
         self._revsetsymbols = []
+        self._templatekws = []
         self._commandwrappers = []
         self._extcommandwrappers = []
         self._functionwrappers = []
         knownexts = {}
         for name, symbol in self._revsetsymbols:
             revset.symbols[name] = symbol
+        for name, kw in self._templatekws:
+            templatekw.keywords[name] = kw
         for ext, command, wrapper in self._extcommandwrappers:
             if ext not in knownexts:
                 e = extensions.find('rebase')
             return symbol
         return dec
 
+
+    def templatekw(self, keywordname):
+        """Decorated function is a revset keyword
+
+        The name of the keyword must be given as the decorator argument.
+        The symbol is added during `extsetup`.
+
+        example::
+
+            @eh.templatekw('babar')
+            def kwbabar(ctx):
+                return 'babar'
+        """
+        def dec(keyword):
+            self._templatekws.append((keywordname, keyword))
+            return keyword
+        return dec
+
     def wrapcommand(self, command, extension=None):
         """Decorated function is a command wrapper
 
 ### template keywords
 #####################
 
+@eh.templatekw('obsolete')
 def obsoletekw(repo, ctx, templ, **args):
     """:obsolete: String. The obsolescence level of the node, could be
     ``stable``, ``unstable``, ``suspended`` or ``extinct``.
         return 'unstable'
     return 'stable'
 
-@eh.extsetup
-def addkeyword(ui):
-    templatekw.keywords['obsolete'] = obsoletekw
-
 ### Other Extension compat
 ############################