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
10 changes: 6 additions & 4 deletions crates/squawk_fmt/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use itertools::Itertools;
use rowan::Direction;
use squawk_syntax::ast::{self, AstNode};
use squawk_syntax::quote::quote_column_alias;
use squawk_syntax::{SyntaxKind, SyntaxNode, SyntaxToken};
use tiny_pretty::Doc;
use tiny_pretty::{PrintOptions, print};
Expand Down Expand Up @@ -445,6 +446,10 @@ fn build_literal<'a>(lit: ast::Literal) -> Doc<'a> {
Doc::text(lit.syntax().to_string())
}

fn build_name<'a>(name: ast::Name) -> Doc<'a> {
Doc::text(quote_column_alias(&name.text()))
}

fn build_type<'a>(ty: ast::Type) -> Doc<'a> {
Doc::text(ty.syntax().to_string())
}
Expand Down Expand Up @@ -538,10 +543,7 @@ fn build_target<'a>(target: ast::Target) -> Option<Doc<'a>> {
}

if let Some(name) = as_name.name() {
// TODO: quoting or not?
doc = doc
.append(Doc::space())
.append(Doc::text(name.syntax().to_string()));
doc = doc.append(Doc::space()).append(build_name(name));
}
}

Expand Down
6 changes: 6 additions & 0 deletions crates/squawk_fmt/tests/after/select.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ select now();
select
'really long string ',
'another really long string';

select
foo as "Quoted Alias"
from "Quoted Table";

select 1 as foo;
4 changes: 4 additions & 0 deletions crates/squawk_fmt/tests/before/select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ select 'hello';
select now();

select 'really long string ', 'another really long string';

select foo as "Quoted Alias" from "Quoted Table";

select 1 as "foo";
29 changes: 27 additions & 2 deletions crates/squawk_fmt/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,43 @@ fn meaningful_tokens(text: &str) -> Vec<(TokenKind, &str)> {
tokens
}

fn tokens_equivalent(before: (TokenKind, &str), after: (TokenKind, &str)) -> bool {
let (bkind, btext) = before;
let (akind, atext) = after;

if bkind == akind {
return btext.eq_ignore_ascii_case(atext);
}

// We convert `select 1 "foo"` to `select 1 foo` so we need to do some quote
// munging
let unquote = |kind: TokenKind, text: &str| match kind {
TokenKind::QuotedIdent { .. } => text
.strip_prefix('"')
.and_then(|t| t.strip_suffix('"'))
.map(|t| t.to_string()),
TokenKind::Ident => Some(text.to_string()),
_ => None,
};

match (unquote(bkind, btext), unquote(akind, atext)) {
(Some(b), Some(a)) => b.eq_ignore_ascii_case(&a),
_ => false,
}
}

fn assert_no_dropped_tokens(before: &str, after: &str) {
let before_tokens = meaningful_tokens(before);
let after_tokens = meaningful_tokens(after);

let before_len = before_tokens.len();
let after_len = after_tokens.len();

for (i, ((bkind, btext), (akind, atext))) in
for (i, (&(bkind, btext), &(akind, atext))) in
before_tokens.iter().zip(after_tokens.iter()).enumerate()
{
assert!(
bkind == akind && btext.eq_ignore_ascii_case(atext),
tokens_equivalent((bkind, btext), (akind, atext)),
"token mismatch at position {i}:\n before: {bkind:?} {btext:?}\n after: {akind:?} {atext:?}"
);
}
Expand Down
Loading