88#include " Util/format.h"
99#include " CodeService/LuaFormatter.h"
1010#include " Util/CommandLine.h"
11+ #include " LuaWorkspaceFormat.h"
1112
1213// https://stackoverflow.com/questions/1598985/c-read-binary-stdin
1314#ifdef _WIN32
@@ -26,81 +27,119 @@ int main(int argc, char** argv)
2627
2728 cmd.AddTarget (" format" )
2829 .Add <std::string>(" file" , " f" , " Specify the input file" )
30+ .Add <std::string>(" workspace" , " w" ,
31+ " Specify workspace directory perform batch formatting" )
2932 .Add <int >(" stdin" , " i" , " Read from stdin and specify read size" )
3033 .Add <std::string>(" config" , " c" ,
3134 " 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 " )
35+ .Add <bool >(" detect-config" , " d" ,
36+ " Configuration will be automatically detected ,\n "
37+ " If this option is set, the config option has no effect " )
3538 .Add <std::string>(" outfile" , " o" ,
3639 " Specify output file" )
3740 .EnableKeyValueArgs ();
3841 cmd.AddTarget (" check" )
3942 .Add <std::string>(" file" , " f" , " Specify the input file" )
43+ .Add <std::string>(" workspace" , " w" ,
44+ " Specify workspace directory perform batch checking" )
4045 .Add <std::string>(" config" , " c" ,
4146 " Specify editorconfig file, it decides on the effect of formatting or diagnosis" )
42- .Add <bool >(" diagnosis-as-error" , " diagnosis-as-error" , " if exist error or diagnosis info , return -1" );
47+ .Add <bool >(" detect-config" , " d" ,
48+ " Configuration will be automatically detected,\n "
49+ " If this option is set, the config option has no effect" )
50+ .Add <bool >(" diagnosis-as-error" , " DAE" , " if exist error or diagnosis info , return -1" )
51+ .EnableKeyValueArgs ();
52+
4353
4454 if (!cmd.Parse (argc, argv))
4555 {
4656 cmd.PrintUsage ();
4757 return -1 ;
4858 }
4959
50- std::shared_ptr<LuaParser> parser = nullptr ;
51- auto luaFormat = std::make_shared<LuaFormat>();
52- if (cmd.HasOption (" file" ))
60+ if (!cmd.HasOption (" workspace" ))
5361 {
54- if (!luaFormat->SetInputFile (cmd.Get <std::string>(" file" )))
62+ auto luaFormat = std::make_shared<LuaFormat>();
63+ if (cmd.HasOption (" file" ))
5564 {
56- std::cerr << format (" Can not find file {}" , cmd.Get <std::string>(" file" )) << std::endl;
57- return -1 ;
65+ if (!luaFormat->SetInputFile (cmd.Get <std::string>(" file" )))
66+ {
67+ std::cerr << format (" Can not find file {}" , cmd.Get <std::string>(" file" )) << std::endl;
68+ return -1 ;
69+ }
5870 }
59- }
60- else if (cmd.HasOption (" stdin" ))
61- {
62- SET_BINARY_MODE ();
63- std::size_t size = cmd.Get <int >(" stdin" );
64- if (!luaFormat->ReadFromStdin (size))
71+ else if (cmd.HasOption (" stdin" ))
6572 {
73+ SET_BINARY_MODE ();
74+ std::size_t size = cmd.Get <int >(" stdin" );
75+ if (!luaFormat->ReadFromStdin (size))
76+ {
77+ return -1 ;
78+ }
79+ }
80+ else
81+ {
82+ std::cerr << " not special input file" << std::endl;
6683 return -1 ;
6784 }
68- }
69- else
70- {
71- std::cerr << " not special input file" << std::endl;
72- return -1 ;
73- }
7485
75- if (cmd.HasOption (" output" ))
76- {
77- luaFormat->SetOutputFile (cmd.Get <std::string>(" output" ));
78- }
86+ if (cmd.HasOption (" output" ))
87+ {
88+ luaFormat->SetOutputFile (cmd.Get <std::string>(" output" ));
89+ }
7990
80- if (cmd.HasOption (" detect-config-root " ))
81- {
82- luaFormat->AutoDetectConfigRoot (cmd. Get <std::string>( " detect-config-root " ) );
83- }
84- else if (cmd.HasOption (" config" ))
85- {
86- luaFormat->SetConfigPath (cmd.Get <std::string>(" config" ));
87- }
91+ if (cmd.Get < bool > (" detect-config" ))
92+ {
93+ luaFormat->AutoDetectConfig ( );
94+ }
95+ else if (cmd.HasOption (" config" ))
96+ {
97+ luaFormat->SetConfigPath (cmd.Get <std::string>(" config" ));
98+ }
8899
89- luaFormat->SetDefaultOptions (cmd.GetKeyValueOptions ());
100+ luaFormat->SetDefaultOptions (cmd.GetKeyValueOptions ());
90101
91- if (cmd.GetTarget () == " format" )
92- {
93- if (!luaFormat->Reformat ())
102+ if (cmd.GetTarget () == " format" )
94103 {
95- std::cerr << format (" Exist lua syntax error" ) << std::endl;
96- return -1 ;
104+ if (!luaFormat->Reformat ())
105+ {
106+ std::cerr << format (" Exist lua syntax error" ) << std::endl;
107+ return -1 ;
108+ }
109+ }
110+ else if (cmd.GetTarget () == " check" )
111+ {
112+ if (!luaFormat->Check () && cmd.Get <bool >(" diagnosis-as-error" ))
113+ {
114+ return -1 ;
115+ }
97116 }
98117 }
99- else if (cmd. GetTarget () == " check " )
118+ else
100119 {
101- if (!luaFormat->Check () && cmd.Get <bool >(" diagnosis-as-error" ))
120+ LuaWorkspaceFormat workspaceFormat (cmd.Get <std::string>(" workspace" ));
121+
122+ if (cmd.Get <bool >(" detect-config" ))
102123 {
103- return -1 ;
124+ workspaceFormat.SetAutoDetectConfig (true );
125+ }
126+ else if (cmd.HasOption (" config" ))
127+ {
128+ workspaceFormat.SetConfigPath (cmd.Get <std::string>(" config" ));
129+ }
130+
131+ workspaceFormat.SetKeyValues (cmd.GetKeyValueOptions ());
132+
133+ if (cmd.GetTarget () == " format" )
134+ {
135+ workspaceFormat.ReformatWorkspace ();
136+ }
137+ else if (cmd.GetTarget () == " check" )
138+ {
139+ if (!workspaceFormat.CheckWorkspace () && cmd.Get <bool >(" diagnosis-as-error" ))
140+ {
141+ return -1 ;
142+ }
104143 }
105144 }
106145 return 0 ;
0 commit comments