Patchwork D7432: templates: make {indent("", " ")} be empty

login
register
mail settings
Submitter phabricator
Date Nov. 15, 2019, 6:18 p.m.
Message ID <differential-rev-PHID-DREV-snpskt2h73t55jdaqwwj-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/43279/
State Superseded
Headers show

Comments

phabricator - Nov. 15, 2019, 6:18 p.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7432

AFFECTED FILES
  mercurial/templatefilters.py
  mercurial/templatefuncs.py
  tests/test-template-functions.t

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -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
diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -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)')
diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -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'