Skip to content

Coverage and ractors #3

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 2 commits into from
Nov 9, 2022
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ source "https://rubygems.org"

gemspec

gem "simplecov"
gem "rake"
gem "test-unit"
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ PATH
GEM
remote: https://rubygems.org/
specs:
docile (1.4.0)
power_assert (2.0.1)
rake (13.0.6)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
test-unit (3.5.5)
power_assert

Expand All @@ -19,6 +26,7 @@ PLATFORMS
DEPENDENCIES
prettier_print!
rake
simplecov
test-unit

BUNDLED WITH
Expand Down
178 changes: 9 additions & 169 deletions lib/prettier_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ def pretty_print(q)

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

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

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

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

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

# When building up the contents in the output buffer, it's convenient to be
# able to trim trailing whitespace before newlines. If the output object is a
Expand Down Expand Up @@ -338,151 +338,6 @@ def self.for(output)
end
end

# PrettierPrint::SingleLine is used by PrettierPrint.singleline_format
#
# It is passed to be similar to a PrettierPrint object itself, by responding to
# all of the same print tree node builder methods, as well as the #flush
# method.
#
# The significant difference here is that there are no line breaks in the
# output. If an IfBreak node is used, only the flat contents are printed.
# LineSuffix nodes are printed at the end of the buffer when #flush is called.
class SingleLine
# The output object. It stores rendered text and should respond to <<.
attr_reader :output

# The current array of contents that the print tree builder methods should
# append to.
attr_reader :target

# A buffer output that wraps any calls to line_suffix that will be flushed
# at the end of printing.
attr_reader :line_suffixes

# Create a PrettierPrint::SingleLine object
#
# Arguments:
# * +output+ - String (or similar) to store rendered text. Needs to respond
# to '<<'.
# * +maxwidth+ - Argument position expected to be here for compatibility.
# This argument is a noop.
# * +newline+ - Argument position expected to be here for compatibility.
# This argument is a noop.
def initialize(output, _maxwidth = nil, _newline = nil)
@output = Buffer.for(output)
@target = @output
@line_suffixes = Buffer::ArrayBuffer.new
end

# Flushes the line suffixes onto the output buffer.
def flush
line_suffixes.output.each { |doc| output << doc }
end

# --------------------------------------------------------------------------
# Markers node builders
# --------------------------------------------------------------------------

# Appends +separator+ to the text to be output. By default +separator+ is
# ' '
#
# The +width+, +indent+, and +force+ arguments are here for compatibility.
# They are all noop arguments.
def breakable(
separator = " ",
_width = separator.length,
indent: nil,
force: nil
)
target << separator
end

# Here for compatibility, does nothing.
def break_parent
end

# Appends +separator+ to the output buffer. +width+ is a noop here for
# compatibility.
def fill_breakable(separator = " ", _width = separator.length)
target << separator
end

# Immediately trims the output buffer.
def trim
target.trim!
end

# --------------------------------------------------------------------------
# Container node builders
# --------------------------------------------------------------------------

# Opens a block for grouping objects to be pretty printed.
#
# Arguments:
# * +indent+ - noop argument. Present for compatibility.
# * +open_obj+ - text appended before the &block. Default is ''
# * +close_obj+ - text appended after the &block. Default is ''
# * +open_width+ - noop argument. Present for compatibility.
# * +close_width+ - noop argument. Present for compatibility.
def group(
_indent = nil,
open_object = "",
close_object = "",
_open_width = nil,
_close_width = nil
)
target << open_object
yield
target << close_object
end

# A class that wraps the ability to call #if_flat. The contents of the
# #if_flat block are executed immediately, so effectively this class and the
# #if_break method that triggers it are unnecessary, but they're here to
# maintain compatibility.
class IfBreakBuilder
def if_flat
yield
end
end

# Effectively unnecessary, but here for compatibility.
def if_break
IfBreakBuilder.new
end

# Also effectively unnecessary, but here for compatibility.
def if_flat
end

# A noop that immediately yields.
def indent
yield
end

# Changes the target output buffer to the line suffix output buffer which
# will get flushed at the end of printing.
def line_suffix
previous_target, @target = @target, line_suffixes
yield
@target = previous_target
end

# Takes +indent+ arg, but does nothing with it.
#
# Yields to a block.
def nest(_indent)
yield
end

# Add +object+ to the text to be output.
#
# +width+ argument is here for compatibility. It is a noop argument.
def text(object = "", _width = nil)
target << object
end
end

# When printing, you can optionally specify the value that should be used
# whenever a group needs to be broken onto multiple lines. In this case the
# default is \n.
Expand All @@ -493,6 +348,7 @@ def text(object = "", _width = nil)
# behavior (for instance to use tabs) by passing a different genspace
# procedure.
DEFAULT_GENSPACE = ->(n) { " " * n }
Ractor.make_shareable(DEFAULT_GENSPACE) if defined?(Ractor)

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

# This is similar to PrettierPrint::format but the result has no breaks.
#
# +maxwidth+, +newline+ and +genspace+ are ignored.
#
# The invocation of +breakable+ in the block doesn't break a line and is
# treated as just an invocation of +text+.
#
def self.singleline_format(
output = +"",
_maxwidth = nil,
_newline = nil,
_genspace = nil
)
q = SingleLine.new(output)
yield q
output
end

# The output object. It represents the final destination of the contents of
# the print tree. It should respond to <<.
#
Expand Down Expand Up @@ -1251,3 +1089,5 @@ def remove_breaks_with(doc, replace)
end
end
end

require_relative "prettier_print/single_line"
Loading