diff options
author | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2022-06-07 15:24:48 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2022-06-07 15:24:48 +0900 |
commit | 11b9dd8ccb26a091b99230640494540ad0cc4e48 (patch) | |
tree | fb80532651cffb2d1609f1a237a1fa72b83bdf45 /lib | |
parent | b9f030954a8a1572032f3548b39c5b8ac35792ce (diff) | |
download | ruby-11b9dd8ccb26a091b99230640494540ad0cc4e48.tar.gz |
Manually merged https://github.com/ruby/did_you_mean/pull/177
Diffstat (limited to 'lib')
-rw-r--r-- | lib/did_you_mean/core_ext/name_error.rb | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/lib/did_you_mean/core_ext/name_error.rb b/lib/did_you_mean/core_ext/name_error.rb index eb3ef117a0..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 |