Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/formatter/keywordCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { KeywordCase } from './options';
import {
createInitialSqlLineScanState,
scanSqlLineOutsideLiteralsAndComments,
rewriteSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
} from './sqlLineScanner';

Expand Down Expand Up @@ -40,19 +41,11 @@ export function applyKeywordCaseToLine(
return { line, nextState: scanResult.nextState };
}

const scanResult = scanSqlLineOutsideLiteralsAndComments(line, scanState);
let cursor = 0;
let nextLine = '';

for (const segment of scanResult.outsideSegments) {
nextLine += line.slice(cursor, segment.start);
nextLine += transformSqlWords(line.slice(segment.start, segment.end), dialect, keywordCase);
cursor = segment.end;
}

nextLine += line.slice(cursor);
const result = rewriteSqlLineOutsideLiteralsAndComments(line, scanState, (segmentText) =>
transformSqlWords(segmentText, dialect, keywordCase),
);

return { line: nextLine, nextState: scanResult.nextState };
return { line: result.line, nextState: result.nextState };
}

export function collectSqlWordsOutsideLiteralsAndComments(
Expand Down
26 changes: 11 additions & 15 deletions src/formatter/passes/cleanup/statementContinuationCleanup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createInitialSqlLineScanState,
scanSqlLineOutsideLiteralsAndComments,
rewriteSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
} from '../../sqlLineScanner';

