From 995d3f9f68bf50e49326e1738663d2f09d32089f Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Mon, 29 Jan 2018 22:51:07 +0900 Subject: [PATCH] Fix indentation after comment inside 'when' Fix #5654 --- spec/compiler/formatter/formatter_spec.cr | 2 ++ src/compiler/crystal/tools/formatter.cr | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 7d78a4190133..185f8ec83e4e 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -471,6 +471,8 @@ describe Crystal::Formatter do assert_format "case 1 \n when .foo? \n 3 \n end", "case 1\nwhen .foo?\n 3\nend" assert_format "case 1\nwhen 1 then\n2\nwhen 3\n4\nend", "case 1\nwhen 1\n 2\nwhen 3\n 4\nend" assert_format "case 1 \n when 2 \n 3 \n else 4 \n end", "case 1\nwhen 2\n 3\nelse 4\nend" + assert_format "case 1\nwhen 1, # 1\n 2, # 2\n 3 # 3\n 1\nend" + assert_format "a = case 1\n when 1, # 1\n 2, # 2\n 3 # 3\n 1\n end" assert_format "a = 1\ncase\nwhen 2\nelse\n a /= 3\nend" assert_format "select \n when foo \n 2 \n end", "select\nwhen foo\n 2\nend" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index 04efe21cee3b..38e2e7b2bcd4 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -3346,9 +3346,9 @@ module Crystal write "," slash_is_regex! next_token - skip_space - if @token.type == :NEWLINE - write_line + found_comment = skip_space + if found_comment || @token.type == :NEWLINE + write_line unless found_comment skip_space_or_newline next_needs_indent = true else