Skip to content

Commit 03bd828

Browse files
committed
修复bug
1 parent 1c0fa67 commit 03bd828

File tree

17 files changed

+241
-161
lines changed

17 files changed

+241
-161
lines changed

.editorconfig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
[*]
2-
3-
# ReSharper properties
4-
resharper_namespace_indentation=none
5-
indent_style=tab
6-
tab_width=4
7-
81
[*.yml]
92
indent_style = space
103
indent_size = 2

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ target_include_directories(CodeService PUBLIC
1414
target_sources(CodeService
1515
PRIVATE
1616
# config
17+
${CodeService_SOURCE_DIR}/src/Config/FunctionOption.cpp
1718
${CodeService_SOURCE_DIR}/src/Config/LuaStyle.cpp
1819
${CodeService_SOURCE_DIR}/src/Config/LuaEditorConfig.cpp
1920
${CodeService_SOURCE_DIR}/src/Config/LuaDiagnosticStyle.cpp
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "FunctionOption.h"
2+
#include "LuaParser/Lexer/LuaLexer.h"
3+
#include "LuaParser/lexer/LuaTokenTypeDetail.h"
4+
5+
void FunctionOption::Parse(std::string_view option) {
6+
std::string text(option);
7+
auto virtualFile = std::make_shared<LuaFile>(std::move(text));
8+
LuaLexer lexer(virtualFile);
9+
lexer.Parse();
10+
if (lexer.HasError()) {
11+
return;
12+
}
13+
enum class ParseState {
14+
None,
15+
Key,
16+
ExpectParam,
17+
ExpectCommaOrFinish
18+
} state = ParseState::None;
19+
auto &tokens = lexer.GetTokens();
20+
for (auto token: tokens) {
21+
switch (state) {
22+
case ParseState::None: {
23+
if (token.TokenType != TK_NAME) {
24+
return;
25+
}
26+
_key = std::string(
27+
virtualFile->Slice(token.Range.StartOffset, token.Range.EndOffset));
28+
state = ParseState::Key;
29+
break;
30+
}
31+
case ParseState::Key: {
32+
if (token.TokenType != '(') {
33+
return;
34+
}
35+
state = ParseState::ExpectParam;
36+
break;
37+
}
38+
case ParseState::ExpectParam: {
39+
if (token.TokenType == TK_NAME || token.TokenType == TK_NUMBER || token.TokenType == TK_STRING) {
40+
_params.emplace_back(virtualFile->Slice(token.Range.StartOffset, token.Range.EndOffset));
41+
state = ParseState::ExpectCommaOrFinish;
42+
} else if (token.TokenType == ')') {
43+
return;
44+
}
45+
break;
46+
}
47+
case ParseState::ExpectCommaOrFinish: {
48+
if (token.TokenType == ',') {
49+
state = ParseState::ExpectParam;
50+
} else if (token.TokenType == ')') {
51+
return;
52+
}
53+
break;
54+
}
55+
}
56+
}
57+
}
58+
59+
std::string &FunctionOption::GetKey() {
60+
return _key;
61+
}
62+
63+
std::string FunctionOption::GetParam(std::size_t index) {
64+
if (index < _params.size()) {
65+
return _params[index];
66+
}
67+
return "";
68+
}
69+
70+
std::size_t FunctionOption::GetParamSize() {
71+
return _params.size();
72+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
#include <string>
5+
#include <vector>
6+
7+
class FunctionOption {
8+
public:
9+
void Parse(std::string_view option);
10+
11+
std::string& GetKey();
12+
13+
std::string GetParam(std::size_t index);
14+
15+
std::size_t GetParamSize();
16+
17+
private:
18+
std::string _key;
19+
std::vector<std::string> _params;
20+
};

CodeService/src/Config/LuaStyle.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "CodeService/Config/LuaStyle.h"
22
#include <map>
3-
#include <regex>
3+
#include "FunctionOption.h"
44

55
bool IsNumber(std::string_view source) {
66
for (auto c: source) {
@@ -159,7 +159,7 @@ 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){
162+
if (align_if_branch) {
163163
never_indent_before_if_condition = true;
164164
}
165165

@@ -176,30 +176,33 @@ void LuaStyle::ParseFromMap(std::map<std::string, std::string, std::less<>> &con
176176
{"line_space_after_expression_statement", line_space_after_expression_statement},
177177
{"line_space_after_comment", line_space_after_comment}
178178
};
179-
std::regex minLineRegex = std::regex(R"(minLine:\s*(\d+))");
180-
std::regex keepLineRegex = std::regex(R"(keepLine:\s*(\d+))");
181-
std::regex maxLineRegex = std::regex(R"(maxLine:\s*(\d+))");
182-
for (auto &keepLineOption: fieldList) {
183-
if (configMap.count(keepLineOption.first)) {
184-
std::string value = configMap.at(keepLineOption.first);
185-
if (value == "keepLine") {
186-
keepLineOption.second = LineSpace(LineSpaceType::Keep);
187-
continue;
188-
}
189-
std::smatch m;
190179

191-
if (std::regex_search(value, m, minLineRegex)) {
192-
keepLineOption.second = LineSpace(LineSpaceType::Min, std::stoi(m.str(1)));
193-
continue;
194-
}
180+
for (auto &lineOption: fieldList) {
181+
if (configMap.count(lineOption.first)) {
182+
std::string value = configMap.at(lineOption.first);
183+
FunctionOption option;
184+
option.Parse(value);
195185

196-
if (std::regex_search(value, m, keepLineRegex)) {
197-
keepLineOption.second = LineSpace(LineSpaceType::Fixed, std::stoi(m.str(1)));
198-
continue;
186+
if (option.GetKey() == "keep") {
187+
lineOption.second = LineSpace(LineSpaceType::Keep);
199188
}
200-
201-
if (std::regex_search(value, m, maxLineRegex)) {
202-
keepLineOption.second = LineSpace(LineSpaceType::Max, std::stoi(m.str(1)));
189+
else if(option.GetKey() == "min") {
190+
auto p1 = option.GetParam(0);
191+
if(!p1.empty() && IsNumber(p1)) {
192+
lineOption.second = LineSpace(LineSpaceType::Min, std::stoi(p1));
193+
}
194+
}
195+
else if(option.GetKey() == "fixed") {
196+
auto p1 = option.GetParam(0);
197+
if(!p1.empty() && IsNumber(p1)) {
198+
lineOption.second = LineSpace(LineSpaceType::Fixed, std::stoi(p1));
199+
}
200+
}
201+
else if(option.GetKey() == "max") {
202+
auto p1 = option.GetParam(0);
203+
if(!p1.empty() && IsNumber(p1)) {
204+
lineOption.second = LineSpace(LineSpaceType::Max, std::stoi(p1));
205+
}
203206
}
204207
}
205208
}

CodeService/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ void AlignAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
5050
AnalyzeIfStatement(f, syntaxNode, t);
5151
}
5252
}
53-
54-
// case LuaSyntaxNodeKind::ExpressionStatement: {
55-
// if (f.GetStyle().align_chained_expression_statement) {
56-
// }
57-
// break;
58-
// }
5953
default: {
6054
break;
6155
}

CodeService/src/Format/Analyzer/LineBreakAnalyzer.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ LineBreakAnalyzer::LineBreakAnalyzer() {
1111
}
1212

1313
void LineBreakAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
14+
for (auto syntaxNode: t.GetSyntaxNodes()) {
15+
if (syntaxNode.IsToken(t)) {
16+
switch (syntaxNode.GetTokenKind(t)) {
17+
case TK_SHEBANG:
18+
case TK_SHORT_COMMENT: {
19+
BreakAfter(syntaxNode, t, LineSpace(LineSpaceType::Keep));
20+
break;
21+
}
22+
default: {
23+
break;
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
void LineBreakAnalyzer::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
1431
for (auto syntaxNode: t.GetSyntaxNodes()) {
1532
if (syntaxNode.IsNode(t)) {
1633
switch (syntaxNode.GetSyntaxKind(t)) {
@@ -143,17 +160,6 @@ void LineBreakAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
143160
break;
144161
}
145162
}
146-
} else {
147-
switch (syntaxNode.GetTokenKind(t)) {
148-
case TK_SHEBANG:
149-
case TK_SHORT_COMMENT: {
150-
BreakAfter(syntaxNode, t);
151-
break;
152-
}
153-
default: {
154-
break;
155-
}
156-
}
157163
}
158164
}
159165
}
@@ -411,3 +417,4 @@ bool LineBreakAnalyzer::CanCollapseLines(FormatState &f, LuaSyntaxNode &n, const
411417
}
412418
}
413419
}
420+

CodeService/src/Format/Analyzer/SpaceAnalyzer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ void SpaceAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
8282
case TK_RETURN:
8383
case TK_GOTO:
8484
case TK_FOR:
85-
case TK_FUNCTION: {
85+
case TK_ELSE:
86+
case TK_FUNCTION:
87+
case TK_END: {
8688
SpaceRight(syntaxNode, t);
8789
break;
8890
}

CodeService/src/Format/FormatBuilder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
173173
if (nextLine > currentLine) {
174174
WriteLine(nextLine - currentLine);
175175
return;
176+
} else {
177+
WriteLine(1);
176178
}
177179
}
178180
break;
@@ -189,6 +191,8 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
189191
}
190192
WriteLine(line);
191193
return;
194+
} else {
195+
WriteLine(1);
192196
}
193197
}
194198
break;
@@ -205,6 +209,8 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
205209
}
206210
WriteLine(line);
207211
return;
212+
} else {
213+
WriteLine(1);
208214
}
209215
}
210216
}

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@
1919

2020
## RoadMap
2121

22-
* 重写范围格式化[0%]
23-
* 重新实现type formatting[0%]
2422
* plugin[0%]
2523

2624
## 文档
27-
* [使用方式](docs/usage.md)
2825
* [格式化行为介绍](docs/format_action.md)
2926
* [如何配置格式化](docs/format_config.md)
3027
* [代码诊断配置](docs/diagnosis_config.md)
31-
* [部分屏蔽格式化](docs/disable_format.md)
32-
* [auto import配置](docs/auto_import_config.md)
3328
# Contribute
3429

3530
任何pr或者issue都是欢迎的
@@ -54,6 +49,9 @@ cmake --build .
5449

5550
**Contributors**
5651

52+
[**@obszczymucha**](https://github.com/obszczymucha)
53+
54+
[**@Rainer Poisel**](https://github.com/rpoisel)
5755

5856
## License
5957

0 commit comments

Comments
 (0)