Patchwork [1,of,2,resend] templater: add inheritance support to style maps

login
register
mail settings
Submitter Matt Mackall
Date Aug. 18, 2016, 6:15 p.m.
Message ID <1f402560299be8692c76.1471544105@ruin.waste.org>
Download mbox | patch
Permalink /patch/16358/
State Accepted
Headers show

Comments

Matt Mackall - Aug. 18, 2016, 6:15 p.m.
# HG changeset patch
# User Matt Mackall <mpm@selenic.com>
# Date 1471459227 18000
#      Wed Aug 17 13:40:27 2016 -0500
# Node ID 1f402560299be8692c767db1469afb6618adee81
# Parent  b1809f5d7630a3fff0fa715bbd30dba0f07672a8
templater: add inheritance support to style maps

We can now specify a base map file:

__base__ = path/to/map/file

That map file will be read and used to populate unset elements of the
current map. Unlike using %include, elements in the inherited class
will be read relative to that path.

This makes it much easier to make custom local tweaks to a style.
Yuya Nishihara - Aug. 19, 2016, 2:37 p.m.
On Thu, 18 Aug 2016 13:15:05 -0500, Matt Mackall wrote:
> # HG changeset patch
> # User Matt Mackall <mpm@selenic.com>
> # Date 1471459227 18000
> #      Wed Aug 17 13:40:27 2016 -0500
> # Node ID 1f402560299be8692c767db1469afb6618adee81
> # Parent  b1809f5d7630a3fff0fa715bbd30dba0f07672a8
> templater: add inheritance support to style maps
> 
> We can now specify a base map file:
> 
> __base__ = path/to/map/file
> 
> That map file will be read and used to populate unset elements of the
> current map. Unlike using %include, elements in the inherited class
> will be read relative to that path.
> 
> This makes it much easier to make custom local tweaks to a style.

Queued the series, thanks. The existing behavior of %include is unfortunate.

> diff -r b1809f5d7630 -r 1f402560299b mercurial/templater.py
> --- a/mercurial/templater.py	Mon Aug 15 20:39:33 2016 -0700
> +++ b/mercurial/templater.py	Wed Aug 17 13:40:27 2016 -0500
> @@ -1026,6 +1026,16 @@
>                  raise error.ParseError(_('unmatched quotes'),
>                                         conf.source('', key))
>              cache[key] = unquotestring(val)
> +        elif key == "__base__":
> +            # treat as a pointer to a base class for this style
> +            path = util.normpath(os.path.join(base, val))
> +            bcache, btmap = _readmapfile(path)

Do we want to make __base__ look for templatepaths?
Matt Mackall - Aug. 19, 2016, 4:56 p.m.
On Fri, 2016-08-19 at 23:37 +0900, Yuya Nishihara wrote:
> +        elif key == "__base__":
> > +            # treat as a pointer to a base class for this style
> > +            path = util.normpath(os.path.join(base, val))
> > +            bcache, btmap = _readmapfile(path)
> Do we want to make __base__ look for templatepaths?

Yes, that seems very useful.

Patch

diff -r b1809f5d7630 -r 1f402560299b mercurial/templater.py
--- a/mercurial/templater.py	Mon Aug 15 20:39:33 2016 -0700
+++ b/mercurial/templater.py	Wed Aug 17 13:40:27 2016 -0500
@@ -1026,6 +1026,16 @@ 
                 raise error.ParseError(_('unmatched quotes'),
                                        conf.source('', key))
             cache[key] = unquotestring(val)
+        elif key == "__base__":
+            # treat as a pointer to a base class for this style
+            path = util.normpath(os.path.join(base, val))
+            bcache, btmap = _readmapfile(path)
+            for k in bcache:
+                if k not in cache:
+                    cache[k] = bcache[k]
+            for k in btmap:
+                if k not in tmap:
+                    tmap[k] = btmap[k]
         else:
             val = 'default', val
             if ':' in val[1]: