From 5abf7a4e9ce3388b69b10b0c25674605c663b3fd Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Tue, 27 Dec 2016 11:01:04 +0900 Subject: [PATCH] Follow #3778 changes, support to expand doc in macro --- spec/compiler/crystal/tools/expand_spec.cr | 32 ++++++++++++++++++++++ src/compiler/crystal/command.cr | 8 ++++-- src/compiler/crystal/command/cursor.cr | 6 ++-- src/compiler/crystal/tools/expand.cr | 2 +- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/spec/compiler/crystal/tools/expand_spec.cr b/spec/compiler/crystal/tools/expand_spec.cr index 78d16db78e26..bdab9dc0e283 100644 --- a/spec/compiler/crystal/tools/expand_spec.cr +++ b/spec/compiler/crystal/tools/expand_spec.cr @@ -7,6 +7,7 @@ private def processed_expand_visitor(code, cursor_location) compiler = Compiler.new compiler.no_codegen = true compiler.no_cleanup = true + compiler.wants_doc = true result = compiler.compile(Compiler::Source.new(".", code), "fake-no-build") visitor = ExpandVisitor.new(cursor_location) @@ -469,4 +470,35 @@ describe "expand" do assert_expand_fail code, "no expansion found: foo is not macro" end + + it "expands macro with doc" do + code = <<-CODE + macro foo(x) + # string of {{ x }} + def {{ x }}_str + {{ x.stringify }} + end + # symbol of {{ x }} + def {{ x }}_sym + :{{ x }} + end + end + + foo(hello) + ‸ + CODE + + expanded = <<-CODE + # string of hello + def hello_str + "hello" + end + # symbol of hello + def hello_sym + :hello + end + CODE + + assert_expand_result_simple code, "foo(hello)", expanded + "\n" + end end diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index fc311197903e..3665762c9da4 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -264,8 +264,7 @@ class Crystal::Command end private def create_compiler(command, no_codegen = false, run = false, - hierarchy = false, cursor_command = false, - no_cleanup = false, + hierarchy = false, expand = false, cursor_command = false, single_file = false) compiler = Compiler.new link_flags = [] of String @@ -398,7 +397,10 @@ class Crystal::Command end end - compiler.no_cleanup = no_cleanup + if expand + compiler.no_cleanup = true + compiler.wants_doc = true + end compiler.link_flags = link_flags.join(" ") unless link_flags.empty? diff --git a/src/compiler/crystal/command/cursor.cr b/src/compiler/crystal/command/cursor.cr index afec4a8402dd..19eb14fd16e6 100644 --- a/src/compiler/crystal/command/cursor.cr +++ b/src/compiler/crystal/command/cursor.cr @@ -18,13 +18,13 @@ class Crystal::Command end private def expand - cursor_command("tool expand", no_cleanup: true) do |location, config, result| + cursor_command("tool expand", expand: true) do |location, config, result| result = ExpandVisitor.new(location).process(result) end end - private def cursor_command(command, no_cleanup = false) - config, result = compile_no_codegen command, cursor_command: true, no_cleanup: no_cleanup + private def cursor_command(command, expand = false) + config, result = compile_no_codegen command, cursor_command: true, expand: expand format = config.output_format diff --git a/src/compiler/crystal/tools/expand.cr b/src/compiler/crystal/tools/expand.cr index 7cdbc4564896..683fdc585bb1 100644 --- a/src/compiler/crystal/tools/expand.cr +++ b/src/compiler/crystal/tools/expand.cr @@ -35,7 +35,7 @@ module Crystal end def self.ast_to_s(node) - s = node.to_s + s = String.build { |io| node.to_s(io, emit_doc: true) } return s unless node.is_a?(MacroIf) || node.is_a?(MacroFor) indent = node.location.not_nil!.column_number - 1 s.lines(chomp: false).map do |line|