Skip to content

Add IN list multiline formatting and strip redundant NOT parentheses#14

Merged
kinnell merged 1 commit into
mainfrom
in-list
Mar 30, 2026
Merged

Add IN list multiline formatting and strip redundant NOT parentheses#14
kinnell merged 1 commit into
mainfrom
in-list

Conversation

@kinnell
Copy link
Copy Markdown
Owner

@kinnell kinnell commented Mar 30, 2026

Description

This PR serves to add multiline formatting for IN value lists and strip redundant parentheses after NOT in WHERE/HAVING conditions.

IN lists with 2+ items (e.g. WHERE status IN ('active', 'pending', 'banned')) are now expanded to one item per line with proper indentation. Single-item lists and IN (SELECT ...) subqueries are left inline. This is handled by a new InList module that follows the same scanner-based text transformation pattern used by CaseExpression.format_in_text.

Redundant doubled parentheses after NOT (e.g. NOT ((a = 1 OR b = 2))) are now reduced to the minimal necessary grouping (NOT (a = 1 OR b = 2)). The unwrapping preserves parens that are semantically required to group multiple conditions.

Both features apply across SELECT, UPDATE, and DELETE queries through all three condition rendering code paths.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds new condition-level formatting features to sql_beautifier: multiline expansion for IN (...) value lists and additional parenthesis unwrapping after a leading NOT, applied across SELECT/UPDATE/DELETE condition rendering paths.

Changes:

  • Introduce SqlBeautifier::InList.format_in_text to expand IN / NOT IN lists (2+ items) into one-item-per-line formatting.
  • Extend condition unwrapping to reduce redundant parentheses following a leading NOT.
  • Add/adjust specs and documentation examples covering both features.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
lib/sql_beautifier/in_list.rb New scanner-based formatter to expand IN lists while skipping quoted text and IN (SELECT ...) subqueries.
lib/sql_beautifier/condition.rb Apply InList to leaf rendering; add NOT-prefix parenthesis unwrapping.
lib/sql_beautifier/dml_rendering.rb Apply InList when rendering a single-leaf WHERE clause for DML queries.
lib/sql_beautifier/clauses/condition_clause.rb Apply NOT-paren unwrapping + InList for single-condition WHERE/HAVING style clauses.
lib/sql_beautifier.rb Require the new in_list module.
spec/sql_beautifier/in_list.spec.rb New unit specs for InList.format_in_text.
spec/sql_beautifier/formatter.spec.rb End-to-end coverage for IN list formatting + NOT ((...)) unwrapping.
spec/sql_beautifier/condition_formatter.spec.rb Coverage for NOT paren unwrapping in multiline condition formatting.
spec/sql_beautifier/condition.spec.rb Coverage for NOT paren unwrapping at the parsed condition-expression level.
spec/sql_beautifier/clauses/where.spec.rb Update existing expectations + add more WHERE-focused cases for both features.
spec/readme.spec.rb Locks README examples for both features.
README.md Document new formatting behaviors with examples.
CHANGELOG.md Note the new IN formatting and NOT-paren fix.
Comments suppressed due to low confidence (1)

lib/sql_beautifier/condition.rb:61

  • Condition#render formats leaf expressions with InList, but group conditions can still return render_inline, which currently builds from child.expression (no InList pass). That means an IN list inside a parenthesized group can remain inline when the group meets inline_group_threshold, making IN formatting inconsistent depending on whether the group renders inline vs multiline. Consider applying InList during render_inline construction, or forcing multiline rendering when an inline group would contain an expandable IN list.
    def render(indent_width:)
      return InList.format_in_text(@expression, base_indent: indent_width) if leaf?

      inline_version = render_inline
      return inline_version if inline_version.length <= SqlBeautifier.config_for(:inline_group_threshold)


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/sql_beautifier/condition.rb
Comment thread lib/sql_beautifier/in_list.rb
@kinnell kinnell merged commit 9dd5ec3 into main Mar 30, 2026
6 checks passed
@kinnell kinnell deleted the in-list branch March 30, 2026 15:45
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.

2 participants