@@ -149,24 +149,25 @@ def get_schema_info(klass, header, options = {})
149
149
cols . each do |col |
150
150
col_type = get_col_type ( col )
151
151
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" )
154
154
else
155
- col . name
155
+ nil
156
156
end
157
+ commented_name = comment ? "#{ col . name } (#{ comment } )" : col . name
157
158
158
159
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 "
160
161
elsif options [ :format_yard ]
161
- info << sprintf ( "# @!attribute #{ col_name } " ) + "\n "
162
+ info << sprintf ( "# @!attribute #{ commented_name } " ) + "\n "
162
163
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
164
info << sprintf ( "# @return [#{ ruby_class } ]" ) + "\n "
164
165
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 )
166
167
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 "
168
169
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 )
170
171
end
171
172
end
172
173
@@ -765,7 +766,8 @@ def with_comments?(klass, options)
765
766
def max_schema_info_width ( klass , options )
766
767
cols = columns ( klass , options )
767
768
768
- if with_comments? ( klass , options )
769
+ if with_comments? ( klass , options ) && [
770
+ :format_rdoc , :format_markdown , :format_yard ] . any? { |f | options [ f ] }
769
771
max_size = cols . map do |column |
770
772
column . name . size + ( column . comment ? width ( column . comment ) : 0 )
771
773
end . max || 0
@@ -778,8 +780,21 @@ def max_schema_info_width(klass, options)
778
780
max_size
779
781
end
780
782
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
783
798
end
784
799
785
800
def width ( string )
0 commit comments