alex-hhh / emacs-sql-indent

Syntax based indentation for SQL files inside GNU Emacs
GNU General Public License v3.0
121 stars 18 forks source link

Indentation breaks on case statements with multiple when clauses #39

Closed 121onto closed 7 years ago

121onto commented 7 years ago

Example

Expected behavior:

select pk_id
, case
  when t1.c1 is null then  '(null)'
  when t1.c1 in ('a', 'b','c','d','e') then 'a'
  else t1.c1
  end c1_alt
from t1
limit 10
;

Actual behavior:

select pk_id
, case
  when t1.c1 is null then  '(null)'
  when t1.c1 in ('a', 'b','c','d','e') then 'a'
else t1.c1
end c1_alt
from t1
limit 10
;

Ideal behavior:

select pk_id
, case
    when t1.c1 is null then  '(null)'
    when t1.c1 in ('a', 'b','c','d','e') then 'a'
    else t1.c1
  end c1_alt
from t1
limit 10
;
alex-hhh commented 7 years ago

Thanks for reporting this, it seems the syntactic analysis code is confused about the list in the second when clause. I will push a fix in a few days, but as a workaround, you can use brackets around the CASE statement, like so:

select pk_id
, (case
  when t1.c1 is null then  '(null)'
  when t1.c1 in ('a', 'b','c','d','e') then 'a'
  else t1.c1
  end) c1_alt
from t1
limit 10
;

You might also want to setup your own indentation rules since by default the code indents case statements at the same level as the "case" keyword itself. Have a look at sql-indent.org file for more details and the sql-indent-left.el file for an example.

BTW, the "limit" keyword is also not recognized, I will fix that as well.

alex-hhh commented 7 years ago

I just pushed a fix for this issue. Can you please verify that it is indeed fixed? Thanks, Alex.

121onto commented 7 years ago

Thanks, Alex! I didn't update, just started using parens...