Skip to content

refactor(pretty_print): move shared helpers into nodes/utils behind a prelude - #995

Open
psteinroe wants to merge 3 commits into
fix/pretty-print-statement-semicolonsfrom
refactor/pretty-print-utils
Open

psteinroe wants to merge 3 commits into
fix/pretty-print-statement-semicolonsfrom
refactor/pretty-print-utils

Conversation

@psteinroe

Copy link
Copy Markdown
Collaborator

Stacked on #987.

The helpers the node emitters share were spread over nodes/mod.rs, node_list.rs, string.rs and a few node files, and node files reached them through super::node_list::..., super::string::... or the re-exports in nodes/mod.rs. This PR gives them one place, nodes/utils/, and one way in, a prelude. It moves code and changes no output: every snapshot stays as it is.

What changed

  • lib.rs holds only the public API. The crate's modules are private. config.rs holds FormatConfig and its option enums, IndentStyle and KeywordCase included, and builds the renderer's config in one place (From<&FormatConfig> for RenderConfig). format_statement built it a second time by hand.
  • The test harnesses used the emitter and renderer directly to format without comments and without the AST check. They now call format_statement_unchecked, which does the same through the public API. pgls_configuration imports IndentStyle and KeywordCase from the crate root.
  • nodes/utils/:
    • lists.rs: the list helpers from node_list.rs. The three implementations of the leading comma (comma lists, lists that break in expanded layout, grouped function arguments) and the fill separator become one emit_list_comma. A fill list still leaves own-line comments to the next item, as before.
    • tokens.rs: keywords, identifiers, quoting and literals from string.rs. string.rs keeps only emit_string.
    • layout.rs: call arguments, clause conditions, parenthesized conditions and the layout break, from nodes/mod.rs.
    • comments.rs: emit_node_with, emit_with_comments_at and emit_comments_before_keyword, from nodes/mod.rs.
    • predicates.rs: the shape checks from nodes/mod.rs, and one needs_parentheses(node, Position) in place of the three functions of that name in a_expr.rs, bool_expr.rs and type_cast.rs. The operator precedence table moves along from a_expr.rs.
  • nodes/prelude.rs: every node file imports use super::prelude::*; plus its protobuf types. nodes/mod.rs holds the module declarations, the dispatch and emit_statement.
  • One file per parser node, named after the node's field in pg_query.proto: bitstring.rs → bit_string.rs, checkpoint_stmt.rs → check_point_stmt.rs, sql_value_function.rs → sqlvalue_function.rs, and six more. DistinctExpr, NullIfExpr, AlternativeSubPlan, MergeWhenClause, AlterTableCmd and FunctionParameter get their own files.

Not in this PR: the helpers that print a construct shared by several statements and still live in a node file (emit_filter_clause, emit_set_clause_list, emit_grant_list, the JSON clauses, the function options, ...) move to nodes/clauses/ in a later PR of this stack. The JSON node files are split there too, because they share those clauses.

The printer's own needs_quoting* in tokens.rs were meant to be replaced by pgls_catalog::quote_identifier / needs_quotes from #981, which follow scan.l and gram.y. #981 isn't merged yet, so they stay for now. Swapping them is a small follow-up once it is.

Tests

  • cargo test -p pgls_pretty_print: no snapshot changes.
  • cargo test -p pgls_pretty_print --release --test option_matrix -- --ignored
  • cargo test -p pgls_workspace -p pgls_configuration format and cargo test -p pgls_cli format
  • cargo clippy --workspace --all-targets -- -D warnings

…nd a prelude

lib.rs keeps only the public API, config.rs the options and the one place
that builds the renderer config. The helpers that sat in nodes/mod.rs,
node_list.rs and string.rs move to nodes/utils, and node files import them
with use super::prelude::*. The three implementations of the leading comma
become one.
The parenthesis rules of operators, AND/OR and casts move into one
needs_parentheses in utils/predicates.rs. Files holding several nodes are
split, and files are named after the node's field in pg_query.proto.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant