Skip to content

Commit 7dd9395

Browse files
authored
Merge pull request #4 from vinistock/vs/allow_configuring_base_indentation_level
Allow configuring the base indentation level
2 parents e712bc7 + e862579 commit 7dd9395

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

lib/prettier_print.rb

Lines changed: 14 additions & 5 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,17 +487,20 @@ 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

488494
# This represents how far along the current line we are. It gets reset
489495
# back to 0 when we encounter a newline.
490-
position = 0
496+
position = base_indentation
497+
498+
# Start the buffer with the base indentation level.
499+
buffer << genspace.call(base_indentation) if base_indentation > 0
491500

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

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

test/prettyprint_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,68 @@ 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.text("[")
531+
532+
q.indent {
533+
q.breakable_empty
534+
q.text("1")
535+
}
536+
537+
q.breakable_empty
538+
q.text("]")
539+
}
540+
}.delete_prefix("\n")
541+
end
542+
543+
def test_default
544+
expected = <<'End'.chomp
545+
[
546+
1
547+
]
548+
End
549+
550+
assert_equal(expected, format(0))
551+
end
552+
553+
def test_level_2
554+
expected = <<'End'.chomp
555+
[
556+
1
557+
]
558+
End
559+
560+
assert_equal(expected, format(2))
561+
end
562+
563+
def test_level_4
564+
expected = <<'End'.chomp
565+
[
566+
1
567+
]
568+
End
569+
570+
assert_equal(expected, format(4))
571+
end
572+
573+
def test_level_6
574+
expected = <<'End'.chomp
575+
[
576+
1
577+
]
578+
End
579+
580+
assert_equal(expected, format(6))
581+
end
582+
end
583+
520584
end

0 commit comments

Comments
 (0)