Skip to content

Commit 18b61b5

Browse files
authored
fix(formatter): minor fixes (#680)
from running it on a large schema
1 parent 3a58214 commit 18b61b5

18 files changed

Lines changed: 174 additions & 5 deletions

crates/pgls_pretty_print/src/nodes/comment_stmt.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,14 @@ pub(super) fn emit_comment_stmt(e: &mut EventEmitter, n: &CommentStmt) {
195195
e.space();
196196
super::emit_node(object, e);
197197
} else {
198-
super::emit_node(object, e);
198+
// For most object types (TABLE, INDEX, SEQUENCE, VIEW, etc.), the object
199+
// is a List of String nodes representing a schema-qualified name that must
200+
// be dot-separated (e.g., public.my_table), not comma-separated.
201+
if let Some(pgls_query::NodeEnum::List(list)) = object.node.as_ref() {
202+
emit_dot_separated_list(e, &list.items);
203+
} else {
204+
super::emit_node(object, e);
205+
}
199206
}
200207
}
201208

crates/pgls_pretty_print/src/nodes/create_extension_stmt.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use super::node_list::emit_comma_separated_list;
21
use crate::{
32
TokenKind,
43
emitter::{EventEmitter, GroupKind, LineType},
54
};
6-
use pgls_query::protobuf::CreateExtensionStmt;
5+
use pgls_query::{NodeEnum, protobuf::CreateExtensionStmt};
76

87
pub(super) fn emit_create_extension_stmt(e: &mut EventEmitter, n: &CreateExtensionStmt) {
98
e.group_start(GroupKind::CreateExtensionStmt);
@@ -27,11 +26,57 @@ pub(super) fn emit_create_extension_stmt(e: &mut EventEmitter, n: &CreateExtensi
2726
if !n.options.is_empty() {
2827
e.line(LineType::SoftOrSpace);
2928
e.token(TokenKind::WITH_KW);
30-
e.space();
31-
emit_comma_separated_list(e, &n.options, super::emit_node);
29+
30+
for opt in &n.options {
31+
let def = assert_node_variant!(DefElem, opt);
32+
e.space();
33+
emit_extension_option(e, def);
34+
}
3235
}
3336

3437
e.token(TokenKind::SEMICOLON);
3538

3639
e.group_end();
3740
}
41+
42+
/// Emit a CREATE EXTENSION option.
43+
/// Options use keyword syntax (no equals sign):
44+
/// SCHEMA schema_name
45+
/// VERSION 'version'
46+
/// CASCADE
47+
fn emit_extension_option(e: &mut EventEmitter, def: &pgls_query::protobuf::DefElem) {
48+
match def.defname.as_str() {
49+
"schema" => {
50+
e.token(TokenKind::SCHEMA_KW);
51+
if let Some(ref arg) = def.arg {
52+
e.space();
53+
// Schema name is a String node - emit_node dispatches to
54+
// emit_string which uses emit_identifier_maybe_quoted
55+
super::emit_node(arg, e);
56+
}
57+
}
58+
"new_version" => {
59+
e.token(TokenKind::VERSION_KW);
60+
if let Some(ref arg) = def.arg {
61+
e.space();
62+
// Version must be a quoted string literal
63+
if let Some(NodeEnum::String(s)) = &arg.node {
64+
super::emit_string_literal(e, s);
65+
} else {
66+
super::emit_node(arg, e);
67+
}
68+
}
69+
}
70+
"cascade" => {
71+
e.token(TokenKind::CASCADE_KW);
72+
}
73+
_ => {
74+
// Fallback for unknown options
75+
super::string::emit_keyword(e, &def.defname);
76+
if let Some(ref arg) = def.arg {
77+
e.space();
78+
super::emit_node(arg, e);
79+
}
80+
}
81+
}
82+
}

crates/pgls_pretty_print/src/normalize.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,28 @@ fn normalize_a_indirection(node: &mut NodeEnum) {
686686
}
687687
}
688688
}
689+
NodeEnum::CreateStmt(stmt) => {
690+
// Recurse into table elements (ColumnDef, Constraint, etc.)
691+
for elt in &mut stmt.table_elts {
692+
if let Some(ref mut n) = elt.node {
693+
normalize_a_indirection(n);
694+
}
695+
}
696+
}
697+
NodeEnum::ColumnDef(cd) => {
698+
// Recurse into column constraints
699+
for constraint in &mut cd.constraints {
700+
if let Some(ref mut n) = constraint.node {
701+
normalize_a_indirection(n);
702+
}
703+
}
704+
// Recurse into default value expression
705+
if let Some(ref mut raw) = cd.raw_default
706+
&& let Some(ref mut n) = raw.node
707+
{
708+
normalize_a_indirection(n);
709+
}
710+
}
689711
NodeEnum::RuleStmt(stmt) => {
690712
for action in &mut stmt.actions {
691713
if let Some(ref mut n) = action.node {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMMENT ON TABLE public.api_token IS 'Listing of known API tokens';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMMENT ON TABLE "public"."event" IS 'An event is created everytime something interesting happens';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE EXTENSION vector WITH SCHEMA extensions;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA extensions;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE t (
2+
"type" "public"."my_enum" NOT NULL,
3+
"target_column" "text",
4+
"from_value" "text",
5+
"to_value" "text",
6+
CONSTRAINT "c2" CHECK (
7+
("type" <> 'es_value_changed'::"public"."my_enum") OR
8+
(
9+
("target_column" = 'config'::"text") AND
10+
(("from_value" IS NULL) AND ("to_value" IS NULL))
11+
) OR
12+
(
13+
(("from_value" IS NOT NULL) AND ("to_value" IS NOT NULL)) AND
14+
("target_column" IN ('type', 'name'))
15+
)
16+
)
17+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/pgls_pretty_print/tests/tests.rs
3+
input_file: crates/pgls_pretty_print/tests/data/single/comment_stmt_1.sql
4+
---
5+
comment on table public.api_token is 'Listing of known API tokens';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/pgls_pretty_print/tests/tests.rs
3+
input_file: crates/pgls_pretty_print/tests/data/single/comment_stmt_1.sql
4+
---
5+
comment on table public.api_token is 'Listing of known API tokens';

0 commit comments

Comments
 (0)