Skip to content

Commit 517a27d

Browse files
committed
修改editorconfig读取,兼容常见的匹配模式
1 parent 3507c52 commit 517a27d

File tree

12 files changed

+392
-302
lines changed

12 files changed

+392
-302
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <cstring>
33
#include <fstream>
4+
#include "CodeService/LuaEditorConfig.h"
45
#include "LuaParser/LuaParser.h"
56
#include "Util/format.h"
67
#include "CodeService/LuaFormatter.h"
@@ -26,11 +27,12 @@ int main(int argc, char** argv)
2627

2728
cmd.Add<std::string>("file", "f", "Specify the input file");
2829
cmd.Add<int>("stdin", "i", "Read from stdin and specify read size");
30+
cmd.Add<std::string>("workspace", "w", "Specify a directory for diagnosis(not for format)");
2931
cmd.Add<std::string>("config", "c",
3032
"Specify editorconfig file, it decides on the effect of formatting or diagnosis");
3133
cmd.Add<std::string>("outfile", "o",
3234
"Specify output file");
33-
35+
cmd.Add<bool>("diagnosis-as-error", "diagnosis-as-error", "if exist error or diagnosis info , return -1");
3436
if (!cmd.Parse(argc, argv))
3537
{
3638
cmd.PrintUsage();
@@ -65,7 +67,14 @@ int main(int argc, char** argv)
6567
if (!cmd.Get<std::string>("config").empty())
6668
{
6769
auto editorConfig = LuaEditorConfig::LoadFromFile(cmd.Get<std::string>("config"));
68-
options = LuaCodeStyleOptions::ParseFromEditorConfig(editorConfig);
70+
if (!cmd.Get<std::string>("file").empty())
71+
{
72+
options = editorConfig->Generate(cmd.Get<std::string>("file"));
73+
}
74+
else
75+
{
76+
options = editorConfig->Generate(cmd.Get<std::string>(""));
77+
}
6978
}
7079
else
7180
{
@@ -99,6 +108,10 @@ int main(int argc, char** argv)
99108
std::cout << format("{} from {}:{} to {}:{}", d.Message, d.Range.Start.Line, d.Range.Start.Character,
100109
d.Range.End.Line, d.Range.End.Character) << std::endl;
101110
}
111+
if (cmd.Get<bool>("diagnosis-as-error"))
112+
{
113+
return diagnosis.empty() ? 0 : -1;
114+
}
102115
}
103116
return 0;
104117
}

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "CodeFormatServer/LanguageClient.h"
22

