@@ -138,7 +138,6 @@ def get_schema_info(klass, header, options = {})
138
138
max_size = max_schema_info_width ( klass , options )
139
139
md_names_overhead = 6
140
140
md_type_allowance = 18
141
- bare_type_allowance = 16
142
141
143
142
if options [ :format_markdown ]
144
143
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 = {})
149
148
cols . each do |col |
150
149
col_type = get_col_type ( col )
151
150
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
157
155
158
156
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 "
160
158
elsif options [ :format_yard ]
161
- info << sprintf ( "# @!attribute #{ col_name } " ) + "\n "
159
+ info << sprintf ( "# @!attribute #{ commented_name } " ) + "\n "
162
160
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 )
163
161
info << sprintf ( "# @return [#{ ruby_class } ]" ) + "\n "
164
162
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 )
166
164
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 "
168
166
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 )
170
168
end
171
169
end
172
170
@@ -765,7 +763,9 @@ def with_comments?(klass, options)
765
763
def max_schema_info_width ( klass , options )
766
764
cols = columns ( klass , options )
767
765
768
- if with_comments? ( klass , options )
766
+ if with_comments? ( klass , options ) && [
767
+ :format_rdoc , :format_markdown , :format_yard
768
+ ] . any? { |f | options [ f ] }
769
769
max_size = cols . map do |column |
770
770
column . name . size + ( column . comment ? width ( column . comment ) : 0 )
771
771
end . max || 0
@@ -778,8 +778,21 @@ def max_schema_info_width(klass, options)
778
778
max_size
779
779
end
780
780
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
783
796
end
784
797
785
798
def width ( string )
0 commit comments