Skip to content

Commit 9e0de05

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 9e0de05

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

lib/annotate/annotate_models.rb

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ def get_schema_info(klass, header, options = {})
138138
max_size = max_schema_info_width(klass, options)
139139
md_names_overhead = 6
140140
md_type_allowance = 18
141-
bare_type_allowance = 16
142141

143142
if options[:format_markdown]
144143
info << sprintf( "# %-#{max_size + md_names_overhead}.#{max_size + md_names_overhead}s | %-#{md_type_allowance}.#{md_type_allowance}s | %s\n", 'Name', 'Type', 'Attributes' )
@@ -149,24 +148,23 @@ def get_schema_info(klass, header, options = {})
149148
cols.each do |col|
150149
col_type = get_col_type(col)
151150
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")})"
154-
else
155-
col.name
156-
end
151+
comment = if with_comments?(klass, options) && col.comment
152+
col.comment.gsub(/\n/, "\\n")
153+
end
154+
commented_name = comment ? "#{col.name}(#{comment})" : col.name
157155

158156
if options[:format_rdoc]
159-
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
157+
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{commented_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
160158
elsif options[:format_yard]
161-
info << sprintf("# @!attribute #{col_name}") + "\n"
159+
info << sprintf("# @!attribute #{commented_name}") + "\n"
162160
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)
163161
info << sprintf("# @return [#{ruby_class}]") + "\n"
164162
elsif options[:format_markdown]
165-
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
163+
name_remainder = max_size - commented_name.length - non_ascii_length(commented_name)
166164
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"
165+
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", commented_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
168166
else
169-
info << format_default(col_name, max_size, col_type, bare_type_allowance, attrs)
167+
info << format_default(col.name, max_size, col_type, comment, attrs)
170168
end
171169
end
172170

@@ -765,7 +763,9 @@ def with_comments?(klass, options)
765763
def max_schema_info_width(klass, options)
766764
cols = columns(klass, options)
767765

768-
if with_comments?(klass, options)
766+
if with_comments?(klass, options) && [
767+
:format_rdoc, :format_markdown, :format_yard
768+
].any? { |f| options[f] }
769769
max_size = cols.map do |column|
770770
column.name.size + (column.comment ? width(column.comment) : 0)
771771
end.max || 0
@@ -778,8 +778,21 @@ def max_schema_info_width(klass, options)
778778
max_size
779779
end
780780

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

785798
def width(string)

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 30 additions & 17 deletions
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)