Skip to content

Commit 9601839

Browse files
committed
Use prettier_print
1 parent 53a27bf commit 9601839

File tree

9 files changed

+18
-1282
lines changed

9 files changed

+18
-1282
lines changed

Diff for: Gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
source "https://rubygems.org"
44

55
gemspec
6-
7-
gem "rubocop"

Diff for: Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PATH
22
remote: .
33
specs:
44
syntax_tree (2.4.1)
5+
prettier_print
56

67
GEM
78
remote: https://rubygems.org/
@@ -12,6 +13,7 @@ GEM
1213
parallel (1.22.1)
1314
parser (3.1.2.0)
1415
ast (~> 2.4.1)
16+
prettier_print (0.1.0)
1517
rainbow (3.1.1)
1618
rake (13.0.6)
1719
regexp_parser (2.4.0)

Diff for: lib/syntax_tree.rb

+1-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "json"
44
require "pp"
5-
require "prettyprint"
5+
require "prettier_print"
66
require "ripper"
77
require "stringio"
88

@@ -16,30 +16,6 @@
1616
require_relative "syntax_tree/visitor/match_visitor"
1717
require_relative "syntax_tree/visitor/pretty_print_visitor"
1818

19-
# If PrettyPrint::Align isn't defined, then we haven't gotten the updated
20-
# version of prettyprint. In that case we'll define our own. This is going to
21-
# overwrite a bunch of methods, so silencing them as well.
22-
unless PrettyPrint.const_defined?(:Align)
23-
verbose = $VERBOSE
24-
$VERBOSE = nil
25-
26-
begin
27-
require_relative "syntax_tree/prettyprint"
28-
ensure
29-
$VERBOSE = verbose
30-
end
31-
end
32-
33-
# When PP is running, it expects that everything that interacts with it is going
34-
# to flow through PP.pp, since that's the main entry into the module from the
35-
# perspective of its uses in core Ruby. In doing so, it calls guard_inspect_key
36-
# at the top of the PP.pp method, which establishes some thread-local hashes to
37-
# check for cycles in the pretty printed tree. This means that if you want to
38-
# manually call pp on some object _before_ you have established these hashes,
39-
# you're going to break everything. So this call ensures that those hashes have
40-
# been set up before anything uses pp manually.
41-
PP.new(+"", 0).guard_inspect_key {}
42-
4319
# Syntax Tree is a suite of tools built on top of the internal CRuby parser. It
4420
# provides the ability to generate a syntax tree from source, as well as the
4521
# tools necessary to inspect and manipulate that syntax tree. It can be used to

Diff for: lib/syntax_tree/formatter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module SyntaxTree
44
# A slightly enhanced PP that knows how to format recursively including
55
# comments.
6-
class Formatter < PP
6+
class Formatter < PrettierPrint
77
COMMENT_PRIORITY = 1
88
HEREDOC_PRIORITY = 2
99

Diff for: lib/syntax_tree/node.rb

+9-91
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def to_json(*opts)
123123
end
124124

125125
def construct_keys
126-
PP.format(+"") { |q| Visitor::MatchVisitor.new(q).visit(self) }
126+
PrettierPrint.format(+"") { |q| Visitor::MatchVisitor.new(q).visit(self) }
127127
end
128128
end
129129

@@ -1666,52 +1666,6 @@ def format(q)
16661666
end
16671667
end
16681668

1669-
# This module will remove any breakables from the list of contents so that no
1670-
# newlines are present in the output.
1671-
module RemoveBreaks
1672-
class << self
1673-
def call(doc)
1674-
marker = Object.new
1675-
stack = [doc]
1676-
1677-
while stack.any?
1678-
doc = stack.pop
1679-
1680-
if doc == marker
1681-
stack.pop
1682-
next
1683-
end
1684-
1685-
stack += [doc, marker]
1686-
1687-
case doc
1688-
when PrettyPrint::Align, PrettyPrint::Indent, PrettyPrint::Group
1689-
doc.contents.map! { |child| remove_breaks(child) }
1690-
stack += doc.contents.reverse
1691-
when PrettyPrint::IfBreak
1692-
doc.flat_contents.map! { |child| remove_breaks(child) }
1693-
stack += doc.flat_contents.reverse
1694-
end
1695-
end
1696-
end
1697-
1698-
private
1699-
1700-
def remove_breaks(doc)
1701-
case doc
1702-
when PrettyPrint::Breakable
1703-
text = PrettyPrint::Text.new
1704-
text.add(object: doc.force? ? "; " : doc.separator, width: doc.width)
1705-
text
1706-
when PrettyPrint::IfBreak
1707-
PrettyPrint::Align.new(indent: 0, contents: doc.flat_contents)
1708-
else
1709-
doc
1710-
end
1711-
end
1712-
end
1713-
end
1714-
17151669
# BlockVar represents the parameters being declared for a block. Effectively
17161670
# this node is everything contained within the pipes. This includes all of the
17171671
# various parameter types, as well as block-local variable declarations.
@@ -1752,8 +1706,7 @@ def deconstruct_keys(_keys)
17521706