export function cleanupWatcomStatementContinuations(
Expand Down Expand Up @@ -57,22 +58,17 @@ export function cleanupWatcomStatementContinuations(

function rewriteOutsideSqlText(
line: string,
scanState: ReturnType<typeof createInitialSqlLineScanState>,
scanState: SqlLineScanState,
rewrite: (line: string, start: number, end: number) => string,
): { line: string } {
const result = scanSqlLineOutsideLiteralsAndComments(line, scanState);
let rewritten = '';
let cursor = 0;

for (const segment of result.outsideSegments) {
rewritten += line.slice(cursor, segment.start);
rewritten += rewrite(line, segment.start, segment.end);
cursor = segment.end;
}

rewritten += line.slice(cursor);
): { readonly line: string } {
const result = rewriteSqlLineOutsideLiteralsAndComments(
line,
scanState,
(_segmentText, segment) => rewrite(line, segment.start, segment.end),
);
scanState.inBlockComment = result.nextState.inBlockComment;
return { line: rewritten };

return { line: result.line };
}

function cleanupStatementOutsideSqlText(line: string, start: number, end: number): string {
Expand Down
9 changes: 2 additions & 7 deletions src/formatter/passes/structural/inlineIfFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SqlDialect } from '../../../dialects';
import {
cloneSqlLineScanState,
createSqlOutsideLookup,
scanSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
type SqlOutsideSegment,
Expand Down Expand Up @@ -167,13 +168,7 @@ function splitConditionOnLogicalOperators(condition: string): string[] {
condition,
cloneSqlLineScanState({ inBlockComment: false }),
);
const outside = new Array<boolean>(condition.length).fill(false);

for (const segment of scanResult.outsideSegments) {
for (let index = segment.start; index < segment.end; index += 1) {
outside[index] = true;
}
}
const outside = createSqlOutsideLookup(condition.length, scanResult.outsideSegments);

const parts: string[] = [];
let lastSplit = 0;
Expand Down
21 changes: 3 additions & 18 deletions src/formatter/passes/structural/parenthesisFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
cloneSqlLineScanState,
createSqlOutsideLookup,
scanSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
type SqlOutsideSegment,
} from '../../sqlLineScanner';

export interface ParenthesisFormattingState {
Expand Down Expand Up @@ -70,7 +70,7 @@ export function expandParenthesesInLine(
initialState: ParenthesisFormattingState,
): ParenthesisExpansionResult {
const scanResult = scanSqlLineOutsideLiteralsAndComments(line, initialState.scanState);
const outside = createOutsideLookup(line.length, scanResult.outsideSegments);
const outside = createSqlOutsideLookup(line.length, scanResult.outsideSegments);

if (!lineContainsSplittableParenthesis(line, outside, initialState.parenthesisDepth)) {
return {
Expand Down Expand Up @@ -173,7 +173,7 @@ export function analyzeParenthesesForIndent(
initialState: SqlLineScanState,
): ParenthesisIndentAnalysis {
const scanResult = scanSqlLineOutsideLiteralsAndComments(line, initialState);
const outside = createOutsideLookup(line.length, scanResult.outsideSegments);
const outside = createSqlOutsideLookup(line.length, scanResult.outsideSegments);
let leadingClosingParentheses = 0;
let depthDelta = 0;
let hasSeenNonWhitespace = false;
Expand Down Expand Up @@ -364,21 +364,6 @@ function readWordBefore(line: string, index: number): string | undefined {
return line.slice(cursor + 1, end);
}

function createOutsideLookup(
length: number,
outsideSegments: readonly SqlOutsideSegment[],
): boolean[] {
const outside = new Array<boolean>(length).fill(false);

for (const segment of outsideSegments) {
for (let index = segment.start; index < segment.end; index += 1) {
outside[index] = true;
}
}

return outside;
}

function skipWhitespace(line: string, start: number): number {
let index = start;

Expand Down
18 changes: 2 additions & 16 deletions src/formatter/passes/structural/queryClauseFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SqlDialect } from '../../../dialects';
import {
cloneSqlLineScanState,
createSqlOutsideLookup,
scanSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
type SqlOutsideSegment,
Expand Down Expand Up @@ -48,7 +49,7 @@ export function expandWatcomQueryClauseLine(
initialState: QueryClauseFormattingState,
): ExpandedLineResult {
const scanResult = scanSqlLineOutsideLiteralsAndComments(line, initialState.scanState);
const outside = createOutsideLookup(line.length, scanResult.outsideSegments);
const outside = createSqlOutsideLookup(line.length, scanResult.outsideSegments);
const nextState: QueryClauseFormattingState = {
scanState: scanResult.nextState,
parenthesisDepth: updateParenthesisDepth(line, outside, initialState.parenthesisDepth),
Expand Down Expand Up @@ -268,21 +269,6 @@ function updateParenthesisDepth(
return depth;
}

function createOutsideLookup(
length: number,
outsideSegments: readonly SqlOutsideSegment[],
): boolean[] {
const outside = new Array<boolean>(length).fill(false);

for (const segment of outsideSegments) {
for (let index = segment.start; index < segment.end; index += 1) {
outside[index] = true;
}
}

return outside;
}

function isPhrase(
words: readonly WordMatch[],
index: number,
Expand Down
35 changes: 35 additions & 0 deletions src/formatter/sqlLineScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ export function cloneSqlLineScanState(state: SqlLineScanState): SqlLineScanState
return { inBlockComment: state.inBlockComment };
}

export function createSqlOutsideLookup(
length: number,
outsideSegments: readonly SqlOutsideSegment[],
): readonly boolean[] {
const outside = new Array<boolean>(length).fill(false);

for (const segment of outsideSegments) {
for (let index = segment.start; index < segment.end; index += 1) {
outside[index] = true;
}
}

return outside;
}

export function rewriteSqlLineOutsideLiteralsAndComments(
line: string,
initialState: SqlLineScanState,
rewrite: (segmentText: string, segment: SqlOutsideSegment) => string,
): { readonly line: string; readonly nextState: SqlLineScanState } {
const scanResult = scanSqlLineOutsideLiteralsAndComments(line, initialState);
let rewritten = '';
let cursor = 0;

for (const segment of scanResult.outsideSegments) {
rewritten += line.slice(cursor, segment.start);
rewritten += rewrite(line.slice(segment.start, segment.end), segment);
cursor = segment.end;
}

rewritten += line.slice(cursor);

return { line: rewritten, nextState: scanResult.nextState };
}

export function scanSqlLineOutsideLiteralsAndComments(
line: string,
initialState: SqlLineScanState,
Expand Down
Loading