Skip to content

Formatting #4

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 53 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
/pkg/
/spec/reports/
/tmp/

test.rb
9 changes: 5 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

gemspec

gem 'benchmark-ips'
gem 'parser'
gem 'ruby_parser'
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
19 changes: 19 additions & 0 deletions bin/profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/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

`bundle exec stackprof --d3-flamegraph tmp/profile.dump > tmp/flamegraph.html`
puts "open tmp/flamegraph.html"
83 changes: 81 additions & 2 deletions exe/stree
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative File.expand_path('../lib/syntax_tree', __dir__)
pp SyntaxTree.new(ARGF.read).parse
require_relative File.expand_path("../lib/syntax_tree", __dir__)

help = <<~EOF
stree MDOE FILE

MODE: one of "a", "ast", "d", "doc", "f", "format", "w", or "write"
FILE: one or more paths to files to parse
EOF

if ARGV.length < 2
warn(help)
exit(1)
end

module SyntaxTree::CLI
class AST
def run(filepath)
pp SyntaxTree.parse(File.read(filepath))
end
end

class Doc
def run(filepath)
formatter = SyntaxTree::Formatter.new([])
SyntaxTree.parse(File.read(filepath)).format(formatter)
pp formatter.groups.first
end
end

class Format
def run(filepath)
puts SyntaxTree.format(File.read(filepath))
end
end

class Write
def run(filepath)
File.write(filepath, SyntaxTree.format(File.read(filepath)))
end
end
end

mode =
case ARGV.shift
when "a", "ast"
SyntaxTree::CLI::AST.new
when "d", "doc"
SyntaxTree::CLI::Doc.new
when "f", "format"
SyntaxTree::CLI::Format.new
when "w", "write"
SyntaxTree::CLI::Write.new
else
warn(help)
exit(1)
end

queue = Queue.new
ARGV.each { |pattern| Dir[pattern].each { |filepath| queue << filepath } }

if queue.size <= 1
filepath = queue.shift
mode.run(filepath) if File.file?(filepath)
return
end

count = [8, queue.size].min
threads =
count.times.map do
Thread.new do
loop do
filepath = queue.shift
break if filepath == :exit

mode.run(filepath) if File.file?(filepath)
end
end
end

count.times { queue << :exit }
threads.each(&:join)
Loading