diff --git a/packages/query-splitter/src/splitQuery.ts b/packages/query-splitter/src/splitQuery.ts index cd6d68e3..7e63f1c4 100644 --- a/packages/query-splitter/src/splitQuery.ts +++ b/packages/query-splitter/src/splitQuery.ts @@ -40,6 +40,8 @@ export interface SplitResultItemRich { text: string; start: SplitPositionDefinition; end: SplitPositionDefinition; + trimStart?: SplitPositionDefinition; + trimEnd?: SplitPositionDefinition; } export type SplitResultItem = string | SplitResultItemRich; @@ -211,7 +213,7 @@ function pushQuery(context: SplitLineContext) { if (trimmed) { if (context.options.returnRichInfo) { context.pushOutput( - trimPositions(sql, { + countTrimmedPositions(sql, { text: trimmed, start: { @@ -233,26 +235,26 @@ function pushQuery(context: SplitLineContext) { } } -function trimPositions(full: string, positions: SplitResultItemRich): SplitResultItemRich { +function countTrimmedPositions(full: string, positions: SplitResultItemRich): SplitResultItemRich { const startIndex = full.indexOf(positions.text); - const res = { - ...positions, - start: { - ...positions.start, - }, - }; + + const trimStart = { ...positions.start }; for (let i = 0; i < startIndex; i += 1) { if (full[i] == '\n') { - res.start.position += 1; - res.start.line += 1; - res.start.column = 0; + trimStart.position += 1; + trimStart.line += 1; + trimStart.column = 0; } else { - res.start.position += 1; - res.start.column += 1; + trimStart.position += 1; + trimStart.column += 1; } } - return res; + return { + ...positions, + trimStart, + trimEnd: positions.end, + }; } function markStartCommand(context: SplitLineContext) { @@ -330,7 +332,7 @@ export function finishSplitStream(context: SplitStreamContext) { if (trimmed) { if (context.options.returnRichInfo) { context.pushOutput( - trimPositions(context.commandPart, { + countTrimmedPositions(context.commandPart, { text: trimmed, start: { @@ -364,12 +366,16 @@ export function splitQuery(sql: string, options: SplitterOptions = null): SplitR return [ { text: sql, - startLine: 0, - startPosition: 0, - startColumn: 0, - endLine: lines.length, - endColumn: lines[lines.length - 1]?.length || 0, - endPosition: sql.length, + start: { + position: 0, + line: 0, + column: 0, + }, + end: { + position: sql.length, + line: lines.length, + column: lines[lines.length - 1]?.length || 0, + }, }, ]; } diff --git a/packages/query-splitter/src/splitter.test.ts b/packages/query-splitter/src/splitter.test.ts index 33974a4d..4eeb8e43 100644 --- a/packages/query-splitter/src/splitter.test.ts +++ b/packages/query-splitter/src/splitter.test.ts @@ -100,7 +100,7 @@ test('count lines', () => { expect.objectContaining({ text: 'SELECT * FROM `table1`', - start: expect.objectContaining({ + trimStart: expect.objectContaining({ position: 0, line: 0, column: 0, @@ -115,7 +115,7 @@ test('count lines', () => { expect.objectContaining({ text: 'SELECT * FROM `table2`', - start: expect.objectContaining({ + trimStart: expect.objectContaining({ position: 24, line: 1, column: 0, @@ -141,7 +141,7 @@ test('count lines with flush', () => { expect.objectContaining({ text: 'SELECT * FROM `table1`', - start: expect.objectContaining({ + trimStart: expect.objectContaining({ position: 0, line: 0, column: 0, @@ -156,7 +156,7 @@ test('count lines with flush', () => { expect.objectContaining({ text: 'SELECT * FROM `table2`', - start: expect.objectContaining({ + trimStart: expect.objectContaining({ position: 24, line: 1, column: 0, diff --git a/packages/web/src/query/AceEditor.svelte b/packages/web/src/query/AceEditor.svelte index a2fd756e..a81bb0a1 100644 --- a/packages/web/src/query/AceEditor.svelte +++ b/packages/web/src/query/AceEditor.svelte @@ -189,13 +189,11 @@ currentPart = part; if (currentPart) { + const start = currentPart.trimStart || currentPart.start; + const end = currentPart.trimEnd || currentPart.end; currentPartMarker = editor .getSession() - .addMarker( - new ace.Range(currentPart.start.line, currentPart.start.column, currentPart.end.line, currentPart.end.column), - 'ace_active-line', - 'text' - ); + .addMarker(new ace.Range(start.line, start.column, end.line, end.column), 'ace_active-line', 'text'); } } }