Skip to content

Commit 7b94e88

Browse files
committed
support space_before_inline_comment = keep
1 parent 9eecc85 commit 7b94e88

File tree

8 files changed

+57
-8
lines changed

8 files changed

+57
-8
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class LuaStyle {
7676

7777
bool ignore_spaces_inside_function_call = false;
7878

79-
std::size_t space_before_inline_comment = 1;
79+
SpaceBeforeInlineComment space_before_inline_comment = SpaceBeforeInlineComment();
8080

8181
// [operator space]
8282
bool space_around_math_operator = true;

CodeFormatCore/include/CodeFormatCore/Config/LuaStyleEnum.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,17 @@ enum class SpaceAroundStyle {
106106
Always,
107107
// for no_space_asym
108108
NoSpaceAsym,
109+
};
110+
111+
enum class SpaceBeforeInlineCommentStyle {
112+
Fixed,
113+
Keep,
114+
};
115+
116+
struct SpaceBeforeInlineComment {
117+
explicit SpaceBeforeInlineComment(std::size_t space = 1, SpaceBeforeInlineCommentStyle style = SpaceBeforeInlineCommentStyle::Fixed)
118+
: Style(style), Space(space) {}
119+
120+
SpaceBeforeInlineCommentStyle Style;
121+
std::size_t Space;
109122
};

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
150150

151151
BOOL_OPTION(ignore_spaces_inside_function_call)
152152

153-
NUMBER_OPTION(space_before_inline_comment)
153+
if(configMap.count("space_before_inline_comment")) {
154+
auto &value = configMap.at("space_before_inline_comment");
155+
if (value == "keep") {
156+
space_before_inline_comment = SpaceBeforeInlineComment(0, SpaceBeforeInlineCommentStyle::Keep);
157+
} else if (IsNumber(value)) {
158+
space_before_inline_comment = SpaceBeforeInlineComment(std::stoi(value), SpaceBeforeInlineCommentStyle::Fixed);
159+
}
160+
}
154161

155162
BOOL_OPTION(space_around_math_operator)
156163

CodeFormatCore/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void AlignAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
7070
} else {
7171
switch (syntaxNode.GetTokenKind(t)) {
7272
case TK_SHORT_COMMENT: {
73-
if (f.GetStyle().align_continuous_inline_comment) {
73+
if (f.GetStyle().align_continuous_inline_comment && f.GetStyle().space_before_inline_comment.Style == SpaceBeforeInlineCommentStyle::Fixed) {
7474
AnalyzeInlineComment(f, syntaxNode, t);
7575
}
7676
break;
@@ -441,7 +441,7 @@ void AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, Al
441441
auto prev = comment.GetPrevToken(t);
442442
auto newPos =
443443
file.GetColumn(prev.GetTextRange(t).GetEndOffset()) +
444-
f.GetStyle().space_before_inline_comment +
444+
f.GetStyle().space_before_inline_comment.Space +
445445
1;
446446
if (newPos > maxDis) {
447447
maxDis = newPos;

CodeFormatCore/src/Format/Analyzer/SpaceAnalyzer.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,17 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
177177
}
178178
case TK_LONG_COMMENT:
179179
case TK_SHORT_COMMENT: {
180-
SpaceLeft(syntaxNode, t, f.GetStyle().space_before_inline_comment, SpacePriority::CommentFirst);
180+
if (f.GetStyle().space_before_inline_comment.Style == SpaceBeforeInlineCommentStyle::Fixed) {
181+
SpaceLeft(syntaxNode, t, f.GetStyle().space_before_inline_comment.Space, SpacePriority::CommentFirst);
182+
} else {
183+
auto prevToken = syntaxNode.GetPrevToken(t);
184+
if (prevToken.GetEndLine(t) == syntaxNode.GetStartLine(t)) {
185+
auto space = syntaxNode.GetStartCol(t) - prevToken.GetEndCol(t) - 1;
186+
SpaceLeft(syntaxNode, t, space, SpacePriority::CommentFirst);
187+
} else {
188+
SpaceLeft(syntaxNode, t, 0, SpacePriority::CommentFirst);
189+
}
190+
}
181191
SpaceRight(syntaxNode, t, 1);
182192
break;
183193
}

Test/src/FormatResult_unitest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ local t = {
10401040
TEST(Format, bug_131) {
10411041
LuaStyle style;
10421042

1043-
style.space_before_inline_comment = 2;
1043+
style.space_before_inline_comment = SpaceBeforeInlineComment(2);
10441044
style.align_continuous_inline_comment = false;
10451045
EXPECT_TRUE(TestHelper::TestFormatted(
10461046
R"(

Test/src/FormatStyle_unitest.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ map('k', 123, 456)
769769
TEST(FormatByStyleOption, space_before_inline_comment) {
770770
LuaStyle style;
771771

772-
style.space_before_inline_comment = 1;
772+
style.space_before_inline_comment = SpaceBeforeInlineComment(1);
773773
EXPECT_TRUE(TestHelper::TestFormatted(
774774
R"(
775775
local t = 123 --hello
@@ -778,7 +778,7 @@ local t = 123 --hello
778778
local t = 123 --hello
779779
)",
780780
style));
781-
style.space_before_inline_comment = 2;
781+
style.space_before_inline_comment = SpaceBeforeInlineComment(2);
782782
EXPECT_TRUE(TestHelper::TestFormatted(
783783
R"(
784784
local t = 123 --hello
@@ -787,6 +787,24 @@ local t = 123 --hello
787787
local t = 123 --hello
788788
)",
789789
style));
790+
style.space_before_inline_comment = SpaceBeforeInlineComment(0, SpaceBeforeInlineCommentStyle::Keep);
791+
EXPECT_TRUE(TestHelper::TestFormatted(
792+
R"(
793+
local function fff() --123131
794+
local t = "hello world", 1 --12313131
795+
end
796+
local t = hello --fjoiwjfowifw
797+
yes.it.is.me.a = 123 --fjoiwjfowifw
798+
)",
799+
R"(
800+
local function fff() --123131
801+
local t = "hello world", 1 --12313131
802+
end
803+
local t = hello --fjoiwjfowifw
804+
yes.it.is.me.a = 123 --fjoiwjfowifw
805+
)",
806+
style));
807+
790808
}
791809

792810
TEST(FormatByStyleOption, space_around_math_operator) {

lua.template.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ space_around_table_append_operator = false
6464

6565
ignore_spaces_inside_function_call = false
6666

67+
# detail number or 'keep'
6768
space_before_inline_comment = 1
6869

6970
# [operator space]

0 commit comments

Comments
 (0)