• 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 FORMATTING_CONTEXT_H
17 #define FORMATTING_CONTEXT_H
18 
19 #include "ir/astNode.h"
20 #include "formatting_settings.h"
21 
22 namespace ark::es2panda::lsp {
23 
24 enum class FormattingRequestKind {
25     FORMAT_DOCUMENT,
26     FORMAT_SELECTION,
27     FORMAT_ON_ENTER,
28     FORMAT_ON_SEMICOLON,
29     FORMAT_ON_OPENING_CURLY_BRACE,
30     FORMAT_ON_CLOSING_CURLY_BRACE,
31 };
32 
33 struct RangeWithKind : lexer::SourceRange {
34 private:
35     ir::AstNodeType kind_;
36 
37 public:
38     explicit RangeWithKind(lexer::SourcePosition startPos = lexer::SourcePosition(),
39                            lexer::SourcePosition endPos = lexer::SourcePosition(),
40                            ir::AstNodeType nodeKind = ir::AstNodeType::DUMMYNODE)
SourceRangeRangeWithKind41         : lexer::SourceRange(startPos, endPos), kind_(nodeKind)
42     {
43     }
44 
GetKindRangeWithKind45     const ir::AstNodeType &GetKind()
46     {
47         return kind_;
48     }
49 };
50 
51 struct FormattingContext {
52 public:
53     explicit FormattingContext(FormattingRequestKind requestKind, FormatCodeSettings &formatSettings);
54 
55     void UpdateContext(es2panda_Context *context, RangeWithKind &currentToken, RangeWithKind &nextToken);
56 
57     bool ContextNodeAllOnSameLine() const;
58     bool NextNodeAllOnSameLine() const;
59     bool TokensAreOnSameLine() const;
60     bool ContextNodeBlockIsOnOneLine() const;
61     bool NextNodeBlockIsOnOneLine() const;
62 
GetCurrentTokenSpanFormattingContext63     const RangeWithKind &GetCurrentTokenSpan() const
64     {
65         return currentTokenSpan_;
66     }
GetNextTokenSpanFormattingContext67     const RangeWithKind &GetNextTokenSpan() const
68     {
69         return nextTokenSpan_;
70     }
71 
GetContextNodeFormattingContext72     ir::AstNode *GetContextNode() const
73     {
74         return contextNode_;
75     }
GetCurrentTokenParentFormattingContext76     ir::AstNode *GetCurrentTokenParent() const
77     {
78         return currentTokenParent_;
79     }
GetNextTokenParentFormattingContext80     ir::AstNode *GetNextTokenParent() const
81     {
82         return nextTokenParent_;
83     }
84 
GetFormatCodeSettingsFormattingContext85     FormatCodeSettings GetFormatCodeSettings()
86     {
87         return formatCodeSettings_;
88     }
89 
GetformattingRequestKindFormattingContext90     FormattingRequestKind GetformattingRequestKind()
91     {
92         return formattingRequestKind_;
93     }
94 
95 private:
96     bool NodeIsOnOneLine(ir::AstNode *node) const;
97     bool BlockIsOnOneLine(ir::AstNode *node) const;
98 
99     bool contextNodeAllOnSameLine_ = false;
100     bool nextNodeAllOnSameLine_ = false;
101     bool tokensAreOnSameLine_ = false;
102     bool contextNodeBlockIsOnOneLine_ = false;
103     bool nextNodeBlockIsOnOneLine_ = false;
104 
105     RangeWithKind currentTokenSpan_;
106     RangeWithKind nextTokenSpan_;
107     ir::AstNode *contextNode_ = nullptr;
108     ir::AstNode *currentTokenParent_ = nullptr;
109     ir::AstNode *nextTokenParent_ = nullptr;
110 
111     FormatCodeSettings formatCodeSettings_;
112     FormattingRequestKind formattingRequestKind_;
113 };
114 
115 }  // namespace ark::es2panda::lsp
116 
117 #endif