Skip to content

Commit 0b53ea1

Browse files
authored
Merge pull request #199 from vinistock/vs/allow_formatting_with_a_different_base_level
Allow formatting code with a different base level of indentation
2 parents 73761bb + 83675f9 commit 0b53ea1

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PATH
22
remote: .
33
specs:
44
syntax_tree (5.0.1)
5-
prettier_print (>= 1.1.0)
5+
prettier_print (>= 1.2.0)
66

77
GEM
88
remote: https://rubygems.org/

lib/syntax_tree.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ module SyntaxTree
5757
# It shouldn't really be changed except in very niche circumstances.
5858
DEFAULT_RUBY_VERSION = Formatter::SemanticVersion.new(RUBY_VERSION).freeze
5959

60+
# The default indentation level for formatting. We allow changing this so
61+
# that Syntax Tree can format arbitrary parts of a document.
62+
DEFAULT_INDENTATION = 0
63+
6064
# This is a hook provided so that plugins can register themselves as the
6165
# handler for a particular file type.
6266
def self.register_handler(extension, handler)
@@ -74,12 +78,13 @@ def self.parse(source)
7478
def self.format(
7579
source,
7680
maxwidth = DEFAULT_PRINT_WIDTH,
81+
base_indentation = DEFAULT_INDENTATION,
7782
options: Formatter::Options.new
7883
)
7984
formatter = Formatter.new(source, [], maxwidth, options: options)
8085
parse(source).format(formatter)
8186

82-
formatter.flush
87+
formatter.flush(base_indentation)
8388
formatter.output.join
8489
end
8590

lib/syntax_tree/formatter.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ def initialize(source, *args, options: Options.new)
8484
@target_ruby_version = options.target_ruby_version
8585
end
8686

87-
def self.format(source, node)
87+
def self.format(source, node, base_indentation = 0)
8888
q = new(source, [])
8989
q.format(node)
90-
q.flush
90+
q.flush(base_indentation)
9191
q.output.join
9292
end
9393

syntax_tree.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
2525
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2626
spec.require_paths = %w[lib]
2727

28-
spec.add_dependency "prettier_print", ">= 1.1.0"
28+
spec.add_dependency "prettier_print", ">= 1.2.0"
2929

3030
spec.add_development_dependency "bundler"
3131
spec.add_development_dependency "minitest"

test/formatting_test.rb

+32
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,37 @@ def test_stree_ignore
2727

2828
assert_equal(source, SyntaxTree.format(source))
2929
end
30+
31+
def test_formatting_with_different_indentation_level
32+
source = <<~SOURCE
33+
def foo
34+
puts "a"
35+
end
36+
SOURCE
37+
38+
# Default indentation
39+
assert_equal(source, SyntaxTree.format(source))
40+
41+
# Level 2
42+
assert_equal(<<-EXPECTED.chomp, SyntaxTree.format(source, 80, 2).rstrip)
43+
def foo
44+
puts "a"
45+
end
46+
EXPECTED
47+
48+
# Level 4
49+
assert_equal(<<-EXPECTED.chomp, SyntaxTree.format(source, 80, 4).rstrip)
50+
def foo
51+
puts "a"
52+
end
53+
EXPECTED
54+
55+
# Level 6
56+
assert_equal(<<-EXPECTED.chomp, SyntaxTree.format(source, 80, 6).rstrip)
57+
def foo
58+
puts "a"
59+
end
60+
EXPECTED
61+
end
3062
end
3163
end

0 commit comments

Comments
 (0)