@@ -70,13 +70,13 @@ export const getQueryFromCursor = (
7070 let startRow = 0
7171 let startCol = 0
7272 let startPos = - 1
73- let sql = null
73+ let nextSql = null
7474 let inQuote = false
7575
7676 if ( ! position ) return
7777
7878 for ( let i = 0 ; i < text . length ; i ++ ) {
79- if ( sql !== null ) {
79+ if ( nextSql !== null ) {
8080 break
8181 }
8282
@@ -97,6 +97,8 @@ export const getQueryFromCursor = (
9797 row : startRow ,
9898 col : startCol ,
9999 position : startPos ,
100+ endRow : row ,
101+ endCol : column ,
100102 limit : i ,
101103 } )
102104 startRow = row
@@ -105,7 +107,14 @@ export const getQueryFromCursor = (
105107 column ++
106108 } else {
107109 // empty queries, aka ;; , make sql.length === 0
108- sql = text . substring ( startPos === - 1 ? 0 : startPos , i )
110+ nextSql = {
111+ row : startRow ,
112+ col : startCol ,
113+ position : startPos ,
114+ endRow : row ,
115+ endCol : column ,
116+ limit : i ,
117+ }
109118 }
110119 break
111120 }
@@ -130,7 +139,6 @@ export const getQueryFromCursor = (
130139 startRow = row
131140 startCol = column
132141 startPos = i + 1
133- column ++
134142 }
135143 break
136144 }
@@ -148,28 +156,52 @@ export const getQueryFromCursor = (
148156 }
149157 }
150158
151- if ( sql === null ) {
152- sql = startPos === - 1 ? text : text . substring ( startPos )
153- }
154-
155- if ( sql . length === 0 ) {
156- const prev = sqlTextStack . pop ( )
159+ // lastStackItem is the last query that is completed before the current cursor position.
160+ // nextSql is the next query that is not completed before the current cursor position, or started after the current cursor position.
161+ const normalizedCurrentRow = position . lineNumber - 1
162+ const lastStackItem = sqlTextStack . length > 0 ? sqlTextStack [ sqlTextStack . length - 1 ] : null
157163
158- if ( prev ) {
164+ if ( ! lastStackItem && ! nextSql ) {
165+ return
166+ }
167+ const lastStackItemRowRange = lastStackItem ? {
168+ start : lastStackItem . row ,
169+ end : lastStackItem . endRow ,
170+ } : null
171+ const nextSqlRowRange = nextSql ? {
172+ start : nextSql . row ,
173+ end : nextSql . endRow ,
174+ } : null
175+ const isInLastStackItemRowRange = lastStackItemRowRange && normalizedCurrentRow >= lastStackItemRowRange . start && normalizedCurrentRow <= lastStackItemRowRange . end
176+ const isInNextSqlRowRange = nextSqlRowRange && normalizedCurrentRow >= nextSqlRowRange . start && normalizedCurrentRow <= nextSqlRowRange . end
177+
178+ if ( isInLastStackItemRowRange && ! isInNextSqlRowRange ) {
179+ return {
180+ query : text . substring ( lastStackItem ! . position , lastStackItem ! . limit ) ,
181+ row : lastStackItem ! . row ,
182+ column : lastStackItem ! . endCol ,
183+ }
184+ } else if ( isInNextSqlRowRange && ! isInLastStackItemRowRange ) {
185+ return {
186+ query : text . substring ( nextSql ! . position , nextSql ! . limit ) ,
187+ row : nextSql ! . row ,
188+ column : nextSql ! . col ,
189+ }
190+ } else if ( isInLastStackItemRowRange && isInNextSqlRowRange ) {
191+ const lastStackItemEndCol = lastStackItem ! . endCol
192+ const normalizedCurrentCol = position . column - 1
193+ if ( normalizedCurrentCol > lastStackItemEndCol + 1 ) {
159194 return {
160- column : prev . col ,
161- query : text . substring ( prev . position , prev . limit ) ,
162- row : prev . row ,
195+ query : text . substring ( nextSql ! . position , nextSql ! . limit ) ,
196+ row : nextSql ! . row ,
197+ column : nextSql ! . col ,
163198 }
164199 }
165-
166- return
167- }
168-
169- return {
170- column : startCol ,
171- query : sql ,
172- row : startRow ,
200+ return {
201+ query : text . substring ( lastStackItem ! . position , lastStackItem ! . limit ) ,
202+ row : lastStackItem ! . row ,
203+ column : lastStackItem ! . endCol ,
204+ }
173205 }
174206}
175207
0 commit comments