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 ES2PANDA_LSP_INCLUDE_REFACTOR_PROVIDER_H 17 #define ES2PANDA_LSP_INCLUDE_REFACTOR_PROVIDER_H 18 19 #include "refactors/refactor_types.h" 20 #include <vector> 21 #include <memory> 22 #include <string> 23 24 namespace ark::es2panda::lsp { 25 26 class RefactorProvider { 27 public: 28 void RegisterRefactor(const std::string &name, std::unique_ptr<Refactor> refactor); 29 static RefactorProvider &Instance(); 30 std::unique_ptr<RefactorEditInfo> GetEditsForRefactor(const RefactorContext &context, 31 const std::string &refactorName, 32 const std::string &actionName) const; 33 std::vector<ApplicableRefactorInfo> GetApplicableRefactors(const RefactorContext &context) const; 34 35 const std::unordered_map<std::string, std::shared_ptr<Refactor>> &GetRefactors() const; 36 37 private: 38 std::unordered_map<std::string, std::shared_ptr<Refactor>> refactors_; 39 }; 40 41 template <typename T> 42 struct AutoRefactorRegister { AutoRefactorRegisterAutoRefactorRegister43 constexpr explicit AutoRefactorRegister(const std::string &name, std::unique_ptr<T> refactor) 44 { 45 RefactorProvider::Instance().RegisterRefactor(name, std::move(refactor)); 46 } 47 AutoRefactorRegisterAutoRefactorRegister48 constexpr explicit AutoRefactorRegister(const std::string &name) : AutoRefactorRegister(name, std::make_unique<T>()) 49 { 50 } 51 }; 52 53 } // namespace ark::es2panda::lsp 54 #endif // ES2PANDA_LSP_INCLUDE_REFACTOR_PROVIDER_H 55