Skip to content

Commit

Permalink
Delay DidYouMean until NotFoundError#message is called
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng authored and hsbt committed Oct 17, 2023
1 parent d0d4f46 commit b59ca2f
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/rdoc/ri/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class Error < RDoc::RI::Error; end

class NotFoundError < Error

def initialize(klass, suggestions = nil) # :nodoc:
def initialize(klass, suggestion_proc = nil) # :nodoc:
@klass = klass
@suggestions = suggestions
@suggestion_proc = suggestion_proc
end

##
Expand All @@ -48,8 +48,9 @@ def name

def message # :nodoc:
str = "Nothing known about #{@klass}"
if @suggestions and !@suggestions.empty?
str += "\nDid you mean? #{@suggestions.join("\n ")}"
suggestions = @suggestion_proc&.call
if suggestions and !suggestions.empty?
str += "\nDid you mean? #{suggestions.join("\n ")}"
end
str
end
Expand Down Expand Up @@ -948,8 +949,8 @@ def expand_class klass
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)
suggestion_proc = -> { DidYouMean::SpellChecker.new(dictionary: class_names).correct(klass) }
raise NotFoundError.new(klass, suggestion_proc)
else
raise NotFoundError, klass
end
Expand Down Expand Up @@ -1237,8 +1238,8 @@ def lookup_method name
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)
suggestion_proc = -> { DidYouMean::SpellChecker.new(dictionary: methods).correct(method_name) }
raise NotFoundError.new(name, suggestion_proc)
else
raise NotFoundError, name
end
Expand Down

0 comments on commit b59ca2f

Please sign in to comment.