Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Remaining prettier formatting fixes #56

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 13 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Better formatting for empty hashes and arrays with interior comments
  • Loading branch information
kddnewton committed Apr 22, 2022
commit 6eb63e8a6378feecf34f67d421c20e1a48e98a8a
2 changes: 1 addition & 1 deletion lib/syntax_tree/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def format(node, stackable: true)
# Print all comments that were found after the node.
trailing.each do |comment|
line_suffix(priority: COMMENT_PRIORITY) do
text(" ")
comment.inline? ? text(" ") : breakable
comment.format(self)
break_parent
end
Expand Down
89 changes: 79 additions & 10 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,29 @@ def format(q)
end
end

class EmptyWithCommentsFormatter
# [LBracket] the opening bracket
attr_reader :lbracket

def initialize(lbracket)
@lbracket = lbracket
end

def format(q)
q.group do
q.text("[")
q.indent do
lbracket.comments.each do |comment|
q.breakable(force: true)
comment.format(q)
end
end
q.breakable(force: true)
q.text("]")
end
end
end

# [LBracket] the bracket that opens this array
attr_reader :lbracket

Expand Down Expand Up @@ -867,6 +890,11 @@ def format(q)
return
end

if empty_with_comments?
EmptyWithCommentsFormatter.new(lbracket).format(q)
return
end

q.group do
q.format(lbracket)

Expand Down Expand Up @@ -919,6 +947,12 @@ def var_refs?(q)
q.maxwidth * 2
)
end

# If we have an empty array that contains only comments, then we're going
# to do some special printing to ensure they get indented correctly.
def empty_with_comments?
contents.nil? && lbracket.comments.any? && lbracket.comments.none?(&:inline?)
end
end

# AryPtn represents matching against an array pattern using the Ruby 2.7+
Expand Down Expand Up @@ -4311,6 +4345,29 @@ def format(q)
# { key => value }
#
class HashLiteral < Node
class EmptyWithCommentsFormatter
# [LBrace] the opening brace
attr_reader :lbrace

def initialize(lbrace)
@lbrace = lbrace
end

def format(q)
q.group do
q.text("{")
q.indent do
lbrace.comments.each do |comment|
q.breakable(force: true)
comment.format(q)
end
end
q.breakable(force: true)
q.text("}")
end
end
end

# [LBrace] the left brace that opens this hash
attr_reader :lbrace

Expand Down Expand Up @@ -4355,7 +4412,18 @@ def format_key(q, key)

private

# If we have an empty hash that contains only comments, then we're going
# to do some special printing to ensure they get indented correctly.
def empty_with_comments?
assocs.empty? && lbrace.comments.any? && lbrace.comments.none?(&:inline?)
end

def format_contents(q)
if empty_with_comments?
EmptyWithCommentsFormatter.new(lbrace).format(q)
return
end

q.format(lbrace)

if assocs.empty?
Expand Down Expand Up @@ -7584,19 +7652,20 @@ def attach_comments(start_char, end_char)
comment = parser_comments[comment_index]
location = comment.location

if !comment.inline? && (start_char <= location.start_char) &&
(end_char >= location.end_char) && !comment.ignore?
parser_comments.delete_at(comment_index)

while (node = body[body_index]) &&
(
node.is_a?(VoidStmt) ||
node.location.start_char < location.start_char
)
if !comment.inline? && (start_char <= location.start_char) && (end_char >= location.end_char) && !comment.ignore?
while (node = body[body_index]) && (node.is_a?(VoidStmt) || node.location.start_char < location.start_char)
body_index += 1
end

body.insert(body_index, comment)
if body_index != 0 && body[body_index - 1].location.start_char < location.start_char && body[body_index - 1].location.end_char > location.start_char
# The previous node entirely encapsules the comment, so we don't
# want to attach it here since it will get attached normally. This
# is mostly in the case of hash and array literals.
comment_index += 1
else
parser_comments.delete_at(comment_index)
body.insert(body_index, comment)
end
else
comment_index += 1
end
Expand Down