Skip to content

Commit 1d5cccc

Browse files
committed
FIX #156
1 parent ebcd76a commit 1d5cccc

File tree

9 files changed

+54
-17
lines changed

9 files changed

+54
-17
lines changed

CodeFormatCore/include/CodeFormatCore/Config/EditorconfigPattern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class EditorconfigPattern {
2323
};
2424

2525
struct MatchData {
26-
MatchData(MatchType type)
26+
explicit MatchData(MatchType type)
2727
: Type(type) {}
2828

2929
MatchType Type;

CodeFormatCore/include/CodeFormatCore/Config/LuaEditorConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LuaEditorConfig {
2020

2121
static std::shared_ptr<LuaEditorConfig> LoadFromFile(const std::string &path);
2222

23-
LuaEditorConfig(std::string &&source);
23+
explicit LuaEditorConfig(std::string &&source);
2424

2525
void Parse();
2626

CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "LuaParser/Types/EndOfLineType.h"
44
#include "LuaStyleEnum.h"
5+
#include "LuaStyleStruct.h"
56
#include <map>
67
#include <memory>
78
#include <string>
@@ -31,7 +32,7 @@ class LuaStyle {
3132

3233
QuoteStyle quote_style = QuoteStyle::None;
3334

34-
std::size_t continuation_indent = 4;
35+
ContinuationIndent continuation_indent;
3536

3637
TableSeparatorStyle table_separator_style = TableSeparatorStyle::Comma;
3738

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
struct ContinuationIndent {
4+
std::size_t in_expr = 4;
5+
std::size_t before_block = 4;
6+
std::size_t in_table = 4;
7+
8+
void SetAll(std::size_t value) {
9+
in_expr = value;
10+
before_block = value;
11+
in_table = value;
12+
}
13+
};

CodeFormatCore/src/Config/LuaEditorConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void LuaEditorConfig::Parse() {
2828

2929
std::regex comment = std::regex(R"(^\s*(;|#))");
3030
std::regex luaSection = std::regex(R"(^\s*\[\s*([^\]]+)\s*\]\s*$)");
31-
std::regex valueRegex = std::regex(R"(^\s*([\w\d_]+)\s*=\s*(.+)$)");
31+
std::regex valueRegex = std::regex(R"(^\s*([\w\d_\\.]+)\s*=\s*(.+)$)");
3232
bool sectionFounded = false;
3333

3434
for (auto &lineView: lines) {

CodeFormatCore/src/Config/LuaStyle.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,26 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
108108
}
109109
}
110110

111-
NUMBER_OPTION(continuation_indent)
111+
112+
if (configMap.count("continuation_indent") && IsNumber(configMap.at("continuation_indent"))) {
113+
auto value = std::stoull(configMap.at("continuation_indent"));
114+
continuation_indent.SetAll(value);
115+
}
116+
117+
if (configMap.count("continuation_indent.before_block") && IsNumber(configMap.at("continuation_indent.before_block"))) {
118+
auto value = std::stoull(configMap.at("continuation_indent.before_block"));
119+
continuation_indent.before_block = value;
120+
}
121+
122+
if (configMap.count("continuation_indent.in_expr") && IsNumber(configMap.at("continuation_indent.in_expr"))) {
123+
auto value = std::stoull(configMap.at("continuation_indent.in_expr"));
124+
continuation_indent.in_expr = value;
125+
}
126+
127+
if (configMap.count("continuation_indent.in_table") && IsNumber(configMap.at("continuation_indent.in_table"))) {
128+
auto value = std::stoull(configMap.at("continuation_indent.in_table"));
129+
continuation_indent.in_table = value;
130+
}
112131

113132
BOOL_OPTION(detect_end_of_line)
114133

@@ -150,7 +169,7 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
150169

151170
BOOL_OPTION(ignore_spaces_inside_function_call)
152171

153-
if(configMap.count("space_before_inline_comment")) {
172+
if (configMap.count("space_before_inline_comment")) {
154173
auto &value = configMap.at("space_before_inline_comment");
155174
if (value == "keep") {
156175
space_before_inline_comment = SpaceBeforeInlineComment(0, SpaceBeforeInlineCommentStyle::Keep);

CodeFormatCore/src/Format/Analyzer/IndentationAnalyzer.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
5151
break;
5252
}
5353
case LuaSyntaxNodeKind::ParamList: {
54-
AddIndenter(syntaxNode, t);
54+
AddIndenter(syntaxNode, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent.before_block));
5555
break;
5656
}
5757
case LuaSyntaxNodeKind::CallExpression: {
@@ -92,7 +92,7 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
9292
if (!f.GetStyle().never_indent_before_if_condition) {
9393
auto exprs = syntaxNode.GetChildSyntaxNodes(MultiKind::Expression, t);
9494
for (auto expr: exprs) {
95-
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent));
95+
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent.before_block));
9696
}
9797
}
9898
break;
@@ -102,11 +102,11 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
102102

103103
for (auto expr: suffixedExpression.GetChildren(t)) {
104104
if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::IndexExpression) {
105-
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent));
105+
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent.in_expr));
106106
} else if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::CallExpression) {
107107
auto prevSibling = expr.GetPrevSibling(t);
108108
if (prevSibling.GetSyntaxKind(t) != LuaSyntaxNodeKind::NameExpression) {
109-
AddIndenter(expr, t, IndentData(IndentType::WhenPrevIndent, f.GetStyle().continuation_indent));
109+
AddIndenter(expr, t, IndentData(IndentType::WhenPrevIndent, f.GetStyle().continuation_indent.in_expr));
110110
}
111111
}
112112
}
@@ -131,11 +131,11 @@ void IndentationAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
131131

