Skip to content

Commit 161f8fe

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

File tree

3 files changed

+75
-64
lines changed

3 files changed

+75
-64
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: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -213,63 +213,5 @@ class PrettierPrintTest < Test::Unit::TestCase
213213

214214
assert_equal 8, q.last_position(q.groups.first)
215215
end
216-
217-
test "large integration" do
218-
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
228-
q.breakable_force
229-
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
237-
238-
q.group do
239-
q.text("def ")
240-
q.text("format(")
241-
242-
q.group do
243-
q.seplist(%w[foo bar baz]) do |item|
244-
q.text(item)
245-
end
246-
end
247-
248-
q.text(")")
249-
q.breakable_force
250-
q.text("end")
251-
end
252-
end
253-
254-
q.breakable_force
255-
q.text("end")
256-
end
257-
258-
q.breakable_force
259-
end
260-
261-
expected = <<~RUBY
262-
# This is the main class for the PrettierPrint gem.
263-
class PrettierPrint
264-
=begin
265-
This is embedded documentation.
266-
=end
267-
def format(foo, bar, baz)
268-
end
269-
end
270-
RUBY
271-
272-
assert_equal expected, result.join
273-
end
274216
end
275217
end

test/ractor_test.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
return unless defined?(Ractor)
4+
require "test_helper"
5+
6+
class PrettierPrint
7+
class RactorTest < Test::Unit::TestCase
8+
test "ractor safe" do
9+
result =
10+
Ractor.new do
11+
PrettierPrint.format([]) do |q|
12+
q.group do
13+
q.text("# This is the main class for the PrettierPrint gem.")
14+
q.breakable_force
15+
16+
q.text("class ")
17+
q.group { q.text("PrettierPrint") }
18+
19+
q.indent do
20+
q.breakable_force
21+
22+
q.trim
23+
q.text("=begin")
24+
q.breakable_return
25+
q.text("This is embedded documentation.")
26+
q.breakable_return
27+
q.text("=end")
28+
q.breakable_force
29+
30+
q.group do
31+
q.text("def ")
32+
q.text("format(")
33+
34+
q.group do
35+
q.seplist(%w[foo bar baz]) do |item|
36+
q.text(item)
37+
end
38+
end
39+
40+
q.text(")")
41+
q.breakable_force
42+
q.text("end")
43+
end
44+
end
45+
46+
q.breakable_force
47+
q.text("end")
48+
end
49+
50+
q.breakable_force
51+
end
52+
end
53+
54+
expected = <<~RUBY
55+
# This is the main class for the PrettierPrint gem.
56+
class PrettierPrint
57+
=begin
58+
This is embedded documentation.
59+
=end
60+
def format(foo, bar, baz)
61+
end
62+
end
63+
RUBY
64+
65+
assert_equal expected, result.take.join
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)