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