@@ -5,11 +5,11 @@ defmodule SQL.Format do
55 @ moduledoc false
66 @ moduledoc since: "0.4.0"
77
8- @ compile { :inline , indention: 3 , newline: 1 }
8+ @ compile { :inline , indention: 3 , newline: 2 }
99
1010 @ doc false
1111 @ doc since: "0.4.0"
12- def to_iodata ( tokens , context ) , do: newline ( to_iodata ( tokens , context . binding , context . case , context . errors , 0 , [ ] ) )
12+ def to_iodata ( tokens , context ) , do: newline ( to_iodata ( tokens , context . binding , context . case , context . errors , 0 , [ ] ) , 0 )
1313
1414 defp indention ( acc , [ { :preset , { _ , 0 } } , _ , { :offset , { _ , 0 , _ , _ } } | _ ] , 0 ) , do: acc
1515 defp indention ( acc , [ _ , { :offset , { _ , 0 } } | _ ] , 0 ) , do: acc
@@ -18,8 +18,10 @@ defmodule SQL.Format do
1818 defp indention ( acc , _ , 2 ) , do: [ ?\s , ?\s , ?\s , ?\s | acc ]
1919 defp indention ( acc , _ , indent ) , do: [ :lists . duplicate ( ?\s , indent * 2 ) | acc ]
2020
21- defp newline ( [ ?\n | _ ] = acc ) , do: acc
22- defp newline ( acc ) , do: [ ?\n | acc ]
21+ defp newline ( [ ?\n , ?\s | acc ] , 0 ) , do: [ ?\n | acc ]
22+ defp newline ( [ ?\s | acc ] , 0 ) , do: [ ?\n | acc ]
23+ defp newline ( [ ?\n | _ ] = acc , _indent ) , do: acc
24+ defp newline ( acc , _indent ) , do: [ ?\n | acc ]
2325
2426 newline = ~w[ select from join where group having window order limit offset fetch] a
2527 { reserved , non_reserved , operators } = SQL.BNF . get_rules ( )
@@ -33,17 +35,17 @@ defmodule SQL.Format do
3335 end
3436 for atom <- newline do
3537 defp to_iodata ( { unquote ( atom ) , m , values } , binding , :lower = case , errors , indent , acc ) do
36- newline ( indention ( [ unquote ( "#{ atom } " ) | newline ( to_iodata ( values , binding , case , errors , indent + 1 , acc ) ) ] , m , indent ) )
38+ newline ( indention ( [ unquote ( "#{ atom } " ) | newline ( to_iodata ( values , binding , case , errors , indent + 1 , acc ) , indent + 1 ) ] , m , indent ) , indent )
3739 end
3840 defp to_iodata ( { unquote ( atom ) , m , values } , binding , :upper = case , errors , indent , acc ) do
39- newline ( indention ( [ unquote ( String . upcase ( "#{ atom } " ) ) | newline ( to_iodata ( values , binding , case , errors , indent + 1 , acc ) ) ] , m , indent ) )
41+ newline ( indention ( [ unquote ( String . upcase ( "#{ atom } " ) ) | newline ( to_iodata ( values , binding , case , errors , indent + 1 , acc ) , indent + 1 ) ] , m , indent ) , indent )
4042 end
4143 end
4244 defp to_iodata ( :comma , _binding , _case , _errors , 0 , acc ) do
4345 [ ?, | acc ]
4446 end
45- defp to_iodata ( :comma , _binding , _case , _errors , _ident , acc ) do
46- [ ?, | newline ( acc ) ]
47+ defp to_iodata ( :comma , _binding , _case , _errors , indent , acc ) do
48+ [ ?, | newline ( acc , indent ) ]
4749 end
4850 defp to_iodata ( :dot , _binding , _case , _errors , _indent , acc ) do
4951 [ ?. | acc ]
@@ -91,7 +93,7 @@ defmodule SQL.Format do
9193 to_iodata ( values , binding , case , errors , indent , indention ( to_iodata ( tag , binding , case , errors , indent , acc ) , m , 0 ) )
9294 end
9395 defp to_iodata ( { tag , m , [ left , right ] } , binding , case , errors , indent , acc ) when tag in ~w[ union except intersect] a do
94- to_iodata ( left , binding , case , errors , indent , newline ( indention ( to_iodata ( tag , binding , case , errors , indent , to_iodata ( right , binding , case , errors , indent , acc ) ) , m , indent ) ) )
96+ to_iodata ( left , binding , case , errors , indent , newline ( indention ( to_iodata ( tag , binding , case , errors , indent , to_iodata ( right , binding , case , errors , indent , acc ) ) , m , indent ) , indent ) )
9597 end
9698 defp to_iodata ( { tag , [ _ , _ , { :type , type } | _ ] = m , [ left , right ] } , binding , case , errors , indent , acc ) when type == :operator or tag in ~w[ between cursor for to] a do
9799 to_iodata ( left , binding , case , errors , indent , indention ( to_iodata ( tag , binding , case , errors , indent , to_iodata ( right , binding , case , errors , 0 , acc ) ) , m , 0 ) )
0 commit comments