@@ -12,7 +12,7 @@ import CypherParser, {
12
12
RelationshipPatternContext ,
13
13
StatementsOrCommandsContext ,
14
14
} from './generated-parser/CypherCmdParser' ;
15
- import { ParsingResult } from './parserWrapper' ;
15
+ import { ParsedStatement , ParsingResult } from './parserWrapper' ;
16
16
17
17
/* In antlr we have
18
18
@@ -92,35 +92,6 @@ type AntlrDefaultExport = {
92
92
} ;
93
93
export const antlrUtils = antlrDefaultExport as unknown as AntlrDefaultExport ;
94
94
95
- export function findLatestStatement (
96
- parsingResult : ParsingResult ,
97
- ) : undefined | string {
98
- const tokens = parsingResult . tokens ;
99
- const lastTokenIndex = tokens . length - 1 ;
100
-
101
- let tokenIndex = lastTokenIndex ;
102
- let found = false ;
103
- let lastStatement : undefined | string = undefined ;
104
-
105
- // Last token is always EOF
106
- while ( tokenIndex > 0 && ! found ) {
107
- tokenIndex -- ;
108
- found = tokens [ tokenIndex ] . type == CypherLexer . SEMICOLON ;
109
- }
110
-
111
- if ( found ) {
112
- lastStatement = '' ;
113
-
114
- tokenIndex += 1 ;
115
- while ( tokenIndex < lastTokenIndex ) {
116
- lastStatement += tokens . at ( tokenIndex ) ?. text ?? '' ;
117
- tokenIndex ++ ;
118
- }
119
- }
120
-
121
- return lastStatement ;
122
- }
123
-
124
95
export function inNodeLabel ( stopNode : ParserRuleContext ) {
125
96
const nodePattern = findParent (
126
97
stopNode ,
@@ -139,6 +110,72 @@ export function inRelationshipType(stopNode: ParserRuleContext) {
139
110
return isDefined ( relPattern ) ;
140
111
}
141
112
113
+ export function findCaret (
114
+ parsingResult : ParsingResult ,
115
+ caretPosition : number ,
116
+ ) : { statement : ParsedStatement ; token : Token } | undefined {
117
+ const statements = parsingResult . statementsParsing ;
118
+ let i = 0 ;
119
+ let result : { statement : ParsedStatement ; token : Token } = undefined ;
120
+ let keepLooking = true ;
121
+
122
+ while ( i < statements . length && keepLooking ) {
123
+ let j = 0 ;
124
+ const statement = statements [ i ] ;
125
+ const tokens = statement . tokens ;
126
+
127
+ while ( j < tokens . length && keepLooking ) {
128
+ const currentToken = tokens [ j ] ;
129
+ keepLooking = currentToken . start < caretPosition ;
130
+
131
+ if ( currentToken . channel === 0 && keepLooking ) {
132
+ result = { statement : statement , token : currentToken } ;
133
+ }
134
+
135
+ j ++ ;
136
+ }
137
+ i ++ ;
138
+ }
139
+
140
+ return result ;
141
+ }
142
+
143
+ export function splitIntoStatements (
144
+ tokenStream : CommonTokenStream ,
145
+ lexer : CypherLexer ,
146
+ ) : CommonTokenStream [ ] {
147
+ tokenStream . fill ( ) ;
148
+ const tokens = tokenStream . tokens ;
149
+
150
+ let i = 0 ;
151
+ const result : CommonTokenStream [ ] = [ ] ;
152
+ let chunk : Token [ ] = [ ] ;
153
+ let offset = 0 ;
154
+
155
+ while ( i < tokens . length ) {
156
+ const current = tokens [ i ] . clone ( ) ;
157
+ current . tokenIndex -= offset ;
158
+
159
+ chunk . push ( current ) ;
160
+
161
+ if (
162
+ current . type === CypherLexer . SEMICOLON ||
163
+ current . type === CypherLexer . EOF
164
+ ) {
165
+ // This does not relex since we are not calling fill on the token stream
166
+ const tokenStream = new CommonTokenStream ( lexer ) ;
167
+ tokenStream . tokens = chunk ;
168
+ result . push ( tokenStream ) ;
169
+ offset = i + 1 ;
170
+ chunk = [ ] ;
171
+ }
172
+
173
+ i ++ ;
174
+ }
175
+
176
+ return result ;
177
+ }
178
+
142
179
export const rulesDefiningVariables = [
143
180
CypherParser . RULE_returnItem ,
144
181
CypherParser . RULE_unwindClause ,
0 commit comments