diff options
author | Yuki Nishijima <yk.nishijima@gmail.com> | 2021-12-21 19:03:10 +0900 |
---|---|---|
committer | Yuki Nishijima <yk.nishijima@gmail.com> | 2021-12-21 19:03:25 +0900 |
commit | 505dfae05d56d844ea150676edb87850a406d071 (patch) | |
tree | 76ecd819384b101f76c7d4179922ea41bf4a6e98 /lib/did_you_mean.rb | |
parent | 12fa4f2aceff06aeff028a3c72ec8791f78bfdb0 (diff) |
* gems/default_gems: Sync did_you_mean
Diffstat (limited to 'lib/did_you_mean.rb')
-rw-r--r-- | lib/did_you_mean.rb | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/lib/did_you_mean.rb b/lib/did_you_mean.rb index 6d3a6e8bda..0aef324059 100644 --- a/lib/did_you_mean.rb +++ b/lib/did_you_mean.rb @@ -86,11 +86,24 @@ require_relative 'did_you_mean/tree_spell_checker' # module DidYouMean # Map of error types and spell checker objects. - SPELL_CHECKERS = Hash.new(NullChecker) + @spell_checkers = Hash.new(NullChecker) + + # Returns a sharable hash map of error types and spell checker objects. + def self.spell_checkers + @spell_checkers + end # Adds +DidYouMean+ functionality to an error using a given spell checker def self.correct_error(error_class, spell_checker) - SPELL_CHECKERS[error_class.name] = spell_checker + if defined?(Ractor) + new_mapping = { **@spell_checkers, error_class.name => spell_checker } + new_mapping.default = NullChecker + + @spell_checkers = Ractor.make_shareable(new_mapping) + else + spell_checkers[error_class.name] = spell_checker + end + error_class.prepend(Correctable) unless error_class < Correctable end @@ -100,15 +113,23 @@ module DidYouMean correct_error LoadError, RequirePathChecker if RUBY_VERSION >= '2.8.0' correct_error NoMatchingPatternKeyError, PatternKeyNameChecker if defined?(::NoMatchingPatternKeyError) + # TODO: Remove on 3.3: + SPELL_CHECKERS = @spell_checkers + deprecate_constant :SPELL_CHECKERS + # Returns the currently set formatter. By default, it is set to +DidYouMean::Formatter+. def self.formatter - @formatter + if defined?(Reactor) + Ractor.current[:__did_you_mean_formatter__] || Formatter + else + Formatter + end end # Updates the primary formatter used to format the suggestions. def self.formatter=(formatter) - @formatter = formatter + if defined?(Reactor) + Ractor.current[:__did_you_mean_formatter__] = formatter + end end - - @formatter = Formatter.new end |