Skip to content

Commit

Permalink
Follow #3778 changes, support to expand doc in macro
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Dec 27, 2016
1 parent 5485ed3 commit 5abf7a4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
32 changes: 32 additions & 0 deletions spec/compiler/crystal/tools/expand_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
8 changes: 5 additions & 3 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/command/cursor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/expand.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 5abf7a4

Please sign in to comment.