From d2f0e17454df4c3af96827055343c92726b363f6 Mon Sep 17 00:00:00 2001 From: Aditya Bansal Date: Tue, 13 Jun 2017 10:44:48 +0530 Subject: [PATCH] linter: Fix bug resulting in
 being indented cyclically.

---
 tools/check-templates            |  2 +-
 tools/lib/pretty_print.py        | 15 +++++++++++----
 tools/tests/test_pretty_print.py | 31 +++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/tools/check-templates b/tools/check-templates
index 5461fcaf82..9b6bcb6db8 100755
--- a/tools/check-templates
+++ b/tools/check-templates
@@ -150,7 +150,7 @@ def check_html_templates(templates, all_dups):
         'templates/zerver/markdown_help.html',
         'templates/zerver/navbar.html',
         'templates/zerver/register.html',
-        'templates/zerver/test_emails.html',
+
         'zerver/webhooks/deskdotcom/doc.html',
         'zerver/webhooks/librato/doc.html',
         'zerver/webhooks/splunk/doc.html',
diff --git a/tools/lib/pretty_print.py b/tools/lib/pretty_print.py
index 91386fc05d..be35c53bcb 100644
--- a/tools/lib/pretty_print.py
+++ b/tools/lib/pretty_print.py
@@ -30,7 +30,8 @@ def pretty_print_html(html, num_spaces=4):
         line=-1,
         token_kind='html_start',
         tag='html',
-        extra_indent=0)  # type: Dict[str, Any]
+        extra_indent=0,
+        ignore_lines=[])  # type: Dict[str, Any]
     stack.append(info)
 
     # Our main job is to figure out offsets that we use to nudge lines
@@ -79,7 +80,8 @@ def pretty_print_html(html, num_spaces=4):
                         extra_indent_prev=extra_indent,
                         adjustment=adjustment,
                         indenting=True,
-                        adjust_offset_until=token.line
+                        adjust_offset_until=token.line,
+                        ignore_lines=[]
                     )
                     if token.kind in ('handlebars_start', 'django_start'):
                         info.update(dict(depth=new_depth - 1, indenting=False))
@@ -91,7 +93,8 @@ def pretty_print_html(html, num_spaces=4):
                         line=token.line,
                         tag=token.tag,
                         token_kind=token.kind,
-                        extra_indent=stack[-1]['extra_indent']
+                        extra_indent=stack[-1]['extra_indent'],
+                        ignore_lines=[]
                     )
                 stack.append(info)
         elif token.kind in ('html_end', 'handlebars_end',
@@ -107,6 +110,8 @@ def pretty_print_html(html, num_spaces=4):
                 if token.tag == 'pre':
                     offsets[start_line] = 0
                     offsets[end_line] = 0
+                    stack[-1]['ignore_lines'].append(start_line)
+                    stack[-1]['ignore_lines'].append(end_line)
                 else:
                     offsets[start_line] = info['offset']
                     line = lines[token.line - 1]
@@ -139,7 +144,8 @@ def pretty_print_html(html, num_spaces=4):
                             offsets[line_num] = offset
                         elif (token.kind in ('handlebars_end', 'django_end') and
                                 info['indenting'] and
-                                line_num < info['adjust_offset_until']):
+                                line_num < info['adjust_offset_until'] and
+                                line_num not in info['ignore_lines']):
                             offsets[line_num] += num_spaces
                 elif token.tag != 'pre':
                     for line_num in range(start_line + 1, end_line):
@@ -149,6 +155,7 @@ def pretty_print_html(html, num_spaces=4):
                     for line_num in range(start_line + 1, end_line):
                         if line_num not in offsets:
                             offsets[line_num] = 0
+                            stack[-1]['ignore_lines'].append(line_num)
 
     # Now that we have all of our offsets calculated, we can just
     # join all our lines together, fixing up offsets as needed.
diff --git a/tools/tests/test_pretty_print.py b/tools/tests/test_pretty_print.py
index 42273d10e9..a6efcf83ba 100644
--- a/tools/tests/test_pretty_print.py
+++ b/tools/tests/test_pretty_print.py
@@ -362,6 +362,36 @@ GOOD_HTML13 = """
     
{{this.count}}
""" + +BAD_HTML14 = """ +
+ {{#if this.code}} +
Here goes some cool code.
+ {{else}} +
+ content of first div +
+ content of second div. +
+
+ {{/if}} +
+""" + +GOOD_HTML14 = """ +
+ {{#if this.code}} +
Here goes some cool code.
+ {{else}} +
+ content of first div +
+ content of second div. +
+
+ {{/if}} +
+""" class TestPrettyPrinter(unittest.TestCase): def compare(self, a, b): # type: (str, str) -> None @@ -384,3 +414,4 @@ class TestPrettyPrinter(unittest.TestCase): self.compare(pretty_print_html(BAD_HTML11), GOOD_HTML11) self.compare(pretty_print_html(BAD_HTML12), GOOD_HTML12) self.compare(pretty_print_html(BAD_HTML13), GOOD_HTML13) + self.compare(pretty_print_html(BAD_HTML14), GOOD_HTML14)