|
| 1 | +#include "CodeFormat.h" |
| 2 | + |
| 3 | +#include "CodeFormatCore/Config/LuaEditorConfig.h" |
| 4 | +#include "CodeFormatCore/RangeFormat/RangeFormatBuilder.h" |
| 5 | +#include "LuaParser/Parse/LuaParser.h" |
| 6 | +#include "Util/StringUtil.h" |
| 7 | + |
| 8 | +CodeFormat &CodeFormat::GetInstance() { |
| 9 | + static CodeFormat instance; |
| 10 | + return instance; |
| 11 | +} |
| 12 | + |
| 13 | +CodeFormat::CodeFormat() |
| 14 | + : _supportNonStandardSymbol(false), |
| 15 | + _supportCLikeComments(false) { |
| 16 | +} |
| 17 | + |
| 18 | +void CodeFormat::UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath) { |
| 19 | + for (auto &config: _configs) { |
| 20 | + if (config.Workspace == workspaceUri) { |
| 21 | + config.Editorconfig = LuaEditorConfig::OpenFile(configPath); |
| 22 | + return; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + auto &config = _configs.emplace_back( |
| 27 | + workspaceUri); |
| 28 | + config.Editorconfig = LuaEditorConfig::OpenFile(configPath); |
| 29 | +} |
| 30 | + |
| 31 | +void CodeFormat::UpdateDiagnosticStyle(InfoTree &tree) { |
| 32 | + _diagnosticStyle = LuaDiagnosticStyle(); |
| 33 | + _diagnosticStyle.ParseTree(tree); |
| 34 | + _diagnosticStyle.code_style_check = true; |
| 35 | + _diagnosticStyle.name_style_check = false; |
| 36 | + _diagnosticStyle.spell_check = false; |
| 37 | +} |
| 38 | + |
| 39 | +void CodeFormat::RemoveCodeStyle(const std::string &workspaceUri) { |
| 40 | + for (auto it = _configs.begin(); it != _configs.end(); it++) { |
| 41 | + if (it->Workspace == workspaceUri) { |
| 42 | + _configs.erase(it); |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void CodeFormat::SetDefaultCodeStyle(ConfigMap &configMap) { |
| 49 | + if (!configMap.empty()) { |
| 50 | + LuaStyle style; |
| 51 | + style.Parse(configMap); |
| 52 | + _defaultStyle = style; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void CodeFormat::SupportNonStandardSymbol() { |
| 57 | + _supportNonStandardSymbol = true; |
| 58 | +} |
| 59 | + |
| 60 | +Result<char*> CodeFormat::Reformat(const std::string &uri, std::string &&text) { |
| 61 | + auto file = std::make_shared<LuaSource>(std::move(text)); |
| 62 | + LuaLexer luaLexer(file); |
| 63 | + if (_supportNonStandardSymbol) { |
| 64 | + luaLexer.SupportNonStandardSymbol(); |
| 65 | + } |
| 66 | + if (_supportCLikeComments) { |
| 67 | + luaLexer.SupportCLikeComments(); |
| 68 | + } |
| 69 | + |
| 70 | + luaLexer.Parse(); |
| 71 | + |
| 72 | + LuaParser p(file, std::move(luaLexer.GetTokens())); |
| 73 | + p.Parse(); |
| 74 | + |
| 75 | + if (p.HasError()) { |
| 76 | + return ResultType::Err; |
| 77 | + } |
| 78 | + |
| 79 | + LuaSyntaxTree t; |
| 80 | + t.BuildTree(p); |
| 81 | + |
| 82 | + LuaStyle style = GetStyle(uri); |
| 83 | + |
| 84 | + FormatBuilder f(style); |
| 85 | + |
| 86 | + auto result = f.GetFormatResult(t); |
| 87 | + // 由于可能存在sso string,所以需要拷贝一份 |
| 88 | + char* ptr = new char[result.size() + 1]; |
| 89 | + std::copy(result.begin(), result.end(), ptr); |
| 90 | + ptr[result.size()] = '\0'; // [result.size()] = '\0' |
| 91 | + return ptr; |
| 92 | +} |
| 93 | + |
| 94 | +Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, FormatRange &range, |
| 95 | + std::string &&text) { |
| 96 | + auto file = std::make_shared<LuaSource>(std::move(text)); |
| 97 | + LuaLexer luaLexer(file); |
| 98 | + if (_supportNonStandardSymbol) { |
| 99 | + luaLexer.SupportNonStandardSymbol(); |
| 100 | + } |
| 101 | + if (_supportCLikeComments) { |
| 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 | + |
| 119 | + RangeFormatBuilder f(style, range); |
| 120 | + |
| 121 | + auto formattedText = f.GetFormatResult(t); |
| 122 | + range = f.GetReplaceRange(); |
| 123 | + |
| 124 | + char* ptr = new char[formattedText.size() + 1]; |
| 125 | + std::copy(formattedText.begin(), formattedText.end(), ptr); |
| 126 | + ptr[formattedText.size()] = '\0'; // [formattedText.size()] = '\0' |
| 127 | + return RangeFormatResult{ |
| 128 | + static_cast<int>(range.StartLine), |
| 129 | + static_cast<int>(range.StartCol), |
| 130 | + static_cast<int>(range.EndLine), |
| 131 | + static_cast<int>(range.EndCol), |
| 132 | + ptr |
| 133 | + }; |
| 134 | +} |
| 135 | + |
| 136 | +Result<std::vector<LuaTypeFormat::Result>> |
| 137 | +CodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text) { |
| 138 | + auto file = std::make_shared<LuaSource>(std::move(text)); |
| 139 | + LuaLexer luaLexer(file); |
| 140 | + if (_supportNonStandardSymbol) { |
| 141 | + luaLexer.SupportNonStandardSymbol(); |
| 142 | + } |
| 143 | + if (_supportCLikeComments) { |
| 144 | + luaLexer.SupportCLikeComments(); |
| 145 | + } |
| 146 | + |
| 147 | + luaLexer.Parse(); |
| 148 | + |
| 149 | + LuaParser p(file, std::move(luaLexer.GetTokens())); |
| 150 | + p.Parse(); |
| 151 | + |
| 152 | + if (p.HasError()) { |
| 153 | + return ResultType::Err; |
| 154 | + } |
| 155 | + |
| 156 | + LuaSyntaxTree t; |
| 157 | + t.BuildTree(p); |
| 158 | + |
| 159 | + LuaStyle style = GetStyle(uri); |
| 160 | + |
| 161 | + |
| 162 | + LuaTypeFormatFeatures typeFormatOptions; |
| 163 | + typeFormatOptions.auto_complete_end = false; |
| 164 | + LuaTypeFormat tf(typeFormatOptions); |
| 165 | + tf.Analyze("\n", line, character, t, style); |
| 166 | + return tf.GetResult(); |
| 167 | +} |
| 168 | + |
| 169 | +Result<std::vector<LuaDiagnosticInfo>> CodeFormat::Diagnostic(const std::string &uri, std::string &&text) { |
| 170 | + auto file = std::make_shared<LuaSource>(std::move(text)); |
| 171 | + LuaLexer luaLexer(file); |
| 172 | + if (_supportNonStandardSymbol) { |
| 173 | + luaLexer.SupportNonStandardSymbol(); |
| 174 | + } |
| 175 | + if (_supportCLikeComments) { |
| 176 | + luaLexer.SupportCLikeComments(); |
| 177 | + } |
| 178 | + |
| 179 | + luaLexer.Parse(); |
| 180 | + |
| 181 | + LuaParser p(file, std::move(luaLexer.GetTokens())); |
| 182 | + p.Parse(); |
| 183 | + |
| 184 | + if (p.HasError()) { |
| 185 | + return ResultType::Err; |
| 186 | + } |
| 187 | + |
| 188 | + LuaSyntaxTree t; |
| 189 | + t.BuildTree(p); |
| 190 | + |
| 191 | + LuaStyle style = GetStyle(uri); |
| 192 | + |
| 193 | + DiagnosticBuilder diagnosticBuilder(style, _diagnosticStyle); |
| 194 | + diagnosticBuilder.CodeStyleCheck(t); |
| 195 | + return MakeDiagnosticInfo(diagnosticBuilder.GetDiagnosticResults(t), file); |
| 196 | +} |
| 197 | + |
| 198 | +LuaStyle &CodeFormat::GetStyle(const std::string &uri) { |
| 199 | + std::shared_ptr<LuaEditorConfig> editorConfig = nullptr; |
| 200 | + std::size_t matchProcess = 0; |
| 201 | + for (auto &config: _configs) { |
| 202 | + if (string_util::StartWith(uri, config.Workspace)) { |
| 203 | + if (config.Workspace.size() > matchProcess) { |
| 204 | + matchProcess = config.Workspace.size(); |
| 205 | + editorConfig = config.Editorconfig; |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + if (editorConfig) { |
| 211 | + return editorConfig->Generate(uri); |
| 212 | + } |
| 213 | + return _defaultStyle; |
| 214 | +} |
| 215 | + |
| 216 | +std::vector<LuaDiagnosticInfo> CodeFormat::MakeDiagnosticInfo(const std::vector<LuaDiagnostic> &diagnostics, |
| 217 | + std::shared_ptr<LuaSource> file) { |
| 218 | + std::vector<LuaDiagnosticInfo> results; |
| 219 | + for (auto &diagnostic: diagnostics) { |
| 220 | + auto &result = results.emplace_back(); |
| 221 | + result.Type = diagnostic.Type; |
| 222 | + result.Message = diagnostic.Message; |
| 223 | + result.Data = diagnostic.Data; |
| 224 | + result.Start.Line = file->GetLine(diagnostic.Range.StartOffset); |
| 225 | + result.Start.Col = file->GetColumn(diagnostic.Range.StartOffset); |
| 226 | + result.End.Line = file->GetLine(diagnostic.Range.GetEndOffset()); |
| 227 | + result.End.Col = file->GetColumn(diagnostic.Range.GetEndOffset()) + 1; |
| 228 | + } |
| 229 | + |
| 230 | + return results; |
| 231 | +} |
0 commit comments