17531707
def format(q)
17541708
q.group(0, "|", "|") do
1755-
doc = q.format(params)
1756-
RemoveBreaks.call(doc)
1709+
q.remove_breaks(q.format(params))
17571710

17581711
if locals.any?
17591712
q.text("; ")
@@ -3096,31 +3049,6 @@ def format(q)
30963049

30973050
private
30983051

3099-
# This is a somewhat naive method that is attempting to sum up the width of
3100-
# the doc nodes that make up the given doc node. This is used to align
3101-
# content.
3102-
def doc_width(parent)
3103-
queue = [parent]
3104-
width = 0
3105-
3106-
until queue.empty?
3107-
doc = queue.shift
3108-
3109-
case doc
3110-
when PrettyPrint::Text
3111-
width += doc.width
3112-
when PrettyPrint::Indent, PrettyPrint::Align, PrettyPrint::Group
3113-
queue = doc.contents + queue
3114-
when PrettyPrint::IfBreak
3115-
queue = doc.break_contents + queue
3116-
when PrettyPrint::Breakable
3117-
width = 0
3118-
end
3119-
end
3120-
3121-
width
3122-
end
3123-
31243052
def argument_alignment(q, doc)
31253053
# Very special handling case for rspec matchers. In general with rspec
31263054
# matchers you expect to see something like:
@@ -3138,7 +3066,7 @@ def argument_alignment(q, doc)
31383066
if %w[to not_to to_not].include?(message.value)
31393067
0
31403068
else
3141-
width = doc_width(doc) + 1
3069+
width = q.last_position(doc) + 1
31423070
width > (q.maxwidth / 2) ? 0 : width
31433071
end
31443072
end
@@ -4891,17 +4819,9 @@ def deconstruct_keys(_keys)
48914819
end
48924820

48934821
def format(q)
4894-
# This is a very specific behavior that should probably be included in the
4895-
# prettyprint module. It's when you want to force a newline, but don't
4896-
# want to force the break parent.
4897-
breakable = -> do
4898-
q.target << PrettyPrint::Breakable.new(
4899-
" ",
4900-
1,
4901-
indent: false,
4902-
force: true
4903-
)
4904-
end
4822+
# This is a very specific behavior where you want to force a newline, but
4823+
# don't want to force the break parent.
4824+
breakable = -> { q.breakable(indent: false, force: :skip_break_parent) }
49054825

49064826
q.group do
49074827
q.format(beginning)
@@ -5325,9 +5245,8 @@ def format_ternary(q)
53255245
# force it into the output but we _don't_ want to explicitly
53265246
# break the parent. If a break-parent shows up in the tree, then
53275247
# it's going to force it all the way up to the tree, which is
5328-
# going to negate the ternary. Maybe this should be an option in
5329-
# prettyprint? As in force: :no_break_parent or something.
5330-
q.target << PrettyPrint::Breakable.new(" ", 1, force: true)
5248+
# going to negate the ternary.
5249+
q.breakable(force: :skip_break_parent)
53315250
q.format(node.consequent.statements)
53325251
end
53335252
end
@@ -8314,8 +8233,7 @@ def format(q)
83148233
# same line in the source, then we're going to leave them in place and
83158234
# assume that's the way the developer wanted this expression
83168235
# represented.
8317-
doc = q.group(0, '#{', "}") { q.format(statements) }
8318-
RemoveBreaks.call(doc)
8236+
q.remove_breaks(q.group(0, '#{', "}") { q.format(statements) })
83198237
else
83208238
q.group do
83218239
q.text('#{')

0 commit comments

Comments
 (0)