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

Commit b5154ac

Browse files
committed
each_line instead of split
1 parent 929726b commit b5154ac

File tree

2 files changed

+96
-52
lines changed

2 files changed

+96
-52
lines changed

lib/syntax_tree/formatter.rb

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,6 @@ module SyntaxTree
44
# A slightly enhanced PP that knows how to format recursively including
55
# comments.
66
class Formatter < PrettierPrint
7-
# It's very common to use seplist with ->(q) { q.breakable_return }. We wrap
8-
# that pattern into an object to cut down on having to create a bunch of
9-
# lambdas all over the place.
10-
class BreakableReturnSeparator
11-
def call(q)
12-
q.breakable_return
13-
end
14-
end
15-
16-
# Similar to the previous, it's common to ->(q) { q.breakable_space }. We
17-
# also wrap that pattern into an object to cut down on lambdas.
18-
class BreakableSpaceSeparator
19-
def call(q)
20-
q.breakable_space
21-
end
22-
end
23-
24-
BREAKABLE_RETURN_SEPARATOR = BreakableReturnSeparator.new
25-
BREAKABLE_SPACE_SEPARATOR = BreakableSpaceSeparator.new
26-
277
# We want to minimize as much as possible the number of options that are
288
# available in syntax tree. For the most part, if users want non-default
299
# formatting, they should override the format methods on the specific nodes
@@ -82,20 +62,39 @@ def format(node, stackable: true)
8262
# If there are comments, then we're going to format them around the node
8363
# so that they get printed properly.
8464
if node.comments.any?
85-
leading, trailing = node.comments.partition(&:leading?)
65+
trailing = []
66+
last_leading = nil
8667

87-
# Print all comments that were found before the node.
88-
leading.each do |comment|
89-
comment.format(self)
90-
breakable(force: true)
68+
# First, we're going to print all of the comments that were found before
69+
# the node. We'll also gather up any trailing comments that we find.
70+
node.comments.each do |comment|
71+
if comment.leading?
72+
comment.format(self)
73+
breakable(force: true)
74+
last_leading = comment
75+
else
76+
trailing << comment
77+
end
9178
end
9279

9380
# If the node has a stree-ignore comment right before it, then we're
9481
# going to just print out the node as it was seen in the source.
9582
doc =
96-
if leading.last&.ignore?
83+
if last_leading&.ignore?
9784
range = source[node.location.start_char...node.location.end_char]
98-
seplist(range.split(/\r?\n/, -1), Formatter::BREAKABLE_RETURN_SEPARATOR) { |line| text(line) }
85+
first = true
86+
87+
range.each_line(chomp: true) do |line|
88+
if first
89+
first = false
90+
else
91+
breakable_return
92+
end
93+
94+
text(line)
95+
end
96+
97+
breakable_return if range.end_with?("\n")
9998
else
10099
node.format(self)
101100
end

lib/syntax_tree/node.rb

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,19 @@ def deconstruct_keys(_keys)
328328
def format(q)
329329
q.text("__END__")
330330
q.breakable_force
331-
q.seplist(value.split(/\r?\n/, -1), Formatter::BREAKABLE_RETURN_SEPARATOR) { |line| q.text(line) }
331+
332+
first = true
333+
value.each_line(chomp: true) do |line|
334+
if first
335+
first = false
336+
else
337+
q.breakable_return
338+
end
339+
340+
q.text(line)
341+
end
342+
343+
q.breakable_return if value.end_with?("\n")
332344
end
333345
end
334346

@@ -792,6 +804,17 @@ def format(q)
792804
# [one, two, three]
793805
#
794806
class ArrayLiteral < Node
807+
# It's very common to use seplist with ->(q) { q.breakable_space }. We wrap
808+
# that pattern into an object to cut down on having to create a bunch of
809+
# lambdas all over the place.
810+
class BreakableSpaceSeparator
811+
def call(q)
812+
q.breakable_space
813+
end
814+
end
815+
816+
BREAKABLE_SPACE_SEPARATOR = BreakableSpaceSeparator.new
817+
795818
# Formats an array of multiple simple string literals into the %w syntax.
796819
class QWordsFormatter
797820
# [Args] the contents of the array
@@ -806,7 +829,7 @@ def format(q)
806829
q.group do
807830
q.indent do
808831
q.breakable_empty
809-
q.seplist(contents.parts, Formatter::BREAKABLE_SPACE_SEPARATOR) do |part|
832+
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
810833
if part.is_a?(StringLiteral)
811834
q.format(part.parts.first)
812835
else
@@ -834,7 +857,7 @@ def format(q)
834857
q.group do
835858
q.indent do
836859
q.breakable_empty
837-
q.seplist(contents.parts, Formatter::BREAKABLE_SPACE_SEPARATOR) do |part|
860+
q.seplist(contents.parts, BREAKABLE_SPACE_SEPARATOR) do |part|
838861
q.format(part.value)
839862
end
840863
end
@@ -4034,9 +4057,19 @@ def format(q)
40344057
parts.each do |part|
40354058
if part.is_a?(TStringContent)
40364059
value = Quotes.normalize(part.value, closing_quote)
4037-
q.seplist(value.split(/\r?\n/, -1), Formatter::BREAKABLE_RETURN_SEPARATOR) do |text|
4038-
q.text(text)
4060+
first = true
4061+
4062+
value.each_line(chomp: true) do |line|
4063+
if first
4064+
first = false
4065+
else
4066+
q.breakable_return
4067+
end
4068+
4069+
q.text(line)
40394070
end
4071+
4072+
q.breakable_return if value.end_with?("\n")
40404073
else
40414074
q.format(part)
40424075
end
@@ -4957,30 +4990,32 @@ def deconstruct_keys(_keys)
49574990

