Skip to content

Handle HAML filter blank lines #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/syntax_tree/haml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,30 @@ def accept(visitor)
end
end

# This is our entrypoint for the formatter. We effectively delegate this to
# accepting the Format visitor.
def format(q)
accept(SyntaxTree::Haml::Format.new(q))
end

# When we're formatting a list of children, we need to know the last line a
# node is on. This is because the next node in the list of children should be
# at most 1 blank line below the last line of the previous node. We cache this
# because at worst it requires walking the entire tree because filter nodes
# can take up multiple lines.
def last_line
@last_line ||=
if children.any?
children.last.last_line
elsif type == :filter
line + value[:text].rstrip.count("\n") + 1
else
line
end
end

# This is our entrypoint for the pretty printer. We effectively delegate this
# to accepting the PrettyPrint visitor.
def pretty_print(q)
accept(SyntaxTree::Haml::PrettyPrint.new(q))
end
Expand Down
25 changes: 12 additions & 13 deletions lib/syntax_tree/haml/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ def visit_filter(node)
q.breakable_force
first = true

node.value[:text].each_line(chomp: true) do |line|
if first
first = false
else
q.breakable_force
end
node.value[:text]
.rstrip
.each_line(chomp: true) do |line|
if first
first = false
else
q.breakable_force
end

q.text(line)
end
q.text(line)
end
end
end
end
Expand Down Expand Up @@ -124,8 +126,7 @@ def visit_root(node)

node.children.each do |child|
q.breakable_force if previous_line && (child.line - previous_line) > 1
previous_line =
child.children.any? ? child.children.last.line : child.line
previous_line = child.last_line

visit(child)
q.breakable_force
Expand Down Expand Up @@ -513,10 +514,8 @@ def with_children(node)
q.breakable_force
end

previous_line =
child.children.any? ? child.children.last.line : child.line

visit(child)
previous_line = child.last_line
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions test/filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,27 @@ def test_javascript
1 + 1;
HAML
end

def test_does_not_add_blank_lines
assert_format(<<~HAML)
%html
%head
:javascript
console.log("This is inline script.");
%body
= yield
HAML
end

def test_maintains_blank_lines
assert_format(<<~HAML)
%html
%head
:javascript
console.log("This is inline script.");

%body
= yield
HAML
end
end