diff options
Diffstat (limited to 'lib/syntax_suggest/core_ext.rb')
| -rw-r--r-- | lib/syntax_suggest/core_ext.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/syntax_suggest/core_ext.rb b/lib/syntax_suggest/core_ext.rb new file mode 100644 index 0000000000..ffbc922eed --- /dev/null +++ b/lib/syntax_suggest/core_ext.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module SyntaxSuggest + # SyntaxSuggest.module_for_detailed_message [Private] + # + # Used to monkeypatch SyntaxError via Module.prepend + def self.module_for_detailed_message + Module.new { + def detailed_message(highlight: true, syntax_suggest: true, **kwargs) + return super unless syntax_suggest + + require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE) + + message = super + + if path + file = Pathname.new(path) + io = SyntaxSuggest::MiniStringIO.new + + SyntaxSuggest.call( + io: io, + source: file.read, + filename: file, + terminal: highlight + ) + annotation = io.string + + annotation += "\n" unless annotation.end_with?("\n") + + annotation + message + else + message + end + rescue => e + if ENV["SYNTAX_SUGGEST_DEBUG"] + $stderr.warn(e.message) + $stderr.warn(e.backtrace) + end + + # Ignore internal errors + message + end + } + end +end + +SyntaxError.prepend(SyntaxSuggest.module_for_detailed_message) |
