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 "references.h"
17 #include <cstddef>
18 #include "api.h"
19 #include "compiler/lowering/util.h"
20 #include "ir/astNode.h"
21 #include "public/es2panda_lib.h"
22 #include "public/public.h"
23
24 namespace ark::es2panda::lsp {
25
IsValidReference(ir::AstNode * astNode)26 bool IsValidReference(ir::AstNode *astNode)
27 {
28 switch (astNode->Type()) {
29 case ark::es2panda::ir::AstNodeType::IDENTIFIER:
30 return true;
31 default:
32 return false;
33 }
34 }
35
GetDeclInfoImpl(ir::AstNode * astNode)36 DeclInfoType GetDeclInfoImpl(ir::AstNode *astNode)
37 {
38 if (astNode == nullptr || !astNode->IsIdentifier()) {
39 return {};
40 }
41 auto declNode = compiler::DeclarationFromIdentifier(astNode->AsIdentifier());
42 auto node = declNode;
43 while (node != nullptr) {
44 if (node->IsETSModule()) {
45 auto name = std::string(node->AsETSModule()->Program()->SourceFilePath());
46 return std::make_tuple(name, declNode->DumpEtsSrc());
47 }
48 node = node->Parent();
49 }
50 return {};
51 }
52
GetReferencesAtPositionImpl(es2panda_Context * context,const DeclInfoType & declInfo)53 References GetReferencesAtPositionImpl(es2panda_Context *context, const DeclInfoType &declInfo)
54 {
55 References result;
56 if (context == nullptr) {
57 return result;
58 }
59 auto ctx = reinterpret_cast<public_lib::Context *>(context);
60 if (ctx->parserProgram == nullptr || ctx->parserProgram->Ast() == nullptr) {
61 return result;
62 }
63 auto astNode = reinterpret_cast<ir::AstNode *>(ctx->parserProgram->Ast());
64 astNode->IterateRecursively([ctx, declInfo, &result](ir::AstNode *child) {
65 auto info = GetDeclInfoImpl(child);
66 if (info == declInfo) {
67 size_t startPos = child->Start().index;
68 size_t endPos = child->End().index;
69 result.referenceInfos.emplace_back(ctx->sourceFileName, startPos, endPos - startPos);
70 }
71 });
72 return result;
73 }
74
75 } // namespace ark::es2panda::lsp