Skip to content

Commit

Permalink
fix: for statement format
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Nov 22, 2023
1 parent c862b19 commit 212ebca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
12 changes: 10 additions & 2 deletions wasm/src/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,10 @@ std::string Formatter::Format(std::string_view content) { // NOLINT
break;
case TokenKind::TK_SEMICOLON: { // ;
AppendNoSpace(token);
PipeBeforeNewLine(&lexer, PipeType::kComment);
NewLineActively();
if (!IsInFor()) {
PipeBeforeNewLine(&lexer, PipeType::kComment);
NewLineActively();
}
break;
}
case TokenKind::TK_WHITESPACE: {
Expand Down Expand Up @@ -330,6 +332,12 @@ bool Formatter::IsNewLine() const {
return new_line_type_ != NewLineType::kNone;
}

bool Formatter::IsInFor() const {
return std::ranges::any_of(line_tokens_, [this](const auto& token) {
return content_.substr(token.fOffset, token.fLength) == "for";
});
}

SkSL::Token Formatter::GetLastToken() const {
for (auto iter = line_tokens_.rbegin(); iter != line_tokens_.rend(); ++iter) { // NOLINT
switch (iter->fKind) {
Expand Down
1 change: 1 addition & 0 deletions wasm/src/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Formatter {
bool PipeBeforeNewLine(SkSL::Lexer* lexer, std::uint8_t type);

[[nodiscard]] bool IsNewLine() const;
[[nodiscard]] bool IsInFor() const;
[[nodiscard]] SkSL::Token GetLastToken() const;
[[nodiscard]] SkSL::Token GetLastMaybeEmptyToken() const;
[[nodiscard]] SkSL::Token GetSecondLastToken() const;
Expand Down
4 changes: 4 additions & 0 deletions wasm/src/formatter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ TEST(FormatterTest, Layout) {
"layout(color) uniform vec4 in_color;\n"
);
}

TEST(FormatterTest, For) {
EXPECT_STREQ(Formatter().Format("for(1;1;1)").c_str(), "for (1; 1; 1)\n");
}

0 comments on commit 212ebca

Please sign in to comment.