show: construct changeset templater during dispatch
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 24 Jun 2017 12:47:25 -0700
changeset 33046 11f768258dcc
parent 33045 99c6c9fa9e6d
child 33047 de8e3681c402
show: construct changeset templater during dispatch Previously, we constructed a formatter from a specific template topic. Then from show() we reached into the internals of the formatter to resolve a template string to be used to construct a changeset templater. A downside to this approach was it limited us to having the entire template defined in a single entry in the map file. You couldn't reference other entries in the map file and this would lead to long templates and redundancy in the map file. This commit teaches @showview how to instantiate a changeset templater so we can construct a templater with full access to the map file. To prove it works, we've split "showwork" into components.
hgext/show.py
mercurial/templates/map-cmdline.show
--- a/hgext/show.py	Sat Jun 24 11:47:26 2017 -0700
+++ b/hgext/show.py	Sat Jun 24 12:47:25 2017 -0700
@@ -43,7 +43,7 @@
     # Used by _formatdoc().
     _docformat = '%s -- %s'
 
-    def _extrasetup(self, name, func, fmtopic=None):
+    def _extrasetup(self, name, func, fmtopic=None, csettopic=None):
         """Called with decorator arguments to register a show view.
 
         ``name`` is the sub-command name.
@@ -52,8 +52,16 @@
 
         ``fmtopic`` is the topic in the style that will be rendered for
         this view.
+
+        ``csettopic`` is the topic in the style to be used for a changeset
+        printer.
+
+        If ``fmtopic`` is specified, the view function will receive a
+        formatter instance. If ``csettopic`` is specified, the view
+        function will receive a changeset printer.
         """
         func._fmtopic = fmtopic
+        func._csettopic = csettopic
 
 showview = showcmdfunc()
 
@@ -109,11 +117,21 @@
                           hint=_('run "hg show" to see available views'))
 
     template = template or 'show'
-    fmtopic = 'show%s' % views[view]._fmtopic
 
+    fn = views[view]
     ui.pager('show')
-    with ui.formatter(fmtopic, {'template': template}) as fm:
-        return views[view](ui, repo, fm)
+
+    if fn._fmtopic:
+        fmtopic = 'show%s' % fn._fmtopic
+        with ui.formatter(fmtopic, {'template': template}) as fm:
+            return fn(ui, repo, fm)
+    elif fn._csettopic:
+        ref = 'show%s' % fn._csettopic
+        spec = formatter.lookuptemplate(ui, ref, template)
+        displayer = cmdutil.changeset_templater(ui, repo, spec, buffered=True)
+        return fn(ui, repo, displayer)
+    else:
+        return fn(ui, repo)
 
 @showview('bookmarks', fmtopic='bookmarks')
 def showbookmarks(ui, repo, fm):
@@ -189,15 +207,13 @@
 
     return subset & relevant
 
-@showview('work', fmtopic='work')
-def showwork(ui, repo, fm):
+@showview('work', csettopic='work')
+def showwork(ui, repo, displayer):
     """changesets that aren't finished"""
     # TODO support date-based limiting when calling revset.
     revs = repo.revs('sort(_underway(), topo)')
 
     revdag = graphmod.dagwalker(repo, revs)
-    tmpl = fm._t.load(fm._topic)
-    displayer = cmdutil.makelogtemplater(ui, repo, tmpl, buffered=True)
 
     ui.setconfig('experimental', 'graphshorten', True)
     cmdutil.displaygraph(ui, repo, revdag, displayer, graphmod.asciiedges)
--- a/mercurial/templates/map-cmdline.show	Sat Jun 24 11:47:26 2017 -0700
+++ b/mercurial/templates/map-cmdline.show	Sat Jun 24 12:47:25 2017 -0700
@@ -1,9 +1,12 @@
 # TODO there are a few deficiencies in this file:
-# * Due to the way the file is loaded, references to other entities in the
-#   template doesn't work. That requires us to inline.
 # * The "namespace" of the labels needs to be worked out. We currently
 #   piggyback on existing values so color works.
 # * Obsolescence isn't considered for node labels. See _cset_labels in
 #   map-cmdline.default.
 showbookmarks = '{if(active, "*", " ")} {pad(bookmark, longestbookmarklen + 4)}{shortest(node, 5)}\n'
-showwork = '{label("log.changeset changeset.{phase}", shortest(node, 5))}{if(branches, " ({label("log.branch", branch)})")}{if(bookmarks, " ({label("log.bookmarks", bookmarks)})")} {label("log.description", desc|firstline)}'
+
+showwork = '{cset_shortnode}{cset_names} {cset_shortdesc}'
+
+cset_shortnode = '{label("log.changeset changeset.{phase}", shortest(node, 5))}'
+cset_names = '{if(branches, " ({label("log.branch", branch)})")}{if(bookmarks, " ({label("log.bookmarks", bookmarks)})")}'
+cset_shortdesc = '{label("log.description", desc|firstline)}'