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

Commit 529e096

Browse files
committed
Better formatting for empty hashes and arrays with interior comments
1 parent 5bcf12f commit 529e096

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

lib/syntax_tree/formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def format(node, stackable: true)
5050
# Print all comments that were found after the node.
5151
trailing.each do |comment|
5252
line_suffix(priority: COMMENT_PRIORITY) do
53-
text(" ")
53+
comment.inline? ? text(" ") : breakable
5454
comment.format(self)
5555
break_parent
5656
end

lib/syntax_tree/node.rb

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,29 @@ def format(q)
816816
end
817817
end
818818

819+
class EmptyWithCommentsFormatter
820+
# [LBracket] the opening bracket
821+
attr_reader :lbracket
822+
823+
def initialize(lbracket)
824+
@lbracket = lbracket
825+
end
826+
827+
def format(q)
828+
q.group do
829+
q.text("[")
830+
q.indent do
831+
lbracket.comments.each do |comment|
832+
q.breakable(force: true)
833+
comment.format(q)
834+
end
835+
end
836+
q.breakable(force: true)
837+
q.text("]")
838+
end
839+
end
840+
end
841+
819842
# [LBracket] the bracket that opens this array
820843
attr_reader :lbracket
821844

@@ -867,6 +890,11 @@ def format(q)
867890
return
868891
end
869892

893+
if empty_with_comments?
894+
EmptyWithCommentsFormatter.new(lbracket).format(q)
895+
return
896+
end
897+
870898
q.group do
871899
q.format(lbracket)
872900

@@ -919,6 +947,12 @@ def var_refs?(q)
919947
q.maxwidth * 2
920948
)
921949
end
950+
951+
# If we have an empty array that contains only comments, then we're going
952+
# to do some special printing to ensure they get indented correctly.
953+
def empty_with_comments?
954+
contents.nil? && lbracket.comments.any? && lbracket.comments.none?(&:inline?)
955+
end
922956
end
923957

924958
# AryPtn represents matching against an array pattern using the Ruby 2.7+
@@ -4304,6 +4338,29 @@ def format(q)
43044338
# { key => value }
43054339
#
43064340
class HashLiteral < Node
4341+
class EmptyWithCommentsFormatter
4342+
# [LBrace] the opening brace
4343+
attr_reader :lbrace
4344+
4345+
def initialize(lbrace)
4346+
@lbrace = lbrace
4347+
end
4348+
4349+
def format(q)
4350+
q.group do
4351+
q.text("{")
4352+
q.indent do
4353+
lbrace.comments.each do |comment|
4354+
q.breakable(force: true)
4355+
comment.format(q)
4356+
end
4357+
end
4358+
q.breakable(force: true)
4359+
q.text("}")
4360+
end
4361+
end
4362+
end
4363+
43074364
# [LBrace] the left brace that opens this hash
43084365
attr_reader :lbrace
43094366

@@ -4348,7 +4405,18 @@ def format_key(q, key)
43484405

43494406
private
43504407

4408+
# If we have an empty hash that contains only comments, then we're going
4409+
# to do some special printing to ensure they get indented correctly.
4410+
def empty_with_comments?
4411+
assocs.empty? && lbrace.comments.any? && lbrace.comments.none?(&:inline?)
4412+
end
4413+
43514414
def format_contents(q)
4415+
if empty_with_comments?
4416+
EmptyWithCommentsFormatter.new(lbrace).format(q)
4417+
return
4418+
end
4419+
43524420
q.format(lbrace)
43534421

43544422
if assocs.empty?
@@ -7577,19 +7645,20 @@ def attach_comments(start_char, end_char)
75777645
comment = parser_comments[comment_index]
75787646
location = comment.location
75797647

7580-
if !comment.inline? && (start_char <= location.start_char) &&
7581-
(end_char >= location.end_char) && !comment.ignore?
7582-
parser_comments.delete_at(comment_index)
7583-
7584-
while (node = body[body_index]) &&
7585-
(
7586-
node.is_a?(VoidStmt) ||
7587-
node.location.start_char < location.start_char
7588-
)
7648+
if !comment.inline? && (start_char <= location.start_char) && (end_char >= location.end_char) && !comment.ignore?
7649+
while (node = body[body_index]) && (node.is_a?(VoidStmt) || node.location.start_char < location.start_char)
75897650
body_index += 1
75907651
end
75917652

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

0 commit comments

Comments
 (0)