• 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 "line_column_offset.h"
17 #include "public/es2panda_lib.h"
18 #include "public/public.h"
19 #include "internal_api.h"
20 
21 namespace ark::es2panda::lsp {
22 const size_t NO_POS = 0;
23 
ToLineColumnOffset(es2panda_Context * context,size_t position)24 LineAndCharacter ToLineColumnOffset(es2panda_Context *context, size_t position)
25 {
26     if (position == NO_POS) {
27         return LineAndCharacter(NO_POS, NO_POS);
28     }
29 
30     auto ctx = reinterpret_cast<public_lib::Context *>(context);
31     auto node = lsp::GetTouchingToken(context, position, false);
32     if (node == nullptr) {
33         return LineAndCharacter(NO_POS, NO_POS);
34     }
35 
36     auto program = ctx->parserProgram;
37     lexer::LineIndex index(program->SourceCode());
38     auto pos = node->Start();
39     lexer::SourceLocation loc = index.GetLocation(pos);
40     auto offset = index.GetOffset(loc);
41 
42     return LineAndCharacter(pos.line, offset);
43 }
44 }  // namespace ark::es2panda::lsp
45