| 31 | } |
| 32 | |
| 33 | bool LuaFormat::ReformatSingleFile(const FormatContext &context, std::string_view outPath, std::string &&sourceText, LuaStyle style) { |
| 34 | auto file = LuaSource::From(std::move(sourceText)); |
| 35 | LuaLexer luaLexer(file); |
| 36 | if (context.IsNonStandardLua()) { |
| 37 | luaLexer.SupportNonStandardSymbol(); |
| 38 | } |
| 39 | if (context.IsCLikeCommentsSupport()) { |
| 40 | luaLexer.SupportCLikeComments(); |
| 41 | } |
| 42 | |
| 43 | luaLexer.Parse(); |
| 44 | |
| 45 | LuaParser p(file, std::move(luaLexer.GetTokens())); |
| 46 | p.Parse(); |
| 47 | |
| 48 | if (p.HasError()) { |
| 49 | std::cerr << "Exist Syntax Errors" << std::endl; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | LuaSyntaxTree t; |
| 54 | t.BuildTree(p); |
| 55 | |
| 56 | if (outPath.empty()) { |
| 57 | style.detect_end_of_line = false; |
| 58 | style.end_of_line = EndOfLine::LF; |
| 59 | } |
| 60 | |
| 61 | FormatBuilder f(style); |
| 62 | auto formattedText = f.GetFormatResult(t); |
| 63 | |
| 64 | if (!outPath.empty()) { |
| 65 | std::fstream fout(std::string(outPath), std::ios::out | std::ios::binary); |
| 66 | fout.write(formattedText.data(), formattedText.size()); |
| 67 | fout.close(); |
| 68 | } else { |
| 69 | std::cout.write(formattedText.data(), formattedText.size()); |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | bool LuaFormat::RangeReformat(const FormatContext &context) { |
| 75 | auto text = context.GetInputText(); |
nothing calls this directly
no test coverage detected