| 72 | } |
| 73 | |
| 74 | bool LuaFormat::RangeReformat(const FormatContext &context) { |
| 75 | auto text = context.GetInputText(); |
| 76 | auto file = LuaSource::From(std::move(text)); |
| 77 | LuaLexer luaLexer(file); |
| 78 | if (context.IsNonStandardLua()) { |
| 79 | luaLexer.SupportNonStandardSymbol(); |
| 80 | } |
| 81 | if (context.IsCLikeCommentsSupport()) { |
| 82 | luaLexer.SupportCLikeComments(); |
| 83 | } |
| 84 | |
| 85 | luaLexer.Parse(); |
| 86 | |
| 87 | LuaParser p(file, std::move(luaLexer.GetTokens())); |
| 88 | p.Parse(); |
| 89 | |
| 90 | if (p.HasError()) { |
| 91 | std::cerr << "Exist Syntax Errors" << std::endl; |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | LuaSyntaxTree t; |
| 96 | t.BuildTree(p); |
| 97 | |
| 98 | auto style = context.GetStyle(context.GetInputPath()); |
| 99 | style.detect_end_of_line = false; |
| 100 | style.end_of_line = EndOfLine::LF; |
| 101 | FormatRange range; |
| 102 | |
| 103 | auto rangeStr = context.GetFormatRangeString(); |
| 104 | auto rangeVec = string_util::Split(rangeStr, ":"); |
| 105 | if (rangeVec.size() != 2) { |
| 106 | std::cerr << "Range param format error" << std::endl; |
| 107 | return false; |
| 108 | } |
| 109 | auto start = std::stoull(std::string(rangeVec[0])); |
| 110 | auto end = std::stoull(std::string(rangeVec[1])); |
| 111 | if (context.IsFormatLine()) { |
| 112 | range.StartLine = start; |
| 113 | range.EndLine = end; |
| 114 | range.StartCol = 0; |
| 115 | range.EndCol = 0; |
| 116 | } else { |
| 117 | range.StartLine = file->GetLine(start); |
| 118 | range.EndLine = file->GetLine(end); |
| 119 | } |
| 120 | |
| 121 | RangeFormatBuilder f(style, range); |
| 122 | auto formattedText = f.GetFormatResult(t); |
| 123 | auto replaceRange = f.GetReplaceRange(); |
| 124 | |
| 125 | if (context.IsCompleteRangeFormat()) { |
| 126 | start = file->GetOffset(replaceRange.StartLine, replaceRange.StartCol); |
| 127 | end = file->GetOffset(replaceRange.EndLine + 1, 0); |
| 128 | std::string source = std::string(file->GetSource()); |
| 129 | source.replace(start, end - start, formattedText); |
| 130 | formattedText = source; |
| 131 | } |
no test coverage detected