Skip to content

Commit ec789dc

Browse files
committed
更为正确的支持editorconfig配置
1 parent 452683c commit ec789dc

File tree

17 files changed

+277
-185
lines changed

17 files changed

+277
-185
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ int main(int argc, char** argv)
6464

6565
if (!cmd.Get<std::string>("config").empty())
6666
{
67-
options = LuaCodeStyleOptions::ParseFromEditorConfig(cmd.Get<std::string>("config"));
67+
auto editorConfig = LuaEditorConfig::LoadFromFile(cmd.Get<std::string>("config"));
68+
options = LuaCodeStyleOptions::ParseFromEditorConfig(editorConfig);
6869
}
6970
else
7071
{

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
6565
}
6666

6767
auto options = GetOptions(uri);
68-
if(!options->enable_check_codestyle)
68+
if (!options->enable_check_codestyle)
6969
{
7070
auto vscodeDiagnosis = std::make_shared<vscode::PublishDiagnosticsParams>();
7171
vscodeDiagnosis->uri = uri;
@@ -148,12 +148,16 @@ void LanguageClient::UpdateOptions(std::string_view workspaceUri, std::string_vi
148148
{
149149
if (pair.first == workspaceUri)
150150
{
151-
pair.second = LuaCodeStyleOptions::ParseFromEditorConfig(configPath);
151+
pair.second = LuaCodeStyleOptions::ParseFromEditorConfig(
152+
LuaEditorConfig::LoadFromFile(std::string(configPath)));
152153
return;
153154
}
154155
}
155156

156-
_optionsVector.push_back({std::string(workspaceUri), LuaCodeStyleOptions::ParseFromEditorConfig(configPath)});
157+
_optionsVector.push_back({
158+
std::string(workspaceUri),
159+
LuaCodeStyleOptions::ParseFromEditorConfig(LuaEditorConfig::LoadFromFile(std::string(configPath)))
160+
});
157161
}
158162

159163
void LanguageClient::RemoveOptions(std::string_view workspaceUri)

CodeService/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project(CodeService)
44

55
add_library(CodeService STATIC)
66

7-
add_dependencies(CodeService LuaParser)
7+
add_dependencies(CodeService LuaParser Util)
88

99
target_include_directories(CodeService PUBLIC
1010
${LuaCodeStyle_SOURCE_DIR}/include
@@ -61,4 +61,4 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
6161
target_compile_options(CodeService PUBLIC /utf-8)
6262
endif ()
6363

64-
target_link_libraries(CodeService LuaParser)
64+
target_link_libraries(CodeService LuaParser Util)

CodeService/src/FormatElement/DiagnosisContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DiagnosisContext::DiagnosisContext(std::shared_ptr<LuaParser> parser, LuaCodeSty
1010

1111
void DiagnosisContext::AddIndent(int specialIndent)
1212
{
13-
if (!_options.use_tab)
13+
if (_options.indent_style == IndentStyle::Space)
1414
{
1515
int newIndent = 0;
1616
if (specialIndent == -1)
@@ -22,7 +22,7 @@ void DiagnosisContext::AddIndent(int specialIndent)
2222
}
2323

2424
auto& topIndent = _indentStack.top();
25-
newIndent = _options.indent + topIndent;
25+
newIndent = _options.indent_size + topIndent;
2626
}
2727
else
2828
{

CodeService/src/FormatElement/FormatContext.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ FormatContext::FormatContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOpti
55
: _options(options),
66
_characterCount(0),
77
_parser(parser)
8-
98
{
109
}
1110

@@ -28,7 +27,7 @@ void FormatContext::PrintLine(int line)
2827
{
2928
for (int i = 0; i < line; i++)
3029
{
31-
_os << _options.line_separator;
30+
_os << _options.end_of_line;
3231
_characterCount = 0;
3332
}
3433
}
@@ -54,7 +53,7 @@ void FormatContext::PrintIndent(int indent, const std::string& indentString)
5453

5554
void FormatContext::AddIndent(int specialIndent)
5655
{
57-
if (!_options.use_tab)
56+
if (_options.indent_style == IndentStyle::Space)
5857
{
5958
int newIndent = 0;
6059
if (specialIndent == -1)
@@ -66,7 +65,7 @@ void FormatContext::AddIndent(int specialIndent)
6665
}
6766

6867
auto& topIndent = _indentStack.top();
69-
newIndent = _options.indent + topIndent.Indent;
68+
newIndent = _options.indent_size + topIndent.Indent;
7069
}
7170
else
7271
{
@@ -93,7 +92,7 @@ void FormatContext::AddIndent(int specialIndent)
9392
{
9493
// 我会认为存在一个换算
9594
// 当你制定了一个缩进,则我会认为保底有一个缩进
96-
newIndent = std::max(1, specialIndent / 8);
95+
newIndent = std::max(1, specialIndent / _options.tab_width);
9796
}
9897
std::string indentString = "\t";
9998
_indentStack.push({newIndent, "\t"});

CodeService/src/FormatElement/IndentElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void IndentElement::Diagnosis(DiagnosisContext& ctx, int position, FormatElement
3030
{
3131
auto child = _children[i];
3232

33-
if (child->HasValidTextRange() && !ctx.GetOptions().use_tab && child->GetType() != FormatElementType::IndentElement)
33+
if (child->HasValidTextRange() && ctx.GetOptions().indent_style == IndentStyle::Space && child->GetType() != FormatElementType::IndentElement)
3434
{
3535
auto range = child->GetTextRange();
3636
auto character = ctx.GetColumn(range.StartOffset);

CodeService/src/FormatElement/RangeFormatContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void RangeFormatContext::PrintLine(int line)
6767
{
6868
for (int i = 0; i < line; i++)
6969
{
70-
_os << _options.line_separator;
70+
_os << _options.end_of_line;
7171
}
7272
}
7373
}
@@ -82,7 +82,7 @@ std::string RangeFormatContext::GetText()
8282

8383
if (ch != '\n' && ch != '\r')
8484
{
85-
formattedText = formattedText.substr(0, i + 1).append(_options.line_separator);
85+
formattedText = formattedText.substr(0, i + 1).append(_options.end_of_line);
8686
break;
8787
}
8888
}

CodeService/src/LuaCodeStyleOptions.cpp

Lines changed: 41 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,96 @@
11
#include "CodeService/LuaCodeStyleOptions.h"
2-
#include <fstream>
32
#include <filesystem>
43
#include <regex>
54
#include "CodeService/FormatElement/MinLineElement.h"
65
#include "CodeService/FormatElement/KeepLineElement.h"
76

8-
static std::vector<std::string> Split(std::string_view source, std::string_view separator)
7+
std::shared_ptr<LuaCodeStyleOptions> LuaCodeStyleOptions::ParseFromEditorConfig(
8+
std::shared_ptr<LuaEditorConfig> editorConfig)
99
{
10-
if (source.empty() || separator.empty())
11-
{
12-
return {};
13-
}
14-
15-
std::size_t sepLen = separator.size();
16-
std::vector<std::string> result;
17-
18-
while (true)
10+
auto options = std::make_shared<LuaCodeStyleOptions>();
11+
if (editorConfig->Exist("indent_style"))
1912
{
20-
const std::size_t sepIndex = source.find_first_of(separator, 0);
21-
if (sepIndex == std::string_view::npos)
13+
if (editorConfig->Get("indent_style") == "space")
2214
{
23-
result.push_back(std::string(source));
24-
break;
15+
options->indent_style = IndentStyle::Space;
2516
}
26-
else
17+
else if (editorConfig->Get("indent_style") == "tab")
2718
{
28-
result.push_back(std::string(source.substr(0, sepIndex)));
29-
source = source.substr(sepIndex + sepLen);
19+
options->indent_style = IndentStyle::Tab;
3020
}
3121
}
32-
return result;
33-
}
34-
35-
std::shared_ptr<LuaCodeStyleOptions> LuaCodeStyleOptions::ParseFromEditorConfig(std::string_view filename)
36-
{
37-
std::filesystem::path filePath(filename);
38-
std::string realPath = filePath.string();
39-
std::fstream fin(realPath, std::ios::in | std::ios::binary);
40-
41-
if (!fin.is_open())
42-
{
43-
return {};
44-
}
45-
std::stringstream s;
46-
s << fin.rdbuf();
47-
fin.close();
48-
std::string text = s.str();
49-
50-
auto lines = Split(text, "\n");
5122

52-
bool luaSectionFounded = false;
53-
std::regex comment = std::regex(R"(^\s*(;|#))");
54-
std::regex luaSection = std::regex(R"(^\s*\[\s*([\w\.\*]+)\s*\]\s*$)");
55-
std::regex valueRegex = std::regex(R"(^\s*([\w\d_]+)\s*=\s*([\w\:\._]+)\s*$)");
56-
std::map<std::string, std::string> optionMap;
57-
58-
for (auto& line : lines)
23+
if (editorConfig->Exist("indent_size"))
5924
{
60-
if (std::regex_search(line, comment))
61-
{
62-
continue;
63-
}
64-
65-
std::smatch m;
66-
67-
if (std::regex_search(line, m, luaSection))
68-
{
69-
if (m.str(1) == "*.lua")
70-
{
71-
luaSectionFounded = true;
72-
}
73-
else
74-
{
75-
luaSectionFounded = false;
76-
}
77-
78-
continue;
79-
}
80-
81-
if (luaSectionFounded)
82-
{
83-
if (std::regex_search(line, m, valueRegex))
84-
{
85-
optionMap.insert({m.str(1), m.str(2)});
86-
}
87-
}
25+
options->indent_size = std::stoi(editorConfig->Get("indent_size"));
8826
}
8927

90-
91-
return ParseOptionsFromMap(optionMap);
92-
}
93-
94-
std::shared_ptr<LuaCodeStyleOptions> LuaCodeStyleOptions::ParseOptionsFromMap(std::map<std::string, std::string>& optionMap)
95-
{
96-
auto options = std::make_shared<LuaCodeStyleOptions>();
97-
if (optionMap.count("use_tab"))
28+
if (editorConfig->Exist("tab_width"))
9829
{
99-
options->use_tab = optionMap["use_tab"] == "true";
30+
options->tab_width = std::stoi(editorConfig->Get("tab_width"));
10031
}
10132

102-
if (optionMap.count("indent"))
103-
{
104-
options->indent = std::stoi(optionMap["indent"]);
105-
}
10633

107-
if (optionMap.count("continuation_indent"))
34+
if (editorConfig->Exist("continuation_indent_size"))
10835
{
109-
options->continuation_indent = std::stoi(optionMap["indent"]);
36+
options->continuation_indent_size = std::stoi(editorConfig->Get("continuation_indent_size"));
11037
}
11138

112-
if (optionMap.count("align_call_args"))
39+
if (editorConfig->Exist("align_call_args"))
11340
{
114-
options->align_call_args = optionMap["align_call_args"] == "true";
41+
options->align_call_args = editorConfig->Get("align_call_args") == "true";
11542
}
11643

117-
if (optionMap.count("keep_one_space_between_call_args_and_bracket"))
44+
if (editorConfig->Exist("keep_one_space_between_call_args_and_bracket"))
11845
{
11946
options->keep_one_space_between_call_args_and_bracket =
120-
optionMap["keep_one_space_between_call_args_and_bracket"] == "true";
47+
editorConfig->Get("keep_one_space_between_call_args_and_bracket") == "true";
12148
}
12249

123-
if (optionMap.count("keep_one_space_between_table_and_bracket"))
50+
if (editorConfig->Exist("keep_one_space_between_table_and_bracket"))
12451
{
12552
options->keep_one_space_between_table_and_bracket =
126-
optionMap["keep_one_space_between_table_and_bracket"] == "true";
53+
editorConfig->Get("keep_one_space_between_table_and_bracket") == "true";
12754
}
12855

129-
if (optionMap.count("align_table_field_to_first_field"))
56+
if (editorConfig->Exist("align_table_field_to_first_field"))
13057
{
131-
options->align_table_field_to_first_field = optionMap["align_table_field_to_first_field"] == "true";
58+
options->align_table_field_to_first_field = editorConfig->Get("align_table_field_to_first_field") == "true";
13259
}
13360

134-
if (optionMap.count("continuous_assign_statement_align_to_equal_sign"))
61+
if (editorConfig->Exist("continuous_assign_statement_align_to_equal_sign"))
13562
{
13663
options->continuous_assign_statement_align_to_equal_sign =
137-
optionMap["continuous_assign_statement_align_to_equal_sign"] == "true";
64+
editorConfig->Get("continuous_assign_statement_align_to_equal_sign") == "true";
13865
}
13966

140-
if (optionMap.count("continuous_assign_table_field_align_to_equal_sign"))
67+
if (editorConfig->Exist("continuous_assign_table_field_align_to_equal_sign"))
14168
{
14269
options->continuous_assign_table_field_align_to_equal_sign =
143-
optionMap["continuous_assign_table_field_align_to_equal_sign"] == "true";
70+
editorConfig->Get("continuous_assign_table_field_align_to_equal_sign") == "true";
14471
}
14572

146-
if (optionMap.count("line_separator"))
73+
if (editorConfig->Exist("end_of_line"))
14774
{
148-
auto lineSeparatorSymbol = optionMap["line_separator"];
149-
if (lineSeparatorSymbol == "CRLF")
75+
auto lineSeparatorSymbol = editorConfig->Get("end_of_line");
76+
if (lineSeparatorSymbol == "crlf")
15077
{
151-
options->line_separator = "\r\n";
78+
options->end_of_line = "\r\n";
15279
}
153-
else if (lineSeparatorSymbol == "LF")
80+
else if (lineSeparatorSymbol == "lf")
15481
{
155-
options->line_separator = "\n";
82+
options->end_of_line = "\n";
15683
}
15784
}
15885

159-
if (optionMap.count("max_line_length"))
86+
if (editorConfig->Exist("max_line_length"))
16087
{
161-
options->max_line_length = std::stoi(optionMap["max_line_length"]);
88+
options->max_line_length = std::stoi(editorConfig->Get("max_line_length"));
16289
}
16390

164-
if (optionMap.count("enable_check_codestyle"))
91+
if (editorConfig->Exist("enable_check_codestyle"))
16592
{
166-
options->enable_check_codestyle = optionMap["enable_check_codestyle"] == "true";
93+
options->enable_check_codestyle = editorConfig->Get("enable_check_codestyle") == "true";
16794
}
16895

16996
std::vector<std::pair<std::string, std::shared_ptr<FormatElement>&>> fieldList = {
@@ -179,23 +106,23 @@ std::shared_ptr<LuaCodeStyleOptions> LuaCodeStyleOptions::ParseOptionsFromMap(st
179106
std::regex keepLineRegex = std::regex(R"(keepLine:\s*(\d+))");
180107
for (auto& keepLineOption : fieldList)
181108
{
182-
auto it = optionMap.find(keepLineOption.first);
183-
if (it != optionMap.end())
109+
if (editorConfig->Exist(keepLineOption.first))
184110
{
185-
if (it->second == "keepLine")
111+
std::string value = editorConfig->Get(keepLineOption.first);
112+
if (value == "keepLine")
186113
{
187114
keepLineOption.second = std::make_shared<KeepLineElement>();
188115
continue;
189116
}
190117
std::smatch m;
191118

192-
if (std::regex_search(it->second, m, minLineRegex))
119+
if (std::regex_search(value, m, minLineRegex))
193120
{
194121
keepLineOption.second = std::make_shared<MinLineElement>(std::stoi(m.str(1)));
195122
continue;
196123
}
197124

198-
if (std::regex_search(it->second, m, keepLineRegex))
125+
if (std::regex_search(value, m, keepLineRegex))
199126
{
200127
keepLineOption.second = std::make_shared<KeepLineElement>(std::stoi(m.str(1)));
201128
}

0 commit comments

Comments
 (0)