From 189a1cb7f2f52fe46bf979251281f1719214509f Mon Sep 17 00:00:00 2001 From: Peter Tripp Date: Thu, 16 May 2024 10:40:26 -0400 Subject: [PATCH 1/2] Make long comment + short comment formatting stable. Fixes #170. --- .../src/Format/Analyzer/LineBreakAnalyzer.cpp | 9 +++++- Test/src/FormatResult_unitest.cpp | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp b/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp index 0c26ac8f..c8496be6 100644 --- a/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp +++ b/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp @@ -3,6 +3,7 @@ #include "CodeFormatCore/Format/FormatState.h" #include "LuaParser/Lexer/LuaTokenTypeDetail.h" #include +#include using NodeKind = LuaSyntaxNodeKind; @@ -107,8 +108,14 @@ void LineBreakAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) { } else { switch (stmt.GetTokenKind(t)) { - case TK_SHORT_COMMENT: case TK_LONG_COMMENT: + { + auto nextToken = stmt.GetNextToken(t).GetTokenKind(t); + if (nextToken == TK_SHORT_COMMENT) { + break; + } + } + case TK_SHORT_COMMENT: case TK_SHEBANG: { BreakAfter(stmt, t, style.line_space_after_comment); break; diff --git a/Test/src/FormatResult_unitest.cpp b/Test/src/FormatResult_unitest.cpp index 712dbd83..8018b28b 100644 --- a/Test/src/FormatResult_unitest.cpp +++ b/Test/src/FormatResult_unitest.cpp @@ -1147,3 +1147,31 @@ only_latin = { } )", style)); } + +TEST(Format, issue_170) { + EXPECT_TRUE(TestHelper::TestFormatted( + R"( +--[[]] +)", + R"( +--[[]] +)")); + EXPECT_TRUE(TestHelper::TestFormatted( + R"( +--[[]]-- +)", + R"( +--[[]] -- +)")); + EXPECT_TRUE(TestHelper::TestFormatted( + R"( +--[[----------- + +]]------------- +)", + R"( +--[[----------- + +]] ------------- +)")); +} From eb59d99cd68bb71c4fec200f165f0f524f750f56 Mon Sep 17 00:00:00 2001 From: Peter Tripp Date: Fri, 17 May 2024 10:51:18 -0400 Subject: [PATCH 2/2] Fixup. --- CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp b/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp index c8496be6..9bb668a0 100644 --- a/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp +++ b/CodeFormatCore/src/Format/Analyzer/LineBreakAnalyzer.cpp @@ -3,7 +3,6 @@ #include "CodeFormatCore/Format/FormatState.h" #include "LuaParser/Lexer/LuaTokenTypeDetail.h" #include -#include using NodeKind = LuaSyntaxNodeKind; @@ -114,6 +113,7 @@ void LineBreakAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) { if (nextToken == TK_SHORT_COMMENT) { break; } + // Fall through } case TK_SHORT_COMMENT: case TK_SHEBANG: {