Skip to content

Commit 7abb3bd

Browse files
committed
Unify position handling and improve docs
See #13179. See #13184.
1 parent 67fd982 commit 7abb3bd

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

lib/elixir/lib/code.ex

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,36 @@ defmodule Code do
196196

197197
@typedoc """
198198
Diagnostics returned by the compiler and code evaluation.
199+
200+
If there is a file and position, then the diagnostic is precise
201+
and you can use the given file and position for generating snippets,
202+
IDEs annotations, and so on.
203+
204+
Otherwise, a stacktrace may be given, which you can place your own
205+
heuristics to provide better reporting.
199206
"""
200207
@type diagnostic(severity) :: %{
201-
required(:file) => Path.t(),
208+
required(:file) => Path.t() | nil,
202209
required(:severity) => severity,
203210
required(:message) => String.t(),
204-
required(:position) => position,
211+
required(:position) => position(),
205212
required(:stacktrace) => Exception.stacktrace(),
206-
required(:span) => {non_neg_integer, non_neg_integer} | nil,
213+
required(:span) => {non_neg_integer(), non_neg_integer()} | nil,
207214
optional(:exception) => Exception.t() | nil,
208215
optional(any()) => any()
209216
}
210217

211218
@typedoc "The line. 0 indicates no line."
212219
@type line() :: non_neg_integer()
213-
@type position() :: line() | {pos_integer(), column :: non_neg_integer}
220+
221+
@typedoc """
222+
The position of the diagnostic.
223+
224+
Can be either a line number or a `{line, column}`.
225+
Line and columns numbers are one-based.
226+
A position of `0` represents unknown.
227+
"""
228+
@type position() :: line() | {line :: pos_integer(), column :: pos_integer()}
214229

215230
@boolean_compiler_options [
216231
:docs,

lib/mix/lib/mix/task.compiler.ex

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ defmodule Mix.Task.Compiler do
3131
defmodule Diagnostic do
3232
@moduledoc """
3333
Diagnostic information such as a warning or compilation error.
34+
35+
If there is a file and position, then the diagnostic is precise
36+
and you can use the given file and position for generating snippets,
37+
IDEs annotations, and so on.
38+
39+
Otherwise, a stacktrace may be given, which you can place your own
40+
heuristics to provide better reporting.
3441
"""
3542

3643
@type t :: %__MODULE__{
37-
file: Path.t(),
44+
file: Path.t() | nil,
3845
severity: severity,
3946
message: IO.chardata(),
40-
position: position,
47+
position: Code.position(),
4148
compiler_name: String.t(),
4249
details: Exception.t() | any,
4350
stacktrace: Exception.stacktrace(),
@@ -61,21 +68,6 @@ defmodule Mix.Task.Compiler do
6168
"""
6269
@type severity :: :error | :warning | :information | :hint
6370

64-
@typedoc """
65-
Where in a file the diagnostic applies. Can be either a line number,
66-
a `{line, column}` tuple, a range specified as `{start_line, start_col,
67-
end_line, end_col}`. `0` line represents unknown.
68-
69-
Line numbers are one-based, and column numbers in a range are zero-based and refer
70-
to the cursor position at the start of the character at that index. For example,
71-
to indicate that a diagnostic applies to the first `n` characters of the
72-
first line, the range would be `{1, 0, 1, n}`.
73-
"""
74-
@type position ::
75-
non_neg_integer
76-
| {pos_integer, non_neg_integer}
77-
| {pos_integer, non_neg_integer, pos_integer, non_neg_integer}
78-
7971
@enforce_keys [:file, :severity, :message, :position, :compiler_name]
8072
defstruct [
8173
:file,

0 commit comments

Comments
 (0)