Skip to content

Commit

Permalink
Use syntax tree formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 20, 2023
1 parent 2481870 commit 0bd9ea3
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 374 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ jobs:
bundler-cache: true
ruby-version: ${{ matrix.ruby }}
- name: Test
run: bundle exec rake test
run: |
bundle exec rake stree:check
bundle exec rake test
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ source "https://rubygems.org"
gemspec

gem "simplecov"
gem "syntax_tree"
gem "rake"
gem "test-unit"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
test-unit (3.6.1)
power_assert

Expand All @@ -28,6 +30,7 @@ DEPENDENCIES
prettier_print!
rake
simplecov
syntax_tree
test-unit

BUNDLED WITH
Expand Down
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

require "bundler/gem_tasks"
require "rake/testtask"
require "syntax_tree/rake_tasks"

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end

configure = ->(task) do
task.source_files =
FileList[%w[Gemfile Rakefile *.gemspec lib/**/*.rb test/**/*.rb]]
end

SyntaxTree::Rake::CheckTask.new(&configure)
SyntaxTree::Rake::WriteTask.new(&configure)

task default: :test
68 changes: 45 additions & 23 deletions lib/prettier_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,19 +529,26 @@ def flush(base_indentation = DEFAULT_INDENTATION)
when Group
if mode == MODE_FLAT && !should_remeasure
next_mode = doc.break? ? MODE_BREAK : MODE_FLAT
commands += doc.contents.reverse.map { |part| [indent, next_mode, part] }
commands +=
doc.contents.reverse.map { |part| [indent, next_mode, part] }
else
should_remeasure = false

if doc.break?
commands += doc.contents.reverse.map { |part| [indent, MODE_BREAK, part] }
commands +=
doc.contents.reverse.map { |part| [indent, MODE_BREAK, part] }
else
next_commands = doc.contents.reverse.map { |part| [indent, MODE_FLAT, part] }
next_commands =
doc.contents.reverse.map { |part| [indent, MODE_FLAT, part] }

if fits?(next_commands, commands, maxwidth - position)
commands += next_commands
else
commands += next_commands.map { |command| command[1] = MODE_BREAK; command }
commands +=
next_commands.map do |command|
command[1] = MODE_BREAK
command
end
end
end
end
Expand All @@ -566,9 +573,12 @@ def flush(base_indentation = DEFAULT_INDENTATION)
if line_suffixes.any?
commands << [indent, mode, doc]

line_suffixes.sort_by(&line_suffix_sort).each do |(indent, mode, doc)|
commands += doc.contents.reverse.map { |part| [indent, mode, part] }
end
line_suffixes
.sort_by(&line_suffix_sort)
.each do |(indent, mode, doc)|
commands +=
doc.contents.reverse.map { |part| [indent, mode, part] }
end

line_suffixes.clear
next
Expand All @@ -585,17 +595,21 @@ def flush(base_indentation = DEFAULT_INDENTATION)
end
when Indent
next_indent = indent + 2
commands += doc.contents.reverse.map { |part| [next_indent, mode, part] }
commands +=
doc.contents.reverse.map { |part| [next_indent, mode, part] }
when Align
next_indent = indent + doc.indent
commands += doc.contents.reverse.map { |part| [next_indent, mode, part] }
commands +=
doc.contents.reverse.map { |part| [next_indent, mode, part] }
when Trim
position -= buffer.trim!
when IfBreak
if mode == MODE_BREAK && doc.break_contents.any?
commands += doc.break_contents.reverse.map { |part| [indent, mode, part] }
commands +=
doc.break_contents.reverse.map { |part| [indent, mode, part] }
elsif mode == MODE_FLAT && doc.flat_contents.any?
commands += doc.flat_contents.reverse.map { |part| [indent, mode, part] }
commands +=
doc.flat_contents.reverse.map { |part| [indent, mode, part] }
end
when LineSuffix
line_suffixes << [indent, mode, doc]
Expand All @@ -616,9 +630,11 @@ def flush(base_indentation = DEFAULT_INDENTATION)
end

if commands.empty? && line_suffixes.any?
line_suffixes.sort_by(&line_suffix_sort).each do |(indent, mode, doc)|
commands += doc.contents.reverse.map { |part| [indent, mode, part] }
end
line_suffixes
.sort_by(&line_suffix_sort)
.each do |(indent, mode, doc)|
commands += doc.contents.reverse.map { |part| [indent, mode, part] }
end

