changeset 43695:fa246ada356b

templates: make {indent("", " ")} be empty indent() is documented to indent all non-empty lines, but it made an exception for the first line, which always got indented. I also made indent() not indent the first line even if an indent override was given for the first line. I think that is what one would usually want. Differential Revision: https://phab.mercurial-scm.org/D7432
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 15 Nov 2019 10:16:27 -0800
parents 0fd9e7a1cf36
children 794426e96970
files mercurial/templatefilters.py mercurial/templatefuncs.py relnotes/next tests/test-template-functions.t
diffstat 4 files changed, 11 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Fri Nov 15 10:16:22 2019 -0800
+++ b/mercurial/templatefilters.py	Fri Nov 15 10:16:27 2019 -0800
@@ -299,7 +299,7 @@
     return dateutil.datestr(text, b'%Y-%m-%d %H:%M:%S %1%2')
 
 
-def indent(text, prefix):
+def indent(text, prefix, firstline=b''):
     '''indent each non-empty line of text after first with prefix.'''
     lines = text.splitlines()
     num_lines = len(lines)
@@ -308,8 +308,8 @@
     def indenter():
         for i in pycompat.xrange(num_lines):
             l = lines[i]
-            if i and l.strip():
-                yield prefix
+            if l.strip():
+                yield prefix if i else firstline
             yield l
             if i < num_lines - 1 or endswithnewline:
                 yield b'\n'
--- a/mercurial/templatefuncs.py	Fri Nov 15 10:16:22 2019 -0800
+++ b/mercurial/templatefuncs.py	Fri Nov 15 10:16:27 2019 -0800
@@ -310,13 +310,11 @@
     text = evalstring(context, mapping, args[0])
     indent = evalstring(context, mapping, args[1])
 
+    firstline = indent
     if len(args) == 3:
         firstline = evalstring(context, mapping, args[2])
-    else:
-        firstline = indent
 
-    # the indent function doesn't indent the first line, so we do it here
-    return templatefilters.indent(firstline + text, indent)
+    return templatefilters.indent(text, indent, firstline=firstline)
 
 
 @templatefunc(b'get(dict, key)')
--- a/relnotes/next	Fri Nov 15 10:16:22 2019 -0800
+++ b/relnotes/next	Fri Nov 15 10:16:27 2019 -0800
@@ -6,6 +6,9 @@
 
 == Bug Fixes  ==
 
+ * The `indent()` template function was documented to not indent empty lines,
+   but it still indented the first line even if it was empty. It no longer does
+   that.
 
 == Backwards Compatibility Changes ==
 
--- a/tests/test-template-functions.t	Fri Nov 15 10:16:22 2019 -0800
+++ b/tests/test-template-functions.t	Fri Nov 15 10:16:27 2019 -0800
@@ -1507,16 +1507,16 @@
 Test indent with empty first line
 
   $ hg version -T "{indent('', '>> ')}\n"
-  >> 
+  
 
   $ hg version -T "{indent('
   > second', '>> ')}\n"
-  >> 
+  
   >> second
 
   $ hg version -T "{indent('
   > second', '>> ', ' > ')}\n"
-   > 
+  
   >> second
 
 Test with non-strings like dates