49584991
# This is a very specific behavior where you want to force a newline, but
49594992
# don't want to force the break parent.
4960-
class Separator
4961-
DOC = PrettierPrint::Breakable.new(" ", 1, indent: false, force: true)
4962-
4963-
def call(q)
4964-
q.target << DOC
4965-
end
4966-
end
4967-
4968-
# We're going to keep an instance around so we don't have to allocate a new
4969-
# one every time we format a heredoc.
4970-
SEPARATOR = Separator.new
4993+
SEPARATOR = PrettierPrint::Breakable.new(" ", 1, indent: false, force: true)
49714994

49724995
def format(q)
49734996
q.group do
49744997
q.format(beginning)
49754998

49764999
q.line_suffix(priority: Formatter::HEREDOC_PRIORITY) do
49775000
q.group do
4978-
SEPARATOR.call(q)
5001+
q.target << SEPARATOR
49795002

49805003
parts.each do |part|
49815004
if part.is_a?(TStringContent)
4982-
texts = part.value.split(/\r?\n/, -1)
4983-
q.seplist(texts, SEPARATOR) { |text| q.text(text) }
5005+
value = part.value
5006+
first = true
5007+
5008+
value.each_line(chomp: true) do |line|
5009+
if first
5010+
first = false
5011+
else
5012+
q.target << SEPARATOR
5013+
end
5014+
5015+
q.text(line)
5016+
end
5017+
5018+
q.target << SEPARATOR if value.end_with?("\n")
49845019
else
49855020
q.format(part)
49865021
end
@@ -7295,7 +7330,7 @@ def format(q)
72957330
q.group do
72967331
q.indent do
72977332
q.breakable_empty
7298-
q.seplist(elements, Formatter::BREAKABLE_SPACE_SEPARATOR) do |element|
7333+
q.seplist(elements, ArrayLiteral::BREAKABLE_SPACE_SEPARATOR) do |element|
72997334
q.format(element)
73007335
end
73017336
end
@@ -7388,7 +7423,7 @@ def format(q)
73887423
q.group do
73897424
q.indent do
73907425
q.breakable_empty
7391-
q.seplist(elements, Formatter::BREAKABLE_SPACE_SEPARATOR) do |element|
7426+
q.seplist(elements, ArrayLiteral::BREAKABLE_SPACE_SEPARATOR) do |element|
73927427
q.format(element)
73937428
end
73947429
end
@@ -8626,9 +8661,19 @@ def format(q)
86268661
parts.each do |part|
86278662
if part.is_a?(TStringContent)
86288663
value = Quotes.normalize(part.value, closing_quote)
8629-
q.seplist(value.split(/\r?\n/, -1), Formatter::BREAKABLE_RETURN_SEPARATOR) do |text|
8630-
q.text(text)
8664+
first = true
8665+
8666+
value.each_line(chomp: true) do |line|
8667+
if first
8668+
first = false
8669+
else
8670+
q.breakable_return
8671+
end
8672+
8673+
q.text(line)
86318674
end
8675+
8676+
q.breakable_return if value.end_with?("\n")
86328677
else
86338678
q.format(part)
86348679
end
@@ -8845,7 +8890,7 @@ def format(q)
88458890
q.group do
88468891
q.indent do
88478892
q.breakable_empty
8848-
q.seplist(elements, Formatter::BREAKABLE_SPACE_SEPARATOR) do |element|
8893+
q.seplist(elements, ArrayLiteral::BREAKABLE_SPACE_SEPARATOR) do |element|
88498894
q.format(element)
88508895
end
88518896
end
@@ -10184,7 +10229,7 @@ def format(q)
1018410229
q.group do
1018510230
q.indent do
1018610231
q.breakable_empty
10187-
q.seplist(elements, Formatter::BREAKABLE_SPACE_SEPARATOR) do |element|
10232+
q.seplist(elements, ArrayLiteral::BREAKABLE_SPACE_SEPARATOR) do |element|
1018810233
q.format(element)
1018910234
end
1019010235
end

0 commit comments

Comments
 (0)