Preserve sig parameter comments#612
Conversation
1c12951 to
b24a431
Compare
| params( | ||
| a: Integer, # `a` comment | ||
| b: String # `b` comment 1 | ||
| # `b` comment 2 |
There was a problem hiding this comment.
I opted to keep the existing RBI printer behaviour here so the comments are printed as trailing but we could also make it multiline like the RBS printer.
29b52dd to
15ffc31
Compare
15ffc31 to
11d51eb
Compare
11d51eb to
2e5d08d
Compare
Moves `node_comments` and comment parsing onto the shared parser visitor so `TreeBuilder` and `SigBuilder` consume the same comment map. This lets sig parameter parsing attach comments without leaving them behind as dangling node comments.
2e5d08d to
43ea118
Compare
|
|
||
| #: (String content, comments_by_line: Hash[Integer, Prism::Comment], file: String) -> void | ||
| def initialize(content, comments_by_line:, file:) | ||
| super(content, comments_by_line: comments_by_line, file: file) |
There was a problem hiding this comment.
Shouldn't this work?
| super(content, comments_by_line: comments_by_line, file: file) | |
| super |
|
|
||
| start_line = node.location.start_line | ||
| start_line -= 1 unless @comments_by_line.key?(start_line) | ||
| start_line = [start_line, min_line].max if min_line |
There was a problem hiding this comment.
I'm not sure I follow the new logic here.
Isn't the idea to lookup from the param's node line start down to where the params start to avoid going back to 1?
So shouldn't we do something like start_line.downto(min_line) do |line|?
There was a problem hiding this comment.
Logic is complex due to edge cases, I refactored a bit, added more tests and comments to explain it better. Lmk what you think.
d45e93f to
26e7d3b
Compare
|
|
||
| start_line = node.location.start_line | ||
| start_line -= 1 unless @comments_by_line.key?(start_line) | ||
| start_line = [start_line, min_line].max if min_line |
There was a problem hiding this comment.
Logic is complex due to edge cases, I refactored a bit, added more tests and comments to explain it better. Lmk what you think.
| def initialize(source, file:, comments: nil, comments_by_line: {}) | ||
| if comments | ||
| comments_by_line = comments.to_h { |comment| [comment.location.start_line, comment] } | ||
| end |
There was a problem hiding this comment.
bundle exec spoom srb sigs export instantiates TreeBuilder with comments so for now we need to keep compatibility.
| end | ||
|
|
||
| #: (Prism::Node node, ?min_line: Integer?) -> Array[Comment] | ||
| def node_comments(node, min_line: nil) |
There was a problem hiding this comment.
This is mostly a move from TreeBuilder to Visitor so SigBuilder can use it too. It also gained a min_line parameter so callers can bound how far upward node comment lookup will scan.
Attaches comments inside `params(...)` to the matching `RBI::SigParam` and makes `RBSPrinter` render printable param comments in multiline signatures. This keeps parameter documentation attached to the emitted RBS parameter line instead of floating above the method.
Extends the multiline RBS path to handle comments on block sig params, including overloads and generic block-only signatures. Comments are only printed when the block clause is emitted, so skipped `NilClass` block params do not leave detached comments or force multiline output.
Updates `rbi/rbi.rbi` after the parser and RBS printer changes.
26e7d3b to
3b8d195
Compare
To be used by Shopify/spoom#901