@@ -47,13 +47,24 @@ class Error < RDoc::RI::Error; end
4747
4848 class NotFoundError < Error
4949
50+ def initialize ( klass , suggestions = nil ) # :nodoc:
51+ @klass = klass
52+ @suggestions = suggestions
53+ end
54+
5055 ##
5156 # Name that wasn't found
5257
53- alias name message
58+ def name
59+ @klass
60+ end
5461
5562 def message # :nodoc:
56- "Nothing known about #{ super } "
63+ str = "Nothing known about #{ @klass } "
64+ if @suggestions and !@suggestions . empty?
65+ str += "\n Did you mean? #{ @suggestions . join ( "\n " ) } "
66+ end
67+ str
5768 end
5869 end
5970
@@ -917,13 +928,38 @@ def display_page_list store, pages = store.cache[:pages], search = nil
917928 display out
918929 end
919930
931+ def check_did_you_mean # :nodoc:
932+ if defined? DidYouMean ::SpellChecker
933+ true
934+ else
935+ begin
936+ require 'did_you_mean'
937+ if defined? DidYouMean ::SpellChecker
938+ true
939+ else
940+ false
941+ end
942+ rescue LoadError
943+ false
944+ end
945+ end
946+ end
947+
920948 ##
921949 # Expands abbreviated klass +klass+ into a fully-qualified class. "Zl::Da"
922950 # will be expanded to Zlib::DataError.
923951
924952 def expand_class klass
925- ary = classes . keys . grep ( Regexp . new ( "\\ A#{ klass . gsub ( /(?=::|\z )/ , '[^:]*' ) } \\ z" ) )
926- raise NotFoundError , klass if ary . length != 1 && ary . first != klass
953+ class_names = classes . keys
954+ ary = class_names . grep ( Regexp . new ( "\\ A#{ klass . gsub ( /(?=::|\z )/ , '[^:]*' ) } \\ z" ) )
955+ if ary . length != 1 && ary . first != klass
956+ if check_did_you_mean
957+ suggestions = DidYouMean ::SpellChecker . new ( dictionary : class_names ) . correct ( klass )
958+ raise NotFoundError . new ( klass , suggestions )
959+ else
960+ raise NotFoundError , klass
961+ end
962+ end
927963 ary . first
928964 end
929965
@@ -1235,7 +1271,21 @@ def load_methods_matching name
12351271 def lookup_method name
12361272 found = load_methods_matching name
12371273
1238- raise NotFoundError , name if found . empty?
1274+ if found . empty?
1275+ if check_did_you_mean
1276+ methods = [ ]
1277+ _ , _ , method_name = parse_name name
1278+ find_methods name do |store , klass , ancestor , types , method |
1279+ methods . push ( *store . class_methods [ klass ] ) if [ :class , :both ] . include? types
1280+ methods . push ( *store . instance_methods [ klass ] ) if [ :instance , :both ] . include? types
1281+ end
1282+ methods = methods . uniq
1283+ suggestions = DidYouMean ::SpellChecker . new ( dictionary : methods ) . correct ( method_name )
1284+ raise NotFoundError . new ( name , suggestions )
1285+ else
1286+ raise NotFoundError , name
1287+ end
1288+ end
12391289
12401290 filter_methods found , name
12411291 end
0 commit comments