Commit 79c86761 by chenyujia

针对备注重新格式化

parent 22ab6768
......@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'annotate/version'
Gem::Specification.new do |s|
s.name = 'annotate'
s.name = 'ta_annotate'
s.version = Annotate.version
s.required_ruby_version = '>= 2.4.0'
......
......@@ -140,6 +140,7 @@ module AnnotateModels
info << get_schema_header_text(klass, options)
max_size = max_schema_info_width(klass, options)
all_attrs, max_attr_size = get_attrs_and_width(klass, options)
md_names_overhead = 6
md_type_allowance = 18
bare_type_allowance = 16
......@@ -152,12 +153,9 @@ module AnnotateModels
cols = columns(klass, options)
cols.each do |col|
col_type = get_col_type(col)
attrs = get_attributes(col, col_type, klass, options)
col_name = if with_comments?(klass, options) && col.comment
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})"
else
col.name
end
attrs = all_attrs[col.name]
col_name = col.name
col_comment = with_comments?(klass, options) && col.comment ? col.comment.gsub(/\n/, "\\n") : ''
if options[:format_rdoc]
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
......@@ -170,7 +168,7 @@ module AnnotateModels
type_remainder = (md_type_allowance - 2) - col_type.length
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
else
info << format_default(col_name, max_size, col_type, bare_type_allowance, attrs)
info << format_default(col_name, max_size, col_type, bare_type_allowance, attrs, max_attr_size, col_comment)
end
end
......@@ -801,21 +799,36 @@ module AnnotateModels
def max_schema_info_width(klass, options)
cols = columns(klass, options)
if with_comments?(klass, options)
max_size = cols.map do |column|
column.name.size + (column.comment ? width(column.comment) : 0)
end.max || 0
max_size += 2
else
max_size = cols.map(&:name).map(&:size).max
end
max_size += options[:format_rdoc] ? 5 : 1
max_size
end
def format_default(col_name, max_size, col_type, bare_type_allowance, attrs)
sprintf("# %s:%s %s", mb_chars_ljust(col_name, max_size), mb_chars_ljust(col_type, bare_type_allowance), attrs.join(", ")).rstrip + "\n"
def get_attrs_and_width(klass, options)
attrs = {}
width = 0
cols = columns(klass, options)
cols.each do |col|
col_type = get_col_type(col)
attrs[col.name] = get_attributes(col, col_type, klass, options)
attr_size = attrs[col.name].join(', ').size
width = attr_size if attr_size > width
end
[attrs, width]
end
def format_default(col_name, max_size, col_type, bare_type_allowance, attrs, max_attr_size, col_comment)
sprintf(
"# %s:%s %s # %s",
mb_chars_ljust(col_name, max_size),
mb_chars_ljust(col_type, bare_type_allowance),
mb_chars_ljust(attrs.join(", "), max_attr_size),
col_comment
).rstrip + "\n"
end
def width(string)
......
module Annotate
def self.version
'3.2.0'
'3.2.1'
end
end
......@@ -1191,11 +1191,11 @@ describe AnnotateModels do
#
# Table name: users
#
# id(ID) :integer not null, primary key
# active(Active) :boolean not null
# name(Name) :string(50) not null
# notes(Notes) :text(55) not null
# no_comment :text(20) not null
# id :integer not null, primary key # ID
# active :boolean not null # Active
# name :string(50) not null # Name
# notes :text(55) not null # Notes
# no_comment :text(20) not null #
#
EOS
end
......@@ -1226,15 +1226,15 @@ describe AnnotateModels do
#
# Table name: users
#
# id(ID) :integer not null, primary key
# active(ACTIVE) :boolean not null
# name(NAME) :string(50) not null
# notes(NOTES) :text(55) not null
# cyrillic(Кириллица) :text(30) not null
# japanese(熊本大学 イタリア 宝島) :text(60) not null
# arabic(لغة) :text(20) not null
# no_comment :text(20) not null
# location :geometry_collect not null
# id :integer not null, primary key # ID
# active :boolean not null # ACTIVE
# name :string(50) not null # NAME
# notes :text(55) not null # NOTES
# cyrillic :text(30) not null # Кириллица
# japanese :text(60) not null # 熊本大学 イタリア 宝島
# arabic :text(20) not null # لغة
# no_comment :text(20) not null #
# location :geometry_collect not null #
#
EOS
end
......@@ -1259,9 +1259,9 @@ describe AnnotateModels do
#
# Table name: users
#
# id(ID) :integer not null, primary key
# notes(Notes.\\nMay include things like notes.):text(55) not null
# no_comment :text(20) not null
# id :integer not null, primary key # ID
# notes :text(55) not null # Notes.\\nMay include things like notes.
# no_comment :text(20) not null #
#
EOS
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment