Commit 95b1b204 by chenyujia

Merge branch 'feature/reformat_comments' into 'develop'

针对备注重新格式化 See merge request !1
parents 22ab6768 e154aacf
...@@ -111,9 +111,16 @@ Into environment gems from Github checkout: ...@@ -111,9 +111,16 @@ Into environment gems from Github checkout:
### Usage in Rails ### Usage in Rails
To annotate all your models, tests, fixtures, and factories: Enter your app directory:
cd /path/to/app cd /path/to/app
(Recommended) To annotate all your models with comments:
annotate --models --with-comment
To annotate all your models, tests, fixtures, and factories:
annotate annotate
To annotate just your models, tests, and factories: To annotate just your models, tests, and factories:
......
...@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) ...@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'annotate/version' require 'annotate/version'
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = 'annotate' s.name = 'ta_annotate'
s.version = Annotate.version s.version = Annotate.version
s.required_ruby_version = '>= 2.4.0' s.required_ruby_version = '>= 2.4.0'
......
...@@ -140,6 +140,7 @@ module AnnotateModels ...@@ -140,6 +140,7 @@ module AnnotateModels
info << get_schema_header_text(klass, options) info << get_schema_header_text(klass, options)
max_size = max_schema_info_width(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_names_overhead = 6
md_type_allowance = 18 md_type_allowance = 18
bare_type_allowance = 16 bare_type_allowance = 16
...@@ -152,12 +153,9 @@ module AnnotateModels ...@@ -152,12 +153,9 @@ module AnnotateModels
cols = columns(klass, options) cols = columns(klass, options)
cols.each do |col| cols.each do |col|
col_type = get_col_type(col) col_type = get_col_type(col)
attrs = get_attributes(col, col_type, klass, options) attrs = all_attrs[col.name]
col_name = if with_comments?(klass, options) && col.comment col_name = col.name
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})" col_comment = with_comments?(klass, options) && col.comment ? col.comment.gsub(/\n/, "\\n") : ''
else
col.name
end
if options[:format_rdoc] if options[:format_rdoc]
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n" 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 ...@@ -170,7 +168,7 @@ module AnnotateModels
type_remainder = (md_type_allowance - 2) - col_type.length 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" info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
else 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
end end
...@@ -801,21 +799,36 @@ module AnnotateModels ...@@ -801,21 +799,36 @@ module AnnotateModels
def max_schema_info_width(klass, options) def max_schema_info_width(klass, options)
cols = columns(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 max_size = cols.map(&:name).map(&:size).max
end
max_size += options[:format_rdoc] ? 5 : 1 max_size += options[:format_rdoc] ? 5 : 1
max_size max_size
end end
def format_default(col_name, max_size, col_type, bare_type_allowance, attrs) def get_attrs_and_width(klass, options)
sprintf("# %s:%s %s", mb_chars_ljust(col_name, max_size), mb_chars_ljust(col_type, bare_type_allowance), attrs.join(", ")).rstrip + "\n" 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 end
def width(string) def width(string)
......
module Annotate module Annotate
def self.version def self.version
'3.2.0' '3.2.1'
end end
end end
...@@ -1191,11 +1191,11 @@ describe AnnotateModels do ...@@ -1191,11 +1191,11 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id(ID) :integer not null, primary key # id :integer not null, primary key # ID
# active(Active) :boolean not null # active :boolean not null # Active
# name(Name) :string(50) not null # name :string(50) not null # Name
# notes(Notes) :text(55) not null # notes :text(55) not null # Notes
# no_comment :text(20) not null # no_comment :text(20) not null #
# #
EOS EOS
end end
...@@ -1226,15 +1226,15 @@ describe AnnotateModels do ...@@ -1226,15 +1226,15 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id(ID) :integer not null, primary key # id :integer not null, primary key # ID
# active(ACTIVE) :boolean not null # active :boolean not null # ACTIVE
# name(NAME) :string(50) not null # name :string(50) not null # NAME
# notes(NOTES) :text(55) not null # notes :text(55) not null # NOTES
# cyrillic(Кириллица) :text(30) not null # cyrillic :text(30) not null # Кириллица
# japanese(熊本大学 イタリア 宝島) :text(60) not null # japanese :text(60) not null # 熊本大学 イタリア 宝島
# arabic(لغة) :text(20) not null # arabic :text(20) not null # لغة
# no_comment :text(20) not null # no_comment :text(20) not null #
# location :geometry_collect not null # location :geometry_collect not null #
# #
EOS EOS
end end
...@@ -1259,9 +1259,9 @@ describe AnnotateModels do ...@@ -1259,9 +1259,9 @@ describe AnnotateModels do
# #
# Table name: users # Table name: users
# #
# id(ID) :integer not null, primary key # id :integer not null, primary key # ID
# notes(Notes.\\nMay include things like notes.):text(55) not null # notes :text(55) not null # Notes.\\nMay include things like notes.
# no_comment :text(20) not null # no_comment :text(20) not null #
# #
EOS EOS
end 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