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 <string_view>
17 #include "refactors/convert_export.h"
18 #include "refactor_provider.h"
19 #include "internal_api.h"
20
21 namespace ark::es2panda::lsp {
22
ConvertExportRefactor()23 ConvertExportRefactor::ConvertExportRefactor()
24 {
25 AddKind(std::string(TO_NAMED_EXPORT_ACTION.kind));
26 AddKind(std::string(TO_DEFAULT_EXPORT_ACTION.kind));
27 }
28
GetAvailableActions(const RefactorContext & refContext) const29 ApplicableRefactorInfo ConvertExportRefactor::GetAvailableActions(const RefactorContext &refContext) const
30 {
31 es2panda_Context *context = refContext.context;
32 size_t position = refContext.span.pos;
33
34 ApplicableRefactorInfo res;
35
36 if (!IsKind(refContext.kind)) {
37 return res;
38 }
39
40 auto node = GetTouchingToken(context, position, false);
41 if (node == nullptr) {
42 return res;
43 }
44 auto cb = [](ir::AstNode *ancestorNode) { return ancestorNode->IsDefaultExported() || ancestorNode->IsExported(); };
45 auto ancestor = FindAncestor(node, cb);
46 if (ancestor == nullptr) {
47 return res;
48 }
49 if (ancestor->IsDefaultExported()) {
50 res.name = refactor_name::CONVERT_EXPORT_REFACTOR_NAME;
51 res.description = std::string(TO_NAMED_EXPORT_ACTION.description);
52 res.action.kind = std::string(TO_NAMED_EXPORT_ACTION.kind);
53 res.action.name = std::string(TO_NAMED_EXPORT_ACTION.name);
54 res.action.description = std::string(TO_NAMED_EXPORT_ACTION.description);
55 } else if (ancestor->IsExported()) {
56 res.name = refactor_name::CONVERT_EXPORT_REFACTOR_NAME;
57 res.description = std::string(TO_DEFAULT_EXPORT_ACTION.description);
58 res.action.kind = std::string(TO_DEFAULT_EXPORT_ACTION.kind);
59 res.action.name = std::string(TO_DEFAULT_EXPORT_ACTION.name);
60 res.action.description = std::string(TO_DEFAULT_EXPORT_ACTION.description);
61 }
62
63 return res;
64 }
65
GetEditsForAction(const RefactorContext & context,const std::string & actionName) const66 std::unique_ptr<RefactorEditInfo> ConvertExportRefactor::GetEditsForAction(const RefactorContext &context,
67 const std::string &actionName) const
68 {
69 (void)context;
70 (void)actionName;
71 return std::make_unique<RefactorEditInfo>();
72 }
73 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects, cert-err58-cpp)
74 AutoRefactorRegister<ConvertExportRefactor> g_convertExportRefactorRegister("ConvertExportRefactor");
75
76 } // namespace ark::es2panda::lsp
77