Skip to content

Commit a13099c

Browse files
committed
Make sure prettier print works with ractors
1 parent ea0bdcd commit a13099c

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

lib/prettier_print.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ def pretty_print(q)
122122

123123
# Below here are the most common combination of options that are created when
124124
# creating new breakables. They are here to cut down on some allocations.
125-
BREAKABLE_SPACE = Breakable.new(" ", 1, indent: true, force: false)
126-
BREAKABLE_EMPTY = Breakable.new("", 0, indent: true, force: false)
127-
BREAKABLE_FORCE = Breakable.new(" ", 1, indent: true, force: true)
128-
BREAKABLE_RETURN = Breakable.new(" ", 1, indent: false, force: true)
125+
BREAKABLE_SPACE = Breakable.new(" ", 1, indent: true, force: false).freeze
126+
BREAKABLE_EMPTY = Breakable.new("", 0, indent: true, force: false).freeze
127+
BREAKABLE_FORCE = Breakable.new(" ", 1, indent: true, force: true).freeze
128+
BREAKABLE_RETURN = Breakable.new(" ", 1, indent: false, force: true).freeze
129129

130130
# A node in the print tree that forces the surrounding group to print out in
131131
# the "break" mode as opposed to the "flat" mode. Useful for when you need to
@@ -138,7 +138,7 @@ def pretty_print(q)
138138

139139
# Since there's really no difference in these instances, just using the same
140140
# one saves on some allocations.
141-
BREAK_PARENT = BreakParent.new
141+
BREAK_PARENT = BreakParent.new.freeze
142142

143143
# A node in the print tree that represents a group of items which the printer
144144
# should try to fit onto one line. This is the basic command to tell the
@@ -267,7 +267,7 @@ def pretty_print(q)
267267

268268
# Since all of the instances here are the same, we can reuse the same one to
269269
# cut down on allocations.
270-
TRIM = Trim.new
270+
TRIM = Trim.new.freeze
271271

272272
# When building up the contents in the output buffer, it's convenient to be
273273
# able to trim trailing whitespace before newlines. If the output object is a
@@ -348,6 +348,7 @@ def self.for(output)
348348
# behavior (for instance to use tabs) by passing a different genspace
349349
# procedure.
350350
DEFAULT_GENSPACE = ->(n) { " " * n }
351+
Ractor.make_shareable(DEFAULT_GENSPACE) if defined?(Ractor)
351352

352353
# There are two modes in printing, break and flat. When we're in break mode,
353354
# any lines will use their newline, any if-breaks will use their break

test/prettier_print_test.rb

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,46 +216,48 @@ class PrettierPrintTest < Test::Unit::TestCase
216216

217217
test "large integration" do
218218
result =
219-
PrettierPrint.format([]) do |q|
220-
q.group do
221-
q.text("# This is the main class for the PrettierPrint gem.")
222-
q.breakable_force
223-
224-
q.text("class ")
225-
q.group { q.text("PrettierPrint") }
226-
227-
q.indent do
219+
Ractor.new do
220+
PrettierPrint.format([]) do |q|
221+
q.group do
222+
q.text("# This is the main class for the PrettierPrint gem.")
228223
q.breakable_force
229224

230-
q.trim
231-
q.text("=begin")
232-
q.breakable_return
233-
q.text("This is embedded documentation.")
234-
q.breakable_return
235-
q.text("=end")
236-
q.breakable_force
225+
q.text("class ")
226+
q.group { q.text("PrettierPrint") }
237227

238-
q.group do
239-
q.text("def ")
240-
q.text("format(")
228+
q.indent do
229+
q.breakable_force
230+
231+
q.trim
232+
q.text("=begin")
233+
q.breakable_return
234+
q.text("This is embedded documentation.")
235+
q.breakable_return
236+
q.text("=end")
237+
q.breakable_force
241238

242239
q.group do
243-
q.seplist(%w[foo bar baz]) do |item|
244-
q.text(item)
240+
q.text("def ")
241+
q.text("format(")
242+
243+
q.group do
244+
q.seplist(%w[foo bar baz]) do |item|
245+
q.text(item)
246+
end
245247
end
246-
end
247248

248-
q.text(")")
249-
q.breakable_force
250-
q.text("end")
249+
q.text(")")
250+
q.breakable_force
251+
q.text("end")
252+
end
251253
end
254+
255+
q.breakable_force
256+
q.text("end")
252257
end
253258

254259
q.breakable_force
255-
q.text("end")
256260
end
257-
258-
q.breakable_force
259261
end
260262

261263
expected = <<~RUBY
@@ -269,7 +271,7 @@ def format(foo, bar, baz)
269271
end
270272
RUBY
271273

272-
assert_equal expected, result.join
274+
assert_equal expected, result.take.join
273275
end
274276
end
275277
end

0 commit comments

Comments
 (0)