@@ -47,13 +47,24 @@ class Error < RDoc::RI::Error; end
47
47
48
48
class NotFoundError < Error
49
49
50
+ def initialize ( klass , suggestions = nil ) # :nodoc:
51
+ @klass = klass
52
+ @suggestions = suggestions
53
+ end
54
+
50
55
##
51
56
# Name that wasn't found
52
57
53
- alias name message
58
+ def name
59
+ @klass
60
+ end
54
61
55
62
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
57
68
end
58
69
end
59
70
@@ -917,13 +928,38 @@ def display_page_list store, pages = store.cache[:pages], search = nil
917
928
display out
918
929
end
919
930
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
+
920
948
##
921
949
# Expands abbreviated klass +klass+ into a fully-qualified class. "Zl::Da"
922
950
# will be expanded to Zlib::DataError.
923
951
924
952
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
927
963
ary . first
928
964
end
929
965
@@ -1235,7 +1271,21 @@ def load_methods_matching name
1235
1271
def lookup_method name
1236
1272
found = load_methods_matching name
1237
1273
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
1239
1289
1240
1290
filter_methods found , name
1241
1291
end
0 commit comments