• 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 CODE_FIX_TYPES_H
17 #define CODE_FIX_TYPES_H
18 
19 #include <cstddef>
20 #include <iostream>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 #include "../user_preferences.h"
25 #include "../cancellation_token.h"
26 #include "../types.h"
27 #include "../api.h"
28 #include "es2panda.h"
29 #include "public/es2panda_lib.h"
30 #include "public/public.h"
31 #include "../get_class_property_info.h"
32 
33 namespace ark::es2panda::lsp {
34 
35 enum DiagnosticCategory { WARNING, ERROR, SUGGESTION, MESSAGE };
36 
37 struct DiagnosticMessage {
38     std::string key;
39     DiagnosticCategory category;
40     size_t code;
41     std::string message;
42     std::string reportsUnnecessary = {};
43     std::string reportsDeprecated = {};
44     bool elidedInCompatabilityPyramid;
45 };
46 struct DiagnosticAndArguments {
47     DiagnosticMessage message;
48     std::vector<std::string> arguments;
49 };
50 
51 struct CodeAction {
52     std::string description;
53     std::vector<FileTextChanges> changes;
54     std::vector<CodeActionCommand> commands;
55 };
56 struct CombinedCodeActions {
57     std::vector<FileTextChanges> changes;
58     std::vector<CodeActionCommand> commands;
59 };
60 struct CodeFixAction : CodeAction {
61     std::string fixName;
62     std::string fixId = {};
63     std::string fixAllDescription = {};
64 };
65 
66 struct CodeFixContextBase : TextChangesContext {
67     es2panda_Context *context;
68     CancellationToken cancellationToken;
69 };
70 
71 struct CodeFixAllContext : CodeFixContextBase {
72     std::string fixId = {};
73 };
74 
75 struct DiagnosticWithLocation : Diagnostic {
76     SourceFile file;
77     size_t start = 0;
78     size_t length = 0;
79 };
80 
81 struct CodeFixContext : CodeFixContextBase {
82     int errorCode = 0;
83     TextSpan span = {0, 0};
84 };
85 
86 class CodeFixRegistration {
87 private:
88     std::vector<int> errorCodes_;
89     std::vector<std::string> fixIds_;
90 
91 public:
GetErrorCodes()92     std::vector<int> GetErrorCodes() const
93     {
94         return errorCodes_;
95     }
GetFixIds()96     std::vector<std::string> GetFixIds() const
97     {
98         return fixIds_;
99     }
SetErrorCodes(const std::vector<int> & codes)100     void SetErrorCodes(const std::vector<int> &codes)
101     {
102         errorCodes_ = codes;
103     }
SetFixIds(const std::vector<std::string> & ids)104     void SetFixIds(const std::vector<std::string> &ids)
105     {
106         fixIds_ = ids;
107     }
108     virtual std::vector<CodeFixAction> GetCodeActions(const CodeFixContext &) = 0;
109     virtual CombinedCodeActions GetAllCodeActions(const CodeFixAllContext &) = 0;
110     CodeFixRegistration() = default;
111     CodeFixRegistration(const CodeFixRegistration &) = delete;
112     CodeFixRegistration &operator=(const CodeFixRegistration &) = delete;
113     CodeFixRegistration(CodeFixRegistration &&) = delete;
114     CodeFixRegistration &operator=(CodeFixRegistration &&) = delete;
115     virtual ~CodeFixRegistration() = default;
116 };
117 
118 }  // namespace ark::es2panda::lsp
119 
120 #endif
121