Skip to content

Commit

Permalink
Make the formatter mechanism support Ractor
Browse files Browse the repository at this point in the history
Now the formatter configuration is per Ractor. DefaultFormatter is used
if not set.

DefaultFormatter#message_for is now a class method to allow sub-Ractors
to call the method.
  • Loading branch information
mame committed Oct 27, 2021
1 parent a76a917 commit 9fbaa8a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 3 additions & 5 deletions lib/error_highlight/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ErrorHighlight
class DefaultFormatter
def message_for(spot)
def self.message_for(spot)
# currently only a one-line code snippet is supported
if spot[:first_lineno] == spot[:last_lineno]
indent = spot[:snippet][0...spot[:first_column]].gsub(/[^\t]/, " ")
Expand All @@ -14,12 +14,10 @@ def message_for(spot)
end

def self.formatter
@@formatter
Ractor.current[:__error_highlight_formatter__] || DefaultFormatter
end

def self.formatter=(formatter)
@@formatter = formatter
Ractor.current[:__error_highlight_formatter__] = formatter
end

self.formatter = DefaultFormatter.new
end
4 changes: 2 additions & 2 deletions test/test_error_highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

class ErrorHighlightTest < Test::Unit::TestCase
class DummyFormatter
def message_for(corrections)
def self.message_for(corrections)
""
end
end

def setup
if defined?(DidYouMean)
@did_you_mean_old_formatter = DidYouMean.formatter
DidYouMean.formatter = DummyFormatter.new
DidYouMean.formatter = DummyFormatter
end
end

Expand Down

0 comments on commit 9fbaa8a

Please sign in to comment.