11#include < iostream>
22#include < cstring>
33#include < fstream>
4+
5+ #include " LuaFormat.h"
46#include " CodeService/LuaEditorConfig.h"
57#include " LuaParser/LuaParser.h"
68#include " Util/format.h"
@@ -27,6 +29,9 @@ int main(int argc, char** argv)
2729 .Add <int >(" stdin" , " i" , " Read from stdin and specify read size" )
2830 .Add <std::string>(" config" , " c" ,
2931 " Specify .editorconfig file, it decides on the effect of formatting" )
32+ .Add <std::string>(" detect-config-root" , " d" ,
33+ " Set the root directory for automatic detection of config,\n "
34+ " If this option is set, the config option has no effect " )
3035 .Add <std::string>(" outfile" , " o" ,
3136 " Specify output file" )
3237 .EnableKeyValueArgs ();
@@ -43,97 +48,59 @@ int main(int argc, char** argv)
4348 }
4449
4550 std::shared_ptr<LuaParser> parser = nullptr ;
46-
51+ auto luaFormat = std::make_shared<LuaFormat>();
4752 if (cmd.HasOption (" file" ))
4853 {
49- parser = LuaParser::LoadFromFile (cmd.Get <std::string>(" file" ));
50- if (!parser)
54+ if (!luaFormat->SetInputFile (cmd.Get <std::string>(" file" )))
5155 {
52- std::cerr << format (" can not find file: {}" , cmd.Get <std::string>(" file" )) << std::endl;
56+ std::cerr << format (" Can not find file {}" , cmd.Get <std::string>(" file" )) << std::endl;
5357 return -1 ;
5458 }
5559 }
5660 else if (cmd.HasOption (" stdin" ))
5761 {
5862 SET_BINARY_MODE ();
5963 std::size_t size = cmd.Get <int >(" stdin" );
60-
61- std::string buffer;
62- buffer.resize (size);
63- std::cin.get (buffer.data (), size, EOF);
64- auto realSize = strnlen (buffer.data (), size);
65- buffer.resize (realSize);
66- parser = LuaParser::LoadFromBuffer (std::move (buffer));
64+ if (!luaFormat->ReadFromStdin (size))
65+ {
66+ return -1 ;
67+ }
6768 }
6869 else
6970 {
7071 std::cerr << " not special input file" << std::endl;
7172 return -1 ;
7273 }
7374
74- std::shared_ptr<LuaCodeStyleOptions> options = nullptr ;
75-
76- if (cmd.HasOption (" config" ))
75+ if (cmd.HasOption (" output" ))
7776 {
78- auto editorConfig = LuaEditorConfig::LoadFromFile (cmd.Get <std::string>(" config" ));
79- if (cmd.HasOption (" file" ))
80- {
81- options = editorConfig->Generate (cmd.Get <std::string>(" file" ));
82- }
83- else
84- {
85- options = editorConfig->Generate (cmd.Get <std::string>(" " ));
86- }
87- }
88- else
89- {
90- options = std::make_shared<LuaCodeStyleOptions>();
91- if (!cmd.GetKeyValueOptions ().empty ())
92- {
93- LuaEditorConfig::ParseFromSection (options, cmd.GetKeyValueOptions ());
94- }
77+ luaFormat->SetOutputFile (cmd.Get <std::string>(" output" ));
9578 }
9679
97- if (! cmd.HasOption (" outfile " ))
80+ if (cmd.HasOption (" detect-config-root " ))
9881 {
99- options-> end_of_line = " \n " ;
82+ luaFormat-> AutoDetectConfigRoot (cmd. Get <std::string>( " detect-config-root " )) ;
10083 }
101-
102- parser->BuildAstWithComment ();
103-
104- if (parser->HasError ())
84+ else if (cmd.HasOption (" config" ))
10585 {
106- return - 1 ;
86+ luaFormat-> SetConfigPath (cmd. Get <std::string>( " config " )) ;
10787 }
10888
109- LuaFormatter formatter (parser, *options);
110- formatter.BuildFormattedElement ();
89+ luaFormat->SetDefaultOptions (cmd.GetKeyValueOptions ());
11190
11291 if (cmd.GetTarget () == " format" )
11392 {
114- auto formattedText = formatter.GetFormattedText ();
115- if (cmd.HasOption (" outfile" ))
93+ if (!luaFormat->Reformat ())
11694 {
117- std::fstream f (cmd.Get <std::string>(" outfile" ), std::ios::out | std::ios::binary);
118- f.write (formattedText.data (), formattedText.size ());
119- f.close ();
120- }
121- else
122- {
123- std::cout.write (formattedText.data (), formattedText.size ());
95+ std::cerr << format (" Exist lua syntax error" ) << std::endl;
96+ return -1 ;
12497 }
12598 }
12699 else if (cmd.GetTarget () == " check" )
127100 {
128- auto diagnosis = formatter.GetDiagnosisInfos ();
129- for (auto & d : diagnosis)
101+ if (!luaFormat->Check () && cmd.Get <bool >(" diagnosis-as-error" ))
130102 {
131- std::cout << format (" {} from {}:{} to {}:{}" , d.Message , d.Range .Start .Line , d.Range .Start .Character ,
132- d.Range .End .Line , d.Range .End .Character ) << std::endl;
133- }
134- if (cmd.Get <bool >(" diagnosis-as-error" ))
135- {
136- return diagnosis.empty () ? 0 : -1 ;
103+ return -1 ;
137104 }
138105 }
139106 return 0 ;
0 commit comments