Skip to content

Commit 1e806c0

Browse files
committed
完善align_if_branch
1 parent 72a6e8d commit 1e806c0

File tree

7 files changed

+114
-58
lines changed

7 files changed

+114
-58
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int type_format(lua_State *L) {
173173
auto line = lua_tointeger(L, 3);
174174
auto character = lua_tointeger(L, 4);
175175

176-
if(line < 0 || character < 0){
176+
if (line < 0 || character < 0) {
177177
lua_pushboolean(L, false);
178178
lua_pushstring(L, "line or character param error");
179179
return 2;
@@ -454,33 +454,17 @@ int set_default_config(lua_State *L) {
454454
int set_nonstandard_symbol(lua_State *L) {
455455
int top = lua_gettop(L);
456456

457-
if (top != 2) {
458-
return 0;
457+
try {
458+
LuaCodeFormat::GetInstance().SupportNonStandardSymbol();
459+
lua_pushboolean(L, true);
460+
return 1;
459461
}
460-
461-
if (lua_isstring(L, 1) && lua_istable(L, 2)) {
462-
try {
463-
std::string type = lua_tostring(L, 1);
464-
std::vector<std::string> tokens;
465-
lua_pushnil(L);
466-
while (lua_next(L, -2) != 0) {
467-
auto value = luaToString(L, -1);
468-
tokens.push_back(value);
469-
lua_pop(L, 1);
470-
}
471-
lua_settop(L, top);
472-
473-
LuaCodeFormat::GetInstance().SetSupportNonStandardSymbol(type, tokens);
474-
lua_pushboolean(L, true);
475-
return 1;
476-
}
477-
catch (std::exception &e) {
478-
std::string err = e.what();
479-
lua_settop(L, top);
480-
lua_pushboolean(L, false);
481-
lua_pushlstring(L, err.c_str(), err.size());
482-
return 2;
483-
}
462+
catch (std::exception &e) {
463+
std::string err = e.what();
464+
lua_settop(L, top);
465+
lua_pushboolean(L, false);
466+
lua_pushlstring(L, err.c_str(), err.size());
467+
return 2;
484468
}
485469

486470
return 0;

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ LuaCodeFormat &LuaCodeFormat::GetInstance() {
1010
return instance;
1111
}
1212

13-
LuaCodeFormat::LuaCodeFormat() {
13+
LuaCodeFormat::LuaCodeFormat()
14+
: _supportNonStandardSymbol(false) {
1415
}
1516

1617
void LuaCodeFormat::UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath) {
@@ -46,10 +47,8 @@ void LuaCodeFormat::SetDefaultCodeStyle(ConfigMap &configMap) {
4647
}
4748
}
4849

49-
void LuaCodeFormat::SetSupportNonStandardSymbol(const std::string &tokenType, const std::vector<std::string> &tokens) {
50-
// if (tokenType.size() == 1) {
51-
// _customParser->SetTokens(tokenType.front(), tokens);
52-
// }
50+
void LuaCodeFormat::SupportNonStandardSymbol() {
51+
_supportNonStandardSymbol = true;
5352
}
5453

5554
void LuaCodeFormat::LoadSpellDictionary(const std::string &path) {
@@ -72,6 +71,8 @@ std::string LuaCodeFormat::Reformat(const std::string &uri, std::string &&text,
7271
t.BuildTree(p);
7372

7473
LuaStyle style = GetStyle(uri);
74+
CalculateTempStyle(style, configMap);
75+
7576
FormatBuilder f(style);
7677

7778
return f.GetFormatResult(t);
@@ -91,6 +92,8 @@ std::string LuaCodeFormat::RangeFormat(const std::string &uri, FormatRange &rang
9192
t.BuildTree(p);
9293

9394
LuaStyle style = GetStyle(uri);
95+
CalculateTempStyle(style, configMap);
96+
9497
RangeFormatBuilder f(style, range);
9598

9699
auto formattedText = f.GetFormatResult(t);
@@ -112,8 +115,10 @@ LuaCodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t
112115
t.BuildTree(p);
113116

114117
LuaStyle style = GetStyle(uri);
118+
CalculateTempStyle(style, configMap);
119+
120+
LuaTypeFormatOptions typeFormatOptions = LuaTypeFormatOptions::ParseFromMap(stringTypeOptions);
115121

116-
LuaTypeFormatOptions typeFormatOptions;
117122
LuaTypeFormat tf(typeFormatOptions);
118123
tf.Analyze("\n", line, character, t, style);
119124
return tf.GetResult();
@@ -234,3 +239,22 @@ std::vector<LuaDiagnosticInfo> LuaCodeFormat::MakeDiagnosticInfo(const std::vect
234239

235240
return results;
236241
}
242+
243+
void LuaCodeFormat::CalculateTempStyle(LuaStyle &style, ConfigMap &configMap) {
244+
if (configMap.empty()) {
245+
return;
246+
}
247+
248+
if (configMap.count("insertSpaces")) {
249+
style.indent_style = configMap.at("insertSpaces") == "true"
250+
? IndentStyle::Space
251+
: IndentStyle::Tab;
252+
}
253+
if (configMap.count("tabSize")) {
254+
if (style.indent_style == IndentStyle::Tab) {
255+
style.tab_width = std::stoi(configMap.at("tabSize"));
256+
} else if (style.indent_style == IndentStyle::Space) {
257+
style.indent_size = std::stoi(configMap.at("tabSize"));
258+
}
259+
}
260+
}

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class LuaCodeFormat {
2727

2828
void SetDefaultCodeStyle(ConfigMap &configMap);
2929

30-
void SetSupportNonStandardSymbol(const std::string &tokenType, const std::vector<std::string> &tokens);
30+
void SupportNonStandardSymbol();
3131

3232
void LoadSpellDictionary(const std::string &path);
3333

@@ -55,8 +55,11 @@ class LuaCodeFormat {
5555
std::vector<LuaDiagnosticInfo> MakeDiagnosticInfo(const std::vector<LuaDiagnostic>& diagnostics,
5656
std::shared_ptr<LuaFile> file);
5757

58+
void CalculateTempStyle(LuaStyle& style, ConfigMap &configMap);
59+
5860
std::vector<LuaConfig> _configs;
5961
LuaStyle _defaultStyle;
6062
CodeSpellChecker _spellChecker;
6163
LuaDiagnosticStyle _diagnosticStyle;
64+
bool _supportNonStandardSymbol;
6265
};

CodeService/src/Config/LuaStyle.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ void LuaStyle::ParseFromMap(std::map<std::string, std::string, std::less<>> &con
159159

160160
BOOL_OPTION(never_indent_before_if_condition)
161161

162+
if(align_if_branch){
163+
never_indent_before_if_condition = true;
164+
}
165+
162166
BOOL_OPTION(never_indent_comment_on_if_branch)
163167

164168
std::vector<std::pair<std::string, LineSpace &>> fieldList = {

CodeService/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -372,48 +372,76 @@ void AlignAnalyzer::AnalyzeParamList(FormatState &f, LuaSyntaxNode &syntaxNode,
372372
PushAlignGroup(AlignStrategy::AlignToFirst, group);
373373
}
374374

375-
375+
// 需求真是复杂
376376
void AlignAnalyzer::AnalyzeIfStatement(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
377-
auto ifExprs = syntaxNode.GetChildSyntaxNodes(LuaSyntaxNodeKind::BinaryExpression, t);
377+
auto if_ = syntaxNode.GetChildToken(TK_IF, t);
378+
auto elseifs = syntaxNode.GetChildTokens(TK_ELSEIF, t);
379+
std::vector<std::size_t> group;
380+
381+
// if 之后的表达式可以有多种对齐方式
382+
group.push_back(if_.GetNextToken(t).GetIndex());
383+
auto ifCondition = if_.GetNextSibling(t);
378384
std::vector<LuaSyntaxNode> logicOps;
379-
for (auto ifExpr: ifExprs) {
380-
auto ops = helper::CollectBinaryOperator(ifExpr, t, [](LuaTokenKind kind) {
385+
std::size_t ifAlignPos = 0;
386+
if (ifCondition.GetSyntaxKind(t) == LuaSyntaxNodeKind::BinaryExpression) {
387+
auto ifConditionLogicOps = helper::CollectBinaryOperator(ifCondition, t, [](LuaTokenKind kind) {
381388
return kind == TK_AND || kind == TK_OR;
382389
});
383-
for (auto op: ops) {
384-
logicOps.push_back(op);
390+
391+
for (auto op: ifConditionLogicOps) {
392+
if (op.GetPrevToken(t).GetEndLine(t) != op.GetStartLine(t)) {
393+
if (op.GetTokenKind(t) == TK_AND) {
394+
ifAlignPos = std::max(ifAlignPos, 4u);
395+
} else if (op.GetTokenKind(t) == TK_OR) {
396+
ifAlignPos = std::max(ifAlignPos, 3u);
397+
}
398+
}
385399
}
400+
logicOps = ifConditionLogicOps;
386401
}
387402

388-
std::size_t alignPos = 0;
389-
auto elseifs = syntaxNode.GetChildTokens(TK_ELSEIF, t);
390-
if (!elseifs.empty()) {
391-
alignPos = 7; // sizeof 'elseif '
392-
} else {
393-
alignPos = 3; // sizeof 'if '
394-
for (auto &n: logicOps) {
395-
if (n.GetTokenKind(t) == TK_AND) {
396-
alignPos = 4; // sizeof 'and '
397-
break;
403+
// 如果仅仅if语句
404+
if (elseifs.empty()) {
405+
if (ifAlignPos != 0) {
406+
for (auto &n: logicOps) {
407+
auto nextToken = n.GetNextToken(t);
408+
if (nextToken.IsToken(t)) {
409+
group.push_back(nextToken.GetIndex());
410+
}
398411
}
412+
PushNormalAlignGroup(ifAlignPos, group);
413+
}
414+
return;
415+
}
416+
417+
// 若有 elseif
418+
419+
auto spaceAfterIf = if_.GetNextToken(t).GetStartCol(t) - if_.GetStartCol(t);
420+
if(spaceAfterIf == 3 && ifAlignPos == 0){
421+
group.clear();
422+
logicOps.clear();
423+
}
424+
425+
ifAlignPos = 7;
426+
427+
for (auto elseif_: elseifs) {
428+
auto elseifCondition = elseif_.GetNextSibling(t);
429+
auto ops = helper::CollectBinaryOperator(elseifCondition, t, [](LuaTokenKind kind) {
430+
return kind == TK_AND || kind == TK_OR;
431+
});
432+
for (auto op: ops) {
433+
logicOps.push_back(op);
399434
}
400435
}
401436

402-
std::vector<std::size_t> group;
403437
for (auto &n: logicOps) {
404438
auto nextToken = n.GetNextToken(t);
405439
if (nextToken.IsToken(t)) {
406440
group.push_back(nextToken.GetIndex());
407441
}
408442
}
409-
auto if_ = syntaxNode.GetChildToken(TK_IF, t);
410-
group.push_back(if_.GetNextToken(t).GetIndex());
411-
412-
for (auto elseif_: elseifs) {
413-
group.push_back(elseif_.GetNextToken(t).GetIndex());
414-
}
415443

416-
PushNormalAlignGroup(alignPos, group);
444+
PushNormalAlignGroup(ifAlignPos, group);
417445
}
418446

419447
void AlignAnalyzer::PushNormalAlignGroup(std::size_t alignPos, std::vector<std::size_t> &data) {

LuaParser/src/Lexer/LuaLexer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ std::map<std::string, LuaTokenKind, std::less<>> LuaLexer::LuaReserved = {
4545
LuaLexer::LuaLexer(std::shared_ptr<LuaFile> file)
4646
:
4747
_linenumber(0),
48+
_supportNonStandardSymbol(false),
4849
_reader(file->GetSource()),
4950
_file(file) {
5051
}
@@ -106,6 +107,9 @@ LuaTokenKind LuaLexer::Lex() {
106107
case '-': {
107108
_reader.SaveAndNext();
108109
if (_reader.GetCurrentChar() != '-') {
110+
if (_supportNonStandardSymbol && _reader.CheckNext1('=')) {
111+
return '=';
112+
}
109113
return '-';
110114
}
111115
// is comment
@@ -130,6 +134,13 @@ LuaTokenKind LuaLexer::Lex() {
130134

131135
return type;
132136
}
137+
case '+': {
138+
_reader.SaveAndNext();
139+
if (_supportNonStandardSymbol && _reader.CheckNext1('=')) {
140+
return '=';
141+
}
142+
return '+';
143+
}
133144
case '[': {
134145
std::size_t sep = SkipSep();
135146
if (sep >= 2) {

include/LuaParser/Lexer/LuaLexer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LuaLexer
3131

3232
std::vector<LuaToken>& GetTokens();
3333

34+
void SupportNonStandardSymbol();
3435

3536
// void SetCustomParser(std::shared_ptr<LuaCustomParser> parser);
3637
private:
@@ -55,6 +56,7 @@ class LuaLexer
5556
void TokenError(std::string_view message, TextRange range);
5657

5758
int _linenumber;
59+
bool _supportNonStandardSymbol;
5860
TextReader _reader;
5961
std::vector<LuaToken> _tokens;
6062
std::vector<LuaTokenError> _errors;

0 commit comments

Comments
 (0)