• 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 #include "brace_matching.h"
17 #include <cstddef>
18 #include "public/public.h"
19 
20 namespace ark::es2panda::lsp {
CheckNodeKindForBraceMatching(ark::es2panda::ir::AstNode * node)21 bool CheckNodeKindForBraceMatching(ark::es2panda::ir::AstNode *node)
22 {
23     switch (node->Type()) {
24         case ark::es2panda::ir::AstNodeType::BLOCK_STATEMENT:
25         case ark::es2panda::ir::AstNodeType::ARRAY_EXPRESSION:
26         case ark::es2panda::ir::AstNodeType::ETS_TUPLE:
27         case ark::es2panda::ir::AstNodeType::TS_TYPE_PARAMETER_INSTANTIATION:
28         case ark::es2panda::ir::AstNodeType::TS_TYPE_PARAMETER_DECLARATION:
29             return true;
30         default:
31             return false;
32     }
33 }
34 
GetBraceMatchingAtPosition(es2panda_Context * context,size_t position)35 std::vector<TextSpan> GetBraceMatchingAtPosition(es2panda_Context *context, size_t position)
36 {
37     auto token = GetTouchingToken(context, position, false);
38     if (token == nullptr) {
39         return {};
40     }
41 
42     if (CheckNodeKindForBraceMatching(token) && token->Start().index == position) {
43         TextSpan startSpan(token->Start().index, token->End().index - token->Start().index);
44         TextSpan endSpan(token->End().index, token->End().index - token->Start().index);
45 
46         std::vector<TextSpan> spans = {startSpan, endSpan};
47         return spans;
48     }
49     return {};
50 }
51 }  // namespace ark::es2panda::lsp