Skip to content

Commit 472c4e1

Browse files
committed
Avoid crashes on diagnostics with tabs, closes #14073
1 parent 305d8e6 commit 472c4e1

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ highlight_at_position(Column, Severity, Length) ->
216216

217217
highlight_below_line(Line, Severity) ->
218218
% Don't highlight leading whitespaces in line
219-
{_, SpacesMatched} = trim_line(Line, 0),
219+
{Rest, SpacesMatched} = trim_line(Line, 0),
220220

221-
Length = string:length(Line),
221+
Length = string:length(Rest),
222222
Highlight = case Severity of
223-
warning -> highlight(lists:duplicate(Length - SpacesMatched, $~), warning);
224-
error -> highlight(lists:duplicate(Length - SpacesMatched, $^), error)
223+
warning -> highlight(lists:duplicate(Length, $~), warning);
224+
error -> highlight(lists:duplicate(Length, $^), error)
225225
end,
226226

227227
[n_spaces(SpacesMatched), Highlight].

lib/elixir/test/elixir/kernel/diagnostics_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,35 @@ defmodule Kernel.DiagnosticsTest do
859859
purge(Sample)
860860
end
861861

862+
@tag :tmp_dir
863+
test "simple warning with tabs (line + file)", %{tmp_dir: tmp_dir} do
864+
path = make_relative_tmp(tmp_dir, "long-warning.ex")
865+
866+
source = """
867+
defmodule Sample do
868+
\t@file "#{path}"
869+
\tdefp a do
870+
\t\tUnknown.b()
871+
\tend
872+
end
873+
"""
874+
875+
File.write!(path, source)
876+
877+
expected = """
878+
warning: Unknown.b/0 is undefined (module Unknown is not available or is yet to be defined). Make sure the module name is correct and has been specified in full (or that an alias has been defined)
879+
880+
4 │ \t\tUnknown.b()
881+
│ ~~~~~~~~~~~
882+
883+
└─ #{path}:4: Sample.a/0
884+
"""
885+
886+
assert capture_eval(source, columns: false) =~ expected
887+
after
888+
purge(Sample)
889+
end
890+
862891
test "simple warning (no file)" do
863892
source = """
864893
defmodule Sample do

0 commit comments

Comments
 (0)