| 92 | } |
| 93 | |
| 94 | Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, FormatRange &range, |
| 95 | std::string &&text, FormattingOptions options) { |
| 96 | auto file = std::make_shared<LuaSource>(std::move(text)); |
| 97 | LuaLexer luaLexer(file); |
| 98 | if (_supportNonStandardSymbol || options.non_standard_symbol) { |
| 99 | luaLexer.SupportNonStandardSymbol(); |
| 100 | } |
| 101 | if (_supportCLikeComments || options.non_standard_symbol) { |
| 102 | luaLexer.SupportCLikeComments(); |
| 103 | } |
| 104 | |
| 105 | luaLexer.Parse(); |
| 106 | |
| 107 | LuaParser p(file, std::move(luaLexer.GetTokens())); |
| 108 | p.Parse(); |
| 109 | |
| 110 | if (p.HasError()) { |
| 111 | return ResultType::Err; |
| 112 | } |
| 113 | |
| 114 | LuaSyntaxTree t; |
| 115 | t.BuildTree(p); |
| 116 | |
| 117 | LuaStyle style = GetStyle(uri); |
| 118 | SetupStyle(style, options); |
| 119 | |
| 120 | RangeFormatBuilder f(style, range); |
| 121 | |
| 122 | auto formattedText = f.GetFormatResult(t); |
| 123 | range = f.GetReplaceRange(); |
| 124 | |
| 125 | char *ptr = new char[formattedText.size() + 1]; |
| 126 | std::copy(formattedText.begin(), formattedText.end(), ptr); |
| 127 | ptr[formattedText.size()] = '\0';// [formattedText.size()] = '\0' |
| 128 | return RangeFormatResult{ |
| 129 | static_cast<int32_t>(range.StartLine), |
| 130 | static_cast<int32_t>(range.StartCol), |
| 131 | static_cast<int32_t>(range.EndLine), |
| 132 | static_cast<int32_t>(range.EndCol), |
| 133 | ptr}; |
| 134 | } |
| 135 | |
| 136 | void CodeFormat::SetupStyle(LuaStyle &style, const FormattingOptions &options) { |
| 137 | if (options.use_tabs) { |
no test coverage detected