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.rb57
1 files changed, 41 insertions, 16 deletions
diff --git a/lib/did_you_mean/core_ext/name_error.rb b/lib/did_you_mean/core_ext/name_error.rb
index 7e99282b41..8c170c4b90 100644
--- a/lib/did_you_mean/core_ext/name_error.rb
+++ b/lib/did_you_mean/core_ext/name_error.rb
@@ -1,24 +1,49 @@
module DidYouMean
module Correctable
- SKIP_TO_S_FOR_SUPER_LOOKUP = true
- private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
+ 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
- def original_message
- meth = method(:to_s)
- while meth.owner.const_defined?(:SKIP_TO_S_FOR_SUPER_LOOKUP)
- meth = meth.super_method
+ 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
- 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.include?(suggestion)
- msg
- rescue
- super
+ msg << suggestion if !msg.include?(suggestion)
+ msg
+ rescue
+ super
+ end
end
def corrections
@@ -26,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