Skip to content

Commit 0b3d518

Browse files
karreiromatzbot
authored andcommitted
[ruby/error_highlight] Rename the ErrorHighlight::DefaultFormatter setting to max_snippet_width for clarity
ruby/error_highlight@e13cbd4335
1 parent 60c0c32 commit 0b3d518

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

lib/error_highlight/formatter.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ def self.message_for(spot)
1212
ellipsis = "..."
1313

1414
# truncate snippet to fit in the viewport
15-
if snippet_max_width && snippet.size > snippet_max_width
16-
available_width = snippet_max_width - ellipsis.size
17-
center = first_column - snippet_max_width / 2
15+
if max_snippet_width && snippet.size > max_snippet_width
16+
available_width = max_snippet_width - ellipsis.size
17+
center = first_column - max_snippet_width / 2
1818

1919
visible_start = last_column < available_width ? 0 : [center, 0].max
20-
visible_end = visible_start + snippet_max_width
21-
visible_start = snippet.size - snippet_max_width if visible_end > snippet.size
20+
visible_end = visible_start + max_snippet_width
21+
visible_start = snippet.size - max_snippet_width if visible_end > snippet.size
2222

2323
prefix = visible_start.positive? ? ellipsis : ""
2424
suffix = visible_end < snippet.size ? ellipsis : ""
@@ -36,27 +36,27 @@ def self.message_for(spot)
3636
"\n\n#{ snippet }#{ marker }"
3737
end
3838

39-
def self.snippet_max_width
39+
def self.max_snippet_width
4040
return if Ractor.current[:__error_highlight_max_snippet_width__] == :disabled
4141

4242
Ractor.current[:__error_highlight_max_snippet_width__] ||= terminal_width
4343
end
4444

45-
def self.snippet_max_width=(width)
45+
def self.max_snippet_width=(width)
4646
return Ractor.current[:__error_highlight_max_snippet_width__] = :disabled if width.nil?
4747

4848
width = width.to_i
4949

5050
if width < MIN_SNIPPET_WIDTH
51-
warn "'snippet_max_width' adjusted to minimum value of #{MIN_SNIPPET_WIDTH}."
51+
warn "'max_snippet_width' adjusted to minimum value of #{MIN_SNIPPET_WIDTH}."
5252
width = MIN_SNIPPET_WIDTH
5353
end
5454

5555
Ractor.current[:__error_highlight_max_snippet_width__] = width
5656
end
5757

5858
def self.terminal_width
59-
# lazy load io/console, so it's not loaded when snippet_max_width is set
59+
# lazy load io/console, so it's not loaded when 'max_snippet_width' is set
6060
require "io/console"
6161
STDERR.winsize[1] if STDERR.tty?
6262
rescue LoadError, NoMethodError, SystemCallError

test/error_highlight/test_error_highlight.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require "tempfile"
66

77
class ErrorHighlightTest < Test::Unit::TestCase
8-
ErrorHighlight::DefaultFormatter.snippet_max_width = 80
8+
ErrorHighlight::DefaultFormatter.max_snippet_width = 80
99

1010
class DummyFormatter
1111
def self.message_for(corrections)
@@ -1338,9 +1338,9 @@ def test_errors_on_small_terminal_window_at_the_middle
13381338

13391339
def test_errors_on_extremely_small_terminal_window
13401340
custom_max_width = 30
1341-
original_max_width = ErrorHighlight::DefaultFormatter.snippet_max_width
1341+
original_max_width = ErrorHighlight::DefaultFormatter.max_snippet_width
13421342

1343-
ErrorHighlight::DefaultFormatter.snippet_max_width = custom_max_width
1343+
ErrorHighlight::DefaultFormatter.max_snippet_width = custom_max_width
13441344

13451345
assert_error_message(NoMethodError, <<~END) do
13461346
undefined method `time' for #{ ONE_RECV_MESSAGE }
@@ -1352,14 +1352,14 @@ def test_errors_on_extremely_small_terminal_window
13521352
100000000000000 + 1.time { 100000000000000 }
13531353
end
13541354
ensure
1355-
ErrorHighlight::DefaultFormatter.snippet_max_width = original_max_width
1355+
ErrorHighlight::DefaultFormatter.max_snippet_width = original_max_width
13561356
end
13571357

13581358
def test_errors_on_terminal_window_smaller_than_min_width
13591359
custom_max_width = 5
1360-
original_max_width = ErrorHighlight::DefaultFormatter.snippet_max_width
1360+
original_max_width = ErrorHighlight::DefaultFormatter.max_snippet_width
13611361

1362-
ErrorHighlight::DefaultFormatter.snippet_max_width = custom_max_width
1362+
ErrorHighlight::DefaultFormatter.max_snippet_width = custom_max_width
13631363

13641364
assert_error_message(NoMethodError, <<~END) do
13651365
undefined method `time' for #{ ONE_RECV_MESSAGE }
@@ -1371,14 +1371,14 @@ def test_errors_on_terminal_window_smaller_than_min_width
13711371
100000000000000 + 1.time { 100000000000000 }
13721372
end
13731373
ensure
1374-
ErrorHighlight::DefaultFormatter.snippet_max_width = original_max_width
1374+
ErrorHighlight::DefaultFormatter.max_snippet_width = original_max_width
13751375
end
13761376

13771377
def test_errors_on_terminal_window_when_truncation_is_disabled
13781378
custom_max_width = nil
1379-
original_max_width = ErrorHighlight::DefaultFormatter.snippet_max_width
1379+
original_max_width = ErrorHighlight::DefaultFormatter.max_snippet_width
13801380

1381-
ErrorHighlight::DefaultFormatter.snippet_max_width = custom_max_width
1381+
ErrorHighlight::DefaultFormatter.max_snippet_width = custom_max_width
13821382

13831383
assert_error_message(NoMethodError, <<~END) do
13841384
undefined method `time' for #{ ONE_RECV_MESSAGE }
@@ -1390,7 +1390,7 @@ def test_errors_on_terminal_window_when_truncation_is_disabled
13901390
10000000000000000000000000000000000000000000000000000000000000000000000 + 1.time { 1000000000000000000000000000000 }
13911391
end
13921392
ensure
1393-
ErrorHighlight::DefaultFormatter.snippet_max_width = original_max_width
1393+
ErrorHighlight::DefaultFormatter.max_snippet_width = original_max_width
13941394
end
13951395

13961396
def test_errors_on_small_terminal_window_when_larger_than_viewport

0 commit comments

Comments
 (0)