Skip to content

Commit f47d5d5

Browse files
committed
add option table_separator_style
1 parent bbf5833 commit f47d5d5

File tree

7 files changed

+278
-103
lines changed

7 files changed

+278
-103
lines changed

CodeFormatCore/include/CodeFormatCore/Config/LuaStyleEnum.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ enum class ContinuousAlign {
3131
enum class TableSeparatorStyle {
3232
None,
3333
Comma,
34-
Semicolon
34+
Semicolon,
35+
OnlyKVColon
3536
};
3637

3738
enum class TrailingTableSeparator {

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/FormatStrategy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ enum class TokenStrategy {
2929

3030
enum class TokenAddStrategy {
3131
None,
32-
TableAddSep,
32+
TableAddColon,
33+
TableAddComma
3334
};
3435

3536
enum class IndentStrategy {

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ void LuaStyle::ParseFromMap(std::map<std::string, std::string, std::less<>> &con
5353
table_separator_style = TableSeparatorStyle::Comma;
5454
} else if (style == "semicolon") {
5555
table_separator_style = TableSeparatorStyle::Semicolon;
56+
} else if (style == "only_kv_colon") {
57+
table_separator_style = TableSeparatorStyle::OnlyKVColon;
5658
}
5759
}
5860

CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,20 @@ void TokenAnalyzer::TableFieldAddSep(FormatState &f, LuaSyntaxNode n, const LuaS
9494
if (lastToken.GetTokenKind(t) == TK_SHORT_COMMENT || lastToken.GetTokenKind(t) == TK_LONG_COMMENT) {
9595
lastToken = lastToken.GetPrevToken(t);
9696
}
97-
MarkAdd(lastToken, t, TokenAddStrategy::TableAddSep);
97+
switch (f.GetStyle().table_separator_style) {
98+
case TableSeparatorStyle::Semicolon: {
99+
return MarkAdd(lastToken, t, TokenAddStrategy::TableAddColon);
100+
}
101+
case TableSeparatorStyle::OnlyKVColon: {
102+
if (n.GetChildToken('=', t).IsToken(t)) {
103+
return MarkAdd(lastToken, t, TokenAddStrategy::TableAddColon);
104+
}
105+
// fallthrough
106+
}
107+
default: {
108+
return MarkAdd(lastToken, t, TokenAddStrategy::TableAddComma);
109+
}
110+
}
98111
}
99112
}
100113

@@ -111,6 +124,20 @@ void TokenAnalyzer::AnalyzeTableField(FormatState &f, LuaSyntaxNode &syntaxNode,
111124
if (semicolon.IsToken(t)) {
112125
Mark(semicolon, t, TokenStrategy::TableSepComma);
113126
}
127+
} else if (f.GetStyle().table_separator_style == TableSeparatorStyle::OnlyKVColon) {
128+
if (syntaxNode.GetChildToken('=', t).IsToken(t)) {
129+
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
130+
auto semicolon = sep.GetChildToken(',', t);
131+
if (semicolon.IsToken(t)) {
132+
Mark(semicolon, t, TokenStrategy::TableSepSemicolon);
133+
}
134+
} else {
135+
auto sep = syntaxNode.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldSep, t);
136+
auto semicolon = sep.GetChildToken(';', t);
137+
if (semicolon.IsToken(t)) {
138+
Mark(semicolon, t, TokenStrategy::TableSepComma);
139+
}
140+
}
114141
}
115142

116143
if (f.GetStyle().trailing_table_separator != TrailingTableSeparator::Keep) {

CodeFormatCore/src/Format/FormatBuilder.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
145145
}
146146

147147
switch (resolve.GetTokenAddStrategy()) {
148-
case TokenAddStrategy::TableAddSep: {
149-
if (_state.GetStyle().table_separator_style == TableSeparatorStyle::Semicolon) {
150-
WriteChar(';');
151-
} else {
152-
WriteChar(',');
153-
}
148+
case TokenAddStrategy::TableAddComma: {
149+
WriteChar(',');
150+
break;
151+
}
152+
case TokenAddStrategy::TableAddColon: {
153+
WriteChar(';');
154154
break;
155155
}
156156
default: {

0 commit comments

Comments
 (0)