@@ -23,19 +23,11 @@ void LuaFormat::SetWorkspace(std::string_view workspace) {
2323
2424bool LuaFormat::SetInputFile (std::string_view input) {
2525 _inputPath = std::string (input);
26- #ifdef _WIN32
27- std::fstream fin (_inputPath, std::ios::in | std::ios::binary);
28- #else
29- std::fstream fin (_inputPath, std::ios::in);
30- #endif
31-
32- if (fin.is_open ()) {
33- std::stringstream s;
34- s << fin.rdbuf ();
35- _inputFileText = std::move (s.str ());
26+ auto opText = ReadFile (input);
27+ if (opText.has_value ()) {
28+ _inputFileText = std::move (opText.value ());
3629 return true ;
3730 }
38-
3931 return false ;
4032}
4133
@@ -48,7 +40,7 @@ bool LuaFormat::ReadFromStdin() {
4840}
4941
5042void LuaFormat::SetOutputFile (std::string_view path) {
51- _outFile = std::string (path);
43+ _outPath = std::string (path);
5244}
5345
5446bool IsSubRelative (std::filesystem::path &path, std::filesystem::path base) {
@@ -123,7 +115,7 @@ void LuaFormat::SetDefaultStyle(std::map<std::string, std::string, std::less<>>
123115
124116bool LuaFormat::Reformat () {
125117 if (_mode == WorkMode::File) {
126- return ReformatSingleFile (std::move (_inputFileText));
118+ return ReformatSingleFile (_inputPath, _outPath, std::move (_inputFileText));
127119 }
128120 return ReformatWorkspace ();
129121}
@@ -143,17 +135,17 @@ bool LuaFormat::ReformatSingleFile(std::string_view inputPath, std::string_view
143135 LuaSyntaxTree t;
144136 t.BuildTree (p);
145137
146- LuaStyle style = GetStyle (_inputPath );
147- if (_outFile .empty ()) {
138+ LuaStyle style = GetStyle (inputPath );
139+ if (outPath .empty ()) {
148140 style.detect_end_of_line = false ;
149141 style.end_of_line = EndOfLine::LF;
150142 }
151143
152144 FormatBuilder f (style);
153145 auto formattedText = f.GetFormatResult (t);
154146
155- if (!_outFile .empty ()) {
156- std::fstream fout (_outFile , std::ios::out | std::ios::binary);
147+ if (!outPath .empty ()) {
148+ std::fstream fout (outPath , std::ios::out | std::ios::binary);
157149 fout.write (formattedText.data (), formattedText.size ());
158150 fout.close ();
159151 } else {
@@ -164,7 +156,7 @@ bool LuaFormat::ReformatSingleFile(std::string_view inputPath, std::string_view
164156
165157bool LuaFormat::Check () {
166158 if (_mode == WorkMode::File) {
167- return CheckSingleFile ();
159+ return CheckSingleFile (_inputPath, std::move (_inputFileText) );
168160 }
169161 return CheckWorkspace ();
170162}
@@ -183,6 +175,23 @@ void LuaFormat::SetWorkMode(WorkMode mode) {
183175 _mode = mode;
184176}
185177
178+ std::optional<std::string> LuaFormat::ReadFile (std::string_view path) {
179+ std::string newPath (path);
180+ #ifdef _WIN32
181+ std::fstream fin (newPath, std::ios::in | std::ios::binary);
182+ #else
183+ std::fstream fin (newPath, std::ios::in);
184+ #endif
185+
186+ if (fin.is_open ()) {
187+ std::stringstream s;
188+ s << fin.rdbuf ();
189+ return std::move (s.str ());
190+ }
191+
192+ return std::nullopt ;
193+ }
194+
186195LuaStyle LuaFormat::GetStyle (std::string_view path) {
187196 std::shared_ptr<LuaEditorConfig> editorConfig = nullptr ;
188197 std::size_t matchProcess = 0 ;
@@ -201,8 +210,8 @@ LuaStyle LuaFormat::GetStyle(std::string_view path) {
201210 return _defaultStyle;
202211}
203212
204- bool LuaFormat::CheckSingleFile () {
205- auto file = std::make_shared<LuaFile>(std::move (_inputFileText ));
213+ bool LuaFormat::CheckSingleFile (std::string_view inputPath, std::string &&sourceText ) {
214+ auto file = std::make_shared<LuaFile>(std::move (sourceText ));
206215 LuaLexer luaLexer (file);
207216 luaLexer.Parse ();
208217
@@ -215,9 +224,9 @@ bool LuaFormat::CheckSingleFile() {
215224
216225 if (p.HasError ()) {
217226 auto &errors = p.GetErrors ();
218- std::cout << util::format (" Check {} ...\t {} error" , _inputPath , errors.size ()) << std::endl;
227+ std::cout << util::format (" Check {} ...\t {} error" , inputPath , errors.size ()) << std::endl;
219228 for (auto &error: errors) {
220- DiagnosticInspection (error.ErrorMessage , error.ErrorRange , file, _inputPath );
229+ DiagnosticInspection (error.ErrorMessage , error.ErrorRange , file, inputPath );
221230 }
222231
223232 return false ;
@@ -226,26 +235,51 @@ bool LuaFormat::CheckSingleFile() {
226235 LuaSyntaxTree t;
227236 t.BuildTree (p);
228237
229- LuaStyle style = GetStyle (_inputPath );
238+ LuaStyle style = GetStyle (inputPath );
230239 DiagnosticBuilder diagnosticBuilder (style, _diagnosticStyle);
231240 diagnosticBuilder.CodeStyleCheck (t);
232241 diagnosticBuilder.NameStyleCheck (t);
233242 auto diagnostics = diagnosticBuilder.GetDiagnosticResults (t);
234243 if (!diagnostics.empty ()) {
235- std::cout << util::format (" Check {}\t {} warning" , _inputPath , diagnostics.size ()) << std::endl;
244+ std::cout << util::format (" Check {}\t {} warning" , inputPath , diagnostics.size ()) << std::endl;
236245
237246 for (auto &d: diagnostics) {
238- DiagnosticInspection (d.Message , d.Range , file, _inputPath );
247+ DiagnosticInspection (d.Message , d.Range , file, inputPath );
239248 }
240249
241250 return false ;
242251 }
243- std::cout << util::format (" Check {} OK" , _inputPath) << std::endl;
244252 return true ;
245253}
246254
247255bool LuaFormat::CheckWorkspace () {
248- return false ;
256+ FileFinder finder (_workspace);
257+ finder.AddFindExtension (" .lua" );
258+ finder.AddFindExtension (" .lua.txt" );
259+ finder.AddIgnoreDirectory (" .git" );
260+ finder.AddIgnoreDirectory (" .github" );
261+ finder.AddIgnoreDirectory (" .svn" );
262+ finder.AddIgnoreDirectory (" .idea" );
263+ finder.AddIgnoreDirectory (" .vs" );
264+ finder.AddIgnoreDirectory (" .vscode" );
265+ for (auto pattern: _ignorePattern) {
266+ finder.AddignorePatterns (pattern);
267+ }
268+
269+ auto files = finder.FindFiles ();
270+ for (auto &filePath: files) {
271+ auto opText = ReadFile (filePath);
272+ if (opText.has_value ()) {
273+ if (CheckSingleFile (filePath, std::move (opText.value ()))) {
274+ std::cerr << util::format (" Check {} ok." , filePath) << std::endl;
275+ } else {
276+ std::cerr << util::format (" Check {} fail." , filePath) << std::endl;
277+ }
278+ } else {
279+ std::cerr << util::format (" Can not read file {}" , filePath) << std::endl;
280+ }
281+ }
282+ return true ;
249283}
250284
251285void LuaFormat::AddIgnoresByFile (std::string_view ignoreConfigFile) {
@@ -282,16 +316,18 @@ bool LuaFormat::ReformatWorkspace() {
282316
283317 auto files = finder.FindFiles ();
284318 for (auto &filePath: files) {
285-
286- // if (luaFormat.Reformat())
287- // {
288- // std::cerr << util::format("reformat {} succeed.", file) << std::endl;
289- // }
290- // else
291- // {
292- // std::cerr << util::format("reformat {} fail.", file) << std::endl;
293- // }
319+ auto opText = ReadFile (filePath);
320+ if (opText.has_value ()) {
321+ if (ReformatSingleFile (filePath, filePath, std::move (opText.value ()))) {
322+ std::cerr << util::format (" Reformat {} succeed." , filePath) << std::endl;
323+ } else {
324+ std::cerr << util::format (" Reformat {} fail." , filePath) << std::endl;
325+ }
326+ } else {
327+ std::cerr << util::format (" Can not read file {}" , filePath) << std::endl;
328+ }
294329 }
295330 return true ;
296331}
297332
333+
0 commit comments