Skip to content

Commit

Permalink
Fix #4252 (Resolve multiple sensible indents) (#4285)
Browse files Browse the repository at this point in the history
* Fix #4252

* Correct the test case
  • Loading branch information
alanmcruickshank committed Jan 15, 2023
1 parent f462e99 commit cf2d35c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/sqlfluff/utils/reflow/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ class IndentStats:
# Defaults to an empty tuple if unset.
implicit_indents: Tuple[int, ...] = ()

@classmethod
def from_combination(cls, first: Optional["IndentStats"], second: "IndentStats"):
"""Create IndentStats from two consecutive IndentStats.
This is mostly used for combining the effects of indent and dedent
tokens either side of a comment.
NOTE: The *first* is considered optional, because if we're
calling this function, we're assuming that there's always
a second.
"""
# First check for the trivial case that we only have one.
if not first:
return second

# Otherwise, combine the two into one.
return cls(
first.impulse + second.impulse,
min(first.trough, first.impulse + second.trough),
second.implicit_indents,
)


@dataclass(frozen=True)
class ReflowPoint(ReflowElement):
Expand Down
4 changes: 2 additions & 2 deletions src/sqlfluff/utils/reflow/reindent.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ def _crawl_indent_points(
cached_indent_stats: Optional[IndentStats] = None
for idx, elem in enumerate(elements):
if isinstance(elem, ReflowPoint):
indent_stats = cached_indent_stats or elem.get_indent_impulse(
allow_implicit_indents
indent_stats = IndentStats.from_combination(
cached_indent_stats, elem.get_indent_impulse(allow_implicit_indents)
)
cached_indent_stats = None

Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/rules/std_rule_cases/L003.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1570,3 +1570,26 @@ test_fail_coverage_indent_trough:
FROM foo
)
SELECT a FROM bar
test_pass_combined_comment_impulses:
# This tests issue #4252
# https://github.com/sqlfluff/sqlfluff/issues/4252
pass_str: |
WITH cte AS (
SELECT *
FROM (
SELECT *
FROM table
WHERE
NOT bool_column AND NOT bool_column
AND some_column >= 1 -- This is a comment
)
),
SELECT *
FROM cte
;
SELECT *
FROM table3
;

0 comments on commit cf2d35c

Please sign in to comment.