• 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 ES2PANDA_LSP_INCLUDE_INTERNAL_API_H
17 #define ES2PANDA_LSP_INCLUDE_INTERNAL_API_H
18 
19 #include <vector>
20 #include "api.h"
21 #include "checker/types/type.h"
22 #include "ir/astNode.h"
23 #include "public/es2panda_lib.h"
24 
25 namespace ark::es2panda::lsp {
26 
27 class Initializer {
28 public:
29     Initializer();
30 
31     ~Initializer();
32 
Allocator()33     ark::ArenaAllocator *Allocator()
34     {
35         return allocator_;
36     }
37 
38     es2panda_Context *CreateContext(char const *fileName, es2panda_ContextState state, char const *fileSource = nullptr)
39     {
40         es2panda_Context *ctx = nullptr;
41         if (fileSource != nullptr) {
42             ctx = impl_->CreateContextFromString(cfg_, fileSource, fileName);
43         } else {
44             ctx = impl_->CreateContextFromFile(cfg_, fileName);
45         }
46         impl_->ProceedToState(ctx, state);
47         return ctx;
48     }
49 
DestroyContext(es2panda_Context * context)50     void DestroyContext(es2panda_Context *context)
51     {
52         impl_->DestroyContext(context);
53     }
54 
CreateDiagnosticKind(es2panda_Context * context,const char * dmessage,es2panda_PluginDiagnosticType etype)55     const es2panda_DiagnosticKind *CreateDiagnosticKind(es2panda_Context *context, const char *dmessage,
56                                                         es2panda_PluginDiagnosticType etype)
57     {
58         return impl_->CreateDiagnosticKind(context, dmessage, etype);
59     }
60 
CreateSuggestionInfo(es2panda_Context * context,const es2panda_DiagnosticKind * kind,const char ** args,size_t argc,const char * substitutionCode)61     es2panda_SuggestionInfo *CreateSuggestionInfo(es2panda_Context *context, const es2panda_DiagnosticKind *kind,
62                                                   const char **args, size_t argc, const char *substitutionCode)
63     {
64         return impl_->CreateSuggestionInfo(context, kind, args, argc, substitutionCode);
65     }
66 
CreateDiagnosticInfo(es2panda_Context * context,const es2panda_DiagnosticKind * kind,const char ** args,size_t argc)67     es2panda_DiagnosticInfo *CreateDiagnosticInfo(es2panda_Context *context, const es2panda_DiagnosticKind *kind,
68                                                   const char **args, size_t argc)
69     {
70         return impl_->CreateDiagnosticInfo(context, kind, args, argc);
71     }
72 
CreateSourcePosition(es2panda_Context * context,size_t index,size_t line)73     es2panda_SourcePosition *CreateSourcePosition(es2panda_Context *context, size_t index, size_t line)
74     {
75         return impl_->CreateSourcePosition(context, index, line);
76     }
77 
CreateSourceRange(es2panda_Context * context,es2panda_SourcePosition * start,es2panda_SourcePosition * end)78     es2panda_SourceRange *CreateSourceRange(es2panda_Context *context, es2panda_SourcePosition *start,
79                                             es2panda_SourcePosition *end)
80     {
81         return impl_->CreateSourceRange(context, start, end);
82     }
83 
LogDiagnosticWithSuggestion(es2panda_Context * context,const es2panda_DiagnosticInfo * diagnosticInfo,const es2panda_SuggestionInfo * suggestionInfo,es2panda_SourceRange * range)84     void LogDiagnosticWithSuggestion(es2panda_Context *context, const es2panda_DiagnosticInfo *diagnosticInfo,
85                                      const es2panda_SuggestionInfo *suggestionInfo, es2panda_SourceRange *range)
86     {
87         return impl_->LogDiagnosticWithSuggestion(context, diagnosticInfo, suggestionInfo, range);
88     }
89 
LogDiagnostic(es2panda_Context * context,const es2panda_DiagnosticKind * ekind,const char ** args,size_t argc,es2panda_SourcePosition * pos)90     void LogDiagnostic(es2panda_Context *context, const es2panda_DiagnosticKind *ekind, const char **args, size_t argc,
91                        es2panda_SourcePosition *pos)
92     {
93         return impl_->LogDiagnostic(context, ekind, args, argc, pos);
94     }
95 
96     NO_COPY_SEMANTIC(Initializer);
97     NO_MOVE_SEMANTIC(Initializer);
98 
99 private:
100     es2panda_Impl const *impl_;
101     es2panda_Config *cfg_;
102     ark::ArenaAllocator *allocator_;
103 };
104 
105 ir::AstNode *GetTouchingToken(es2panda_Context *context, size_t pos, bool flagFindFirstMatch);
106 References GetFileReferencesImpl(es2panda_Context *referenceFileContext, char const *searchFileName,
107                                  bool isPackageModule);
108 ir::AstNode *FindPrecedingToken(const size_t pos, const ir::AstNode *startNode, ArenaAllocator *allocator);
109 ir::AstNode *GetIdentifierFromSuper(ir::AstNode *super);
110 ir::AstNode *GetOriginalNode(ir::AstNode *astNode);
111 checker::VerifiedType GetTypeOfSymbolAtLocation(checker::ETSChecker *checker, ir::AstNode *astNode);
112 FileDiagnostic CreateDiagnosticForNode(es2panda_AstNode *node, Diagnostic diagnostic,
113                                        const std::vector<std::string> &args = std::vector<std::string>());
114 std::string GetCurrentTokenValueImpl(es2panda_Context *context, size_t position);
115 void GetRangeOfEnclosingComment(es2panda_Context *context, size_t pos, CommentRange *result);
116 Diagnostic CreateDiagnosticForError(es2panda_Context *context, const util::DiagnosticBase &error);
117 Diagnostic CreateDiagnosticWithoutFile(const util::DiagnosticBase &error);
118 void GetGlobalDiagnostics(es2panda_Context *context, DiagnosticReferences &compilerOptionsDiagnostics);
119 void GetOptionDiagnostics(es2panda_Context *context, DiagnosticReferences &compilerOptionsDiagnostics);
120 size_t GetTokenPosOfNode(const ir::AstNode *astNode);
121 std::pair<ir::AstNode *, util::StringView> GetDefinitionAtPositionImpl(es2panda_Context *context, size_t pos);
122 DocumentHighlights GetDocumentHighlightsImpl(es2panda_Context *context, size_t position);
123 
124 void GetReferenceLocationAtPositionImpl(FileNodeInfo fileNodeInfo, es2panda_Context *referenceFileContext,
125                                         ReferenceLocationList *list);
126 void RemoveFromFiles(std::vector<std::string> &files, const std::vector<std::string> &autoGenerateFolders);
127 ir::AstNode *FindRightToken(const size_t pos, const ArenaVector<ir::AstNode *> &nodes);
128 std::string GetOwnerId(ir::AstNode *node);
129 std::string GetIdentifierName(ir::AstNode *node);
130 bool NodeHasTokens(const ir::AstNode *node);
131 void FindAllChild(const ir::AstNode *ast, const ir::NodePredicate &cb, ArenaVector<ir::AstNode *> &results);
132 ir::AstNode *FindAncestor(ir::AstNode *node, const ir::NodePredicate &cb);
133 std::vector<CodeFixActionInfo> GetCodeFixesAtPositionImpl(es2panda_Context *context, size_t startPosition,
134                                                           size_t endPosition, std::vector<int> &errorCodes,
135                                                           CodeFixOptions &codeFixOptions);
136 CombinedCodeActionsInfo GetCombinedCodeFixImpl(es2panda_Context *context, const std::string &fixId,
137                                                CodeFixOptions &codeFixOptions);
138 ir::Identifier *GetIdentFromNewClassExprPart(const ir::Expression *value);
139 }  // namespace ark::es2panda::lsp
140 
141 #endif