1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/error_highlight] Make the formatter mechanism support Ractor

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.

9fbaa8ab7c
This commit is contained in:
Yusuke Endoh 2021-10-27 11:25:58 +09:00 committed by git
parent 33844f3096
commit 4c32fcb84f
2 changed files with 5 additions and 7 deletions

View file

@ -1,6 +1,6 @@
module ErrorHighlight module ErrorHighlight
class DefaultFormatter class DefaultFormatter
def message_for(spot) def self.message_for(spot)
# currently only a one-line code snippet is supported # currently only a one-line code snippet is supported
if spot[:first_lineno] == spot[:last_lineno] if spot[:first_lineno] == spot[:last_lineno]
indent = spot[:snippet][0...spot[:first_column]].gsub(/[^\t]/, " ") indent = spot[:snippet][0...spot[:first_column]].gsub(/[^\t]/, " ")
@ -14,12 +14,10 @@ module ErrorHighlight
end end
def self.formatter def self.formatter
@@formatter Ractor.current[:__error_highlight_formatter__] || DefaultFormatter
end end
def self.formatter=(formatter) def self.formatter=(formatter)
@@formatter = formatter Ractor.current[:__error_highlight_formatter__] = formatter
end end
self.formatter = DefaultFormatter.new
end end

View file

@ -5,7 +5,7 @@ require "tempfile"
class ErrorHighlightTest < Test::Unit::TestCase class ErrorHighlightTest < Test::Unit::TestCase
class DummyFormatter class DummyFormatter
def message_for(corrections) def self.message_for(corrections)
"" ""
end end
end end
@ -13,7 +13,7 @@ class ErrorHighlightTest < Test::Unit::TestCase
def setup def setup
if defined?(DidYouMean) if defined?(DidYouMean)
@did_you_mean_old_formatter = DidYouMean.formatter @did_you_mean_old_formatter = DidYouMean.formatter
DidYouMean.formatter = DummyFormatter.new DidYouMean.formatter = DummyFormatter
end end
end end