From f6d3008cd16248e85f8a2e422da2162c59b4798d Mon Sep 17 00:00:00 2001 From: Ben <9087625+benfdking@users.noreply.github.com> Date: Sun, 14 Sep 2025 16:45:00 +0100 Subject: [PATCH] fix: preserve indentation after comment lines --- crates/lib/src/utils/reflow/reindent.rs | 23 +++++++--- .../rules/std_rule_cases/LT02-indent.yml | 9 ++++ docs/comment-indentation-bug/README.md | 43 +++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 docs/comment-indentation-bug/README.md diff --git a/crates/lib/src/utils/reflow/reindent.rs b/crates/lib/src/utils/reflow/reindent.rs index 8bdf2bf1d..f7affe005 100644 --- a/crates/lib/src/utils/reflow/reindent.rs +++ b/crates/lib/src/utils/reflow/reindent.rs @@ -184,13 +184,24 @@ fn revise_comment_lines(lines: &mut [IndentLine], elements: &ReflowSequenceType) } } - let changes = changes.into_iter().chain( - comment_line_buffer - .into_iter() - .map(|comment_line_idx| (comment_line_idx, 0)), - ); + let changes = changes + .into_iter() + .chain(comment_line_buffer.into_iter().map(|idx| (idx, 0))); + for (comment_line_idx, initial_indent_balance) in changes { - lines[comment_line_idx].initial_indent_balance = initial_indent_balance; + let line = &mut lines[comment_line_idx]; + line.initial_indent_balance = initial_indent_balance; + for ip in &mut line.indent_points { + ip.initial_indent_balance = initial_indent_balance; + ip.untaken_indents.clear(); + } + + if let Some(next) = lines.get_mut(comment_line_idx + 1) { + if let Some(first_ip) = next.indent_points.first_mut() { + first_ip.initial_indent_balance = initial_indent_balance; + first_ip.untaken_indents.clear(); + } + } } } diff --git a/crates/lib/test/fixtures/rules/std_rule_cases/LT02-indent.yml b/crates/lib/test/fixtures/rules/std_rule_cases/LT02-indent.yml index 882695748..dd47c2bbb 100644 --- a/crates/lib/test/fixtures/rules/std_rule_cases/LT02-indent.yml +++ b/crates/lib/test/fixtures/rules/std_rule_cases/LT02-indent.yml @@ -2174,3 +2174,12 @@ test_pass_implicit_where: configs: indentation: allow_implicit_indents: true + +test_pass_comment_at_start_of_parenthesis: + # Comment-only lines shouldn't affect following indentation. + pass_str: | + SELECT + ( + -- comment + 1 + ) AS x diff --git a/docs/comment-indentation-bug/README.md b/docs/comment-indentation-bug/README.md new file mode 100644 index 000000000..6e22a6d64 --- /dev/null +++ b/docs/comment-indentation-bug/README.md @@ -0,0 +1,43 @@ +# Comment-Induced Indentation Bug + +A comment placed as the first line inside a parenthesized expression could cause the +next line to receive an extra level of indentation. For example: + +```sql +SELECT + col1, + ( + -- BIG NOTE 1: Adding this comment breaks the indentation of the output. + file_type ILIKE '%tif%' + ) AS col2 +``` + +Running `sqruff fix` on this query previously produced: + +```sql +SELECT + col1, + ( + -- BIG NOTE 1: Adding this comment breaks the indentation of the output. + file_type ILIKE '%tif%' + ) AS col2 +``` + +The comment-only line after the opening parenthesis was treated as consuming an +additional indent. The following line then inherited this inflated indent balance, +shifting the subsequent lines too far to the right. + +## Fix + +The indentation mapper now resets comment-only lines to the preceding line's +indentation and clears any untaken indents. As a result, comment lines no longer +alter the indent level of the code that follows them: + +```sql +SELECT + col1, + ( + -- BIG NOTE 1: Adding this comment breaks the indentation of the output. + file_type ILIKE '%tif%' + ) AS col2 +```