Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38f20fa

Browse files
committed
Support hard tabs
Now, the highlight line is created by replacing non-tab characters with spaces, and keeping all hard tabs as-is. This means the highlight line has the completely same indentation as the code snippet line. Fixes #7
1 parent 2fc70d7 commit 38f20fa

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/error_highlight/formatter.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ class DefaultFormatter
33
def message_for(spot)
44
# currently only a one-line code snippet is supported
55
if spot[:first_lineno] == spot[:last_lineno]
6-
marker = " " * spot[:first_column] + "^" * (spot[:last_column] - spot[:first_column])
6+
indent = spot[:snippet][0...spot[:first_column]].gsub(/[^\t]/, " ")
7+
marker = indent + "^" * (spot[:last_column] - spot[:first_column])
78

89
"\n\n#{ spot[:snippet] }#{ marker }"
910
else

test/test_error_highlight.rb

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "test/unit"
22

33
require "error_highlight"
4+
require "tempfile"
45

56
class ErrorHighlightTest < Test::Unit::TestCase
67
class DummyFormatter
@@ -999,4 +1000,20 @@ def custom_formatter.message_for(spot)
9991000
ensure
10001001
ErrorHighlight.formatter = original_formatter
10011002
end
1003+
1004+
def test_hard_tabs
1005+
tmp = Tempfile.new(["error_highlight_test", ".rb"])
1006+
tmp << "\t \t1.time {}\n"
1007+
tmp.close(false)
1008+
1009+
assert_error_message(NoMethodError, <<~END.gsub("_", "\t")) do
1010+
undefined method `time' for 1:Integer
1011+
1012+
_ _1.time {}
1013+
_ _ ^^^^^
1014+
END
1015+
1016+
load tmp.path
1017+
end
1018+
end
10021019
end

0 commit comments

Comments
 (0)