From adbf8454f1f9913ab9b8c534ff57848be47c7980 Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Mon, 20 Aug 2018 02:49:06 +0900 Subject: [PATCH 1/2] Format: fix formatting StringLiteral in StringInterpolation Fixed #6565 --- spec/compiler/formatter/formatter_spec.cr | 2 ++ src/compiler/crystal/tools/formatter.cr | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 0714db2e63db..731d67056f0f 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -385,6 +385,8 @@ describe Crystal::Formatter do assert_format %("\#{\n foo = 1\n}") assert_format %("\#{\n foo = 1}"), %("\#{\n foo = 1\n}") assert_format %("\#{ # foo\n foo = 1\n}") + assert_format %("\#{"foo"}") + assert_format %("\#{"\#{foo}"}") assert_format "%w(one two three)", "%w(one two three)" assert_format "%i(one two three)", "%i(one two three)" diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index 251c5acd1478..27c98dd73728 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -468,8 +468,8 @@ module Crystal # This is the case of #{__DIR__} write "\#{" next_token_skip_space_or_newline - write @token.type - next_token_skip_space_or_newline + indent(@column, node) + skip_space_or_newline check :"}" write "}" next_string_token @@ -564,10 +564,10 @@ module Crystal if exp.is_a?(StringLiteral) # It might be #{__DIR__}, for example if @token.type == :INTERPOLATION_START - next_token_skip_space_or_newline write "\#{" - write @token.type next_token_skip_space_or_newline + indent(@column, exp) + skip_space_or_newline check :"}" write "}" else From 2aa60148e029c4458fba97a94e8ca05c0d0ba22b Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Mon, 20 Aug 2018 04:53:17 +0900 Subject: [PATCH 2/2] Add more complex specs --- spec/compiler/formatter/formatter_spec.cr | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 731d67056f0f..6bcc2b585fa7 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -387,6 +387,9 @@ describe Crystal::Formatter do assert_format %("\#{ # foo\n foo = 1\n}") assert_format %("\#{"foo"}") assert_format %("\#{"\#{foo}"}") + assert_format %("foo\#{"bar"} Baz \#{"qux"} ") + assert_format %("1\#{"4\#{"\#{"2"}"}3"}3\#{__DIR__}4\#{5}6") + assert_format %("1\#{"\#{"2"}"}3\#{"4"}5") assert_format "%w(one two three)", "%w(one two three)" assert_format "%i(one two three)", "%i(one two three)"