1919# define SET_BINARY_MODE () ((void )0 )
2020#endif
2121
22+ bool InitFormat (CommandLine& cmd, LuaFormat& format);
23+ bool InitCheck (CommandLine& cmd, LuaFormat& format);
24+ bool InitRangeFormat (CommandLine& cmd, LuaFormat& format);
25+
2226int main (int argc, char **argv) {
2327 CommandLine cmd;
2428 cmd.SetUsage (
2529 " Usage:\n "
26- " CodeFormat [check/format] [options]\n "
30+ " CodeFormat [check/format/rangeformat ] [options]\n "
2731 " for example:\n "
2832 " \t CodeFormat check -w . -d\n "
2933 " \t CodeFormat format -f test.lua -d\n "
3034 " \t CodeFormat check -w . -d --ignores \" Test/*.lua;src/**.lua\"\n "
3135 " \t CodeFormat check -w . -d --ignores-file \" .gitignore\"\n "
36+ " \t CodeFormat rangeformat -i -d --rangeline 1:10\n "
37+ " \t CodeFormat rangeformat -i -d --rangeOffset 0:100\n "
3238 );
3339 cmd.AddTarget (" format" )
3440 .Add <std::string>(" file" , " f" , " Specify the input file" )
@@ -51,6 +57,21 @@ int main(int argc, char **argv) {
5157 " \t\t separated by ';'"
5258 )
5359 .EnableKeyValueArgs ();
60+ cmd.AddTarget (" rangeformat" )
61+ .Add <std::string>(" file" , " f" , " Specify the input file" )
62+ .Add <std::string>(" workspace" , " w" ,
63+ " Specify workspace directory, It can be used to automatically check the configuration" )
64+ .Add <bool >(" stdin" , " i" , " Read from stdin" )
65+ .Add <std::string>(" config" , " c" ,
66+ " Specify .editorconfig file, it decides on the effect of formatting" )
67+ .Add <bool >(" detect-config" , " d" ,
68+ " Configuration will be automatically detected,\n "
69+ " \t\t If this option is set, the config option has no effect " )
70+ .Add <bool >(" complete-output" , " " ,
71+ " If true, all content will be output" )
72+ .Add <std::string>(" range-line" , " " , " the format is startline:endline, for eg: 1:10" )
73+ .Add <std::string>(" range-offset" , " " , " the format is startOffset:endOffset, for eg: 0:256" )
74+ .EnableKeyValueArgs ();
5475 cmd.AddTarget (" check" )
5576 .Add <std::string>(" file" , " f" , " Specify the input file" )
5677 .Add <std::string>(" workspace" , " w" ,
@@ -78,21 +99,46 @@ int main(int argc, char **argv) {
7899 }
79100
80101 LuaFormat format;
102+ if (cmd.GetTarget () == " format" ) {
103+ InitFormat (cmd, format);
104+ if (!format.Reformat ()) {
105+ // special return code for intellij
106+ return 1 ;
107+ }
108+ } else if (cmd.GetTarget () == " check" ) {
109+ InitCheck (cmd, format);
110+ if (!format.Check () && cmd.Get <bool >(" diagnosis-as-error" )) {
111+ return -1 ;
112+ }
113+ } else if (cmd.GetTarget () == " rangeformat" ) {
114+ InitRangeFormat (cmd, format);
115+ if (!format.RangeReformat ()) {
116+ // special return code for intellij
117+ return 1 ;
118+ }
119+ }
120+
121+ return 0 ;
122+ }
123+
124+ bool InitFormat (CommandLine &cmd, LuaFormat& format) {
81125 if (cmd.HasOption (" file" ) || cmd.HasOption (" stdin" )) {
82- format.SetWorkMode (WorkMode::File);
126+ if (cmd.HasOption (" file" )) {
127+ format.SetInputFile (cmd.Get <std::string>(" file" ));
128+ }
129+
83130 if (cmd.HasOption (" file" ) && !cmd.HasOption (" stdin" )) {
84- if (!format.SetInputFile (cmd.Get <std::string>(" file" ))) {
131+ format.SetWorkMode (WorkMode::File);
132+ if (!format.ReadFromInput ()) {
85133 std::cerr << util::format (" Can not find file {}" , cmd.Get <std::string>(" file" )) << std::endl;
86- return - 1 ;
134+ return false ;
87135 }
88- } else if (cmd.HasOption (" stdin" ) && !cmd.HasOption (" file" )) {
136+ } else {
137+ format.SetWorkMode (WorkMode::Stdin);
89138 SET_BINARY_MODE ();
90139 if (!format.ReadFromStdin ()) {
91- return - 1 ;
140+ return false ;
92141 }
93- } else {
94- std::cerr << " Either --file or --stdin must be specified." << std::endl;
95- return -1 ;
96142 }
97143
98144 if (cmd.HasOption (" outfile" )) {
@@ -127,24 +173,128 @@ int main(int argc, char **argv) {
127173 format.AutoDetectConfig ();
128174 } else if (cmd.HasOption (" config" )) {
129175 format.SetConfigPath (cmd.Get <std::string>(" config" ));
176+ }
177+
178+ format.SetDefaultStyle (cmd.GetKeyValueOptions ());
179+ return true ;
180+ }
181+
182+ bool InitCheck (CommandLine &cmd, LuaFormat& format) {
183+
184+ if (cmd.HasOption (" file" ) || cmd.HasOption (" stdin" )) {
185+ if (cmd.HasOption (" file" )) {
186+ format.SetInputFile (cmd.Get <std::string>(" file" ));
187+ }
188+
189+ if (cmd.HasOption (" file" ) && !cmd.HasOption (" stdin" )) {
190+ format.SetWorkMode (WorkMode::File);
191+ if (!format.ReadFromInput ()) {
192+ std::cerr << util::format (" Can not find file {}" , cmd.Get <std::string>(" file" )) << std::endl;
193+ return false ;
194+ }
195+ } else {
196+ format.SetWorkMode (WorkMode::Stdin);
197+ SET_BINARY_MODE ();
198+ if (!format.ReadFromStdin ()) {
199+ return false ;
200+ }
201+ }
130202 } else {
131- format.SetDefaultStyle (cmd.GetKeyValueOptions ());
203+ format.SetWorkMode (WorkMode::Workspace);
204+ }
205+
206+ if (cmd.HasOption (" workspace" )) {
207+ format.SetWorkspace (cmd.Get <std::string>(" workspace" ));
132208 }
133209
210+ if (cmd.HasOption (" ignores" )) {
211+ auto ignores = cmd.Get <std::string>(" ignores" );
212+ auto patterns = string_util::Split (ignores, " ;" );
213+ for (auto pattern: patterns) {
214+ auto patternNoSpace = string_util::TrimSpace (pattern);
215+ if (!patternNoSpace.empty ()) {
216+ format.AddIgnores (patternNoSpace);
217+ }
218+ }
219+ }
220+
221+ if (cmd.HasOption (" ignores-file" )) {
222+ format.AddIgnoresByFile (cmd.Get <std::string>(" ignores-file" ));
223+ }
224+
225+ if (cmd.Get <bool >(" detect-config" )) {
226+ format.AutoDetectConfig ();
227+ } else if (cmd.HasOption (" config" )) {
228+ format.SetConfigPath (cmd.Get <std::string>(" config" ));
229+ }
230+
231+ format.SetDefaultStyle (cmd.GetKeyValueOptions ());
232+
134233 if (cmd.Get <bool >(" name-style" )){
135234 format.SupportNameStyleCheck ();
136235 }
236+ return true ;
237+ }
137238
138- if ( cmd. GetTarget () == " format" ) {
139- if (!format. Reformat ( )) {
140- std::cerr << util::format ( " Exist lua syntax error " ) << std::endl;
141- return - 1 ;
239+ bool InitRangeFormat (CommandLine & cmd, LuaFormat& format) {
240+ if (cmd. HasOption ( " file " ) || cmd. HasOption ( " stdin " )) {
241+ if (cmd. HasOption ( " file " )) {
242+ format. SetInputFile (cmd. Get <std::string>( " file " )) ;
142243 }
143- } else if (cmd.GetTarget () == " check" ) {
144- if (!format.Check () && cmd.Get <bool >(" diagnosis-as-error" )) {
145- return -1 ;
244+
245+ if (cmd.HasOption (" file" ) && !cmd.HasOption (" stdin" )) {
246+ format.SetWorkMode (WorkMode::File);
247+ if (!format.ReadFromInput ()) {
248+ std::cerr << util::format (" Can not find file {}" , cmd.Get <std::string>(" file" )) << std::endl;
249+ return false ;
250+ }
251+ } else {
252+ format.SetWorkMode (WorkMode::Stdin);
253+ SET_BINARY_MODE ();
254+ if (!format.ReadFromStdin ()) {
255+ return false ;
256+ }
146257 }
258+ } else {
259+ format.SetWorkMode (WorkMode::Workspace);
147260 }
148261
149- return 0 ;
150- }
262+ if (cmd.HasOption (" workspace" )) {
263+ format.SetWorkspace (cmd.Get <std::string>(" workspace" ));
264+ }
265+
266+ if (cmd.HasOption (" ignores" )) {
267+ auto ignores = cmd.Get <std::string>(" ignores" );
268+ auto patterns = string_util::Split (ignores, " ;" );
269+ for (auto pattern: patterns) {
270+ auto patternNoSpace = string_util::TrimSpace (pattern);
271+ if (!patternNoSpace.empty ()) {
272+ format.AddIgnores (patternNoSpace);
273+ }
274+ }
275+ }
276+
277+ if (cmd.HasOption (" ignores-file" )) {
278+ format.AddIgnoresByFile (cmd.Get <std::string>(" ignores-file" ));
279+ }
280+
281+ if (cmd.Get <bool >(" detect-config" )) {
282+ format.AutoDetectConfig ();
283+ } else if (cmd.HasOption (" config" )) {
284+ format.SetConfigPath (cmd.Get <std::string>(" config" ));
285+ }
286+
287+ format.SetDefaultStyle (cmd.GetKeyValueOptions ());
288+ if (cmd.Get <bool >(" complete-output" )) {
289+ format.SupportCompleteOutputRange ();
290+ }
291+
292+ if (cmd.HasOption (" range-line" )) {
293+ format.SetFormatRange (true , cmd.Get <std::string>(" range-line" ));
294+ }
295+ else if (cmd.HasOption (" range-offset" )){
296+ format.SetFormatRange (false , cmd.Get <std::string>(" range-offset" ));
297+ }
298+
299+ return true ;
300+ }
0 commit comments