summaryrefslogtreecommitdiff
path: root/lib/did_you_mean/core_ext/name_error.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/did_you_mean/core_ext/name_error.rb')
-rw-r--r--lib/did_you_mean/core_ext/name_error.rb54
1 files changed, 43 insertions, 11 deletions
diff --git a/lib/did_you_mean/core_ext/name_error.rb b/lib/did_you_mean/core_ext/name_error.rb
index 77dcd520c0..8c170c4b90 100644
--- a/lib/did_you_mean/core_ext/name_error.rb
+++ b/lib/did_you_mean/core_ext/name_error.rb
@@ -1,17 +1,49 @@
module DidYouMean
module Correctable
- def original_message
- method(:to_s).super_method.call
- end
+ if Exception.method_defined?(:detailed_message)
+ # just for compatibility
+ def original_message
+ # we cannot use alias here because
+ to_s
+ end
+
+ def detailed_message(highlight: true, did_you_mean: true, **)
+ msg = super.dup
+
+ return msg unless did_you_mean
+
+ suggestion = DidYouMean.formatter.message_for(corrections)
+
+ if highlight
+ suggestion = suggestion.gsub(/.+/) { "\e[1m" + $& + "\e[m" }
+ end
+
+ msg << suggestion
+ msg
+ rescue
+ super
+ end
+ else
+ SKIP_TO_S_FOR_SUPER_LOOKUP = true
+ private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
+
+ def original_message
+ meth = method(:to_s)
+ while meth.owner.const_defined?(:SKIP_TO_S_FOR_SUPER_LOOKUP)
+ meth = meth.super_method
+ end
+ meth.call
+ end
- def to_s
- msg = super.dup
- suggestion = DidYouMean.formatter.message_for(corrections)
+ def to_s
+ msg = super.dup
+ suggestion = DidYouMean.formatter.message_for(corrections)
- msg << suggestion if !msg.end_with?(suggestion)
- msg
- rescue
- super
+ msg << suggestion if !msg.include?(suggestion)
+ msg
+ rescue
+ super
+ end
end
def corrections
@@ -19,7 +51,7 @@ module DidYouMean
end
def spell_checker
- SPELL_CHECKERS[self.class.to_s].new(self)
+ DidYouMean.spell_checkers[self.class.to_s].new(self)
end
end
end