From 472c4e1a24af9efdbddf3dfd0c3af9b437dbbd37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 16 Dec 2024 12:09:02 +0100 Subject: [PATCH] Avoid crashes on diagnostics with tabs, closes #14073 --- lib/elixir/src/elixir_errors.erl | 8 ++--- .../test/elixir/kernel/diagnostics_test.exs | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/elixir/src/elixir_errors.erl b/lib/elixir/src/elixir_errors.erl index 6dd3197fc6..0f47189050 100644 --- a/lib/elixir/src/elixir_errors.erl +++ b/lib/elixir/src/elixir_errors.erl @@ -216,12 +216,12 @@ highlight_at_position(Column, Severity, Length) -> highlight_below_line(Line, Severity) -> % Don't highlight leading whitespaces in line - {_, SpacesMatched} = trim_line(Line, 0), + {Rest, SpacesMatched} = trim_line(Line, 0), - Length = string:length(Line), + Length = string:length(Rest), Highlight = case Severity of - warning -> highlight(lists:duplicate(Length - SpacesMatched, $~), warning); - error -> highlight(lists:duplicate(Length - SpacesMatched, $^), error) + warning -> highlight(lists:duplicate(Length, $~), warning); + error -> highlight(lists:duplicate(Length, $^), error) end, [n_spaces(SpacesMatched), Highlight]. diff --git a/lib/elixir/test/elixir/kernel/diagnostics_test.exs b/lib/elixir/test/elixir/kernel/diagnostics_test.exs index f016ac75cc..0ce2ed7a88 100644 --- a/lib/elixir/test/elixir/kernel/diagnostics_test.exs +++ b/lib/elixir/test/elixir/kernel/diagnostics_test.exs @@ -859,6 +859,35 @@ defmodule Kernel.DiagnosticsTest do purge(Sample) end + @tag :tmp_dir + test "simple warning with tabs (line + file)", %{tmp_dir: tmp_dir} do + path = make_relative_tmp(tmp_dir, "long-warning.ex") + + source = """ + defmodule Sample do + \t@file "#{path}" + \tdefp a do + \t\tUnknown.b() + \tend + end + """ + + File.write!(path, source) + + expected = """ + 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) + │ + 4 │ \t\tUnknown.b() + │ ~~~~~~~~~~~ + │ + └─ #{path}:4: Sample.a/0 + """ + + assert capture_eval(source, columns: false) =~ expected + after + purge(Sample) + end + test "simple warning (no file)" do source = """ defmodule Sample do