From ff45edd7f69008a394b4b553ed122d237510a722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 14 Mar 2026 18:07:35 +0100 Subject: [PATCH] Reduce the amount of clauses --- lib/format.ex | 17 ++++++----------- lib/token.ex | 7 ++----- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/format.ex b/lib/format.ex index 7bebc1c..8195bd2 100644 --- a/lib/format.ex +++ b/lib/format.ex @@ -31,20 +31,15 @@ defmodule SQL.Format do defp pad([?\n|_]=acc), do: acc defp pad(acc), do: [?\n|acc] + defp color(true, string, acc), do: [@keyword, string, @reset | acc] + defp color(false, string, acc), do: [string | acc] + newline = ~w[select from join where having window limit offset fetch when else end returning set]a {reserved, non_reserved, operators} = SQL.BNF.get_rules() for atom <- Enum.uniq(Enum.map(reserved++non_reserved++operators,&elem(&1, 0))), atom not in newline do - defp to_iodata(unquote(atom), true, _binding ,:lower, _errors, _indent, acc) do - [@keyword, unquote("#{atom}"), @reset|acc] - end - defp to_iodata(unquote(atom), true, _binding, :upper, _errors, _indent, acc) do - [@keyword, unquote(String.upcase("#{atom}")), @reset|acc] - end - defp to_iodata(unquote(atom), false, _binding ,:lower, _errors, _indent, acc) do - [unquote("#{atom}")|acc] - end - defp to_iodata(unquote(atom), false, _binding, :upper, _errors, _indent, acc) do - [unquote(String.upcase("#{atom}"))|acc] + defp to_iodata(unquote(atom), color, _binding, case, _errors, _indent, acc) do + string = if(case == :upper, do: unquote(String.upcase("#{atom}")), else: unquote("#{atom}")) + color(color, string, acc) end end for atom <- newline do diff --git a/lib/token.ex b/lib/token.ex index 437efd6..56341c0 100644 --- a/lib/token.ex +++ b/lib/token.ex @@ -31,11 +31,8 @@ defmodule SQL.Token do {reserved, non_reserved, operators} = SQL.BNF.get_rules() for atom <- Enum.uniq(Enum.map(reserved++non_reserved++operators,&elem(&1, 0))) do - defp __to_iodata__(unquote(atom), _format, :lower, acc) do - [unquote("#{atom}")|acc] - end - defp __to_iodata__(unquote(atom), _format, :upper, acc) do - [unquote(String.upcase("#{atom}"))|acc] + defp __to_iodata__(unquote(atom), _format, case, acc) do + [if(case == :upper, do: unquote(String.upcase("#{atom}")), else: unquote("#{atom}"))|acc] end end defp __to_iodata__(:comma, _format, _case, acc) do