Skip to content

Commit

Permalink
Handle case where first token of a partition is multi-line.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 305098932
  • Loading branch information
fangism authored and hzeller committed Apr 7, 2020
1 parent d4f4ec6 commit 22f6630
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions common/formatting/state_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ int StateNode::_UpdateColumnPosition() {
}
// Penalize based on the column position that resulted in appending
// text up to the first newline.
return prev_state->current_column +
current_format_token.before.spaces_required + first_newline_pos;
if (IsRootState()) {
return first_newline_pos;
} else {
return prev_state->current_column +
current_format_token.before.spaces_required + first_newline_pos;
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions common/formatting/state_node_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,21 @@ TEST_F(StateNodeTestFixture, ConstructionAppendingPrevStateOverflow) {
}
}

// Tests that column positions account for multiline tokens.
TEST_F(StateNodeTestFixture, MultiLineTokenFront) {
const int kInitialIndent = 1;
const std::vector<TokenInfo> tokens = {{0, "a23456789\nb234"}};
Initialize(kInitialIndent, tokens);
auto& ftokens = pre_format_tokens_;
ftokens[0].before.spaces_required = 1;

// First token on line:
auto parent_state = std::make_shared<StateNode>(*uwline, style);
EXPECT_EQ(ABSL_DIE_IF_NULL(parent_state)->current_column,
4 /* length("b234") */);
EXPECT_EQ(parent_state->cumulative_cost, 0);
}

// Tests that newly calculated column positions account for multiline tokens.
TEST_F(StateNodeTestFixture, MultiLineToken) {
const int kInitialIndent = 1;
Expand Down
3 changes: 3 additions & 0 deletions verilog/formatting/formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ static const std::initializer_list<FormatterTestCase> kFormatterTestCases = {
{"", ""},
{"\n", "\n"},
{"\n\n", "\n\n"},
{"\t//comment\n", "//comment\n"},
{"\t/*comment*/\n", "/*comment*/\n"},
{"\t/*multi-line\ncomment*/\n", "/*multi-line\ncomment*/\n"},
// preprocessor test cases
{"`include \"path/to/file.vh\"\n", "`include \"path/to/file.vh\"\n"},
{"`include `\"path/to/file.vh`\"\n", "`include `\"path/to/file.vh`\"\n"},
Expand Down

0 comments on commit 22f6630

Please sign in to comment.