File tree Expand file tree Collapse file tree 3 files changed +55
-13
lines changed Expand file tree Collapse file tree 3 files changed +55
-13
lines changed Original file line number Diff line number Diff line change @@ -82,10 +82,30 @@ def accept(visitor)
8282 end
8383 end
8484
85+ # This is our entrypoint for the formatter. We effectively delegate this to
86+ # accepting the Format visitor.
8587 def format ( q )
8688 accept ( SyntaxTree ::Haml ::Format . new ( q ) )
8789 end
8890
91+ # When we're formatting a list of children, we need to know the last line a
92+ # node is on. This is because the next node in the list of children should be
93+ # at most 1 blank line below the last line of the previous node. We cache this
94+ # because at worst it requires walking the entire tree because filter nodes
95+ # can take up multiple lines.
96+ def last_line
97+ @last_line ||=
98+ if children . any?
99+ children . last . last_line
100+ elsif type == :filter
101+ line + value [ :text ] . rstrip . count ( "\n " ) + 1
102+ else
103+ line
104+ end
105+ end
106+
107+ # This is our entrypoint for the pretty printer. We effectively delegate this
108+ # to accepting the PrettyPrint visitor.
89109 def pretty_print ( q )
90110 accept ( SyntaxTree ::Haml ::PrettyPrint . new ( q ) )
91111 end
Original file line number Diff line number Diff line change @@ -69,15 +69,17 @@ def visit_filter(node)
6969 q . breakable_force
7070 first = true
7171
72- node . value [ :text ] . each_line ( chomp : true ) do |line |
73- if first
74- first = false
75- else
76- q . breakable_force
77- end
72+ node . value [ :text ]
73+ . rstrip
74+ . each_line ( chomp : true ) do |line |
75+ if first
76+ first = false
77+ else
78+ q . breakable_force
79+ end
7880
79- q . text ( line )
80- end
81+ q . text ( line )
82+ end
8183 end
8284 end
8385 end
@@ -124,8 +126,7 @@ def visit_root(node)
124126
125127 node . children . each do |child |
126128 q . breakable_force if previous_line && ( child . line - previous_line ) > 1
127- previous_line =
128- child . children . any? ? child . children . last . line : child . line
129+ previous_line = child . last_line
129130
130131 visit ( child )
131132 q . breakable_force
@@ -513,10 +514,8 @@ def with_children(node)
513514 q . breakable_force
514515 end
515516
516- previous_line =
517- child . children . any? ? child . children . last . line : child . line
518-
519517 visit ( child )
518+ previous_line = child . last_line
520519 end
521520 end
522521 end
Original file line number Diff line number Diff line change @@ -24,4 +24,27 @@ def test_javascript
2424 1 + 1;
2525 HAML
2626 end
27+
28+ def test_does_not_add_blank_lines
29+ assert_format ( <<~HAML )
30+ %html
31+ %head
32+ :javascript
33+ console.log("This is inline script.");
34+ %body
35+ = yield
36+ HAML
37+ end
38+
39+ def test_maintains_blank_lines
40+ assert_format ( <<~HAML )
41+ %html
42+ %head
43+ :javascript
44+ console.log("This is inline script.");
45+
46+ %body
47+ = yield
48+ HAML
49+ end
2750end
You can’t perform that action at this time.
0 commit comments