• 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 "formatting/formatting_context.h"
17 #include "brace_matching.h"
18 #include "public/public.h"
19 
20 namespace ark::es2panda::lsp {
21 
FormattingContext(FormattingRequestKind requestKind,FormatCodeSettings & formatSettings)22 FormattingContext::FormattingContext(FormattingRequestKind requestKind, FormatCodeSettings &formatSettings)
23 {
24     formatCodeSettings_ = formatSettings;
25     formattingRequestKind_ = requestKind;
26 }
27 
UpdateContext(es2panda_Context * context,RangeWithKind & currentToken,RangeWithKind & nextToken)28 void FormattingContext::UpdateContext(es2panda_Context *context, RangeWithKind &currentToken, RangeWithKind &nextToken)
29 {
30     currentTokenSpan_ = currentToken;
31     nextTokenSpan_ = nextToken;
32 
33     if (context == nullptr) {
34         return;
35     }
36 
37     auto ctx = reinterpret_cast<public_lib::Context *>(context);
38     if (ctx->parserProgram == nullptr || ctx->parserProgram->Ast() == nullptr) {
39         return;
40     }
41 
42     contextNode_ = reinterpret_cast<ir::AstNode *>(ctx->parserProgram->Ast());
43 
44     auto current = GetTouchingToken(context, currentTokenSpan_.start.index, false);
45     if (current == nullptr) {
46         return;
47     }
48     currentTokenParent_ = current->Parent();
49 
50     nextTokenParent_ = GetTouchingToken(context, nextToken.start.index, false)->Parent();
51 
52     contextNodeAllOnSameLine_ = NodeIsOnOneLine(contextNode_);
53     nextNodeAllOnSameLine_ = NodeIsOnOneLine(nextTokenParent_);
54     tokensAreOnSameLine_ = currentTokenSpan_.start.ToLocation().line == nextTokenSpan_.start.ToLocation().line;
55     contextNodeBlockIsOnOneLine_ = BlockIsOnOneLine(contextNode_);
56     nextNodeBlockIsOnOneLine_ = BlockIsOnOneLine(nextTokenParent_);
57 }
58 
ContextNodeAllOnSameLine() const59 bool FormattingContext::ContextNodeAllOnSameLine() const
60 {
61     return contextNodeAllOnSameLine_;
62 }
63 
NextNodeAllOnSameLine() const64 bool FormattingContext::NextNodeAllOnSameLine() const
65 {
66     return nextNodeAllOnSameLine_;
67 }
68 
TokensAreOnSameLine() const69 bool FormattingContext::TokensAreOnSameLine() const
70 {
71     return tokensAreOnSameLine_;
72 }
73 
ContextNodeBlockIsOnOneLine() const74 bool FormattingContext::ContextNodeBlockIsOnOneLine() const
75 {
76     return contextNodeBlockIsOnOneLine_;
77 }
78 
NextNodeBlockIsOnOneLine() const79 bool FormattingContext::NextNodeBlockIsOnOneLine() const
80 {
81     return nextNodeBlockIsOnOneLine_;
82 }
83 
NodeIsOnOneLine(ir::AstNode * node) const84 bool FormattingContext::NodeIsOnOneLine(ir::AstNode *node) const
85 {
86     if (node == nullptr) {
87         return false;
88     }
89 
90     return node->Start().line == node->End().line;
91 }
92 
BlockIsOnOneLine(ir::AstNode * node) const93 bool FormattingContext::BlockIsOnOneLine(ir::AstNode *node) const
94 {
95     if (node == nullptr) {
96         return false;
97     }
98 
99     auto nodeChild = node->FindChild([](ir::AstNode *astnode) { return CheckNodeKindForBraceMatching(astnode); });
100 
101     return NodeIsOnOneLine(nodeChild);
102 }
103 
104 }  // namespace ark::es2panda::lsp