1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CODE_FIX_PROVIDER_H 17 #define CODE_FIX_PROVIDER_H 18 #include <cstddef> 19 #include <iostream> 20 #include <memory> 21 #include <unordered_map> 22 #include "services/text_change/change_tracker.h" 23 #include "code_fixes/code_fix_types.h" 24 #include "es2panda.h" 25 26 namespace ark::es2panda::lsp { 27 28 class CodeFixProvider { 29 private: 30 std::unordered_map<std::string, std::shared_ptr<CodeFixRegistration>> errorCodeToFixes_; 31 std::unordered_map<std::string, std::shared_ptr<CodeFixRegistration>> fixIdToRegistration_; 32 33 public: GetErrorCodeToFixes()34 std::unordered_map<std::string, std::shared_ptr<CodeFixRegistration>> GetErrorCodeToFixes() const 35 { 36 return errorCodeToFixes_; 37 } GetFixIdToRegistration()38 std::unordered_map<std::string, std::shared_ptr<CodeFixRegistration>> GetFixIdToRegistration() const 39 { 40 return fixIdToRegistration_; 41 } 42 43 void RegisterCodeFix(const std::string &aliasName, std::unique_ptr<CodeFixRegistration> registration); 44 static CodeFixProvider &Instance(); 45 46 std::string FormatWithArgs(const std::string &text); 47 std::string DiagnosticToString(const DiagnosticAndArguments &diag); 48 CodeFixAction CreateCodeFixActionWorker(std::string &fixName, std::string &description, 49 std::vector<FileTextChanges> &changes, std::string &fixId, 50 std::string &fixAllDescription, std::vector<CodeActionCommand> command); 51 52 CodeFixAction CreateCodeFixActionWithoutFixAll(std::string &fixName, std::vector<FileTextChanges> &changes, 53 DiagnosticAndArguments &description); 54 CodeFixAction CreateCodeFixAction(std::string fixName, std::vector<FileTextChanges> changes, 55 DiagnosticAndArguments &description, std::string fixId, 56 DiagnosticAndArguments &fixAllDescription, 57 std::vector<CodeActionCommand> &command); 58 std::string GetFileName(const std::string &filePath); 59 std::vector<std::string> GetSupportedErrorCodes(); 60 DiagnosticReferences *GetDiagnostics(const CodeFixContextBase &context); 61 62 bool ShouldIncludeFixAll(const CodeFixRegistration ®istration, const std::vector<Diagnostic> &diagnostics); 63 std::vector<CodeFixAction> GetFixes(const CodeFixContext &context); 64 void EachDiagnostic(const CodeFixAllContext &context, const std::vector<int> &errorCodes, 65 const std::function<void(const DiagnosticWithLocation &)> &cb); 66 CombinedCodeActions CodeFixAll(const CodeFixAllContext &context, const std::vector<int> &errorCodes, 67 std::function<void(ChangeTracker &, const DiagnosticWithLocation &)> use); 68 FileTextChanges CreateFileTextChanges(const std::string &fileName, const std::vector<TextChange> &textChanges); 69 70 CombinedCodeActions GetAllFixes(const CodeFixAllContext &context); 71 }; 72 73 template <typename T> 74 struct AutoCodeFixRegister { AutoCodeFixRegisterAutoCodeFixRegister75 constexpr explicit AutoCodeFixRegister(const std::string &name, std::unique_ptr<T> registration) 76 { 77 CodeFixProvider::Instance().RegisterCodeFix(name, std::move(registration)); 78 } 79 AutoCodeFixRegisterAutoCodeFixRegister80 constexpr explicit AutoCodeFixRegister(const std::string &name) : AutoCodeFixRegister(name, std::make_unique<T>()) 81 { 82 } 83 }; 84 } // namespace ark::es2panda::lsp 85 #endif 86