2021-06-30 06:28:22 +03:00
|
|
|
require_relative "formatter"
|
|
|
|
|
2021-06-28 07:27:35 +03:00
|
|
|
module ErrorHighlight
|
2021-06-18 11:11:39 +03:00
|
|
|
module CoreExt
|
2022-06-07 11:40:03 +03:00
|
|
|
private def generate_snippet
|
2022-08-10 12:36:59 +03:00
|
|
|
spot = ErrorHighlight.spot(self)
|
|
|
|
return "" unless spot
|
|
|
|
return ErrorHighlight.formatter.message_for(spot)
|
2022-06-07 11:40:03 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
if Exception.method_defined?(:detailed_message)
|
|
|
|
def detailed_message(highlight: false, error_highlight: true, **)
|
|
|
|
return super unless error_highlight
|
|
|
|
snippet = generate_snippet
|
|
|
|
if highlight
|
|
|
|
snippet = snippet.gsub(/.+/) { "\e[1m" + $& + "\e[m" }
|
|
|
|
end
|
|
|
|
super + snippet
|
2021-06-18 11:11:39 +03:00
|
|
|
end
|
2022-06-07 11:40:03 +03:00
|
|
|
else
|
|
|
|
# This is a marker to let `DidYouMean::Correctable#original_message` skip
|
|
|
|
# the following method definition of `to_s`.
|
|
|
|
# See https://github.com/ruby/did_you_mean/pull/152
|
|
|
|
SKIP_TO_S_FOR_SUPER_LOOKUP = true
|
|
|
|
private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
|
2021-06-18 11:11:39 +03:00
|
|
|
|
2022-06-07 11:40:03 +03:00
|
|
|
def to_s
|
|
|
|
msg = super
|
|
|
|
snippet = generate_snippet
|
|
|
|
if snippet != "" && !msg.include?(snippet)
|
|
|
|
msg + snippet
|
|
|
|
else
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
end
|
2021-06-18 11:11:39 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
NameError.prepend(CoreExt)
|
2022-11-14 08:36:24 +03:00
|
|
|
|
|
|
|
if Exception.method_defined?(:detailed_message)
|
|
|
|
# ErrorHighlight is enabled for TypeError and ArgumentError only when Exception#detailed_message is available.
|
|
|
|
# This is because changing ArgumentError#message is highly incompatible.
|
|
|
|
TypeError.prepend(CoreExt)
|
|
|
|
ArgumentError.prepend(CoreExt)
|
|
|
|
end
|
2021-06-18 11:11:39 +03:00
|
|
|
end
|