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
41 changes: 2 additions & 39 deletions src/formatter/passes/structural/blockEndFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SqlDialect } from '../../../dialects';
import {
collectSqlWordsFromSegments,
scanSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
type SqlOutsideSegment,
Expand All @@ -21,14 +22,6 @@ interface EndPhraseToken {
readonly hasSemicolon: boolean;
}

interface WordMatch {
readonly start: number;
readonly end: number;
readonly normalized: string;
}

const SQL_WORD_START = /[A-Za-z_]/u;
const SQL_WORD_PART = /[A-Za-z0-9_$#]/u;
const BLOCK_END_FOLLOWERS = new Set(['if', 'for', 'loop', 'while', 'try', 'catch']);

export function createInitialBlockEndFormattingState(): BlockEndFormattingState {
Expand Down Expand Up @@ -89,7 +82,7 @@ function collectBlockEndPhrases(
line: string,
outsideSegments: readonly SqlOutsideSegment[],
): EndPhraseToken[] {
const words = collectWords(line, outsideSegments);
const words = collectSqlWordsFromSegments(line, outsideSegments);
const phrases: EndPhraseToken[] = [];
let index = 0;

Expand Down Expand Up @@ -171,33 +164,3 @@ function consumeOptionalSemicolon(

return { end: start, hasSemicolon: false };
}

function collectWords(line: string, outsideSegments: readonly SqlOutsideSegment[]): WordMatch[] {
const words: WordMatch[] = [];

for (const segment of outsideSegments) {
let index = segment.start;

while (index < segment.end) {
if (!SQL_WORD_START.test(line[index])) {
index += 1;
continue;
}

const start = index;
index += 1;

while (index < segment.end && SQL_WORD_PART.test(line[index])) {
index += 1;
}

words.push({
start,
end: index,
normalized: line.slice(start, index).toLowerCase(),
});
}
}

return words;
}
43 changes: 4 additions & 39 deletions src/formatter/passes/structural/caseExpressionFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { SqlDialect } from '../../../dialects';
import {
collectSqlWordsFromSegments,
scanSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
type SqlOutsideSegment,
type SqlWordMatch,
} from '../../sqlLineScanner';

export interface CaseExpressionFormattingState {
Expand All @@ -15,14 +16,8 @@ interface ExpandedLineResult {
readonly nextState: CaseExpressionFormattingState;
}

interface WordMatch {
readonly start: number;
readonly end: number;
readonly normalized: string;
}
type WordMatch = SqlWordMatch;

const SQL_WORD_START = /[A-Za-z_]/u;
const SQL_WORD_PART = /[A-Za-z0-9_$#]/u;
const CASE_SPLIT_WORDS = new Set(['when', 'then', 'else']);
const BLOCK_END_FOLLOWERS = new Set(['for', 'if', 'loop', 'try', 'catch', 'while']);

Expand All @@ -46,7 +41,7 @@ export function expandWatcomCaseExpressionLine(
initialState: CaseExpressionFormattingState,
): ExpandedLineResult {
const scanResult = scanSqlLineOutsideLiteralsAndComments(line, initialState.scanState);
const words = collectWords(line, scanResult.outsideSegments);
const words = collectSqlWordsFromSegments(line, scanResult.outsideSegments);
const nextState: CaseExpressionFormattingState = {
scanState: scanResult.nextState,
caseDepth: calculateNextCaseDepth(words, initialState.caseDepth),
Expand Down Expand Up @@ -182,33 +177,3 @@ function pushTrimmed(lines: string[], text: string): void {
lines.push(trimmed);
}
}

function collectWords(line: string, outsideSegments: readonly SqlOutsideSegment[]): WordMatch[] {
const words: WordMatch[] = [];

for (const segment of outsideSegments) {
let index = segment.start;

while (index < segment.end) {
if (!SQL_WORD_START.test(line[index])) {
index += 1;
continue;
}

const start = index;
index += 1;

while (index < segment.end && SQL_WORD_PART.test(line[index])) {
index += 1;
}

words.push({
start,
end: index,
normalized: line.slice(start, index).toLowerCase(),
});
}
}

return words;
}
61 changes: 4 additions & 57 deletions src/formatter/passes/structural/cursorForFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { SqlDialect } from '../../../dialects';
import {
cloneSqlLineScanState,
collectSqlWordsWithParenthesisDepth,
scanSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
type SqlOutsideSegment,
type SqlWordDepthMatch,
} from '../../sqlLineScanner';

export interface CursorForFormattingState {
Expand All @@ -16,15 +17,7 @@ interface ExpandedLineResult {
readonly nextState: CursorForFormattingState;
}

interface WordMatch {
readonly start: number;
readonly end: number;
readonly normalized: string;
readonly depth: number;
}

const SQL_WORD_START = /[A-Za-z_]/u;
const SQL_WORD_PART = /[A-Za-z0-9_$#]/u;
type WordMatch = SqlWordDepthMatch;

export function createInitialCursorForFormattingState(): CursorForFormattingState {
return {
Expand Down Expand Up @@ -56,7 +49,7 @@ export function expandWatcomCursorForLine(
return { lines: [line], nextState: baseNextState };
}

const words = collectWords(line, scanResult.outsideSegments);
const words = collectSqlWordsWithParenthesisDepth(line, scanResult.outsideSegments);
const { splitPoints, inCursorQuery } = findCursorForSplitPoints(
words,
initialState.inCursorQuery,
Expand Down Expand Up @@ -205,52 +198,6 @@ function splitLineAtIndexes(line: string, indexes: readonly number[]): string[]
return lines.length > 0 ? lines : [line];
}

function collectWords(line: string, outsideSegments: readonly SqlOutsideSegment[]): WordMatch[] {
const words: WordMatch[] = [];
let depth = 0;

for (const segment of outsideSegments) {
let index = segment.start;

while (index < segment.end) {
const char = line[index];

if (char === '(') {
depth += 1;
index += 1;
continue;
}

if (char === ')') {
depth = Math.max(0, depth - 1);
index += 1;
continue;
}

if (!SQL_WORD_START.test(char)) {
index += 1;
continue;
}

const start = index;
index += 1;

while (index < segment.end && SQL_WORD_PART.test(line[index])) {
index += 1;
}

words.push({
start,
end: index,
normalized: line.slice(start, index).toLowerCase(),
depth,
});
}
}

return words;
}

function pushTrimmed(lines: string[], value: string): void {
const trimmed = value.trim();

Expand Down
44 changes: 4 additions & 40 deletions src/formatter/passes/structural/exceptionFormatting.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { SqlDialect } from '../../../dialects';
import {
collectSqlWordsFromSegments,
scanSqlLineOutsideLiteralsAndComments,
type SqlLineScanState,
type SqlOutsideSegment,
type SqlWordMatch,
} from '../../sqlLineScanner';

export interface ExceptionFormattingState {
Expand All @@ -15,14 +16,7 @@ interface ExpandedLineResult {
readonly nextState: ExceptionFormattingState;
}

interface WordMatch {
readonly start: number;
readonly end: number;
readonly normalized: string;
}

const SQL_WORD_START = /[A-Za-z_]/u;
const SQL_WORD_PART = /[A-Za-z0-9_$#]/u;
type WordMatch = SqlWordMatch;

export function createInitialExceptionFormattingState(): ExceptionFormattingState {
return {
Expand All @@ -45,7 +39,7 @@ export function expandWatcomExceptionLine(
initialState: ExceptionFormattingState,
): ExpandedLineResult {
const scanResult = scanSqlLineOutsideLiteralsAndComments(line, initialState.scanState);
const words = collectWords(line, scanResult.outsideSegments);
const words = collectSqlWordsFromSegments(line, scanResult.outsideSegments);
const nextState: ExceptionFormattingState = {
scanState: scanResult.nextState,
inExceptionSection: calculateNextExceptionSectionState(words, initialState.inExceptionSection),
Expand Down Expand Up @@ -187,33 +181,3 @@ function pushTrimmed(lines: string[], text: string): void {
lines.push(trimmed);
}
}

function collectWords(line: string, outsideSegments: readonly SqlOutsideSegment[]): WordMatch[] {
const words: WordMatch[] = [];

for (const segment of outsideSegments) {
let index = segment.start;

while (index < segment.end) {
if (!SQL_WORD_START.test(line[index])) {
index += 1;
continue;
}

const start = index;
index += 1;

while (index < segment.end && SQL_WORD_PART.test(line[index])) {
index += 1;
}

words.push({
start,
end: index,
normalized: line.slice(start, index).toLowerCase(),
});
}
}

return words;
}
Loading
Loading