summaryrefslogtreecommitdiff
path: root/lib/rdoc/ri/driver.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-10 02:01:00 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-10 02:01:00 +0000
commitdb6c51ec4f9d1b7386215cad5924e6e03733b6d6 (patch)
treea9c5accde00bbc0263aebb5b6d65f0922b9aef20 /lib/rdoc/ri/driver.rb
parent4e1fe5357e0286c738e9aaca5201765d187fbed3 (diff)
Merge rdoc-6.0.0.beta3.
* It version introduced did you mean? feature for ri command: https://github.com/ruby/rdoc/pull/533 * Removed obbsoleted ruby_token.rbb. [Bug #13990][ruby-core:83180] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60146 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rdoc/ri/driver.rb')
-rw-r--r--lib/rdoc/ri/driver.rb60
1 files changed, 55 insertions, 5 deletions
diff --git a/lib/rdoc/ri/driver.rb b/lib/rdoc/ri/driver.rb
index 8e68f72d53..51d633be58 100644
--- a/lib/rdoc/ri/driver.rb
+++ b/lib/rdoc/ri/driver.rb
@@ -47,13 +47,24 @@ class RDoc::RI::Driver
class NotFoundError < Error
+ def initialize(klass, suggestions = nil) # :nodoc:
+ @klass = klass
+ @suggestions = suggestions
+ end
+
##
# Name that wasn't found
- alias name message
+ def name
+ @klass
+ end
def message # :nodoc:
- "Nothing known about #{super}"
+ str = "Nothing known about #{@klass}"
+ if @suggestions and !@suggestions.empty?
+ str += "\nDid you mean? #{@suggestions.join("\n ")}"
+ end
+ str
end
end
@@ -917,13 +928,38 @@ or the PAGER environment variable.
display out
end
+ def check_did_you_mean # :nodoc:
+ if defined? DidYouMean::SpellChecker
+ true
+ else
+ begin
+ require 'did_you_mean'
+ if defined? DidYouMean::SpellChecker
+ true
+ else
+ false
+ end
+ rescue LoadError
+ false
+ end
+ end
+ end
+
##
# Expands abbreviated klass +klass+ into a fully-qualified class. "Zl::Da"
# will be expanded to Zlib::DataError.
def expand_class klass
- ary = classes.keys.grep(Regexp.new("\\A#{klass.gsub(/(?=::|\z)/, '[^:]*')}\\z"))
- raise NotFoundError, klass if ary.length != 1 && ary.first != klass
+ class_names = classes.keys
+ ary = class_names.grep(Regexp.new("\\A#{klass.gsub(/(?=::|\z)/, '[^:]*')}\\z"))
+ if ary.length != 1 && ary.first != klass
+ if check_did_you_mean
+ suggestions = DidYouMean::SpellChecker.new(dictionary: class_names).correct(klass)
+ raise NotFoundError.new(klass, suggestions)
+ else
+ raise NotFoundError, klass
+ end
+ end
ary.first
end
@@ -1235,7 +1271,21 @@ or the PAGER environment variable.
def lookup_method name
found = load_methods_matching name
- raise NotFoundError, name if found.empty?
+ if found.empty?
+ if check_did_you_mean
+ methods = []
+ _, _, method_name = parse_name name
+ find_methods name do |store, klass, ancestor, types, method|
+ methods.push(*store.class_methods[klass]) if [:class, :both].include? types
+ methods.push(*store.instance_methods[klass]) if [:instance, :both].include? types
+ end
+ methods = methods.uniq
+ suggestions = DidYouMean::SpellChecker.new(dictionary: methods).correct(method_name)
+ raise NotFoundError.new(name, suggestions)
+ else
+ raise NotFoundError, name
+ end
+ end
filter_methods found, name
end