@@ -33,7 +33,14 @@ abstract class SemanticContextCollector {
33
33
}
34
34
i -- ;
35
35
}
36
- if ( tokenIndex === 0 || i === - 1 ) {
36
+
37
+ // Current token is the first token of tokenStream or the previous token is semicolon
38
+ if (
39
+ tokenIndex === 0 ||
40
+ i === - 1 ||
41
+ ( this . _prevTokenIndex &&
42
+ this . _allTokens [ this . _prevTokenIndex ] . text === SQL_SPLIT_SYMBOL_TEXT )
43
+ ) {
37
44
this . _isNewStatement = true ;
38
45
}
39
46
}
@@ -86,7 +93,7 @@ abstract class SemanticContextCollector {
86
93
87
94
/**
88
95
* Caret position is white space, so it will not visited as terminal node or error node.
89
- * We can find the previous no-white-space token,
96
+ * We can find the first previous no-white-space token,
90
97
* and if previous token is the last leaf node of the statement,
91
98
* it can be considered as being in the context of new statement
92
99
*/
@@ -97,34 +104,21 @@ abstract class SemanticContextCollector {
97
104
// PostgreSQL whiteSpace not inlcudes '\n' symbol
98
105
this . _allTokens [ this . _tokenIndex ] ?. text === '\n' ;
99
106
100
- const isPrevTokenSplitSymbol =
101
- this . _prevTokenIndex &&
102
- this . _allTokens [ this . _prevTokenIndex ] . text === SQL_SPLIT_SYMBOL_TEXT ;
103
-
104
107
const isPrevTokenEndOfStatement =
105
- this . _prevTokenIndex !== undefined &&
106
- ctx . stop ?. tokenIndex === this . _prevTokenIndex &&
107
- ctx . exception === null ;
108
+ this . _prevTokenIndex && ctx . stop ?. tokenIndex === this . _prevTokenIndex ;
108
109
109
- if ( isWhiteSpaceToken && ( isPrevTokenSplitSymbol || isPrevTokenEndOfStatement ) ) {
110
- if ( ! this . previousStatementHasError ( ctx ) ) {
111
- this . _isNewStatement = true ;
112
- }
110
+ if ( isWhiteSpaceToken && isPrevTokenEndOfStatement && ctx . exception === null ) {
111
+ this . _isNewStatement = ! this . previousStatementHasError ( ctx )
112
+ ? true
113
+ : this . _isNewStatement ;
113
114
}
114
115
}
115
116
116
117
/**
117
118
* Uncomplete keyword will be error node
118
119
*/
119
120
visitErrorNode ( node : ErrorNode ) : void {
120
- if ( node . symbol . tokenIndex !== this . _tokenIndex ) return ;
121
- if (
122
- this . _prevTokenIndex &&
123
- this . _allTokens [ this . _prevTokenIndex ] . text === SQL_SPLIT_SYMBOL_TEXT
124
- ) {
125
- this . _isNewStatement = true ;
126
- return ;
127
- }
121
+ if ( node . symbol . tokenIndex !== this . _tokenIndex || this . _isNewStatement ) return ;
128
122
129
123
let parent : ParserRuleContext | null = node . parent as ParserRuleContext ;
130
124
let currentNode : TerminalNode | ParserRuleContext = node ;
@@ -176,14 +170,7 @@ abstract class SemanticContextCollector {
176
170
}
177
171
178
172
visitTerminal ( node : TerminalNode ) : void {
179
- if ( node . symbol . tokenIndex !== this . _tokenIndex ) return ;
180
- if (
181
- this . _prevTokenIndex &&
182
- this . _allTokens [ this . _prevTokenIndex ] . text === SQL_SPLIT_SYMBOL_TEXT
183
- ) {
184
- this . _isNewStatement = true ;
185
- return ;
186
- }
173
+ if ( node . symbol . tokenIndex !== this . _tokenIndex || this . _isNewStatement ) return ;
187
174
188
175
let currentNode : TerminalNode | ParserRuleContext = node ;
189
176
let parent = node . parent as ParserRuleContext | null ;
0 commit comments