• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 REFACTOR_TYPES_H
17 #define REFACTOR_TYPES_H
18 
19 #include "public/es2panda_lib.h"
20 #include "../cancellation_token.h"
21 #include "../user_preferences.h"
22 #include "../types.h"
23 #include "es2panda.h"
24 #include <string>
25 #include <string_view>
26 #include <vector>
27 
28 namespace ark::es2panda::lsp {
29 
30 struct RefactorEditInfo {
31 private:
32     std::vector<FileTextChanges> fileTextChanges_;
33 
34 public:
35     explicit RefactorEditInfo(std::vector<FileTextChanges> fileTextChanges = {})
fileTextChanges_RefactorEditInfo36         : fileTextChanges_(std::move(fileTextChanges))
37     {
38     }
39 
GetFileTextChangesRefactorEditInfo40     std::vector<FileTextChanges> &GetFileTextChanges()
41     {
42         return fileTextChanges_;
43     }
44 };
45 
46 struct TextRange {
47     size_t pos;
48     size_t end;
49 };
50 
51 struct RefactorContext {
52     ark::es2panda::lsp::CancellationToken *cancellationToken = nullptr;
53     ark::es2panda::lsp::UserPreferences *preferences = nullptr;
54     TextRange span = {0, 0};
55     es2panda_Context *context = nullptr;
56     std::string kind;
57 };
58 
59 using RefactorActionView = struct RefactorActionView {
60     std::string_view name;
61     std::string_view description;
62     std::string_view kind;
63 };
64 
65 using RefactorAction = struct RefactorAction {
66     std::string name;
67     std::string description;
68     std::string kind;
69 };
70 
71 using ApplicableRefactorInfo = struct ApplicableRefactorInfo {
72     std::string name;
73     std::string description;
74     RefactorAction action;
75 };
76 
77 namespace refactor_name {
78 constexpr std::string_view CONVERT_FUNCTION_REFACTOR_NAME = "Convert arrow function or function expression";
79 constexpr std::string_view CONVERT_EXPORT_REFACTOR_NAME = "Convert export";
80 constexpr std::string_view CONVERT_IMPORT_REFACTOR_NAME = "Convert import";
81 constexpr std::string_view CONVERT_TEMPLATE_REFACTOR_NAME = "Convert to template string";
82 constexpr std::string_view CONVERT_CHAIN_REFACTOR_NAME = "Convert to optional chain expression";
83 }  // namespace refactor_name
84 
85 namespace refactor_description {
86 constexpr std::string_view CONVERT_FUNCTION_REFACTOR_DESC = "Convert arrow function or function expression";
87 constexpr std::string_view CONVERT_TEMPLATE_REFACTOR_DESC = "Convert to template string";
88 constexpr std::string_view CONVERT_CHAIN_REFACTOR_DESC = "Convert to optional chain expression";
89 }  // namespace refactor_description
90 
91 class Refactor {
92 private:
93     std::vector<std::string> kinds_;
94 
95 public:
96     bool IsKind(const std::string &kind) const;
97     void AddKind(const std::string &kind);
98     virtual ApplicableRefactorInfo GetAvailableActions(const RefactorContext &context) const = 0;
99 
100     virtual std::unique_ptr<RefactorEditInfo> GetEditsForAction(const RefactorContext &context,
101                                                                 const std::string &actionName) const = 0;
102     virtual ~Refactor() = default;
103     Refactor() = default;
104     Refactor &operator=(const Refactor &other);
105     Refactor &operator=(Refactor &&other);
106     Refactor(const Refactor &other);
107     Refactor(Refactor &&other);
108 };
109 
110 }  // namespace ark::es2panda::lsp
111 
112 #endif  // REFACTOR_TYPES_H