Skip to content

Commit fc6d8ce

Browse files
authored
Merge pull request #533 from aycabta/support-did_you_mean
Support did_you_mean
2 parents 1ae47bb + b7dfb9c commit fc6d8ce

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

lib/rdoc/ri/driver.rb

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 += "\nDid 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

test/test_rdoc_ri_driver.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,27 @@ def test_find_store
969969
assert_equal 'nonexistent', e.name
970970
end
971971

972+
def test_did_you_mean
973+
skip 'skip test with did_you_men' unless defined? DidYouMean::SpellChecker
974+
975+
util_ancestors_store
976+
977+
e = assert_raises RDoc::RI::Driver::NotFoundError do
978+
@driver.lookup_method 'Foo.i_methdo'
979+
end
980+
assert_equal "Nothing known about Foo.i_methdo\nDid you mean? i_method", e.message
981+
982+
e = assert_raises RDoc::RI::Driver::NotFoundError do
983+
@driver.lookup_method 'Foo#i_methdo'
984+
end
985+
assert_equal "Nothing known about Foo#i_methdo\nDid you mean? i_method", e.message
986+
987+
e = assert_raises RDoc::RI::Driver::NotFoundError do
988+
@driver.lookup_method 'Foo::i_methdo'
989+
end
990+
assert_equal "Nothing known about Foo::i_methdo\nDid you mean? c_method", e.message
991+
end
992+
972993
def test_formatter
973994
tty = Object.new
974995
def tty.tty?() true; end

0 commit comments

Comments
 (0)