@@ -322,49 +322,84 @@ function splitRangesAtBlankLines(ranges: StatementRange[], content: string): Sta
322322
323323 let blankRun = 0
324324 let seenContent = false
325- const splitLineIndices : number [ ] = [ ]
325+ let inBlockComment = false
326+ // Track the line index of the last actual SQL content (for split boundaries)
327+ let lastContentLine = - 1
328+ // When the segment starts with `WITH`, suppress soft-split for the next DML
329+ // keyword — it's the main query of a CTE, not a new statement.
330+ let segmentFirstKeyword : string | null = null
331+ const FIRST_DML_KEYWORDS = new Set ( [ 'SELECT' , 'INSERT' , 'UPDATE' , 'DELETE' , 'MERGE' , 'REPLACE' ] )
332+ // Each split: { sqlLine: the SQL keyword line, prevContentLine: last content before blank run }
333+ const splits : { sqlLine : number , prevContentLine : number } [ ] = [ ]
326334
327335 for ( let i = 0 ; i < lines . length ; i ++ ) {
328336 const trimmed = lines [ i ] . trim ( )
329337
338+ // Lines inside a multi-line block comment — skip without resetting blankRun
339+ if ( inBlockComment ) {
340+ if ( trimmed . endsWith ( '*/' ) )
341+ inBlockComment = false
342+ continue
343+ }
344+
330345 if ( trimmed . length === 0 ) {
331346 if ( seenContent )
332347 blankRun ++
348+ continue
333349 }
334- else {
335- if ( ! seenContent ) {
336- seenContent = true
337- }
338- else if ( blankRun >= 2 ) {
339- const firstWord = trimmed . split ( / \s + / ) [ 0 ] ?. toUpperCase ( )
340- if ( firstWord && SOFT_STATEMENT_KEYWORDS . has ( firstWord ) )
341- splitLineIndices . push ( i )
342- }
343- blankRun = 0
350+
351+ // Comment-only lines should not reset the blank-run counter,
352+ // so that `SELECT ...\n\n\n-- comment\nSELECT ...` still splits.
353+ if ( trimmed . startsWith ( '--' ) || trimmed . startsWith ( '#' ) ) {
354+ continue
344355 }
356+ if ( trimmed . startsWith ( '/*' ) ) {
357+ if ( ! trimmed . endsWith ( '*/' ) )
358+ inBlockComment = true
359+ continue
360+ }
361+
362+ // Actual SQL content
363+ const firstWord = trimmed . split ( / \s + / ) [ 0 ] ?. toUpperCase ( )
364+ if ( ! seenContent ) {
365+ seenContent = true
366+ segmentFirstKeyword = firstWord ?? null
367+ }
368+ else if ( blankRun >= 2 && firstWord && SOFT_STATEMENT_KEYWORDS . has ( firstWord ) ) {
369+ // When the segment starts with WITH, the next DML keyword is the main
370+ // query of the CTE (e.g. `WITH cte AS (...) \n\n\n SELECT ...`).
371+ // Splitting there would orphan the CTE definition.
372+ const isCteMainQuery = segmentFirstKeyword === 'WITH' && firstWord && FIRST_DML_KEYWORDS . has ( firstWord )
373+ if ( ! isCteMainQuery )
374+ splits . push ( { sqlLine : i , prevContentLine : lastContentLine } )
375+ }
376+ blankRun = 0
377+ lastContentLine = i
345378 }
346379
347- if ( splitLineIndices . length === 0 ) {
380+ if ( splits . length === 0 ) {
348381 result . push ( range )
349382 continue
350383 }
351384
352- let prevIdx = 0
353- for ( const splitIdx of splitLineIndices ) {
354- const segStart = range . startOffset + lineOffsets [ prevIdx ]
355- const segEnd = range . startOffset + lineOffsets [ splitIdx ]
356- // Trim trailing whitespace so the preceding range ends at its last content
357- const segText = content . slice ( segStart , segEnd ) . trimEnd ( )
358- if ( segText . length > 0 )
359- result . push ( buildRange ( content , segStart , segStart + segText . length ) )
360- prevIdx = splitIdx
385+ // Build sub-ranges. Each preceding segment ends at its last content line
386+ // (excluding trailing blank/comment lines). Each new segment starts at the
387+ // SQL keyword line in `splits[].sqlLine`.
388+ let segStart = range . startOffset + lineOffsets [ 0 ]
389+
390+ for ( const { sqlLine, prevContentLine } of splits ) {
391+ // End the preceding segment right after `prevContentLine`
392+ const segEnd = range . startOffset + lineOffsets [ prevContentLine ] + lines [ prevContentLine ] . length
393+ result . push ( buildRange ( content , segStart , segEnd ) )
394+ segStart = range . startOffset + lineOffsets [ sqlLine ]
361395 }
362- // Trim leading whitespace on the final segment
363- const lastStart = range . startOffset + lineOffsets [ prevIdx ]
364- const lastText = content . slice ( lastStart , range . endOffset ) . trimStart ( )
365- if ( lastText . length > 0 ) {
366- const trimmed = lastText . length - lastText . trimStart ( ) . length
367- result . push ( buildRange ( content , lastStart + trimmed , range . endOffset ) )
396+
397+ // Final segment (trim leading whitespace)
398+ const rawFinal = content . slice ( segStart , range . endOffset )
399+ const finalTrimmed = rawFinal . trimStart ( )
400+ if ( finalTrimmed . length > 0 ) {
401+ const skipped = rawFinal . length - finalTrimmed . length
402+ result . push ( buildRange ( content , segStart + skipped , range . endOffset ) )
368403 }
369404 }
370405
0 commit comments