Skip to content

Commit

Permalink
Improve comment generation in templates
Browse files Browse the repository at this point in the history
The existing comment generation was hard to read and was making a lot of string manipulation. However, ERB files are already designed to do string manipulation, so we can use that instead.

So, instead of doing a split and a map, I opted to use the `#each_line` method to iterate over the lines of the file.

Also, in order to add an optional space padding at the beginning of the line, I opted to pad it with a space and to then right trim it. This makes sure that no space is left behind if the line is empty, but a space is added if the line is not empty.
  • Loading branch information
paracycle committed Oct 29, 2023
1 parent d85e576 commit 9a07fd7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion templates/java/org/prism/Nodes.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ public abstract class Nodes {
<%# NODES -%>
<%- nodes.each do |node| -%>

<%= "#{node.comment.split("\n").map { |line| "// #{line}" }.join("\n ")}\n" if node.comment -%>
<%- node.comment.each_line do |line| -%>
//<%= line.rjust(line.size + 1).rstrip %>
<%- end -%>
public static final class <%= node.name -%> extends Node {
<%- if node.needs_serialized_length? -%>
public final int serializedLength;
Expand Down
4 changes: 3 additions & 1 deletion templates/javascript/src/nodes.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const <%= flag.name %> = {
<%- nodes.each do |node| -%>

/**
<%= "#{node.comment.split("\n").map { |line| line.empty? ? " *" : " * #{line}" }.join("\n")}" %>
<%- node.comment.each_line do |line| -%>
*<%= line.rjust(line.size + 1).rstrip %>
<%- end -%>
*/
export class <%= node.name -%> {
<%- node.fields.each do |field| -%>
Expand Down
5 changes: 4 additions & 1 deletion templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ module Prism
end
<%- nodes.each do |node| -%>
<%= "#{node.comment.split("\n").map { |line| line.empty? ? "#" : "# #{line}" }.join("\n ")}\n " if node.comment %>class <%= node.name -%> < Node
<%- node.comment.each_line do |line| -%>
#<%= line.rjust(line.size + 1).rstrip %>
<%- end -%>
class <%= node.name -%> < Node
<%- node.fields.each do |field| -%>
# attr_reader <%= field.name %>: <%= field.rbs_class %>
<%= "private " if field.is_a?(Prism::FlagsField) %>attr_reader :<%= field.name %>
Expand Down
5 changes: 4 additions & 1 deletion templates/rbi/prism.rbi.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

module Prism
<%- nodes.each do |node| -%>
<%= "#{node.comment.split("\n").map { |line| line.empty? ? "#" : "# #{line}" }.join("\n ")}\n " if node.comment %>class <%= node.name -%> < Node
<%- node.comment.each_line do |line| -%>
#<%= line.rjust(line.size + 1).rstrip %>
<%- end -%>
class <%= node.name -%> < Node
<%- node.fields.each do |field| -%>
sig { returns(<%= field.rbi_class %>) }
attr_reader :<%= field.name %>
Expand Down
5 changes: 4 additions & 1 deletion templates/sig/prism.rbs.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Prism
<%- nodes.each do |node| -%>
<%= "#{node.comment.split("\n").map { |line| line.empty? ? "#" : "# #{line}" }.join("\n ")}\n " if node.comment %>class <%= node.name -%> < Node
<%- node.comment.each_line do |line| -%>
#<%= line.rjust(line.size + 1).rstrip %>
<%- end -%>
class <%= node.name -%> < Node
<%- node.fields.each do |field| -%>
attr_reader <%= field.name %>: <%= field.rbs_class %>
<%- end -%>
Expand Down

0 comments on commit 9a07fd7

Please sign in to comment.