提交 7692a163 编写于 作者: S ShunsukeAida

Improve generator name suggestions a bit.

Previously, the Levenshtein distances to most commands were wrongly
calculated due to a big shortcut. This might be included in the original
code for the performance sake, but I'm not sure that's something we should
take over accuracy in Rails.

Before
'foo' to 'assets'                  #=> 3
'foo' to 'controller'              #=> 3
'foo' to 'generator'               #=> 3
'foo' to 'helper'                  #=> 3
'foo' to 'integration_test'        #=> 3
'foo' to 'jbuilder'                #=> 3
'foo' to 'job'                     #=> 2
'foo' to 'mailer'                  #=> 3
'foo' to 'migration'               #=> 3
'foo' to 'model'                   #=> 3
'foo' to 'resource'                #=> 3
'foo' to 'resource_route'          #=> 3
'foo' to 'scaffold'                #=> 3
'foo' to 'scaffold_controller'     #=> 3
'foo' to 'task'                    #=> 4
'foo' to 'active_record:migration' #=> 3
'foo' to 'active_record:model'     #=> 3
'foo' to 'coffee:assets'           #=> 3
'foo' to 'css:assets'              #=> 3
'foo' to 'css:scaffold'            #=> 3
'foo' to 'erb:controller'          #=> 3
'foo' to 'erb:mailer'              #=> 3
'foo' to 'erb:scaffold'            #=> 3
'foo' to 'js:assets'               #=> 3
'foo' to 'scss:assets'             #=> 3
'foo' to 'scss:scaffold'           #=> 3
'foo' to 'test_unit:controller'    #=> 3
'foo' to 'test_unit:generator'     #=> 3
'foo' to 'test_unit:helper'        #=> 3
'foo' to 'test_unit:integration'   #=> 3
'foo' to 'test_unit:job'           #=> 3
'foo' to 'test_unit:mailer'        #=> 3
'foo' to 'test_unit:model'         #=> 3
'foo' to 'test_unit:plugin'        #=> 3
'foo' to 'test_unit:scaffold'      #=> 3

After
'foo' to 'assets'                  #=> 6
'foo' to 'controller'              #=> 8
'foo' to 'generator'               #=> 8
'foo' to 'helper'                  #=> 6
'foo' to 'integration_test'        #=> 15
'foo' to 'jbuilder'                #=> 8
'foo' to 'job'                     #=> 2
'foo' to 'mailer'                  #=> 6
'foo' to 'migration'               #=> 8
'foo' to 'model'                   #=> 4
'foo' to 'resource'                #=> 7
'foo' to 'resource_route'          #=> 12
'foo' to 'scaffold'                #=> 6
'foo' to 'scaffold_controller'     #=> 16
'foo' to 'task'                    #=> 4
'foo' to 'active_record:migration' #=> 21
'foo' to 'active_record:model'     #=> 17
'foo' to 'coffee:assets'           #=> 12
'foo' to 'css:assets'              #=> 10
'foo' to 'css:scaffold'            #=> 10
'foo' to 'erb:controller'          #=> 12
'foo' to 'erb:mailer'              #=> 10
'foo' to 'erb:scaffold'            #=> 10
'foo' to 'js:assets'               #=> 9
'foo' to 'scss:assets'             #=> 11
'foo' to 'scss:scaffold'           #=> 11
'foo' to 'test_unit:controller'    #=> 18
'foo' to 'test_unit:generator'     #=> 18
'foo' to 'test_unit:helper'        #=> 16
'foo' to 'test_unit:integration'   #=> 20
'foo' to 'test_unit:job'           #=> 12
'foo' to 'test_unit:mailer'        #=> 16
'foo' to 'test_unit:model'         #=> 14
'foo' to 'test_unit:plugin'        #=> 16
'foo' to 'test_unit:scaffold'      #=> 16

Besides that, the conjunction "or" of the message now appears only between
the last two suggestions.
上级 9892d445
......@@ -159,7 +159,7 @@ def self.invoke(namespace, args=ARGV, config={})
options = sorted_groups.map(&:last).flatten
suggestions = options.sort_by {|suggested| levenshtein_distance(namespace.to_s, suggested) }.first(3)
msg = "Could not find generator '#{namespace}'. "
msg << "Maybe you meant #{ suggestions.map {|s| "'#{s}'"}.join(" or ") }\n"
msg << "Maybe you meant #{ suggestions.map {|s| "'#{s}'"}.to_sentence(last_word_connector: " or ") }\n"
msg << "Run `rails generate --help` for more options."
puts msg
end
......@@ -260,11 +260,9 @@ def self.levenshtein_distance str1, str2
t = str2
n = s.length
m = t.length
max = n/2
return m if (0 == n)
return n if (0 == m)
return n if (n - m).abs > max
d = (0..m).to_a
x = nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册