Add a Multiline option for CASE expressions (WHEN/THEN/ELSE on their own lines)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 56/100
Hướng nghiên cứu
Bắt đầu bằng cách đọc SqlScriptGeneratorOptions và hành vi của Sql170ScriptGenerator đối với các tùy chọn Multiline* hiện có, sau đó so sánh cách các biểu thức CASE được phát sinh. Truy vết các bài kiểm thử generator liên quan nếu có, và xác minh rằng mặc định vẫn giữ đầu ra inline, trong khi MultilineCaseExpression đặt các nhánh WHEN và ELSE trên những dòng riêng được thụt lề.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Is your feature request related to a problem? Please describe
When Sql170ScriptGenerator regenerates a script, a CASE expression is always emitted inline — every
WHEN/THEN/ELSE is joined onto one logical line. For multi-branch CASE expressions this makes the
output substantially harder to read than the input, and where a WHEN predicate has multiple conjuncts
the hanging indent compounds: each AND is indented to the width of the preceding WHEN, and the next
WHEN is then appended after that indented continuation, producing a staircase.
Input (longest line 81 chars):
SELECT CASE
WHEN o.status = 'P' AND o.paid_amount >= o.total_amount THEN 'settled'
WHEN o.status = 'P' AND o.paid_amount > 0 THEN 'part-paid'
WHEN o.status = 'C' THEN 'cancelled'
ELSE 'open'
END AS settlement_state,
CASE WHEN o.due_date < SYSUTCDATETIME() THEN 1 ELSE 0 END AS is_overdue
FROM orders AS o
WHERE o.tenant_id = 42;
Output with MultilineSelectElementsList = true, IndentationSize = 4 (longest line 185 chars):
SELECT CASE WHEN o.status = 'P'
AND o.paid_amount >= o.total_amount THEN 'settled' WHEN o.status = 'P'
AND o.paid_amount > 0 THEN 'part-paid' WHEN o.status = 'C' THEN 'cancelled' ELSE 'open' END AS settlement_state,
CASE WHEN o.due_date < SYSUTCDATETIME() THEN 1 ELSE 0 END AS is_overdue
FROM orders AS o
WHERE o.tenant_id = 42;
An 81-character input becomes a 185-character line. There is currently no option that affects this:
of the 46 properties on SqlScriptGeneratorOptions, none matches Case or When, and none provides a
maximum line width. AlignClauseBodies, ClauseBodyAlignment and the Multiline* options do not reach
inside a CASE.
Describe the solution you'd like
A MultilineCaseExpression option (default false, preserving current output) that places each WHEN
branch and the ELSE on its own line, indented one level from the CASE:
SELECT CASE
WHEN o.status = 'P' AND o.paid_amount >= o.total_amount THEN 'settled'
WHEN o.status = 'P' AND o.paid_amount > 0 THEN 'part-paid'
WHEN o.status = 'C' THEN 'cancelled'
ELSE 'open'
END AS settlement_state,
This fits the existing naming and behaviour of the Multiline* family —
MultilineSelectElementsList, MultilineWherePredicatesList, MultilineInValuesList,
MultilineViewColumnsList, MultilineSetClauseItems, MultilineInsertTargetsList,
MultilineInsertSourcesList, MultilineProcedureParametersList — so it should need no new concepts.
A single-branch CASE staying inline when the option is off (as
CASE WHEN … THEN 1 ELSE 0 END above) seems the right default; whether a one-branch CASE should also
break under the option is a judgement call I have no strong view on.
Describe alternatives you've considered
- Existing options —
AlignClauseBodies,ClauseBodyAlignment = Indented,
MultilineSelectElementsList, and reducedIndentationSize. None affects the interior of aCASE;
the best combination I found did not shorten these lines. - A general maximum-line-width option. More broadly useful, but a much larger change and a different
model from the current per-construct toggles, so I have not requested it here. - Post-processing the generated text. Re-wrapping
CASEwith a regex outside the generator defeats
the point of using an AST-based formatter and is not reliable in the presence of string literals.
Additional context
Microsoft.SqlServer.TransactSql.ScriptDom180.78.1 (assembly 18.0.0.0),net8.0; behaviour is the
same onSql160ScriptGenerator,Sql170ScriptGeneratorandSql180ScriptGenerator.- Related open requests of the same shape, both asking for a construct to be broken across lines:
#189 (INSERT columns) and #21 (stored-procedure parameters). - Happy to test a change against a large real-world T-SQL corpus if that is useful.
- Ngôn ngữ chính
- GAP
- Star
- 277
- Fork
- 43
- Merge trung bình
- 6 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 3
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/SqlScriptDOM
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
microsoft/SqlScriptDOM#228 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
microsoft/SqlScriptDOM#183 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
microsoft/SqlScriptDOM#225 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 58/100
microsoft/SqlScriptDOM#224 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
microsoft/SqlScriptDOM#222 ·
Tất cả issue của microsoft/SqlScriptDOM
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 bình luận ·
-
good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
rust-bitcoin/rust-bitcoin#6930 · 1 bình luận ·
-
Solved site promotion gate fails on runner PHP patch drift (expects 8.2.33, runner installs 8.2.34) Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Automattic/blocks-engine#2161 ·