Skip to content

Commit 693abdd

Browse files
hasimoto1009ctran
authored andcommitted
Fix shifted when format_markdown option enabled and used non-ascii (#650)
1 parent 5da92c4 commit 693abdd

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ Lint/ShadowingOuterLocalVariable:
408408

409409
# Offense count: 20
410410
Metrics/AbcSize:
411-
Max: 138
411+
Max: 139
412412

413413
# Offense count: 28
414414
# Configuration parameters: CountComments, ExcludedMethods.

lib/annotate/annotate_models.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def get_schema_info(klass, header, options = {})
305305
if options[:format_rdoc]
306306
info << sprintf("# %-#{max_size}.#{max_size}s<tt>%s</tt>", "*#{col_name}*::", attrs.unshift(col_type).join(", ")).rstrip + "\n"
307307
elsif options[:format_markdown]
308-
name_remainder = max_size - col_name.length
308+
name_remainder = max_size - col_name.length - non_ascii_length(col_name)
309309
type_remainder = (md_type_allowance - 2) - col_type.length
310310
info << (sprintf("# **`%s`**%#{name_remainder}s | `%s`%#{type_remainder}s | `%s`", col_name, " ", col_type, " ", attrs.join(", ").rstrip)).gsub('``', ' ').rstrip + "\n"
311311
else
@@ -932,6 +932,10 @@ def mb_chars_ljust(string, length)
932932
string[0..length-1]
933933
end
934934
end
935+
936+
def non_ascii_length(string)
937+
string.to_s.chars.reject(&:ascii_only?).length
938+
end
935939
end
936940

937941
class BadModelFileError < LoadError

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,28 @@ def self.when_called_with(options = {})
10461046
EOS
10471047
end
10481048

1049+
it 'should get schema info as Markdown with multibyte comment' do
1050+
klass = mock_class(:users,
1051+
:id,
1052+
[
1053+
mock_column(:id, :integer, comment: 'ID'),
1054+
mock_column(:name, :string, limit: 50, comment: 'NAME')
1055+
])
1056+
expect(AnnotateModels.get_schema_info(klass, AnnotateModels::PREFIX, format_markdown: true, with_comment: true)).to eql(<<-EOS.strip_heredoc)
1057+
# #{AnnotateModels::PREFIX}
1058+
#
1059+
# Table name: `users`
1060+
#
1061+
# ### Columns
1062+
#
1063+
# Name | Type | Attributes
1064+
# --------------------- | ------------------ | ---------------------------
1065+
# **`id(ID)`** | `integer` | `not null, primary key`
1066+
# **`name(NAME)`** | `string(50)` | `not null`
1067+
#
1068+
EOS
1069+
end
1070+
10491071
it 'should get schema info as Markdown' do
10501072
klass = mock_class(:users,
10511073
:id,

0 commit comments

Comments
 (0)