New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split clauses into two groups: multiline & single line #483
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
c022749
Remove SET SCHEMA support from Hive
nene 4efe16a
Implement single-line formatted clauses
nene 5ef7143
Apply oneline formatting to TRUNCATE TABLE
nene 8847bad
Use oneline format for many BigQuery clauses
nene f38641a
Format all extra (untested) clauses on single line
nene 76db367
Optimize lookup of onelineClauses
nene 36dd581
Ensure alwaysDenseOperators field is always an array
nene 462112d
Extract utility for saving data to class fields
nene 024492a
Format BigQuery CREATE SCHEMA on single line
nene 0d76d38
Don't indent oneline clause children
nene 697901d
Eliminate complex conditional logic in formatClause
nene 7a113b2
Use oneline formatting for BigQuery procedure creation
nene 6dd0283
Additional oneline clauses for BigQuery
nene ea9ecaa
Add BigQuery search index creation to oneline clauses
nene 56a7049
Add BigQuery DROP clauses to online clauses list
nene dfc3d39
Drop excessive tests for BigQuery syntax variants
nene 38fef82
Drop tests for CREATE TABLE LIKE/COPY/CLONE
nene b97e27d
GROUP tests for statements using AS JSON
nene 5f6e88e
Delete DROP SCHEMA CASCADE/RESTRICT tests
nene 1df0064
Remove test for DROP IF EXISTS variant
nene b94f81f
Group all DROP clauses to single test
nene fdf06b7
Avoid repeating the same string
nene f029c5b
More compact DROP clauses test
nene 53a6af8
Merge pull request #485 from sql-formatter-org/bigquery-tests
nene 0cfc6e8
Merge branch 'master' into oneline-clauses
nene cdfbf33
Merge remote-tracking branch 'refs/remotes/origin/oneline-clauses' in…
nene 0cc7f0c
Format ALTER TABLE using oneline style
nene 9d0fead
Use oneline style for DROP TABLE
nene b3a18aa
Use oneline formatting for DELETE FROM
nene 80b8ef6
Format UPDATE statement using online style
nene 5237348
Format N1QL USE KEYS clause in oneline style
nene 4c60475
Merge PR #490: Format ALTER TABLE and some other statements using one…
nene File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -36,7 +36,7 @@ import InlineLayout, { InlineLayoutError } from './InlineLayout.js'; | |||||
|
||||||
interface ExpressionFormatterParams { | ||||||
cfg: FormatOptions; | ||||||
dialectCfg: DialectFormatOptions; | ||||||
dialectCfg: ProcessedDialectFormatOptions; | ||||||
params: Params; | ||||||
layout: Layout; | ||||||
inline?: boolean; | ||||||
|
@@ -45,12 +45,21 @@ interface ExpressionFormatterParams { | |||||
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[]; | ||||||
} | ||||||
|
||||||
// Contains the same data as DialectFormatOptions, | ||||||
// but optimized for faster and more conventient lookup. | ||||||
export interface ProcessedDialectFormatOptions { | ||||||
alwaysDenseOperators: string[]; | ||||||
onelineClauses: Record<string, boolean>; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried that, but in this case TypeScript thinks that |
||||||
} | ||||||
|
||||||
/** Formats a generic SQL expression */ | ||||||
export default class ExpressionFormatter { | ||||||
private cfg: FormatOptions; | ||||||
private dialectCfg: DialectFormatOptions; | ||||||
private dialectCfg: ProcessedDialectFormatOptions; | ||||||
private params: Params; | ||||||
private layout: Layout; | ||||||
|
||||||
|
@@ -207,18 +216,36 @@ export default class ExpressionFormatter { | |||||
} | ||||||
|
||||||
private formatClause(node: ClauseNode) { | ||||||
if (isTabularStyle(this.cfg)) { | ||||||
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node.nameKw), WS.SPACE); | ||||||
if (this.isOnelineClause(node)) { | ||||||
this.formatClauseInOnelineStyle(node); | ||||||
} else if (isTabularStyle(this.cfg)) { | ||||||
this.formatClauseInTabularStyle(node); | ||||||
} else { | ||||||
this.layout.add(WS.NEWLINE, WS.INDENT, this.showKw(node.nameKw), WS.NEWLINE); | ||||||
this.formatClauseInIndentedStyle(node); | ||||||
} | ||||||
} | ||||||
|
||||||
private isOnelineClause(node: ClauseNode): boolean { | ||||||
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(); | ||||||
} | ||||||
|
||||||
if (!isTabularStyle(this.cfg)) { | ||||||
this.layout.add(WS.INDENT); | ||||||
} | ||||||
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(); | ||||||
} | ||||||
|
||||||
|
@@ -267,7 +294,7 @@ export default class ExpressionFormatter { | |||||
} | ||||||
|
||||||
private formatOperator({ text }: OperatorNode) { | ||||||
if (this.cfg.denseOperators || this.dialectCfg.alwaysDenseOperators?.includes(text)) { | ||||||
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); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we not just use this as the default interface for DialectFormatOptions and add a wrapper function to transform the string[] into Record ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not only the
onlineClauses
field that differs. It's also thealwaysDenseOperators
field which is originally optional, but then processed to be empty array if missing.