Skip to content

Commit

Permalink
Merge pull request #81 from Shopify/at-multiline-comments
Browse files Browse the repository at this point in the history
Properly print comments with new lines
  • Loading branch information
Morriar committed Sep 29, 2021
2 parents b567fb6 + 7b883a6 commit 5de3f47
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 8 deletions.
35 changes: 27 additions & 8 deletions lib/rbi/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,18 @@ class Comment

sig { override.params(v: Printer).void }
def accept_printer(v)
text = self.text.strip
v.printt("#")
v.print(" #{text}") unless text.empty?
v.printn
lines = text.lines

if lines.empty?
v.printl("#")
end

lines.each do |line|
text = line.strip
v.printt("#")
v.print(" #{text}") unless text.empty?
v.printn
end
end
end

Expand Down Expand Up @@ -366,13 +374,14 @@ def accept_printer(v)
v.printt
v.visit(param)
v.print(",") if pindex < params.size - 1
param.comments.each_with_index do |comment, cindex|

param.comments_lines.each_with_index do |comment, cindex|
if cindex > 0
param.print_comment_leading_space(v, last: pindex == params.size - 1)
else
v.print(" ")
end
v.print("# #{comment.text.strip}")
v.print("# #{comment}")
end
v.printn
end
Expand Down Expand Up @@ -410,6 +419,11 @@ def print_comment_leading_space(v, last:)
v.print(" " * (name.size + 1))
v.print(" ") unless last
end

sig { returns(T::Array[String]) }
def comments_lines
comments.flat_map { |comment| comment.text.lines.map(&:strip) }
end
end

class OptParam
Expand Down Expand Up @@ -576,13 +590,13 @@ def accept_printer(v)
v.printt
v.visit(param)
v.print(",") if pindex < params.size - 1
param.comments.each_with_index do |comment, cindex|
param.comments_lines.each_with_index do |comment, cindex|
if cindex == 0
v.print(" ")
else
param.print_comment_leading_space(v, last: pindex == params.size - 1)
end
v.print("# #{comment.text.strip}")
v.print("# #{comment}")
end
v.printn
end
Expand Down Expand Up @@ -633,6 +647,11 @@ def print_comment_leading_space(v, last:)
v.print(" " * (name.size + type.size + 3))
v.print(" ") unless last
end

sig { returns(T::Array[String]) }
def comments_lines
comments.flat_map { |comment| comment.text.lines.map(&:strip) }
end
end

class TStructField
Expand Down
93 changes: 93 additions & 0 deletions test/rbi/printer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,99 @@ class Foo < ::T::Enum
RBI
end

def test_print_nodes_with_multiline_comments
comments = [RBI::Comment.new("This is a\nmultiline comment")]

rbi = RBI::Tree.new do |tree|
tree << RBI::Module.new("Foo", comments: comments) do |mod|
mod << RBI::TypeMember.new("A", "type_member", comments: comments)
mod << RBI::Method.new("foo", comments: comments)
end
end

rbi << RBI::Method.new("foo", comments: comments) do |method|
method << RBI::ReqParam.new("a", comments: comments)
method << RBI::OptParam.new("b", "42", comments: comments)
method << RBI::RestParam.new("c", comments: comments)
method << RBI::KwParam.new("d", comments: comments)
method << RBI::KwOptParam.new("e", "'bar'", comments: comments)
method << RBI::KwRestParam.new("f", comments: comments)
method << RBI::BlockParam.new("g", comments: comments)

sig = RBI::Sig.new
sig << RBI::SigParam.new("a", "Integer", comments: comments)
sig << RBI::SigParam.new("b", "String", comments: comments)
sig << RBI::SigParam.new("c", "T.untyped", comments: comments)
method.sigs << sig
end

assert_equal(<<~RBI, rbi.string)
# This is a
# multiline comment
module Foo
# This is a
# multiline comment
A = type_member
# This is a
# multiline comment
def foo; end
end
# This is a
# multiline comment
sig do
params(
a: Integer, # This is a
# multiline comment
b: String, # This is a
# multiline comment
c: T.untyped # This is a
# multiline comment
).void
end
def foo(
a, # This is a
# multiline comment
b = 42, # This is a
# multiline comment
*c, # This is a
# multiline comment
d:, # This is a
# multiline comment
e: 'bar', # This is a
# multiline comment
**f, # This is a
# multiline comment
&g # This is a
# multiline comment
); end
RBI
end

def test_print_nodes_with_heredoc_comments
comments = [RBI::Comment.new(<<~COMMENT)]
This
is
a
multiline
comment
COMMENT

rbi = RBI::Tree.new do |tree|
tree << RBI::Module.new("Foo", comments: comments)
end

assert_equal(<<~RBI, rbi.string)
# This
# is
# a
# multiline
# comment
module Foo; end
RBI
end

def test_print_methods_with_signatures_and_comments
comments_single = [RBI::Comment.new("This is a single line comment")]

Expand Down

0 comments on commit 5de3f47

Please sign in to comment.