Skip to content

Commit ad9ac91

Browse files
committed
Put Comments on New Line for Bare Formatting
Prevent unusably spaced formatting that occurs when comments are the length of normal sentences.
1 parent 9099c62 commit ad9ac91

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

lib/annotate/annotate_models.rb

+26-11
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,25 @@ def get_schema_info(klass, header, options = {})
149149
cols.each do |col|
150150
col_type = get_col_type(col)
151151
attrs = get_attributes(col, col_type, klass, options)
152-
col_name = if with_comments?(klass, options) && col.comment
153-
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})"
152+
comment = if with_comments?(klass, options) && col.comment
153+
col.comment.gsub(/\n/, "\\n")
154154
else
155-
col.name
155+
nil
156156
end
157+
commented_name = comment ? "#{col.name}(#{comment})" : col.name
157158

158159
if options[:format_rdoc]
159-
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
160+
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{commented_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
160161
elsif options[:format_yard]
161-
info << sprintf("# @!attribute #{col_name}") + "\n"
162+
info << sprintf("# @!attribute #{commented_name}") + "\n"
162163
ruby_class = col.respond_to?(:array) && col.array ? "Array<#{map_col_type_to_ruby_classes(col_type)}>": map_col_type_to_ruby_classes(col_type)
163164
info << sprintf("# @return [#{ruby_class}]") + "\n"
164165
elsif options[:format_markdown]
165-
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
166+
name_remainder = max_size - commented_name.length - non_ascii_length(commented_name)
166167
type_remainder = (md_type_allowance - 2) - col_type.length
167-
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
168+
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", commented_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
168169
else
169-
info << format_default(col_name, max_size, col_type, bare_type_allowance, attrs)
170+
info << format_default(col.name, max_size, col_type, bare_type_allowance, comment, attrs)
170171
end
171172
end
172173

@@ -765,7 +766,8 @@ def with_comments?(klass, options)
765766
def max_schema_info_width(klass, options)
766767
cols = columns(klass, options)
767768

768-
if with_comments?(klass, options)
769+
if with_comments?(klass, options) && [
770+
:format_rdoc, :format_markdown, :format_yard].any? { |f| options[f] }
769771
max_size = cols.map do |column|
770772
column.name.size + (column.comment ? width(column.comment) : 0)
771773
end.max || 0
@@ -778,8 +780,21 @@ def max_schema_info_width(klass, options)
778780
max_size
779781
end
780782

781-
def format_default(col_name, max_size, col_type, bare_type_allowance, attrs)
782-
sprintf("# %s:%s %s", mb_chars_ljust(col_name, max_size), mb_chars_ljust(col_type, bare_type_allowance), attrs.join(", ")).rstrip + "\n"
783+
def format_default(col_name, max_size, col_type, bare_type_allowance, comment, attrs)
784+
output = sprintf(
785+
"# %s:%s %s",
786+
mb_chars_ljust(col_name, max_size),
787+
mb_chars_ljust(col_type, bare_type_allowance),
788+
attrs.join(", ")
789+
).rstrip + "\n"
790+
if comment
791+
# Limit comment line length to 73 characters with padding
792+
comment.scan(/.{1,73}(?: |$)/).each_with_index do |comment_part, idx|
793+
template = idx == 0 ? '# ^ %s' : '# %s'
794+
output += sprintf(template, comment_part).rstrip + "\n"
795+
end
796+
end
797+
output
783798
end
784799

785800
def width(string)

spec/lib/annotate/annotate_models_spec.rb

+30-17
Original file line numberDiff line numberDiff line change
@@ -1078,11 +1078,15 @@ def mock_column(name, type, options = {})
10781078
#
10791079
# Table name: users
10801080
#
1081-
# id(ID) :integer not null, primary key
1082-
# active(Active) :boolean not null
1083-
# name(Name) :string(50) not null
1084-
# notes(Notes) :text(55) not null
1085-
# no_comment :text(20) not null
1081+
# id :integer not null, primary key
1082+
# ^ ID
1083+
# active :boolean not null
1084+
# ^ Active
1085+
# name :string(50) not null
1086+
# ^ Name
1087+
# notes :text(55) not null
1088+
# ^ Notes
1089+
# no_comment :text(20) not null
10861090
#
10871091
EOS
10881092
end
@@ -1113,15 +1117,22 @@ def mock_column(name, type, options = {})
11131117
#
11141118
# Table name: users
11151119
#
1116-
# id(ID) :integer not null, primary key
1117-
# active(ACTIVE) :boolean not null
1118-
# name(NAME) :string(50) not null
1119-
# notes(NOTES) :text(55) not null
1120-
# cyrillic(Кириллица) :text(30) not null
1121-
# japanese(熊本大学 イタリア 宝島) :text(60) not null
1122-
# arabic(لغة) :text(20) not null
1123-
# no_comment :text(20) not null
1124-
# location :geometry_collect not null
1120+
# id :integer not null, primary key
1121+
# ^ ID
1122+
# active :boolean not null
1123+
# ^ ACTIVE
1124+
# name :string(50) not null
1125+
# ^ NAME
1126+
# notes :text(55) not null
1127+
# ^ NOTES
1128+
# cyrillic :text(30) not null
1129+
# ^ Кириллица
1130+
# japanese :text(60) not null
1131+
# ^ 熊本大学 イタリア 宝島
1132+
# arabic :text(20) not null
1133+
# ^ لغة
1134+
# no_comment :text(20) not null
1135+
# location :geometry_collect not null
11251136
#
11261137
EOS
11271138
end
@@ -1146,9 +1157,11 @@ def mock_column(name, type, options = {})
11461157
#
11471158
# Table name: users
11481159
#
1149-
# id(ID) :integer not null, primary key
1150-
# notes(Notes.\\nMay include things like notes.):text(55) not null
1151-
# no_comment :text(20) not null
1160+
# id :integer not null, primary key
1161+
# ^ ID
1162+
# notes :text(55) not null
1163+
# ^ Notes.\\nMay include things like notes.
1164+
# no_comment :text(20) not null
11521165
#
11531166
EOS
11541167
end

0 commit comments

Comments
 (0)