Commit a5970404 authored by 段廷瑞's avatar 段廷瑞

pref: sql格式化缩进优化

parent a73cf70d
......@@ -849,6 +849,7 @@ namespace Tools.UI
TokenType prevType = TokenType.Whitespace;
string prevValue = "";
ClauseCtx ctx = ClauseCtx.None;
var caseIndentStack = new System.Collections.Generic.Stack<int>(); // CASE 缩进栈,用于 END 对齐
// 局部函数:计算内容缩进 = clauseBase + IndentSize * (局部嵌套层数 + 1)
int ContentIndent() => clauseBase + IndentSize * (parenDepth - clauseParenBase + 1);
......@@ -942,11 +943,42 @@ namespace Tools.UI
continue;
}
// ---- CASE: 块起始关键字,与同级字段缩进一致(非顶级关键字) ----
if (kw == "CASE")
{
// 嵌套CASE(已在 CASE 内部):与 THEN 同行,缩进比外层 CASE 深一级
if (ctx == ClauseCtx.CaseWhen)
{
int nestedIndent = (caseIndentStack.Count > 0 ? caseIndentStack.Peek() : ContentIndent()) + IndentSize;
if (NeedsLeadingSpace(prevType))
sb.Append(' ');
sb.Append(kw);
sb.Append(' ');
newline = false;
caseIndentStack.Push(nestedIndent);
}
else
{
if (!newline) { sb.AppendLine(); newline = true; }
int caseIndent = ContentIndent();
WriteIndent(sb, ref newline, caseIndent);
sb.Append(kw);
sb.AppendLine();
newline = true;
caseIndentStack.Push(caseIndent);
ctx = ClauseCtx.CaseWhen;
}
prevType = TokenType.Keyword;
prevValue = kw;
continue;
}
// ---- WHEN / ELSE / THEN / END: CASE 子句 ----
if (kw == "WHEN" || kw == "ELSE")
{
if (!newline) { sb.AppendLine(); newline = true; }
WriteIndent(sb, ref newline, ContentIndent());
int caseBase = caseIndentStack.Count > 0 ? caseIndentStack.Peek() : ContentIndent();
WriteIndent(sb, ref newline, caseBase + IndentSize);
sb.Append(kw);
sb.Append(' ');
newline = false;
......@@ -967,7 +999,9 @@ namespace Tools.UI
if (kw == "END")
{
if (!newline) { sb.AppendLine(); newline = true; }
WriteIndent(sb, ref newline, clauseBase);
// END 与对应的 CASE 保持对齐
int caseIndent = caseIndentStack.Count > 0 ? caseIndentStack.Pop() : clauseBase;
WriteIndent(sb, ref newline, caseIndent);
sb.Append(kw);
sb.Append(' ');
newline = false;
......@@ -1116,8 +1150,9 @@ namespace Tools.UI
continue;
}
// ---- 非子句关键字(AS / IN / NOT / NULL / BY 等): 内联 ----
if (NeedsLeadingSpace(prevType))
// ---- 非子句关键字(AS / IN / NOT / NULL / BY / CAST / COALESCE 等): 内联 ----
if (newline) { WriteIndent(sb, ref newline, ContentIndent()); }
else if (NeedsLeadingSpace(prevType))
sb.Append(' ');
sb.Append(kw);
// 字符串前缀(N等)与紧随的字符串字面量之间不能有空格
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment