Skip to content

Commit 9b7d64d

Browse files
authored
Merge pull request #3 from ruby-syntax-tree/coverage
Coverage and ractors
2 parents 8fc91f5 + 161f8fe commit 9b7d64d

File tree

8 files changed

+942
-639
lines changed

8 files changed

+942
-639
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ source "https://rubygems.org"
44

55
gemspec
66

7+
gem "simplecov"
78
gem "rake"
89
gem "test-unit"

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ PATH
66
GEM
77
remote: https://rubygems.org/
88
specs:
9+
docile (1.4.0)
910
power_assert (2.0.1)
1011
rake (13.0.6)
12+
simplecov (0.21.2)
13+
docile (~> 1.1)
14+
simplecov-html (~> 0.11)
15+
simplecov_json_formatter (~> 0.1)
16+
simplecov-html (0.12.3)
17+
simplecov_json_formatter (0.1.4)
1118
test-unit (3.5.5)
1219
power_assert
1320

@@ -19,6 +26,7 @@ PLATFORMS
1926
DEPENDENCIES
2027
prettier_print!
2128
rake
29+
simplecov
2230
test-unit
2331

2432
BUNDLED WITH

lib/prettier_print.rb

Lines changed: 9 additions & 169 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
@@ -338,151 +338,6 @@ def self.for(output)
338338
end
339339
end
340340

341-
# PrettierPrint::SingleLine is used by PrettierPrint.singleline_format
342-
#
343-
# It is passed to be similar to a PrettierPrint object itself, by responding to
344-
# all of the same print tree node builder methods, as well as the #flush
345-
# method.
346-
#
347-
# The significant difference here is that there are no line breaks in the
348-
# output. If an IfBreak node is used, only the flat contents are printed.
349-
# LineSuffix nodes are printed at the end of the buffer when #flush is called.
350-
class SingleLine
351-
# The output object. It stores rendered text and should respond to <<.
352-
attr_reader :output
353-
354-
# The current array of contents that the print tree builder methods should
355-
# append to.
356-
attr_reader :target
357-
358-
# A buffer output that wraps any calls to line_suffix that will be flushed
359-
# at the end of printing.
360-
attr_reader :line_suffixes
361-
362-
# Create a PrettierPrint::SingleLine object
363-
#
364-
# Arguments:
365-
# * +output+ - String (or similar) to store rendered text. Needs to respond
366-
# to '<<'.
367-
# * +maxwidth+ - Argument position expected to be here for compatibility.
368-
# This argument is a noop.
369-
# * +newline+ - Argument position expected to be here for compatibility.
370-
# This argument is a noop.
371-
def initialize(output, _maxwidth = nil, _newline = nil)
372-
@output = Buffer.for(output)
373-
@target = @output
374-
@line_suffixes = Buffer::ArrayBuffer.new
375-
end
376-
377-
# Flushes the line suffixes onto the output buffer.
378-
def flush
379-
line_suffixes.output.each { |doc| output << doc }
380-
end
381-
382-
# --------------------------------------------------------------------------
383-
# Markers node builders
384-
# --------------------------------------------------------------------------
385-
386-
# Appends +separator+ to the text to be output. By default +separator+ is
387-
# ' '
388-
#
389-
# The +width+, +indent+, and +force+ arguments are here for compatibility.
390-
# They are all noop arguments.
391-
def breakable(
392-
separator = " ",
393-
_width = separator.length,
394-
indent: nil,
395-
force: nil
396-
)
397-
target << separator
398-
end
399-
400-
# Here for compatibility, does nothing.
401-
def break_parent
402-
end
403-
404-
# Appends +separator+ to the output buffer. +width+ is a noop here for
405-
# compatibility.
406-
def fill_breakable(separator = " ", _width = separator.length)
407-
target << separator
408-
end
409-
410-
# Immediately trims the output buffer.
411-
def trim
412-
target.trim!
413-
end
414-
415-
# --------------------------------------------------------------------------
416-
# Container node builders
417-
# --------------------------------------------------------------------------
418-
419-
# Opens a block for grouping objects to be pretty printed.
420-
#
421-
# Arguments:
422-
# * +indent+ - noop argument. Present for compatibility.
423-
# * +open_obj+ - text appended before the &block. Default is ''
424-
# * +close_obj+ - text appended after the &block. Default is ''
425-
# * +open_width+ - noop argument. Present for compatibility.
426-
# * +close_width+ - noop argument. Present for compatibility.
427-
def group(
428-
_indent = nil,
429-
open_object = "",
430-
close_object = "",
431-
_open_width = nil,
432-
_close_width = nil
433-
)
434-
target << open_object
435-
yield
436-
target << close_object
437-
end
438-
439-
# A class that wraps the ability to call #if_flat. The contents of the
440-
# #if_flat block are executed immediately, so effectively this class and the
441-
# #if_break method that triggers it are unnecessary, but they're here to
442-
# maintain compatibility.
443-
class IfBreakBuilder
444-
def if_flat
445-
yield
446-
end
447-
end
448-
449-
# Effectively unnecessary, but here for compatibility.
450-
def if_break
451-
IfBreakBuilder.new
452-
end
453-
454-
# Also effectively unnecessary, but here for compatibility.
455-
def if_flat
456-
end
457-
458-
# A noop that immediately yields.
459-
def indent
460-
yield
461-
end
462-
463-
# Changes the target output buffer to the line suffix output buffer which
464-
# will get flushed at the end of printing.
465-
def line_suffix
466-
previous_target, @target = @target, line_suffixes
467-
yield
468-
@target = previous_target
469-
end
470-
471-
# Takes +indent+ arg, but does nothing with it.
472-
#
473-
# Yields to a block.
474-
def nest(_indent)
475-
yield
476-
end
477-
478-
# Add +object+ to the text to be output.
479-
#
480-
# +width+ argument is here for compatibility. It is a noop argument.
481-
def text(object = "", _width = nil)
482-
target << object
483-
end
484-
end
485-
486341
# When printing, you can optionally specify the value that should be used
487342
# whenever a group needs to be broken onto multiple lines. In this case the
488343
# default is \n.
@@ -493,6 +348,7 @@ def text(object = "", _width = nil)
493348
# behavior (for instance to use tabs) by passing a different genspace
494349
# procedure.
495350
DEFAULT_GENSPACE = ->(n) { " " * n }
351+
Ractor.make_shareable(DEFAULT_GENSPACE) if defined?(Ractor)
496352

497353
# There are two modes in printing, break and flat. When we're in break mode,
498354
# any lines will use their newline, any if-breaks will use their break
@@ -525,24 +381,6 @@ def self.format(
525381
output
526382
end
527383

528-
# This is similar to PrettierPrint::format but the result has no breaks.
529-
#
530-
# +maxwidth+, +newline+ and +genspace+ are ignored.
531-
#
532-
# The invocation of +breakable+ in the block doesn't break a line and is
533-
# treated as just an invocation of +text+.
534-
#
535-
def self.singleline_format(
536-
output = +"",
537-
_maxwidth = nil,
538-
_newline = nil,
539-
_genspace = nil
540-
)
541-
q = SingleLine.new(output)
542-
yield q
543-
output
544-
end
545-
546384
# The output object. It represents the final destination of the contents of
547385
# the print tree. It should respond to <<.
548386
#
@@ -1251,3 +1089,5 @@ def remove_breaks_with(doc, replace)
12511089
end
12521090
end
12531091
end
1092+
1093+
require_relative "prettier_print/single_line"

0 commit comments

Comments
 (0)