Skip to content

Commit 9105c60

Browse files
committed
Allow configuring the base indentation level
1 parent e712bc7 commit 9105c60

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

lib/prettier_print.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ def self.for(output)
360360
# a forced line, or the maximum width.
361361
MODE_FLAT = 2
362362

363+
# The default indentation for printing is zero, assuming that the code starts
364+
# at the top level. That can be changed if desired to start from a different
365+
# indentation level.
366+
DEFAULT_INDENTATION = 0
367+
363368
# This is a convenience method which is same as follows:
364369
#
365370
# begin
@@ -373,11 +378,12 @@ def self.format(
373378
output = "".dup,
374379
maxwidth = 80,
375380
newline = DEFAULT_NEWLINE,
376-
genspace = DEFAULT_GENSPACE
381+
genspace = DEFAULT_GENSPACE,
382+
indentation = DEFAULT_INDENTATION
377383
)
378384
q = new(output, maxwidth, newline, &genspace)
379385
yield q
380-
q.flush
386+
q.flush(indentation)
381387
output
382388
end
383389

@@ -481,7 +487,7 @@ def current_group
481487

482488
# Flushes all of the generated print tree onto the output buffer, then clears
483489
# the generated tree from memory.
484-
def flush
490+
def flush(base_indentation = DEFAULT_INDENTATION)
485491
# First, get the root group, since we placed one at the top to begin with.
486492
doc = groups.first
487493

@@ -491,7 +497,7 @@ def flush
491497

492498
# This is our command stack. A command consists of a triplet of an
493499
# indentation level, the mode (break or flat), and a doc node.
494-
commands = [[0, MODE_BREAK, doc]]
500+
commands = [[base_indentation, MODE_BREAK, doc]]
495501

496502
# This is a small optimization boolean. It keeps track of whether or not
497503
# when we hit a group node we should check if it fits on the same line.

test/prettyprint_test.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,69 @@ def test_27
517517

518518
end
519519

520+
class DifferentIndentationLevelExample < Test::Unit::TestCase # :nodoc:
521+
def format(indentation)
522+
PrettierPrint.format(
523+
''.dup,
524+
2,
525+
PrettierPrint::DEFAULT_NEWLINE,
526+
PrettierPrint::DEFAULT_GENSPACE,
527+
indentation
528+
) {|q|
529+
q.group {
530+
q.breakable_empty
531+
q.text("[")
532+
533+
q.indent {
534+
q.breakable_empty
535+
q.text("1")
536+
}
537+
538+
q.breakable_empty
539+
q.text("]")
540+
}
541+
}.delete_prefix("\n")
542+
end
543+
544+
def test_default
545+
expected = <<'End'.chomp
546+
[
547+
1
548+
]
549+
End
550+
551+
assert_equal(expected, format(0))
552+
end
553+
554+
def test_level_2
555+
expected = <<'End'.chomp
556+
[
557+
1
558+
]
559+
End
560+
561+
assert_equal(expected, format(2))
562+
end
563+
564+
def test_level_4
565+
expected = <<'End'.chomp
566+
[
567+
1
568+
]
569+
End
570+
571+
assert_equal(expected, format(4))
572+
end
573+
574+
def test_level_6
575+
expected = <<'End'.chomp
576+
[
577+
1
578+
]
579+
End
580+
581+
assert_equal(expected, format(6))
582+
end
583+
end
584+
520585
end

0 commit comments

Comments
 (0)