Skip to content

Commit

Permalink
Adds formatting for backtick strings (ruby-formatter#2)
Browse files Browse the repository at this point in the history
* feat(backtick_strings): Format backtick_string & %x()

* chore(NewFormatter): clean up

Just a few bits of cleanup for consistency.
  • Loading branch information
bweave authored and Daniel Ma committed Aug 14, 2017
1 parent 32ab281 commit 6a24794
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
29 changes: 22 additions & 7 deletions lib/rufo/new_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(code, **options)
unless @sexp
raise ::Rufo::SyntaxError.new
end

@indent_size = 2
@line_length = options.fetch(:line_length, 80)

Expand Down Expand Up @@ -53,7 +53,7 @@ def visit(node)
#
# [:program, exps]
visit_exps node[1] #, with_indent: true
when :string_literal
when :string_literal, :xstring_literal
visit_string_literal(node)
when :string_content
# [:string_content, exp]
Expand Down Expand Up @@ -266,13 +266,28 @@ def visit_begin(node)

def visit_string_literal(node)
# [:string_literal, [:string_content, exps]]
consume_token :on_tstring_beg

inner = node[1..-1]

case current_token_kind
when :on_backtick
consume_token :on_backtick
else
consume_token :on_tstring_beg
end

visit_string_literal_end(node)
end

def visit_string_literal_end(node)
# [:string_literal, [:string_content, exps]]
inner = node[1]
inner = inner[1..-1] unless node[0] == :xstring_literal
visit_exps(inner, with_lines: false)

consume_token :on_tstring_end
case current_token_kind
when :on_backtick
consume_token :on_backtick
else
consume_token :on_tstring_end
end
end

def visit_string_interpolation(node)
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/rufo/formatter_source_specs/backtick_strings.rb.spec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#~# ORIGINAL
#~# ORIGINAL backtick_strings

`cat meow`

#~# EXPECTED

`cat meow`

#~# ORIGINAL
#~# ORIGINAL %x()

%x( cat meow )

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/rufo/new_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def assert_format(code, expected)
assert_source_specs(source_specs) if File.file?(source_specs)
end

%w(array_literal hash_literal and_or_not assignment_operators assignments).each do |source_spec_name|
%w(array_literal hash_literal and_or_not assignment_operators assignments backtick_strings).each do |source_spec_name|
file = File.join(NEW_FORMATTER_FILE_PATH, "/formatter_source_specs/#{source_spec_name}.rb.spec")
fail "missing #{source_spec_name}" unless File.exist?(file)
assert_source_specs(file) if File.file?(file)
Expand Down

0 comments on commit 6a24794

Please sign in to comment.