line_suffixes.clear
end
Expand Down Expand Up @@ -757,9 +773,9 @@ def remove_breaks(node, replace = "; ")
# xxx 2
# q.comma_breakable
# xxx 3
def seplist(list, sep=nil, iter_method=:each) # :yield: element
def seplist(list, sep = nil, iter_method = :each) # :yield: element
first = true
list.__send__(iter_method) {|*v|
list.__send__(iter_method) do |*v|
if first
first = false
elsif sep
Expand All @@ -768,7 +784,7 @@ def seplist(list, sep=nil, iter_method=:each) # :yield: element
comma_breakable
end
RUBY_VERSION >= "3.0" ? yield(*v, **{}) : yield(*v)
}
end
end

# ----------------------------------------------------------------------------
Expand Down Expand Up @@ -918,7 +934,8 @@ def if_break
break_contents = []
flat_contents = []

doc = IfBreak.new(break_contents: break_contents, flat_contents: flat_contents)
doc =
IfBreak.new(break_contents: break_contents, flat_contents: flat_contents)
target << doc

with_target(break_contents) { yield }
Expand Down Expand Up @@ -1048,7 +1065,8 @@ def fits?(next_commands, rest_commands, remaining)
remaining -= doc.length
when Group
next_mode = doc.break? ? MODE_BREAK : mode
commands += doc.contents.reverse.map { |part| [indent, next_mode, part] }
commands +=
doc.contents.reverse.map { |part| [indent, next_mode, part] }
when Breakable
if mode == MODE_FLAT && !doc.force?
fit_buffer << doc.separator
Expand All @@ -1059,17 +1077,21 @@ def fits?(next_commands, rest_commands, remaining)
return true
when Indent
next_indent = indent + 2
commands += doc.contents.reverse.map { |part| [next_indent, mode, part] }
commands +=
doc.contents.reverse.map { |part| [next_indent, mode, part] }
when Align
next_indent = indent + doc.indent
commands += doc.contents.reverse.map { |part| [next_indent, mode, part] }
commands +=
doc.contents.reverse.map { |part| [next_indent, mode, part] }
when Trim
remaining += fit_buffer.trim!
when IfBreak
if mode == MODE_BREAK && doc.break_contents.any?
commands += doc.break_contents.reverse.map { |part| [indent, mode, part] }
commands +=
doc.break_contents.reverse.map { |part| [indent, mode, part] }
elsif mode == MODE_FLAT && doc.flat_contents.any?
commands += doc.flat_contents.reverse.map { |part| [indent, mode, part] }
commands +=
doc.flat_contents.reverse.map { |part| [indent, mode, part] }
end
when Text
doc.objects.each { |object| fit_buffer << object }
Expand Down
3 changes: 2 additions & 1 deletion prettier_print.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Gem::Specification.new do |spec|
spec.authors = ["Kevin Newton"]
spec.email = ["kddnewton@gmail.com"]

spec.summary = "A drop-in replacement for the prettyprint gem with more functionality."
spec.summary =
"A drop-in replacement for the prettyprint gem with more functionality."
spec.homepage = "https://github.com/ruby-syntax-tree/prettier_print"
spec.license = "MIT"
spec.metadata = { "rubygems_mfa_required" => "true" }
Expand Down
21 changes: 7 additions & 14 deletions test/prettier_print_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class PrettierPrintTest < Test::Unit::TestCase
end

test "Breakable#pretty_print force=true" do
assert_equal "breakable(force=true)\n", PP.pp(Breakable.new(force: true), +"")
assert_equal "breakable(force=true)\n",
PP.pp(Breakable.new(force: true), +"")
end

test "Breakable#pretty_print indent=false" do
assert_equal "breakable(indent=false)\n", PP.pp(Breakable.new(indent: false), +"")
assert_equal "breakable(indent=false)\n",
PP.pp(Breakable.new(indent: false), +"")
end

test "BreakParent#pretty_print" do
Expand Down Expand Up @@ -121,10 +123,7 @@ class PrettierPrintTest < Test::Unit::TestCase
end

test "PrettierPrint#if_flat" do
result =
PrettierPrint.format do |q|
q.if_flat { q.text("flat") }
end
result = PrettierPrint.format { |q| q.if_flat { q.text("flat") } }

assert_equal "flat", result
end
Expand Down Expand Up @@ -153,10 +152,7 @@ class PrettierPrintTest < Test::Unit::TestCase
end

test "PrettierPrint#line_suffix without a break" do
result =
PrettierPrint.format do |q|
q.line_suffix { q.text("# suffix") }
end
result = PrettierPrint.format { |q| q.line_suffix { q.text("# suffix") } }

assert_equal "# suffix", result
end
Expand All @@ -177,10 +173,7 @@ class PrettierPrintTest < Test::Unit::TestCase
end

test "PrettierPrint pushing strings" do
result =
PrettierPrint.format do |q|
q.target << "content"
end
result = PrettierPrint.format { |q| q.target << "content" }

assert_equal "content", result
end
Expand Down
Loading

0 comments on commit 0bd9ea3

Please sign in to comment.