132132
for (auto expr: suffixedExpression.GetChildren(t)) {
133133
if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::IndexExpression) {
134-
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent));
134+
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent.in_table));
135135
} else if (expr.GetSyntaxKind(t) == LuaSyntaxNodeKind::CallExpression) {
136136
auto prevSibling = expr.GetPrevSibling(t);
137137
if (prevSibling.GetSyntaxKind(t) != LuaSyntaxNodeKind::NameExpression) {
138-
AddIndenter(expr, t, IndentData(IndentType::WhenPrevIndent, f.GetStyle().continuation_indent));
138+
AddIndenter(expr, t, IndentData(IndentType::WhenPrevIndent, f.GetStyle().continuation_indent.in_table));
139139
}
140140
}
141141
}
@@ -245,10 +245,10 @@ void IndentationAnalyzer::AnalyzeExprList(FormatState &f, LuaSyntaxNode &exprLis
245245
}
246246

247247
if (shouldIndent) {
248-
AddIndenter(exprList, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent));
248+
AddIndenter(exprList, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent.in_expr));
249249
} else {
250250
std::vector<LuaSyntaxNode> group = exprList.GetChildren(t);
251-
AddLinebreakGroup(exprList, group, t, f.GetStyle().continuation_indent);
251+
AddLinebreakGroup(exprList, group, t, f.GetStyle().continuation_indent.in_expr);
252252
}
253253
}
254254

@@ -331,7 +331,7 @@ void IndentationAnalyzer::AnalyzeTableFieldKeyValuePairExpr(FormatState &f, LuaS
331331
}
332332

333333
if (IsExprShouldIndent(expr, t)) {
334-
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent));
334+
AddIndenter(expr, t, IndentData(IndentType::Standard, f.GetStyle().continuation_indent.in_expr));
335335
}
336336
}
337337

Test/src/FormatStyle_unitest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ local t = 'aaa' .. 'bbb' .. '\"\"' .. [[123]] .. '""'
155155
TEST(FormatByStyleOption, continuation_indent) {
156156
LuaStyle style;
157157

158-
style.continuation_indent = 4;
158+
style.continuation_indent.SetAll(4);
159159
EXPECT_TRUE(TestHelper::TestFormatted(
160160
R"(
161161
if a
@@ -176,7 +176,7 @@ table.insert(t, {
176176
})
177177
)",
178178
style));
179-
style.continuation_indent = 8;
179+
style.continuation_indent.SetAll(8);
180180
EXPECT_TRUE(TestHelper::TestFormatted(
181181
R"(
182182
if a

lua.template.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ tab_width = 4
1313
quote_style = none
1414

1515
continuation_indent = 4
16+
## extend option
17+
# continuation_indent.before_block = 4
18+
# continuation_indent.in_expr = 4
19+
# continuation_indent.in_table = 4
1620

1721
# this mean utf8 length , if this is 'unset' then the line width is no longer checked
1822
# this option decides when to chopdown the code

0 commit comments

Comments
 (0)