Skip to content

Commit f1fc094

Browse files
committed
More improvements
1 parent 66c02d4 commit f1fc094

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

docs/source/configuration/layout.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,27 @@ up visually in the editor, regardless of the rendered length of
192192
Advanced: coordinate space override
193193
-----------------------------------
194194

195-
You can optionally force the coordinate space via the alignment constraint by
196-
adding a final modifier to the align value (available for spacing_before and
197-
spacing_after):
195+
You can optionally force the coordinate space either:
196+
197+
1) via the alignment constraint suffix (available for `spacing_before` and
198+
`spacing_after`), or
199+
2) via the `alignment_coordinate_space` key in layout config for the target type.
198200

199201
.. code-block:: ini
200202
201203
[sqlfluff:layout:type:alias_expression]
202204
spacing_before = align:alias_expression:select_clause:bracketed:source
203205
206+
Alternatively, the equivalent can be configured more declaratively:
207+
208+
.. code-block:: ini
209+
210+
[sqlfluff:layout:type:alias_expression]
211+
spacing_before = align
212+
align_within = select_clause
213+
align_scope = bracketed
214+
alignment_coordinate_space = source
215+
204216
Supported values are ``source`` and ``templated``. In most cases, ``source`` is
205217
the recommended choice for readability.
206218

src/sqlfluff/utils/reflow/respace.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,12 @@ def _determine_aligned_inline_spacing(
404404
if seg.is_code:
405405
last_code = seg
406406

407-
# Compute desired whitespace size in the chosen coordinate space
407+
# Compute desired whitespace size in the chosen coordinate space.
408+
# Ensure we always return at least a single space to avoid deleting
409+
# whitespace when the current position already exceeds the target.
408410
current_ws_pos = _pos_col(whitespace_seg.pos_marker, use_source_positions)
409-
desired_space = " " * (1 + max_desired_line_pos - current_ws_pos)
411+
pad_width = max(1, 1 + max_desired_line_pos - current_ws_pos)
412+
desired_space = " " * pad_width
410413
reflow_logger.debug(
411414
" desired_space: %r (based on max line pos of %s)",
412415
desired_space,

test/rules/std_LT01_jinja_alignment_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,37 @@ def test_lt01_alias_alignment_with_jinja_uses_source_positions() -> None:
6464
first_as_col = select_lines[0].index(" as ")
6565
second_as_col = select_lines[1].index(" as ")
6666
assert first_as_col == second_as_col
67+
68+
69+
def test_lt01_alias_alignment_with_jinja_coordinate_space_config_key() -> None:
70+
"""Coordinate space can be set via alignment_coordinate_space config key."""
71+
sql = "select\n" " {{ 'templ' }} as a,\n" " b as bb\n"
72+
73+
cfg = FluffConfig.from_kwargs(dialect="ansi")
74+
cfg._configs["core"]["templater"] = "jinja"
75+
cfg._configs.setdefault("templater", {}).setdefault("jinja", {}).setdefault(
76+
"context", {}
77+
)
78+
# Force templated coordinate space via config key enrichment path
79+
cfg._configs.setdefault("layout", {}).setdefault("type", {}).setdefault(
80+
"alias_expression", {}
81+
).update(
82+
{
83+
"spacing_before": "align",
84+
"align_within": "select_clause",
85+
"align_scope": "bracketed",
86+
"alignment_coordinate_space": "templated",
87+
}
88+
)
89+
90+
fixed = sqlfluff.fix(sql, config=cfg, rules=["LT01"])
91+
# With templated coordinate space, the second line pads less
92+
lines = fixed.splitlines()
93+
assert any(" as " in line for line in lines)
94+
select_lines = [line for line in lines if " as " in line]
95+
assert len(select_lines) == 2
96+
# Check that there is at least some padding before 'as' on second line
97+
# but not necessarily aligned to the source position of the templated value.
98+
first_as_col = select_lines[0].index(" as ")
99+
second_as_col = select_lines[1].index(" as ")
100+
assert second_as_col <= first_as_col

0 commit comments

Comments
 (0)