From 9af5350cc2302423985812d835eac73b911a4ab7 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Sun, 24 Jan 2016 17:03:29 -0300 Subject: [PATCH] Fixed #2054: can't format heredoc in some cases --- spec/compiler/formatter/formatter_spec.cr | 4 ++++ src/compiler/crystal/tools/formatter.cr | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index aed942cfd07b..b5e87e06f8eb 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -777,4 +777,8 @@ describe Crystal::Formatter do assert_format "case 1\nwhen 2 # a\n # b\nend" assert_format "{} of A => B\n{} of Foo => Bar" + + assert_format "<<-HTML\n \#{1}x\n HTML" + assert_format "<<-HTML\n \#{1}x\n y\n HTML" + assert_format "<<-HTML\n \#{1}x\n y\n z\n HTML" end diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr index f148f029c6c0..92874aedabf1 100644 --- a/src/compiler/crystal/tools/formatter.cr +++ b/src/compiler/crystal/tools/formatter.cr @@ -399,6 +399,8 @@ module Crystal end next_string_token else + skip_strings + check :INTERPOLATION_START write "\#{" delimiter_state = @token.delimiter_state @@ -412,6 +414,8 @@ module Crystal end end + skip_strings + check :DELIMITER_END write @token.raw format_regex_modifiers if is_regex @@ -420,6 +424,15 @@ module Crystal false end + private def skip_strings + # Heredocs might indice some spaces that are removed + # because of indentation + while @token.type == :STRING + write @token.raw + next_string_token + end + end + def visit(node : RegexLiteral) accept node.value