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

Remove VarRef array formatting #267

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
Jan 26, 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
Remove VarRef array formatting
  • Loading branch information
kddnewton committed Jan 26, 2023
commit a8ae2af6f6c54d48b1ba7033d4277c5adf6c89bb
140 changes: 44 additions & 96 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1103,58 +1103,6 @@ def format(q)
end
end

# Formats an array that contains only a list of variable references. To make
# things simpler, if there are a bunch, we format them all using the "fill"
# algorithm as opposed to breaking them into a ton of lines. For example,
#
# [foo, bar, baz]
#
# instead of becoming:
#
# [
# foo,
# bar,
# baz
# ]
#
# would instead become:
#
# [
# foo, bar,
# baz
# ]
#
# provided the line length was hit between `bar` and `baz`.
class VarRefsFormatter
# The separator for the fill algorithm.
class Separator
def call(q)
q.text(",")
q.fill_breakable
end
end

# [Args] the contents of the array
attr_reader :contents

def initialize(contents)
@contents = contents
end

def format(q)
q.text("[")
q.group do
q.indent do
q.breakable_empty
q.seplist(contents.parts, Separator.new) { |part| q.format(part) }
q.if_break { q.text(",") } if q.trailing_comma?
end
q.breakable_empty
end
q.text("]")
end
end

# This is a special formatter used if the array literal contains no values
# but _does_ contain comments. In this case we do some special formatting to
# make sure the comments gets indented properly.
Expand Down Expand Up @@ -1229,19 +1177,17 @@ def deconstruct_keys(_keys)
end

def format(q)
if qwords?
QWordsFormatter.new(contents).format(q)
return
end

if qsymbols?
QSymbolsFormatter.new(contents).format(q)
return
end
if lbracket.comments.empty? && contents && contents.comments.empty? &&
contents.parts.length > 1
if qwords?
QWordsFormatter.new(contents).format(q)
return
end

if var_refs?(q)
VarRefsFormatter.new(contents).format(q)
return
if qsymbols?
QSymbolsFormatter.new(contents).format(q)
return
end
end

if empty_with_comments?
Expand Down Expand Up @@ -1273,39 +1219,24 @@ def ===(other)
private

def qwords?
lbracket.comments.empty? && contents && contents.comments.empty? &&
contents.parts.length > 1 &&
contents.parts.all? do |part|
case part
when StringLiteral
part.comments.empty? && part.parts.length == 1 &&
part.parts.first.is_a?(TStringContent) &&
!part.parts.first.value.match?(/[\s\[\]\\]/)
when CHAR
!part.value.match?(/[\[\]\\]/)
else
false
end
contents.parts.all? do |part|
case part
when StringLiteral
part.comments.empty? && part.parts.length == 1 &&
part.parts.first.is_a?(TStringContent) &&
!part.parts.first.value.match?(/[\s\[\]\\]/)
when CHAR
!part.value.match?(/[\[\]\\]/)
else
false
end
end
end

def qsymbols?
lbracket.comments.empty? && contents && contents.comments.empty? &&
contents.parts.length > 1 &&
contents.parts.all? do |part|
part.is_a?(SymbolLiteral) && part.comments.empty?
end
end

def var_refs?(q)
lbracket.comments.empty? && contents && contents.comments.empty? &&
contents.parts.all? do |part|
part.is_a?(VarRef) && part.comments.empty?
end &&
(
contents.parts.sum { |part| part.value.value.length + 2 } >
q.maxwidth * 2
)
contents.parts.all? do |part|
part.is_a?(SymbolLiteral) && part.comments.empty?
end
end

# If we have an empty array that contains only comments, then we're going
Expand Down Expand Up @@ -6551,9 +6482,26 @@ def deconstruct_keys(_keys)

def format(q)
force_flat = [
AliasNode, Assign, Break, Command, CommandCall, Heredoc, IfNode, IfOp,
Lambda, MAssign, Next, OpAssign, RescueMod, ReturnNode, Super, Undef,
UnlessNode, VoidStmt, YieldNode, ZSuper
AliasNode,
Assign,
Break,
Command,
CommandCall,
Heredoc,
IfNode,
IfOp,
Lambda,
MAssign,
Next,
OpAssign,
RescueMod,
ReturnNode,
Super,
Undef,
UnlessNode,
VoidStmt,
YieldNode,
ZSuper
]

if q.parent.is_a?(Paren) || force_flat.include?(truthy.class) ||
Expand Down
13 changes: 10 additions & 3 deletions test/fixtures/array_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
-
fooooooooooooooooo = 1
[
fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo,
fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo,
fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo, fooooooooooooooooo
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo,
fooooooooooooooooo
]
%
[
Expand Down