Skip to content

Commit b29d058

Browse files
committed
feat: improve white-space token logic
1 parent 0b2b764 commit b29d058

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

src/parser/common/semanticContextCollector.ts

+16-29
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ abstract class SemanticContextCollector {
3333
}
3434
i--;
3535
}
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+
) {
3744
this._isNewStatement = true;
3845
}
3946
}
@@ -86,7 +93,7 @@ abstract class SemanticContextCollector {
8693

8794
/**
8895
* 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,
9097
* and if previous token is the last leaf node of the statement,
9198
* it can be considered as being in the context of new statement
9299
*/
@@ -97,34 +104,21 @@ abstract class SemanticContextCollector {
97104
// PostgreSQL whiteSpace not inlcudes '\n' symbol
98105
this._allTokens[this._tokenIndex]?.text === '\n';
99106

100-
const isPrevTokenSplitSymbol =
101-
this._prevTokenIndex &&
102-
this._allTokens[this._prevTokenIndex].text === SQL_SPLIT_SYMBOL_TEXT;
103-
104107
const isPrevTokenEndOfStatement =
105-
this._prevTokenIndex !== undefined &&
106-
ctx.stop?.tokenIndex === this._prevTokenIndex &&
107-
ctx.exception === null;
108+
this._prevTokenIndex && ctx.stop?.tokenIndex === this._prevTokenIndex;
108109

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;
113114
}
114115
}
115116

116117
/**
117118
* Uncomplete keyword will be error node
118119
*/
119120
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;
128122

129123
let parent: ParserRuleContext | null = node.parent as ParserRuleContext;
130124
let currentNode: TerminalNode | ParserRuleContext = node;
@@ -176,14 +170,7 @@ abstract class SemanticContextCollector {
176170
}
177171

178172
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;
187174

188175
let currentNode: TerminalNode | ParserRuleContext = node;
189176
let parent = node.parent as ParserRuleContext | null;

0 commit comments

Comments
 (0)