3+
#include "CodeService/LuaEditorConfig.h"
34
#include "CodeService/LuaFormatter.h"
45
#include "LuaParser/LuaParser.h"
56
#include "Util/format.h"
@@ -130,12 +131,12 @@ std::shared_ptr<LuaCodeStyleOptions> LanguageClient::GetOptions(std::string_view
130131
{
131132
std::size_t matchLength = 0;
132133
std::shared_ptr<LuaCodeStyleOptions> options = _defaultOptions;
133-
for (auto it = _optionsVector.begin(); it != _optionsVector.end(); it++)
134+
for (auto it = _editorConfigVector.begin(); it != _editorConfigVector.end(); it++)
134135
{
135136
if (uri.starts_with(it->first) && it->first.size() > matchLength)
136137
{
137138
matchLength = it->first.size();
138-
options = it->second;
139+
options = it->second->Generate(uri);
139140
}
140141
}
141142

@@ -144,35 +145,31 @@ std::shared_ptr<LuaCodeStyleOptions> LanguageClient::GetOptions(std::string_view
144145

145146
void LanguageClient::UpdateOptions(std::string_view workspaceUri, std::string_view configPath)
146147
{
147-
for (auto& pair : _optionsVector)
148+
for (auto& pair : _editorConfigVector)
148149
{
149150
if (pair.first == workspaceUri)
150151
{
151-
pair.second = LuaCodeStyleOptions::ParseFromEditorConfig(
152-
LuaEditorConfig::LoadFromFile(std::string(configPath)));
152+
pair.second = LuaEditorConfig::LoadFromFile(std::string(configPath));
153+
pair.second->SetWorkspace(workspaceUri);
153154
return;
154155
}
155156
}
156157

157-
_optionsVector.push_back({
158+
_editorConfigVector.push_back({
158159
std::string(workspaceUri),
159-
LuaCodeStyleOptions::ParseFromEditorConfig(LuaEditorConfig::LoadFromFile(std::string(configPath)))
160+
LuaEditorConfig::LoadFromFile(std::string(configPath))
160161
});
162+
_editorConfigVector.back().second->SetWorkspace(workspaceUri);
161163
}
162164

163165
void LanguageClient::RemoveOptions(std::string_view workspaceUri)
164166
{
165-
for (auto it = _optionsVector.begin(); it != _optionsVector.end(); it++)
167+
for (auto it = _editorConfigVector.begin(); it != _editorConfigVector.end(); it++)
166168
{
167169
if (it->first == workspaceUri)
168170
{
169-
_optionsVector.erase(it);
171+
_editorConfigVector.erase(it);
170172
break;
171173
}
172174
}
173175
}
174-
175-
// LuaCodeStyleOptions& LanguageClient::GetOptions()
176-
// {
177-
// return _optionsVector;
178-
// }

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ target_sources(CodeService
5454
${CodeService_SOURCE_DIR}/src/FormatElement/DiagnosisContext.cpp
5555
${CodeService_SOURCE_DIR}/src/FormatElement/RangeFormatContext.cpp
5656
${CodeService_SOURCE_DIR}/src/LuaCodeStyleOptions.cpp
57+
${CodeService_SOURCE_DIR}/src/LuaEditorConfig.cpp
5758
${CodeService_SOURCE_DIR}/src/LanguageTranslator.cpp
5859
${CodeService_SOURCE_DIR}/src/NameStyle/NameStyleChecker.cpp
5960
)

CodeService/src/LuaCodeStyleOptions.cpp

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -4,174 +4,6 @@
44
#include "CodeService/FormatElement/MinLineElement.h"
55
#include "CodeService/FormatElement/KeepLineElement.h"
66

7-
std::shared_ptr<LuaCodeStyleOptions> LuaCodeStyleOptions::ParseFromEditorConfig(
8-
std::shared_ptr<LuaEditorConfig> editorConfig)
9-
{
10-
auto options = std::make_shared<LuaCodeStyleOptions>();
11-
if (editorConfig->Exist("indent_style"))
12-
{
13-
if (editorConfig->Get("indent_style") == "space")
14-
{
15-
options->indent_style = IndentStyle::Space;
16-
}
17-
else if (editorConfig->Get("indent_style") == "tab")
18-
{
19-
options->indent_style = IndentStyle::Tab;
20-
}
21-
}
22-
23-
if (editorConfig->Exist("indent_size"))
24-
{
25-
options->indent_size = std::stoi(editorConfig->Get("indent_size"));
26-
}
27-
28-
if (editorConfig->Exist("tab_width"))
29-
{
30-
options->tab_width = std::stoi(editorConfig->Get("tab_width"));
31-
}
32-
33-
34-
if (editorConfig->Exist("continuation_indent_size"))
35-
{
36-
options->continuation_indent_size = std::stoi(editorConfig->Get("continuation_indent_size"));
37-
}
38-
39-
if (editorConfig->Exist("align_call_args"))
40-
{
41-
options->align_call_args = editorConfig->Get("align_call_args") == "true";
42-
}
43-
44-
if (editorConfig->Exist("keep_one_space_between_call_args_and_bracket"))
45-
{
46-
options->keep_one_space_between_call_args_and_bracket =
47-
editorConfig->Get("keep_one_space_between_call_args_and_bracket") == "true";
48-
}
49-
50-
if (editorConfig->Exist("keep_one_space_between_table_and_bracket"))
51-
{
52-
options->keep_one_space_between_table_and_bracket =
53-
editorConfig->Get("keep_one_space_between_table_and_bracket") == "true";
54-
}
55-
56-
if (editorConfig->Exist("align_table_field_to_first_field"))
57-
{
58-
options->align_table_field_to_first_field = editorConfig->Get("align_table_field_to_first_field") == "true";
59-
}
60-
61-
if (editorConfig->Exist("continuous_assign_statement_align_to_equal_sign"))
62-
{
63-
options->continuous_assign_statement_align_to_equal_sign =
64-
editorConfig->Get("continuous_assign_statement_align_to_equal_sign") == "true";
65-
}
66-
67-
if (editorConfig->Exist("continuous_assign_table_field_align_to_equal_sign"))
68-
{
69-
options->continuous_assign_table_field_align_to_equal_sign =
70-
editorConfig->Get("continuous_assign_table_field_align_to_equal_sign") == "true";
71-
}
72-
73-
if (editorConfig->Exist("end_of_line"))
74-
{
75-
auto lineSeparatorSymbol = editorConfig->Get("end_of_line");
76-
if (lineSeparatorSymbol == "crlf")
77-
{
78-
options->end_of_line = "\r\n";
79-
}
80-
else if (lineSeparatorSymbol == "lf")
81-
{
82-
options->end_of_line = "\n";
83-
}
84-
}
85-
86-
if (editorConfig->Exist("max_line_length"))
87-
{
88-
options->max_line_length = std::stoi(editorConfig->Get("max_line_length"));
89-
}
90-
91-
if (editorConfig->Exist("enable_check_codestyle"))
92-
{
93-
options->enable_check_codestyle = editorConfig->Get("enable_check_codestyle") == "true";
94-
}
95-
96-
std::vector<std::pair<std::string, std::shared_ptr<FormatElement>&>> fieldList = {
97-
{"keep_line_after_if_statement", options->keep_line_after_if_statement},
98-
{"keep_line_after_do_statement", options->keep_line_after_do_statement},
99-
{"keep_line_after_while_statement", options->keep_line_after_while_statement},
100-
{"keep_line_after_repeat_statement", options->keep_line_after_repeat_statement},
101-
{"keep_line_after_for_statement", options->keep_line_after_for_statement},
102-
{"keep_line_after_local_or_assign_statement", options->keep_line_after_local_or_assign_statement},
103-
{"keep_line_after_function_define_statement", options->keep_line_after_function_define_statement}
104-
};
105-
std::regex minLineRegex = std::regex(R"(minLine:\s*(\d+))");
106-
std::regex keepLineRegex = std::regex(R"(keepLine:\s*(\d+))");
107-
for (auto& keepLineOption : fieldList)
108-
{
109-
if (editorConfig->Exist(keepLineOption.first))
110-
{
111-
std::string value = editorConfig->Get(keepLineOption.first);
112-
if (value == "keepLine")
113-
{
114-
keepLineOption.second = std::make_shared<KeepLineElement>();
115-
continue;
116-
}
117-
std::smatch m;
118-
119-
if (std::regex_search(value, m, minLineRegex))
120-
{
121-
keepLineOption.second = std::make_shared<MinLineElement>(std::stoi(m.str(1)));
122-
continue;
123-
}
124-
125-
if (std::regex_search(value, m, keepLineRegex))
126-
{
127-
keepLineOption.second = std::make_shared<KeepLineElement>(std::stoi(m.str(1)));
128-
}
129-
}
130-
}
131-
132-
if (editorConfig->Exist("insert_final_newline"))
133-
{
134-
options->insert_final_newline = editorConfig->Get("insert_final_newline") == "true";
135-
}
136-
137-
if (editorConfig->Exist("enable_name_style_check"))
138-
{
139-
options->enable_name_style_check = editorConfig->Get("enable_name_style_check") == "true";
140-
}
141-
142-
std::vector<std::pair<std::string, NameStyle&>> styleList = {
143-
{"local_name_define_style", options->local_name_define_style},
144-
{"function_name_define_style", options->function_name_define_style},
145-
{"table_field_name_define_style", options->table_field_name_define_style},
146-
};
147-
148-
for (auto& styleOption : styleList)
149-
{
150-
if (editorConfig->Exist(styleOption.first))
151-
{
152-
std::string value = editorConfig->Get(styleOption.first);
153-
if (value == "off")
154-
{
155-
styleOption.second = NameStyle::Off;
156-
}
157-
else if(value == "snake_case")
158-
{
159-
styleOption.second = NameStyle::SnakeCase;
160-
}
161-
else if(value == "camel_case")
162-
{
163-
styleOption.second = NameStyle::CamelCase;
164-
}
165-
else if(value == "pascal_case")
166-
{
167-
styleOption.second = NameStyle::PascalCase;
168-
}
169-
}
170-
}
171-
172-
return options;
173-
}
174-
1757
LuaCodeStyleOptions::LuaCodeStyleOptions()
1768
:
1779
keep_line_after_if_statement(std::make_shared<MinLineElement>(1)),

0 commit comments

Comments
 (0)