Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
29445cf
Ignore test.rb
kddnewton Nov 29, 2021
a0ca55f
Support for embedding comments in the tree
kddnewton Nov 29, 2021
4a9cecc
Formatting checkpoint
kddnewton Nov 19, 2021
1f6b2be
Tests for formatting
kddnewton Nov 19, 2021
66c05d8
prettyprint
kddnewton Nov 29, 2021
d70f5f2
Print comments surrounding the node
kddnewton Nov 29, 2021
f50f236
Get comments printing
kddnewton Nov 29, 2021
4a7d506
Update with test cases for comments
kddnewton Nov 29, 2021
25eefba
VarAlias tests
kddnewton Nov 29, 2021
00717fa
A bunch of tests for formatting keywords without arguments
kddnewton Nov 29, 2021
fdd718a
Tests for xstring_literal and yield
kddnewton Nov 29, 2021
43aed5c
Tests for special array literals
kddnewton Nov 29, 2021
e791625
Until, UntilMod, While, WhileMod tests
kddnewton Nov 29, 2021
d2318b2
IfMod and UnlessMod formatting
kddnewton Nov 29, 2021
1fbe689
Tests for various leaf nodes
kddnewton Nov 29, 2021
547d16f
Tests for SClass and StringConcat
kddnewton Nov 29, 2021
14bb1e3
Tests for ConstPathField, ConstPathRef, KwRestParam, RestParam, TopCo…
kddnewton Nov 29, 2021
24b0ff9
Tests for Undef
kddnewton Nov 30, 2021
76fbec6
Tests for Unary
kddnewton Nov 30, 2021
c878170
Tests for VarRef
kddnewton Nov 30, 2021
40a3251
Tests for VarField
kddnewton Nov 30, 2021
e88a6e2
Tests for Not, SymbolLiteral, VCall
kddnewton Nov 30, 2021
4de0091
Tests for Int, Module, MRHS, OpAssign, Program, StringDVar, and some …
kddnewton Nov 30, 2021
b2ec4bd
Tests for ArrayLiteral, BraceBlock, DoBlock, Hash, and IfOp
kddnewton Nov 30, 2021
b3701eb
Tests for Case and When
kddnewton Nov 30, 2021
7446c58
More tests for When
kddnewton Nov 30, 2021
a87cd06
Tests for RAssign
kddnewton Nov 30, 2021
81a604e
Tests for RAssign
kddnewton Nov 30, 2021
98aeacf
Tests for Command, Const, ConstRef, Def
kddnewton Nov 30, 2021
b076fe4
Tests for Defined, Dot2, Dot3, and ExcessedComma
kddnewton Nov 30, 2021
c8bdb1c
Better heredocs, tests for Heredoc, Defs, and DefEndless
kddnewton Nov 30, 2021
e562cc7
Tests for Elsif, EmbDoc, Params
kddnewton Nov 30, 2021
ee1f4ed
Tests for MLHSParen
kddnewton Nov 30, 2021
0ff165f
Tests for MLHS
kddnewton Nov 30, 2021
8bddece
Tests for Lambda and MAssign
kddnewton Nov 30, 2021
374611b
Tests for In and When
kddnewton Nov 30, 2021
73c584f
Tests for Break, FCall, Kw, Label, MethodAddArg, MethodAddBlock, Next…
kddnewton Nov 30, 2021
517d576
Tests for AccessCtrl, CommandCall, Else, Ensure, Field, FndPtn, For, …
kddnewton Nov 30, 2021
b9c355b
Tests for RegexpLiteral
kddnewton Nov 30, 2021
a926d56
Visitors to remove lines
kddnewton Dec 1, 2021
fa6c741
StringLiteral and DynaSymbol
kddnewton Dec 1, 2021
c139cb4
Add a better CLI that will format files
kddnewton Dec 1, 2021
257c5a5
Add a write mode for the CLI
kddnewton Dec 1, 2021
9f6d1f8
Format with stree
kddnewton Dec 1, 2021
ed2167b
Attach comments to statements as inline nodes directly
kddnewton Dec 1, 2021
41365d6
Fix up comment formatting
kddnewton Dec 1, 2021
30e9394
Format yourself
kddnewton Dec 1, 2021
f4682a1
Format the test suite
kddnewton Dec 1, 2021
7ab01a1
A bunch of these nodes do not need to go on the token list
kddnewton Dec 1, 2021
46d949a
Speed up
kddnewton Dec 1, 2021
29af9b7
Remove need for visitor
kddnewton Dec 1, 2021
410e7e3
Format everything
kddnewton Dec 1, 2021
613396e
Group stuff with nest
kddnewton Dec 1, 2021
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
Prev Previous commit
Next Next commit
Speed up
  • Loading branch information
kddnewton committed Dec 1, 2021
commit 46d949ab6ad59f911b5a992de4dcf843577bf5a4
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ gemspec
gem 'benchmark-ips'
gem 'parser'
gem 'ruby_parser'
gem 'stackprof'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
stackprof (0.2.17)

PLATFORMS
x86_64-darwin-19
Expand All @@ -35,6 +36,7 @@ DEPENDENCIES
rake
ruby_parser
simplecov
stackprof
syntax_tree!

BUNDLED WITH
Expand Down
16 changes: 16 additions & 0 deletions bin/profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'stackprof'

filepath = File.expand_path('../lib/syntax_tree', __dir__)
require_relative filepath

GC.disable

StackProf.run(mode: :cpu, out: "tmp/profile.dump", raw: true) do
SyntaxTree.format(File.read("#{filepath}.rb"))
end

GC.enable
64 changes: 32 additions & 32 deletions lib/syntax_tree/prettyprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,37 +540,6 @@ def indent(part = IndentPart)
end
end

# This is a visitor that can be passed to PrettyPrint.visit that will
# propagate BreakParent nodes all of the way up the tree. When a BreakParent
# is encountered, it will break the surrounding group, and then that group
# will break its parent, and so on.
class PropagateBreaksVisitor
attr_reader :groups, :visited

def initialize
@groups = []
@visited = []
end

def on_enter(doc)
case doc
when BreakParent
groups.last&.break
when Group
groups << doc
return false if visited.include?(doc)

visited << doc
end

true
end

def on_exit(doc)
groups.last&.break if doc.is_a?(Group) && groups.pop.break?
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 Down Expand Up @@ -751,7 +720,7 @@ def flush
# First, ensure that we've propagated all of the necessary break-parent
# nodes throughout the tree.
doc = groups.first
PrettyPrint.visit(doc, PropagateBreaksVisitor.new)
propagate_breaks(doc)

# This represents how far along the current line we are. It gets reset
# back to 0 when we encounter a newline.
Expand Down Expand Up @@ -1132,6 +1101,37 @@ def fits?(next_command, rest_commands, remaining)
false
end

# This will walk the doc tree and propagate BreakParent nodes all of the way
# up the tree. When a BreakParent is encountered, it will break the
# surrounding group, and then that group will break its parent, and so on.
def propagate_breaks(doc)
marker = Object.new
stack = [doc]
groups = []

while doc = stack.pop
if doc == marker
groups.last&.break if stack.pop.is_a?(Group) && groups.pop.break?
else
stack += [doc, marker]
enter = false

case doc
when BreakParent
groups.last&.break
when Group
groups << doc
stack += doc.contents.reverse
when IfBreak
stack += doc.break_contents.reverse
stack += doc.flat_contents.reverse
when Align, Indent, LineSuffix
stack += doc.contents.reverse
end
end
end
end

# Resets the group stack and target array so that this pretty printer object
# can continue to be used before calling flush again if desired.
def reset
Expand Down