From 938d278e8c7ce3f5b58a59616adeb98a9c2166b1 Mon Sep 17 00:00:00 2001 From: Michael Paulus Date: Fri, 29 Aug 2025 07:46:58 -0400 Subject: [PATCH 1/3] make formatter customizable for each method --- SQL.Formatter/Core/AbstractFormatter.cs | 36 +++++++++++++------------ 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/SQL.Formatter/Core/AbstractFormatter.cs b/SQL.Formatter/Core/AbstractFormatter.cs index 2daaf5c..84a7c12 100644 --- a/SQL.Formatter/Core/AbstractFormatter.cs +++ b/SQL.Formatter/Core/AbstractFormatter.cs @@ -26,6 +26,8 @@ public AbstractFormatter(FormatConfig cfg) _index = 0; } + protected Indentation Indentation => _indentation; + public Tokenizer Tokenizer() { return new Tokenizer(DoDialectConfig()); @@ -120,29 +122,29 @@ private string GetFormattedQueryFromTokens() return formattedQuery; } - private string FormatLineComment(Token token, string query) + protected virtual string FormatLineComment(Token token, string query) { return AddNewline(query + Show(token)); } - private string FormatBlockComment(Token token, string query) + protected virtual string FormatBlockComment(Token token, string query) { return AddNewline(AddNewline(query) + IndentComment(token.Value)); } - private string IndentComment(string comment) + protected virtual string IndentComment(string comment) { return comment.Replace("\n", "\n" + _indentation.GetIndent()); } - private string FormatTopLevelReservedWordNoIndent(Token token, string query) + protected virtual string FormatTopLevelReservedWordNoIndent(Token token, string query) { _indentation.DecreaseTopLevel(); query = AddNewline(query) + EqualizeWhitespace(Show(token)); return AddNewline(query); } - private string FormatToplevelReservedWord(Token token, string query) + protected virtual string FormatToplevelReservedWord(Token token, string query) { _indentation.DecreaseTopLevel(); @@ -154,7 +156,7 @@ private string FormatToplevelReservedWord(Token token, string query) return AddNewline(query); } - private string FormatNewlineReservedWord(Token token, string query) + protected virtual string FormatNewlineReservedWord(Token token, string query) { if (Token.IsAnd(token) && Token.IsBetween(TokenLookBehind(2))) { @@ -164,7 +166,7 @@ private string FormatNewlineReservedWord(Token token, string query) return AddNewline(query) + EqualizeWhitespace(Show(token)) + " "; } - private static string EqualizeWhitespace(string str) + protected static string EqualizeWhitespace(string str) { return Regex.Replace(str, @"\s+", " "); } @@ -176,7 +178,7 @@ private static string EqualizeWhitespace(string str) TokenTypes.OPERATOR, TokenTypes.RESERVED_NEWLINE}; - private string FormatOpeningParentheses(Token token, string query) + protected virtual string FormatOpeningParentheses(Token token, string query) { if (string.IsNullOrEmpty(token.WhitespaceBefore) && (TokenLookBehind() == default || !s_preserveWhitespaceFor.Contains(TokenLookBehind().Type))) @@ -200,7 +202,7 @@ private string FormatOpeningParentheses(Token token, string query) return query; } - private string FormatClosingParentheses(Token token, string query) + protected virtual string FormatClosingParentheses(Token token, string query) { if (_inlineBlock.IsActive()) { @@ -220,33 +222,33 @@ private string FormatClosingParentheses(Token token, string query) } } - private string FormatPlaceholder(Token token, string query) + protected virtual string FormatPlaceholder(Token token, string query) { return query + _parameters.Get(token) + " "; } - private string FormatComma(Token token, string query) + protected virtual string FormatComma(Token token, string query) { query = query.TrimEnd() + Show(token) + " "; return _inlineBlock.IsActive() || Token.IsLimit(_previousReservedToken) ? query : AddNewline(query); } - private string FormatWithSpaceAfter(Token token, string query) + protected virtual string FormatWithSpaceAfter(Token token, string query) { return query.TrimEnd() + Show(token) + " "; } - private string FormatWithoutSpaces(Token token, string query) + protected virtual string FormatWithoutSpaces(Token token, string query) { return query.TrimEnd() + Show(token); } - private string FormatWithSpaces(Token token, string query) + protected virtual string FormatWithSpaces(Token token, string query) { return query + Show(token) + " "; } - private string FormatQuerySeparator(Token token, string query) + protected virtual string FormatQuerySeparator(Token token, string query) { _indentation.ResetIndentation(); return query.TrimEnd() @@ -254,7 +256,7 @@ private string FormatQuerySeparator(Token token, string query) + Utils.Repeat("\n", _cfg.LinesBetweenQueries == default ? 1 : _cfg.LinesBetweenQueries); } - private string Show(Token token) + protected virtual string Show(Token token) { if (_cfg.Uppercase && (token.Type == TokenTypes.RESERVED @@ -270,7 +272,7 @@ private string Show(Token token) return token.Value; } - private string AddNewline(string query) + protected virtual string AddNewline(string query) { query = query.TrimEnd(); if (!query.EndsWith("\n")) From ce8a8f17e8692c5180aa32e060c69c9a6b6f5933 Mon Sep 17 00:00:00 2001 From: Hoki Min Date: Sat, 30 Aug 2025 06:47:09 +0900 Subject: [PATCH 2/3] Fix whitespace formatting --- SQL.Formatter/Core/AbstractFormatter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SQL.Formatter/Core/AbstractFormatter.cs b/SQL.Formatter/Core/AbstractFormatter.cs index 84a7c12..717bf88 100644 --- a/SQL.Formatter/Core/AbstractFormatter.cs +++ b/SQL.Formatter/Core/AbstractFormatter.cs @@ -26,8 +26,8 @@ public AbstractFormatter(FormatConfig cfg) _index = 0; } - protected Indentation Indentation => _indentation; - + protected Indentation Indentation => _indentation; + public Tokenizer Tokenizer() { return new Tokenizer(DoDialectConfig()); From e36c8190757b860dc245107bf32a49cbf55e48b6 Mon Sep 17 00:00:00 2001 From: Hoki Min Date: Sat, 30 Aug 2025 06:52:21 +0900 Subject: [PATCH 3/3] Fix whitespace formatting --- SQL.Formatter/Core/AbstractFormatter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SQL.Formatter/Core/AbstractFormatter.cs b/SQL.Formatter/Core/AbstractFormatter.cs index 717bf88..dfe13a0 100644 --- a/SQL.Formatter/Core/AbstractFormatter.cs +++ b/SQL.Formatter/Core/AbstractFormatter.cs @@ -27,7 +27,7 @@ public AbstractFormatter(FormatConfig cfg) } protected Indentation Indentation => _indentation; - + public Tokenizer Tokenizer() { return new Tokenizer(DoDialectConfig());