Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4252 (Resolve multiple sensible indents) #4285

Merged
merged 4 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't have a comment in it?

Copy link
Member Author

Choose a reason for hiding this comment

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

🤦 . Good catch

)
),

SELECT *
FROM cte
;

SELECT *
FROM table3
;