|
1 | 1 | #include "CodeFormatCore/Format/Analyzer/SemicolonAnalyzer.h" |
2 | | -#include "CodeFormatCore/Format/FormatState.h" |
3 | 2 | #include "CodeFormatCore/Config/LuaStyleEnum.h" |
4 | | -#include <LuaParser/Lexer/LuaTokenTypeDetail.h> |
| 3 | +#include "CodeFormatCore/Format/FormatState.h" |
| 4 | +#include "LuaParser/Lexer/LuaTokenTypeDetail.h" |
5 | 5 |
|
6 | 6 | SemicolonAnalyzer::SemicolonAnalyzer() { |
7 | 7 | } |
8 | 8 |
|
9 | | -void SemicolonAnalyzer::Analyze(FormatState& f, const LuaSyntaxTree& t) { |
10 | | - if (f.GetStyle().end_statement_with_semicolon == EndStmtWithSemicolon::Keep) { |
11 | | - return; // No analysis needed |
12 | | - } |
| 9 | +void SemicolonAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) { |
| 10 | + if (f.GetStyle().end_statement_with_semicolon == EndStmtWithSemicolon::Keep) { |
| 11 | + return;// No analysis needed |
| 12 | + } |
13 | 13 |
|
14 | | - for (auto syntaxNode : t.GetSyntaxNodes()) { |
15 | | - if (syntaxNode.IsNode(t)) { |
16 | | - if (detail::multi_match::StatementMatch(syntaxNode.GetSyntaxKind(t))) { |
17 | | - auto text = syntaxNode.GetText(t); |
18 | | - switch (f.GetStyle().end_statement_with_semicolon) { |
19 | | - case EndStmtWithSemicolon::Always: |
20 | | - if (!EndsWithSemicolon(syntaxNode, t)) { |
21 | | - AddSemicolon(syntaxNode, t); |
22 | | - } |
23 | | - break; |
24 | | - case EndStmtWithSemicolon::Never: |
25 | | - // is on same line as other statement -> needs to go on new line |
26 | | - if (!IsFirstStmtOfLine(syntaxNode, t)) { |
27 | | - InsertNewLineBeforeNode(syntaxNode, t); |
28 | | - } |
29 | | - if (EndsWithSemicolon(syntaxNode, t)) { |
30 | | - RemoveSemicolon(syntaxNode, t); |
31 | | - } |
32 | | - break; |
33 | | - case EndStmtWithSemicolon::SameLine: |
34 | | - if (EndsWithSemicolon(syntaxNode, t)) { |
35 | | - if (IsLastStmtOfLine(syntaxNode, t)) { |
36 | | - RemoveSemicolon(syntaxNode, t); |
37 | | - } |
38 | | - } |
39 | | - break; |
40 | | - default: |
41 | | - break; |
42 | | - } |
43 | | - } |
44 | | - } |
45 | | - } |
| 14 | + for (auto syntaxNode: t.GetSyntaxNodes()) { |
| 15 | + if (syntaxNode.IsNode(t)) { |
| 16 | + if (detail::multi_match::StatementMatch(syntaxNode.GetSyntaxKind(t))) { |
| 17 | + switch (f.GetStyle().end_statement_with_semicolon) { |
| 18 | + case EndStmtWithSemicolon::Always: { |
| 19 | + if (!EndsWithSemicolon(syntaxNode, t)) { |
| 20 | + AddSemicolon(syntaxNode, t); |
| 21 | + } |
| 22 | + break; |
| 23 | + } |
| 24 | + case EndStmtWithSemicolon::Never: { |
| 25 | + // is on same line as other statement -> needs to go on new line |
| 26 | + if (!IsFirstStmtOfLine(syntaxNode, t)) { |
| 27 | + InsertNewLineBeforeNode(syntaxNode, t); |
| 28 | + } |
| 29 | + if (EndsWithSemicolon(syntaxNode, t)) { |
| 30 | + RemoveSemicolon(syntaxNode, t); |
| 31 | + } |
| 32 | + break; |
| 33 | + } |
| 34 | + case EndStmtWithSemicolon::SameLine: { |
| 35 | + if (EndsWithSemicolon(syntaxNode, t)) { |
| 36 | + if (IsLastStmtOfLine(syntaxNode, t)) { |
| 37 | + RemoveSemicolon(syntaxNode, t); |
| 38 | + } |
| 39 | + } |
| 40 | + break; |
| 41 | + } |
| 42 | + default: { |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
46 | 49 | } |
47 | 50 |
|
48 | | -void SemicolonAnalyzer::Query(FormatState& f, LuaSyntaxNode n, const LuaSyntaxTree& t, FormatResolve& resolve) { |
49 | | - auto it = _semicolon.find(n.GetIndex()); |
50 | | - if (it != _semicolon.end()) { |
51 | | - auto& strategy = it->second; |
52 | | - switch (strategy) { |
53 | | - case SemicolonStrategy::Add: |
54 | | - resolve.SetTokenAddStrategy(TokenAddStrategy::StmtEndSemicolon); |
55 | | - break; |
56 | | - case SemicolonStrategy::Remove: |
57 | | - resolve.SetTokenStrategy(TokenStrategy::StmtEndSemicolon); |
58 | | - break; |
59 | | - case SemicolonStrategy::InsertNewLine: |
60 | | - resolve.SetTokenStrategy(TokenStrategy::NewLineBeforeToken); |
61 | | - break; |
62 | | - default: |
63 | | - break; |
64 | | - } |
65 | | - } |
| 51 | +void SemicolonAnalyzer::Query(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t, FormatResolve &resolve) { |
| 52 | + auto it = _semicolon.find(n.GetIndex()); |
| 53 | + if (it != _semicolon.end()) { |
| 54 | + auto &strategy = it->second; |
| 55 | + switch (strategy) { |
| 56 | + case SemicolonStrategy::Add: { |
| 57 | + resolve.SetTokenAddStrategy(TokenAddStrategy::StmtEndSemicolon); |
| 58 | + break; |
| 59 | + } |
| 60 | + case SemicolonStrategy::Remove: { |
| 61 | + resolve.SetTokenStrategy(TokenStrategy::StmtEndSemicolon); |
| 62 | + break; |
| 63 | + } |
| 64 | + case SemicolonStrategy::InsertNewLine: { |
| 65 | + resolve.SetTokenStrategy(TokenStrategy::NewLineBeforeToken); |
| 66 | + break; |
| 67 | + } |
| 68 | + default: { |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
66 | 73 | } |
67 | 74 |
|
68 | | -void SemicolonAnalyzer::AddSemicolon(LuaSyntaxNode n, const LuaSyntaxTree& t) { |
69 | | - auto token = GetLastNonCommentToken(n, t); |
70 | | - if (token.IsToken(t)) { |
71 | | - _semicolon[token.GetIndex()] = SemicolonStrategy::Add; |
72 | | - } |
| 75 | +void SemicolonAnalyzer::AddSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t) { |
| 76 | + auto token = GetLastNonCommentToken(n, t); |
| 77 | + if (token.IsToken(t)) { |
| 78 | + _semicolon[token.GetIndex()] = SemicolonStrategy::Add; |
| 79 | + } |
73 | 80 | } |
74 | 81 |
|
75 | | -void SemicolonAnalyzer::InsertNewLineBeforeNode(LuaSyntaxNode n, const LuaSyntaxTree& t) { |
76 | | - auto token = n.GetFirstToken(t); // line breaks are put in front of the statement itself by non-first statements |
77 | | - if (token.IsToken(t)) { |
78 | | - _semicolon[token.GetIndex()] = SemicolonStrategy::InsertNewLine; |
79 | | - } |
| 82 | +void SemicolonAnalyzer::InsertNewLineBeforeNode(LuaSyntaxNode n, const LuaSyntaxTree &t) { |
| 83 | + auto token = n.GetFirstToken(t); // line breaks are put in front of the statement itself by non-first statements |
| 84 | + if (token.IsToken(t)) { |
| 85 | + _semicolon[token.GetIndex()] = SemicolonStrategy::InsertNewLine; |
| 86 | + } |
80 | 87 | } |
81 | 88 |
|
82 | | -void SemicolonAnalyzer::RemoveSemicolon(LuaSyntaxNode n, const LuaSyntaxTree& t) { |
83 | | - auto token = GetLastNonCommentToken(n, t); |
84 | | - if (token.IsToken(t)) { |
85 | | - _semicolon[token.GetIndex()] = SemicolonStrategy::Remove; |
86 | | - } |
| 89 | +void SemicolonAnalyzer::RemoveSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t) { |
| 90 | + auto token = GetLastNonCommentToken(n, t); |
| 91 | + if (token.IsToken(t)) { |
| 92 | + _semicolon[token.GetIndex()] = SemicolonStrategy::Remove; |
| 93 | + } |
87 | 94 | } |
88 | 95 |
|
89 | | -bool SemicolonAnalyzer::IsFirstStmtOfLine(LuaSyntaxNode n, const LuaSyntaxTree& t) { |
90 | | - // check if current stmt starts on same line as the previous one ends |
91 | | - auto startCurrent = n.GetStartLine(t); |
92 | | - auto prev = n.GetPrevToken(t); |
93 | | - if (!prev.IsNull(t)) { |
94 | | - auto endPrev = prev.GetEndLine(t); |
95 | | - return endPrev < startCurrent; |
96 | | - } |
97 | | - return true; // there's no previous token |
| 96 | +bool SemicolonAnalyzer::IsFirstStmtOfLine(LuaSyntaxNode n, const LuaSyntaxTree &t) { |
| 97 | + // check if current stmt starts on same line as the previous one ends |
| 98 | + auto startCurrent = n.GetStartLine(t); |
| 99 | + auto prev = n.GetPrevToken(t); |
| 100 | + if (!prev.IsNull(t)) { |
| 101 | + auto endPrev = prev.GetEndLine(t); |
| 102 | + return endPrev < startCurrent; |
| 103 | + } |
| 104 | + return true;// there's no previous token |
98 | 105 | } |
99 | 106 |
|
100 | | -bool SemicolonAnalyzer::IsLastStmtOfLine(LuaSyntaxNode n, const LuaSyntaxTree& t) { |
101 | | - // check if next stmt starts on same line as the current one ends |
102 | | - auto currentEnd = n.GetEndLine(t); |
103 | | - auto nextToken = n.GetNextToken(t); |
104 | | - if (!nextToken.IsNull(t)) { |
105 | | - auto nextStart = nextToken.GetStartLine(t); |
106 | | - return currentEnd != nextStart; |
107 | | - } |
108 | | - return true; // there's no next token |
| 107 | +bool SemicolonAnalyzer::IsLastStmtOfLine(LuaSyntaxNode n, const LuaSyntaxTree &t) { |
| 108 | + // check if next stmt starts on same line as the current one ends |
| 109 | + auto currentEnd = n.GetEndLine(t); |
| 110 | + auto nextToken = n.GetNextToken(t); |
| 111 | + if (!nextToken.IsNull(t)) { |
| 112 | + auto nextStart = nextToken.GetStartLine(t); |
| 113 | + return currentEnd != nextStart; |
| 114 | + } |
| 115 | + return true;// there's no next token |
109 | 116 | } |
110 | 117 |
|
111 | | -bool SemicolonAnalyzer::EndsWithSemicolon(LuaSyntaxNode n, const LuaSyntaxTree& t) { |
112 | | - auto token = GetLastNonCommentToken(n, t); |
113 | | - auto text = token.GetText(t); |
114 | | - |
115 | | - return text == ";"; |
| 118 | +bool SemicolonAnalyzer::EndsWithSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t) { |
| 119 | + auto token = GetLastNonCommentToken(n, t); |
| 120 | + return token.GetTokenKind(t) == ';'; |
116 | 121 | } |
117 | 122 |
|
118 | | -LuaSyntaxNode SemicolonAnalyzer::GetLastNonCommentToken(LuaSyntaxNode n, const LuaSyntaxTree& t) { |
119 | | - auto token = n.GetLastToken(t); |
120 | | - switch (token.GetTokenKind(t)) { |
121 | | - case TK_SHORT_COMMENT: |
122 | | - case TK_LONG_COMMENT: |
123 | | - case TK_SHEBANG: { |
124 | | - return token.GetPrevToken(t); |
125 | | - } |
126 | | - default: { |
127 | | - return token; |
128 | | - } |
129 | | - } |
| 123 | +LuaSyntaxNode SemicolonAnalyzer::GetLastNonCommentToken(LuaSyntaxNode n, const LuaSyntaxTree &t) { |
| 124 | + auto token = n.GetLastToken(t); |
| 125 | + switch (token.GetTokenKind(t)) { |
| 126 | + case TK_SHORT_COMMENT: |
| 127 | + case TK_LONG_COMMENT: |
| 128 | + case TK_SHEBANG: { |
| 129 | + return token.GetPrevToken(t); |
| 130 | + } |
| 131 | + default: { |
| 132 | + return token; |
| 133 | + } |
| 134 | + } |
130 | 135 | } |
0 commit comments