-
Notifications
You must be signed in to change notification settings - Fork 608
implement pretty-printing with {:#}
#1847
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b869b2
implement pretty-printing with {:#}
lovasoa ae9479d
nostd fixes
lovasoa b8e9de9
clippy
lovasoa 5aeb83c
document the new feature
lovasoa 4923316
pretty print case statements
lovasoa 5ef60c4
pretty print window function calls
lovasoa b03a05b
more readable tests
lovasoa 963873b
prettify
lovasoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -40,8 +40,14 @@ use serde::{Deserialize, Serialize}; | |
#[cfg(feature = "visitor")] | ||
use sqlparser_derive::{Visit, VisitMut}; | ||
|
||
use crate::keywords::Keyword; | ||
use crate::tokenizer::{Span, Token}; | ||
use crate::{ | ||
display_utils::SpaceOrNewline, | ||
tokenizer::{Span, Token}, | ||
}; | ||
use crate::{ | ||
display_utils::{Indent, NewLine}, | ||
keywords::Keyword, | ||
}; | ||
|
||
pub use self::data_type::{ | ||
ArrayElemTypeDef, BinaryLength, CharLengthUnits, CharacterLength, DataType, EnumMember, | ||
|
@@ -134,9 +140,9 @@ where | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut delim = ""; | ||
for t in self.slice { | ||
write!(f, "{delim}")?; | ||
f.write_str(delim)?; | ||
delim = self.sep; | ||
write!(f, "{t}")?; | ||
t.fmt(f)?; | ||
} | ||
Ok(()) | ||
} | ||
|
@@ -628,7 +634,12 @@ pub struct CaseWhen { | |
|
||
impl fmt::Display for CaseWhen { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "WHEN {} THEN {}", self.condition, self.result) | ||
f.write_str("WHEN ")?; | ||
self.condition.fmt(f)?; | ||
f.write_str(" THEN")?; | ||
SpaceOrNewline.fmt(f)?; | ||
Indent(&self.result).fmt(f)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
|
@@ -1662,23 +1673,29 @@ impl fmt::Display for Expr { | |
write!(f, "{data_type}")?; | ||
write!(f, " {value}") | ||
} | ||
Expr::Function(fun) => write!(f, "{fun}"), | ||
Expr::Function(fun) => fun.fmt(f), | ||
Expr::Case { | ||
operand, | ||
conditions, | ||
else_result, | ||
} => { | ||
write!(f, "CASE")?; | ||
f.write_str("CASE")?; | ||
if let Some(operand) = operand { | ||
write!(f, " {operand}")?; | ||
f.write_str(" ")?; | ||
operand.fmt(f)?; | ||
} | ||
for when in conditions { | ||
write!(f, " {when}")?; | ||
SpaceOrNewline.fmt(f)?; | ||
Indent(when).fmt(f)?; | ||
} | ||
if let Some(else_result) = else_result { | ||
write!(f, " ELSE {else_result}")?; | ||
SpaceOrNewline.fmt(f)?; | ||
Indent("ELSE").fmt(f)?; | ||
SpaceOrNewline.fmt(f)?; | ||
Indent(Indent(else_result)).fmt(f)?; | ||
} | ||
write!(f, " END") | ||
SpaceOrNewline.fmt(f)?; | ||
f.write_str("END") | ||
} | ||
Expr::Exists { subquery, negated } => write!( | ||
f, | ||
|
@@ -1867,8 +1884,14 @@ pub enum WindowType { | |
impl Display for WindowType { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
WindowType::WindowSpec(spec) => write!(f, "({})", spec), | ||
WindowType::NamedWindow(name) => write!(f, "{}", name), | ||
WindowType::WindowSpec(spec) => { | ||
f.write_str("(")?; | ||
NewLine.fmt(f)?; | ||
Indent(spec).fmt(f)?; | ||
NewLine.fmt(f)?; | ||
f.write_str(")") | ||
} | ||
WindowType::NamedWindow(name) => name.fmt(f), | ||
} | ||
} | ||
} | ||
|
@@ -1896,27 +1919,36 @@ pub struct WindowSpec { | |
|
||
impl fmt::Display for WindowSpec { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut delim = ""; | ||
let mut is_first = true; | ||
if let Some(window_name) = &self.window_name { | ||
delim = " "; | ||
if !is_first { | ||
SpaceOrNewline.fmt(f)?; | ||
} | ||
is_first = false; | ||
write!(f, "{window_name}")?; | ||
} | ||
if !self.partition_by.is_empty() { | ||
f.write_str(delim)?; | ||
delim = " "; | ||
if !is_first { | ||
SpaceOrNewline.fmt(f)?; | ||
} | ||
is_first = false; | ||
write!( | ||
f, | ||
"PARTITION BY {}", | ||
display_comma_separated(&self.partition_by) | ||
)?; | ||
} | ||
if !self.order_by.is_empty() { | ||
f.write_str(delim)?; | ||
delim = " "; | ||
if !is_first { | ||
SpaceOrNewline.fmt(f)?; | ||
} | ||
is_first = false; | ||
write!(f, "ORDER BY {}", display_comma_separated(&self.order_by))?; | ||
} | ||
if let Some(window_frame) = &self.window_frame { | ||
f.write_str(delim)?; | ||
if !is_first { | ||
SpaceOrNewline.fmt(f)?; | ||
} | ||
if let Some(end_bound) = &window_frame.end_bound { | ||
write!( | ||
f, | ||
|
@@ -4204,6 +4236,28 @@ impl fmt::Display for RaisErrorOption { | |
} | ||
|
||
impl fmt::Display for Statement { | ||
/// Formats a SQL statement with support for pretty printing. | ||
/// | ||
/// When using the alternate flag (`{:#}`), the statement will be formatted with proper | ||
/// indentation and line breaks. For example: | ||
/// | ||
/// ``` | ||
/// # use sqlparser::dialect::GenericDialect; | ||
/// # use sqlparser::parser::Parser; | ||
/// let sql = "SELECT a, b FROM table_1"; | ||
/// let ast = Parser::parse_sql(&GenericDialect, sql).unwrap(); | ||
/// | ||
/// // Regular formatting | ||
/// assert_eq!(format!("{}", ast[0]), "SELECT a, b FROM table_1"); | ||
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. this is awesome |
||
/// | ||
/// // Pretty printing | ||
/// assert_eq!(format!("{:#}", ast[0]), | ||
/// r#"SELECT | ||
/// a, | ||
/// b | ||
/// FROM | ||
/// table_1"#); | ||
/// ``` | ||
// Clippy thinks this function is too complicated, but it is painful to | ||
// split up without extracting structs for each `Statement` variant. | ||
#[allow(clippy::cognitive_complexity)] | ||
|
@@ -4219,7 +4273,8 @@ impl fmt::Display for Statement { | |
} => { | ||
write!(f, "FLUSH")?; | ||
if let Some(location) = location { | ||
write!(f, " {location}")?; | ||
f.write_str(" ")?; | ||
location.fmt(f)?; | ||
} | ||
write!(f, " {object_type}")?; | ||
|
||
|
@@ -4301,7 +4356,7 @@ impl fmt::Display for Statement { | |
|
||
write!(f, "{statement}") | ||
} | ||
Statement::Query(s) => write!(f, "{s}"), | ||
Statement::Query(s) => s.fmt(f), | ||
Statement::Declare { stmts } => { | ||
write!(f, "DECLARE ")?; | ||
write!(f, "{}", display_separated(stmts, "; ")) | ||
|
@@ -7056,7 +7111,8 @@ impl fmt::Display for Function { | |
} | ||
|
||
if let Some(o) = &self.over { | ||
write!(f, " OVER {o}")?; | ||
f.write_str(" OVER ")?; | ||
o.fmt(f)?; | ||
} | ||
|
||
if self.uses_odbc_syntax { | ||
|
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.
that is pretty cool
I wonder about how you will connect this back to your original goal of preserving the original whitespace.
Perhaps eventually this could be passed the original span 🤔