-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathExpressionFormatter.ts
More file actions
736 lines (663 loc) · 23.5 KB
/
ExpressionFormatter.ts
File metadata and controls
736 lines (663 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import { FormatOptions } from '../FormatOptions.js';
import { equalizeWhitespace, isMultiline, last } from '../utils.js';
import Params from './Params.js';
import { isTabularStyle } from './config.js';
import { TokenType } from '../lexer/token.js';
import {
AllColumnsAsteriskNode,
ArraySubscriptNode,
AstNode,
BetweenPredicateNode,
SetOperationNode,
ClauseNode,
FunctionCallNode,
LimitClauseNode,
NodeType,
ParenthesisNode,
LiteralNode,
IdentifierNode,
ParameterNode,
OperatorNode,
LineCommentNode,
BlockCommentNode,
CommaNode,
KeywordNode,
PropertyAccessNode,
CommentNode,
CaseExpressionNode,
CaseWhenNode,
CaseElseNode,
DataTypeNode,
ParameterizedDataTypeNode,
DisableCommentNode,
} from '../parser/ast.js';
import Layout, { WS } from './Layout.js';
import toTabularFormat, { isTabularToken } from './tabularStyle.js';
import InlineLayout, { InlineLayoutError } from './InlineLayout.js';
interface ExpressionFormatterParams {
cfg: FormatOptions;
dialectCfg: ProcessedDialectFormatOptions;
params: Params;
layout: Layout;
inline?: boolean;
}
export interface DialectFormatOptions {
// List of operators that should always be formatted without surrounding spaces
alwaysDenseOperators?: string[];
// List of clauses that should be formatted on a single line
onelineClauses: string[];
// List of clauses that should be formatted on a single line in tabular style
tabularOnelineClauses?: string[];
}
// Contains the same data as DialectFormatOptions,
// but optimized for faster and more conventient lookup.
export interface ProcessedDialectFormatOptions {
alwaysDenseOperators: string[];
onelineClauses: Record<string, boolean>;
tabularOnelineClauses: Record<string, boolean>;
}
/** Formats a generic SQL expression */
export default class ExpressionFormatter {
private cfg: FormatOptions;
private dialectCfg: ProcessedDialectFormatOptions;
private params: Params;
private layout: Layout;
private pendingLineComment: (LineCommentNode | BlockCommentNode | DisableCommentNode)[] = []; // Keeps track if any pending Line/Comment is remaining
private inline = false;
private nodes: AstNode[] = [];
private index = -1;
constructor({ cfg, dialectCfg, params, layout, inline = false }: ExpressionFormatterParams) {
this.cfg = cfg;
this.dialectCfg = dialectCfg;
this.inline = inline;
this.params = params;
this.layout = layout;
}
public format(nodes: AstNode[]): Layout {
this.nodes = nodes;
for (this.index = 0; this.index < this.nodes.length; this.index++) {
this.formatNode(this.nodes[this.index]);
}
return this.layout;
}
private formatNode(node: AstNode) {
this.formatComments(node.leadingComments);
this.formatNodeWithoutComments(node);
this.formatComments(node.trailingComments);
}
private formatNodeWithoutComments(node: AstNode) {
switch (node.type) {
case NodeType.function_call:
return this.formatFunctionCall(node);
case NodeType.parameterized_data_type:
return this.formatParameterizedDataType(node);
case NodeType.array_subscript:
return this.formatArraySubscript(node);
case NodeType.property_access:
return this.formatPropertyAccess(node);
case NodeType.parenthesis:
return this.formatParenthesis(node);
case NodeType.between_predicate:
return this.formatBetweenPredicate(node);
case NodeType.case_expression:
return this.formatCaseExpression(node);
case NodeType.case_when:
return this.formatCaseWhen(node);
case NodeType.case_else:
return this.formatCaseElse(node);
case NodeType.clause:
return this.formatClause(node);
case NodeType.set_operation:
return this.formatSetOperation(node);
case NodeType.limit_clause:
return this.formatLimitClause(node);
case NodeType.all_columns_asterisk:
return this.formatAllColumnsAsterisk(node);
case NodeType.literal:
return this.formatLiteral(node);
case NodeType.identifier:
return this.formatIdentifier(node);
case NodeType.parameter:
return this.formatParameter(node);
case NodeType.operator:
return this.formatOperator(node);
case NodeType.comma:
return this.formatComma(node);
case NodeType.line_comment:
return this.formatLineComment(node);
case NodeType.block_comment:
return this.formatBlockComment(node);
case NodeType.disable_comment:
return this.formatBlockComment(node);
case NodeType.data_type:
return this.formatDataType(node);
case NodeType.keyword:
return this.formatKeywordNode(node);
}
}
private formatFunctionCall(node: FunctionCallNode) {
this.withComments(node.nameKw, () => {
this.layout.add(this.showFunctionKw(node.nameKw));
});
this.formatNode(node.parenthesis);
}
private formatParameterizedDataType(node: ParameterizedDataTypeNode) {
this.withComments(node.dataType, () => {
this.layout.add(this.showDataType(node.dataType));
});
this.formatNode(node.parenthesis);
}
private formatArraySubscript(node: ArraySubscriptNode) {
let formattedArray: string;
switch (node.array.type) {
case NodeType.data_type:
formattedArray = this.showDataType(node.array);
break;
case NodeType.keyword:
formattedArray = this.showKw(node.array);
break;
default:
formattedArray = this.showIdentifier(node.array);
break;
}
this.withComments(node.array, () => {
this.layout.add(formattedArray);
});
this.formatNode(node.parenthesis);
}
private formatPropertyAccess(node: PropertyAccessNode) {
this.formatComments(node.object.leadingComments);
this.formatNodeWithoutComments(node.object);
// Handle trailing comments of object BEFORE the dot (inline)
if (node.object.trailingComments && node.object.trailingComments.length > 0) {
for (const comment of node.object.trailingComments) {
if (comment.type === NodeType.block_comment) {
this.layout.add(WS.NO_SPACE, WS.SPACE, (comment as BlockCommentNode).text);
} else if (comment.type === NodeType.line_comment) {
this.layout.add(WS.NO_SPACE, WS.SPACE, (comment as LineCommentNode).text);
}
}
}
// Add the dot operator
this.layout.add(WS.NO_SPACE, node.operator);
// Handle leading comments of property AFTER the dot (inline)
if (node.property.leadingComments && node.property.leadingComments.length > 0) {
for (const comment of node.property.leadingComments) {
if (comment.type === NodeType.block_comment) {
this.layout.add((comment as BlockCommentNode).text, WS.SPACE);
} else if (comment.type === NodeType.line_comment) {
this.layout.add((comment as LineCommentNode).text, WS.SPACE);
}
}
// Format property without leading comments (already handled above)
this.formatNodeWithoutComments(node.property);
this.formatComments(node.property.trailingComments);
} else {
this.formatNode(node.property);
}
}
private formatParenthesis(node: ParenthesisNode) {
const inlineLayout = this.formatInlineExpression(node.children);
if (inlineLayout) {
this.layout.add(node.openParen);
this.layout.add(...inlineLayout.getLayoutItems());
this.layout.add(WS.NO_SPACE, node.closeParen, WS.SPACE);
} else {
this.layout.add(node.openParen, WS.NEWLINE);
if (isTabularStyle(this.cfg)) {
this.layout.add(WS.INDENT);
this.layout = this.formatSubExpression(node.children);
} else {
this.layout.indentation.increaseBlockLevel();
this.layout.add(WS.INDENT);
this.layout = this.formatSubExpression(node.children);
this.layout.indentation.decreaseBlockLevel();
}
this.layout.add(WS.NEWLINE, WS.INDENT, node.closeParen, WS.SPACE);
}
}
private formatBetweenPredicate(node: BetweenPredicateNode) {
this.layout.add(this.showKw(node.betweenKw), WS.SPACE);
this.layout = this.formatSubExpression(node.expr1);
this.layout.add(WS.NO_SPACE, WS.SPACE, this.showNonTabularKw(node.andKw), WS.SPACE);
this.layout = this.formatSubExpression(node.expr2);
this.layout.add(WS.SPACE);
}
private formatCaseExpression(node: CaseExpressionNode) {
this.formatNode(node.caseKw);
this.layout.indentation.increaseBlockLevel();
this.layout = this.formatSubExpression(node.expr);
this.layout = this.formatSubExpression(node.clauses);
this.layout.indentation.decreaseBlockLevel();
this.layout.add(WS.NEWLINE, WS.INDENT);
this.formatNode(node.endKw);
}
private formatCaseWhen(node: CaseWhenNode) {
this.layout.add(WS.NEWLINE, WS.INDENT);
this.formatNode(node.whenKw);
this.layout = this.formatSubExpression(node.condition);
this.formatNode(node.thenKw);
this.layout = this.formatSubExpression(node.result);
}
private formatCaseElse(node: CaseElseNode) {
this.layout.add(WS.NEWLINE, WS.INDENT);
this.formatNode(node.elseKw);
this.layout = this.formatSubExpression(node.result);
}
private formatClause(node: ClauseNode) {
if (this.isOnelineClause(node)) {
this.formatClauseInOnelineStyle(node);
} else if (isTabularStyle(this.cfg)) {
this.formatClauseInTabularStyle(node);
} else {
this.formatClauseInIndentedStyle(node);
}
}
private isOnelineClause(node: ClauseNode): boolean {
if (isTabularStyle(this.cfg)) {
return this.dialectCfg.tabularOnelineClauses[node.nameKw.text];
} else {
return this.dialectCfg.onelineClauses[node.nameKw.text];
}
}
private formatClauseInIndentedStyle(node: ClauseNode) {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node.nameKw), WS.NEWLINE);
this.layout.indentation.increaseTopLevel();
this.layout.add(WS.INDENT);
this.layout = this.formatSubExpression(node.children);
this.layout.indentation.decreaseTopLevel();
}
private formatClauseInOnelineStyle(node: ClauseNode) {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node.nameKw), WS.SPACE);
this.layout = this.formatSubExpression(node.children);
}
private formatClauseInTabularStyle(node: ClauseNode) {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node.nameKw), WS.SPACE);
this.layout.indentation.increaseTopLevel();
this.layout = this.formatSubExpression(node.children);
this.layout.indentation.decreaseTopLevel();
}
private formatSetOperation(node: SetOperationNode) {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node.nameKw), WS.NEWLINE);
this.layout.add(WS.INDENT);
this.layout = this.formatSubExpression(node.children);
}
private formatLimitClause(node: LimitClauseNode) {
this.withComments(node.limitKw, () => {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node.limitKw));
});
this.layout.indentation.increaseTopLevel();
if (isTabularStyle(this.cfg)) {
this.layout.add(WS.SPACE);
} else {
this.layout.add(WS.NEWLINE, WS.INDENT);
}
if (node.offset) {
this.layout = this.formatSubExpression(node.offset);
this.layout.add(WS.NO_SPACE, ',', WS.SPACE);
this.layout = this.formatSubExpression(node.count);
} else {
this.layout = this.formatSubExpression(node.count);
}
this.layout.indentation.decreaseTopLevel();
}
private formatAllColumnsAsterisk(_node: AllColumnsAsteriskNode) {
this.layout.add('*', WS.SPACE);
}
private formatLiteral(node: LiteralNode) {
this.layout.add(node.text, WS.SPACE);
}
private formatIdentifier(node: IdentifierNode) {
this.layout.add(this.showIdentifier(node), WS.SPACE);
}
private formatParameter(node: ParameterNode) {
this.layout.add(this.params.get(node), WS.SPACE);
}
private formatOperator({ text }: OperatorNode) {
if (this.cfg.denseOperators || this.dialectCfg.alwaysDenseOperators.includes(text)) {
this.layout.add(WS.NO_SPACE, text);
} else if (text === ':') {
this.layout.add(WS.NO_SPACE, text, WS.SPACE);
} else {
this.layout.add(text, WS.SPACE);
}
}
private formatComma(_node: CommaNode) {
if (!this.inline) {
if (this.cfg.commaPosition === 'leading' || this.cfg.commaPosition === 'leadingWithSpace') {
// Look ahead: check if next node is a line comment
this.formatLeadingComma();
} else {
this.formatTrailingComma();
}
} else {
this.layout.add(WS.NO_SPACE, ',', WS.SPACE);
}
}
private formatTrailingComma() {
if (this.pendingLineComment && this.pendingLineComment.length > 0) {
// We have a line comment that should come after the comma
while (this.pendingLineComment.length > 0) {
const comment = this.pendingLineComment.shift();
if (comment) {
if (comment.type === NodeType.line_comment) {
this.layout.add(
WS.NO_SPACE,
',',
WS.SPACE,
(comment as LineCommentNode).text,
WS.MANDATORY_NEWLINE,
WS.INDENT
);
} else {
this.layout.add((comment as BlockCommentNode).text, WS.MANDATORY_NEWLINE, WS.INDENT);
}
}
}
} else {
this.layout.add(WS.NO_SPACE, ',', WS.NEWLINE, WS.INDENT);
}
}
private formatLeadingComma() {
const comments: AstNode[] = [];
let lookAheadIndex = this.index + 1;
while (lookAheadIndex < this.nodes.length) {
const nextNode = this.nodes[lookAheadIndex];
if (nextNode.type === NodeType.line_comment || nextNode.type === NodeType.block_comment) {
comments.push(nextNode);
lookAheadIndex++;
} else {
break;
}
}
if (comments.length === 0) {
// No comments - simple case
if (this.cfg.commaPosition === 'leadingWithSpace') {
this.layout.add(WS.NEWLINE, WS.INDENT, ',', WS.SPACE);
} else {
this.layout.add(WS.NEWLINE, WS.INDENT, ',');
}
return;
}
// First: output any line comment on the same line (belongs to previous item)
let lineCommentProcessed = false;
if (comments[0]?.type === NodeType.line_comment) {
this.layout.add((comments[0] as LineCommentNode).text);
lineCommentProcessed = true;
}
// Second: output all block comments on their own lines BEFORE the comma
const startIndex = lineCommentProcessed ? 1 : 0;
for (let i = startIndex; i < comments.length; i++) {
const comment = comments[i];
if (comment.type === NodeType.block_comment) {
const blockComment = comment as BlockCommentNode;
if (this.isMultilineBlockComment(blockComment)) {
this.splitBlockComment(blockComment.text).forEach(line => {
this.layout.add(WS.NEWLINE, WS.INDENT, line);
});
} else {
this.layout.add(WS.NEWLINE, WS.INDENT, blockComment.text);
}
} else if (comment.type === NodeType.line_comment) {
// Additional line comments (rare case) - treat like block comments
this.layout.add(WS.NEWLINE, WS.INDENT, (comment as LineCommentNode).text);
}
}
// Finally: add the comma
if (this.cfg.commaPosition === 'leadingWithSpace') {
this.layout.add(WS.NEWLINE, WS.INDENT, ',', WS.SPACE);
} else {
this.layout.add(WS.NEWLINE, WS.INDENT, ',');
}
// Skip all processed comments
this.index = lookAheadIndex - 1;
}
private withComments(node: AstNode, fn: () => void) {
this.formatComments(node.leadingComments);
fn();
this.formatComments(node.trailingComments);
}
private formatComments(comments: CommentNode[] | undefined) {
if (!comments) {
return;
}
comments.forEach(com => {
if (com.type === NodeType.line_comment) {
this.formatLineComment(com);
} else {
this.formatBlockComment(com);
}
});
}
private formatLineComment(node: LineCommentNode) {
if (!this.inline && this.cfg.commaPosition === 'trailing' && this.isNextNonCommentNodeComma()) {
// Store the comment to be output after the comma
this.pendingLineComment.push(node);
return;
}
if (isMultiline(node.precedingWhitespace || '')) {
this.layout.add(WS.NEWLINE, WS.INDENT, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
} else if (this.layout.getLayoutItems().length > 0) {
this.layout.add(WS.NO_NEWLINE, WS.SPACE, node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
} else {
// comment is the first item in code - no need to add preceding spaces
this.layout.add(node.text, WS.MANDATORY_NEWLINE, WS.INDENT);
}
}
private isNextNonCommentNodeComma(): boolean {
let lookAheadIndex = this.index + 1;
while (lookAheadIndex < this.nodes.length) {
const nextNode = this.nodes[lookAheadIndex];
if (nextNode.type === NodeType.line_comment || nextNode.type === NodeType.block_comment) {
lookAheadIndex++;
} else {
return nextNode.type === NodeType.comma;
}
}
return false;
}
private formatBlockComment(node: BlockCommentNode | DisableCommentNode) {
if (!this.inline && this.cfg.commaPosition === 'trailing' && this.isNextNonCommentNodeComma()) {
// Store the comment to be output after the comma
this.pendingLineComment.push(node);
return;
}
if (node.type === NodeType.block_comment && this.isMultilineBlockComment(node)) {
this.splitBlockComment(node.text).forEach(line => {
this.layout.add(WS.NEWLINE, WS.INDENT, line);
});
this.layout.add(WS.NEWLINE, WS.INDENT);
} else {
this.layout.add(node.text, WS.SPACE);
}
}
private isMultilineBlockComment(node: BlockCommentNode): boolean {
return isMultiline(node.text) || isMultiline(node.precedingWhitespace || '');
}
private isDocComment(comment: string): boolean {
const lines = comment.split(/\n/);
return (
// first line starts with /* or /**
/^\/\*\*?$/.test(lines[0]) &&
// intermediate lines start with *
lines.slice(1, lines.length - 1).every(line => /^\s*\*/.test(line)) &&
// last line ends with */
/^\s*\*\/$/.test(last(lines) as string)
);
}
// Breaks up block comment to multiple lines.
// For example this doc-comment (dots representing leading whitespace):
//
// ..../**
// .....* Some description here
// .....* and here too
// .....*/
//
// gets broken to this array (note the leading single spaces):
//
// [ '/**',
// '.* Some description here',
// '.* and here too',
// '.*/' ]
//
// However, a normal comment (non-doc-comment) like this:
//
// ..../*
// ....Some description here
// ....*/
//
// gets broken to this array (no leading spaces):
//
// [ '/*',
// 'Some description here',
// '*/' ]
//
private splitBlockComment(comment: string): string[] {
if (this.isDocComment(comment)) {
return comment.split(/\n/).map(line => {
if (/^\s*\*/.test(line)) {
return ' ' + line.replace(/^\s*/, '');
} else {
return line;
}
});
} else {
return comment.split(/\n/).map(line => line.replace(/^\s*/, ''));
}
}
private formatSubExpression(nodes: AstNode[]): Layout {
return new ExpressionFormatter({
cfg: this.cfg,
dialectCfg: this.dialectCfg,
params: this.params,
layout: this.layout,
inline: this.inline,
}).format(nodes);
}
private formatInlineExpression(nodes: AstNode[]): Layout | undefined {
const oldParamIndex = this.params.getPositionalParameterIndex();
try {
return new ExpressionFormatter({
cfg: this.cfg,
dialectCfg: this.dialectCfg,
params: this.params,
layout: new InlineLayout(this.cfg.expressionWidth),
inline: true,
}).format(nodes);
} catch (e) {
if (e instanceof InlineLayoutError) {
// While formatting, some of the positional parameters might have
// been consumed, which increased the current parameter index.
// We reset the index to an earlier state, so we can run the
// formatting again and re-consume these parameters in non-inline mode.
this.params.setPositionalParameterIndex(oldParamIndex);
return undefined;
} else {
// forward all unexpected errors
throw e;
}
}
}
private formatKeywordNode(node: KeywordNode): void {
switch (node.tokenType) {
case TokenType.RESERVED_JOIN:
return this.formatJoin(node);
case TokenType.AND:
case TokenType.OR:
case TokenType.XOR:
return this.formatLogicalOperator(node);
default:
return this.formatKeyword(node);
}
}
private formatJoin(node: KeywordNode) {
if (isTabularStyle(this.cfg)) {
// in tabular style JOINs are at the same level as clauses
this.layout.indentation.decreaseTopLevel();
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node), WS.SPACE);
this.layout.indentation.increaseTopLevel();
} else {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node), WS.SPACE);
}
}
private formatKeyword(node: KeywordNode) {
this.layout.add(this.showKw(node), WS.SPACE);
}
private formatLogicalOperator(node: KeywordNode) {
if (this.cfg.logicalOperatorNewline === 'before') {
if (isTabularStyle(this.cfg)) {
// In tabular style AND/OR is placed on the same level as clauses
this.layout.indentation.decreaseTopLevel();
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node), WS.SPACE);
this.layout.indentation.increaseTopLevel();
} else {
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node), WS.SPACE);
}
} else {
this.layout.add(this.showKw(node), WS.NEWLINE, WS.INDENT);
}
}
private formatDataType(node: DataTypeNode) {
this.layout.add(this.showDataType(node), WS.SPACE);
}
private showKw(node: KeywordNode): string {
if (isTabularToken(node.tokenType)) {
return toTabularFormat(this.showNonTabularKw(node), this.cfg.indentStyle);
} else {
return this.showNonTabularKw(node);
}
}
// Like showKw(), but skips tabular formatting
private showNonTabularKw(node: KeywordNode): string {
switch (this.cfg.keywordCase) {
case 'preserve':
return equalizeWhitespace(node.raw);
case 'upper':
return node.text;
case 'lower':
return node.text.toLowerCase();
}
}
private showFunctionKw(node: KeywordNode): string {
if (isTabularToken(node.tokenType)) {
return toTabularFormat(this.showNonTabularFunctionKw(node), this.cfg.indentStyle);
} else {
return this.showNonTabularFunctionKw(node);
}
}
// Like showFunctionKw(), but skips tabular formatting
private showNonTabularFunctionKw(node: KeywordNode): string {
switch (this.cfg.functionCase) {
case 'preserve':
return equalizeWhitespace(node.raw);
case 'upper':
return node.text;
case 'lower':
return node.text.toLowerCase();
}
}
private showIdentifier(node: IdentifierNode): string {
if (node.quoted) {
return node.text;
} else {
switch (this.cfg.identifierCase) {
case 'preserve':
return node.text;
case 'upper':
return node.text.toUpperCase();
case 'lower':
return node.text.toLowerCase();
}
}
}
private showDataType(node: DataTypeNode): string {
switch (this.cfg.dataTypeCase) {
case 'preserve':
return equalizeWhitespace(node.raw);
case 'upper':
return node.text;
case 'lower':
return node.text.toLowerCase();
}
}
}