|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -# Ruby 3.2+ has a cleaner way to hook into Ruby that doesn't use `require` |
4 | | -if SyntaxError.method_defined?(:detailed_message) |
5 | | - module SyntaxSuggest |
6 | | - # SyntaxSuggest.module_for_detailed_message [Private] |
7 | | - # |
8 | | - # Used to monkeypatch SyntaxError via Module.prepend |
9 | | - def self.module_for_detailed_message |
10 | | - Module.new { |
11 | | - def detailed_message(highlight: true, syntax_suggest: true, **kwargs) |
12 | | - return super unless syntax_suggest |
13 | | - |
14 | | - require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE) |
15 | | - |
16 | | - message = super |
17 | | - |
18 | | - if path |
19 | | - file = Pathname.new(path) |
20 | | - io = SyntaxSuggest::MiniStringIO.new |
21 | | - |
22 | | - SyntaxSuggest.call( |
23 | | - io: io, |
24 | | - source: file.read, |
25 | | - filename: file, |
26 | | - terminal: highlight |
27 | | - ) |
28 | | - annotation = io.string |
29 | | - |
30 | | - annotation += "\n" unless annotation.end_with?("\n") |
31 | | - |
32 | | - annotation + message |
33 | | - else |
34 | | - message |
35 | | - end |
36 | | - rescue => e |
37 | | - if ENV["SYNTAX_SUGGEST_DEBUG"] |
38 | | - $stderr.warn(e.message) |
39 | | - $stderr.warn(e.backtrace) |
40 | | - end |
41 | | - |
42 | | - # Ignore internal errors |
| 3 | +module SyntaxSuggest |
| 4 | + # SyntaxSuggest.module_for_detailed_message [Private] |
| 5 | + # |
| 6 | + # Used to monkeypatch SyntaxError via Module.prepend |
| 7 | + def self.module_for_detailed_message |
| 8 | + Module.new { |
| 9 | + def detailed_message(highlight: true, syntax_suggest: true, **kwargs) |
| 10 | + return super unless syntax_suggest |
| 11 | + |
| 12 | + require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE) |
| 13 | + |
| 14 | + message = super |
| 15 | + |
| 16 | + if path |
| 17 | + file = Pathname.new(path) |
| 18 | + io = SyntaxSuggest::MiniStringIO.new |
| 19 | + |
| 20 | + SyntaxSuggest.call( |
| 21 | + io: io, |
| 22 | + source: file.read, |
| 23 | + filename: file, |
| 24 | + terminal: highlight |
| 25 | + ) |
| 26 | + annotation = io.string |
| 27 | + |
| 28 | + annotation += "\n" unless annotation.end_with?("\n") |
| 29 | + |
| 30 | + annotation + message |
| 31 | + else |
43 | 32 | message |
44 | 33 | end |
45 | | - } |
46 | | - end |
47 | | - end |
48 | | - |
49 | | - SyntaxError.prepend(SyntaxSuggest.module_for_detailed_message) |
50 | | -else |
51 | | - autoload :Pathname, "pathname" |
52 | | - |
53 | | - #-- |
54 | | - # Monkey patch kernel to ensure that all `require` calls call the same |
55 | | - # method |
56 | | - #++ |
57 | | - module Kernel |
58 | | - # :stopdoc: |
59 | | - |
60 | | - module_function |
61 | | - |
62 | | - alias_method :syntax_suggest_original_require, :require |
63 | | - alias_method :syntax_suggest_original_require_relative, :require_relative |
64 | | - alias_method :syntax_suggest_original_load, :load |
65 | | - |
66 | | - def load(file, wrap = false) |
67 | | - syntax_suggest_original_load(file) |
68 | | - rescue SyntaxError => e |
69 | | - require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE) |
70 | | - |
71 | | - SyntaxSuggest.handle_error(e) |
72 | | - end |
73 | | - |
74 | | - def require(file) |
75 | | - syntax_suggest_original_require(file) |
76 | | - rescue SyntaxError => e |
77 | | - require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE) |
78 | | - |
79 | | - SyntaxSuggest.handle_error(e) |
80 | | - end |
| 34 | + rescue => e |
| 35 | + if ENV["SYNTAX_SUGGEST_DEBUG"] |
| 36 | + $stderr.warn(e.message) |
| 37 | + $stderr.warn(e.backtrace) |
| 38 | + end |
81 | 39 |
|
82 | | - def require_relative(file) |
83 | | - if Pathname.new(file).absolute? |
84 | | - syntax_suggest_original_require file |
85 | | - else |
86 | | - relative_from = caller_locations(1..1).first |
87 | | - relative_from_path = relative_from.absolute_path || relative_from.path |
88 | | - syntax_suggest_original_require File.expand_path("../#{file}", relative_from_path) |
| 40 | + # Ignore internal errors |
| 41 | + message |
89 | 42 | end |
90 | | - rescue SyntaxError => e |
91 | | - require "syntax_suggest/api" unless defined?(SyntaxSuggest::DEFAULT_VALUE) |
92 | | - |
93 | | - SyntaxSuggest.handle_error(e) |
94 | | - end |
| 43 | + } |
95 | 44 | end |
96 | 45 | end |
| 46 | + |
| 47 | +SyntaxError.prepend(SyntaxSuggest.module_for_detailed_message) |
0 commit comments