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 #include "refactor_provider.h"
17 #include "refactors/refactor_types.h"
18
19 namespace ark::es2panda::lsp {
20
RegisterRefactor(const std::string & name,std::unique_ptr<Refactor> refactor)21 void RefactorProvider::RegisterRefactor(const std::string &name, std::unique_ptr<Refactor> refactor)
22 {
23 refactors_.emplace(name, std::move(refactor));
24 }
25
Instance()26 RefactorProvider &RefactorProvider::Instance()
27 {
28 static RefactorProvider instance;
29 return instance;
30 }
31
GetEditsForRefactor(const RefactorContext & context,const std::string & refactorName,const std::string & actionName) const32 std::unique_ptr<RefactorEditInfo> RefactorProvider::GetEditsForRefactor(const RefactorContext &context,
33 const std::string &refactorName,
34 const std::string &actionName) const
35 {
36 auto it = refactors_.find(refactorName);
37 if (it == refactors_.end()) {
38 return nullptr;
39 }
40
41 return it->second->GetEditsForAction(context, actionName);
42 }
43
GetApplicableRefactors(const RefactorContext & context) const44 std::vector<ApplicableRefactorInfo> RefactorProvider::GetApplicableRefactors(const RefactorContext &context) const
45 {
46 std::vector<ApplicableRefactorInfo> applicable;
47
48 for (const auto &[name, refactor] : refactors_) {
49 auto result = refactor->GetAvailableActions(context);
50 if (!result.name.empty()) {
51 applicable.push_back(result);
52 }
53 }
54
55 return applicable;
56 }
57
GetRefactors() const58 const std::unordered_map<std::string, std::shared_ptr<Refactor>> &RefactorProvider::GetRefactors() const
59 {
60 return refactors_;
61 }
62
63 } // namespace ark::es2panda::lsp
64