Skip to content

Allow configuring the base indentation level #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/prettier_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ def self.for(output)
# a forced line, or the maximum width.
MODE_FLAT = 2

# The default indentation for printing is zero, assuming that the code starts
# at the top level. That can be changed if desired to start from a different
# indentation level.
DEFAULT_INDENTATION = 0

# This is a convenience method which is same as follows:
#
# begin
Expand All @@ -373,11 +378,12 @@ def self.format(
output = "".dup,
maxwidth = 80,
newline = DEFAULT_NEWLINE,
genspace = DEFAULT_GENSPACE
genspace = DEFAULT_GENSPACE,
indentation = DEFAULT_INDENTATION
)
q = new(output, maxwidth, newline, &genspace)
yield q
q.flush
q.flush(indentation)
output
end

Expand Down Expand Up @@ -481,17 +487,20 @@ def current_group

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

# This represents how far along the current line we are. It gets reset
# back to 0 when we encounter a newline.
position = 0
position = base_indentation

# Start the buffer with the base indentation level.
buffer << genspace.call(base_indentation) if base_indentation > 0

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

# This is a small optimization boolean. It keeps track of whether or not
# when we hit a group node we should check if it fits on the same line.
Expand Down
64 changes: 64 additions & 0 deletions test/prettyprint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,68 @@ def test_27

end

class DifferentIndentationLevelExample < Test::Unit::TestCase # :nodoc:
def format(indentation)
PrettierPrint.format(
''.dup,
2,
PrettierPrint::DEFAULT_NEWLINE,
PrettierPrint::DEFAULT_GENSPACE,
indentation
) {|q|
q.group {
q.text("[")

q.indent {
q.breakable_empty
q.text("1")
}

q.breakable_empty
q.text("]")
}
}.delete_prefix("\n")
end

def test_default
expected = <<'End'.chomp
[
1
]
End

assert_equal(expected, format(0))
end

def test_level_2
expected = <<'End'.chomp
[
1
]
End

assert_equal(expected, format(2))
end

def test_level_4
expected = <<'End'.chomp
[
1
]
End

assert_equal(expected, format(4))
end

def test_level_6
expected = <<'End'.chomp
[
1
]
End

assert_equal(expected, format(6))
end
end

end