11import { ErrorNode , ParserRuleContext , TerminalNode , Token } from 'antlr4ng' ;
22import { findCaretTokenIndex } from '../common/findCaretTokenIndex' ;
3- import { CaretPosition , SemanticContext } from '../common/types' ;
3+ import {
4+ CaretPosition ,
5+ SemanticCollectOptions ,
6+ SemanticContext ,
7+ SqlSplitStrategy ,
8+ } from '../common/types' ;
49
510export const SQL_SPLIT_SYMBOL_TEXT = ';' ;
611
712abstract class SemanticContextCollector {
8- constructor ( _input : string , caretPosition : CaretPosition , allTokens : Token [ ] ) {
13+ constructor (
14+ _input : string ,
15+ caretPosition : CaretPosition ,
16+ allTokens : Token [ ] ,
17+ options ?: SemanticCollectOptions
18+ ) {
919 // If caretPosition token is whiteSpace, tokenIndex may be undefined.
1020 const tokenIndex = findCaretTokenIndex ( caretPosition , allTokens ) ;
1121
1222 if ( tokenIndex !== undefined ) {
1323 this . _tokenIndex = tokenIndex ;
1424 }
1525 this . _allTokens = allTokens ;
26+ this . options = {
27+ ...this . options ,
28+ ...options ,
29+ } ;
1630
1731 if ( allTokens ?. length ) {
1832 let i = tokenIndex ? tokenIndex - 1 : allTokens . length - 1 ;
@@ -50,6 +64,10 @@ abstract class SemanticContextCollector {
5064 }
5165 }
5266
67+ public readonly options : SemanticCollectOptions = {
68+ sqlSplitStrategy : SqlSplitStrategy . LOOSE ,
69+ } ;
70+
5371 private _tokenIndex : number ;
5472 private _allTokens : Token [ ] = [ ] ;
5573
@@ -117,6 +135,8 @@ abstract class SemanticContextCollector {
117135 * It should be called in each language's own `enterStatement`.
118136 */
119137 protected visitStatement ( ctx : ParserRuleContext ) {
138+ if ( this . options . sqlSplitStrategy === SqlSplitStrategy . STRICT ) return ;
139+
120140 const isWhiteSpaceToken =
121141 this . _tokenIndex === undefined ||
122142 this . _allTokens [ this . _tokenIndex ] ?. type === this . getWhiteSpaceRuleType ( ) ||
@@ -135,7 +155,12 @@ abstract class SemanticContextCollector {
135155 * Uncomplete keyword will be error node
136156 */
137157 visitErrorNode ( node : ErrorNode ) : void {
138- if ( node . symbol . tokenIndex !== this . _tokenIndex || this . _isNewStatement ) return ;
158+ if (
159+ node . symbol . tokenIndex !== this . _tokenIndex ||
160+ this . _isNewStatement ||
161+ this . options . sqlSplitStrategy === SqlSplitStrategy . STRICT
162+ )
163+ return ;
139164
140165 let parent : ParserRuleContext | null = node . parent as ParserRuleContext ;
141166 let currentNode : TerminalNode | ParserRuleContext = node ;
@@ -188,7 +213,12 @@ abstract class SemanticContextCollector {
188213 }
189214
190215 visitTerminal ( node : TerminalNode ) : void {
191- if ( node . symbol . tokenIndex !== this . _tokenIndex || this . _isNewStatement ) return ;
216+ if (
217+ node . symbol . tokenIndex !== this . _tokenIndex ||
218+ this . _isNewStatement ||
219+ this . options . sqlSplitStrategy === SqlSplitStrategy . STRICT
220+ )
221+ return ;
192222
193223 let currentNode : TerminalNode | ParserRuleContext = node ;
194224 let parent = node . parent as ParserRuleContext | null ;
0 commit comments