Skip to content

Commit

Permalink
Merge pull request #8966 from jonatanklosko/jk-anno-end-location
Browse files Browse the repository at this point in the history
Add erl_anno:set_end_location/2

OTP-19354
  • Loading branch information
bjorng authored Nov 12, 2024
2 parents 06d477a + 16ead33 commit 119b3ff
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
51 changes: 39 additions & 12 deletions lib/stdlib/src/erl_anno.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ for manipulating annotations in abstract code.
-export([column/1, end_location/1, file/1, generated/1,
line/1, location/1, record/1, text/1]).
-export([set_file/2, set_generated/2, set_line/2, set_location/2,
set_record/2, set_text/2]).
set_end_location/2, set_record/2, set_text/2]).

%% To be used when necessary to avoid Dialyzer warnings.
-export([to_term/1, from_term/1]).
Expand All @@ -100,6 +100,7 @@ for manipulating annotations in abstract code.
-type annotation() :: {'file', filename()}
| {'generated', generated()}
| {'location', location()}
| {'end_location', location()}
| {'record', record()}
| {'text', string()}.

Expand Down Expand Up @@ -251,24 +252,37 @@ column(Anno) ->
end.

-doc """
Returns the end location of the text of the annotations Anno. If there is no
text, `undefined` is returned.
Returns the end location of the annotations Anno.
If the end location annotation is present, its value is returned. Otherwise,
if the text annotation is present, the end location is inferred from the
location and the text. Finally, if there is no text, `undefined` is returned.
""".
-doc(#{since => <<"OTP 18.0">>}).
-spec end_location(Anno) -> location() | 'undefined' when
Anno :: anno().

end_location(Line) when ?ALINE(Line) ->
undefined;
end_location({Line, Column}) when ?ALINE(Line), ?ACOLUMN(Column) ->
undefined;
end_location(Anno) ->
case text(Anno) of
case anno_info(Anno, end_location) of
undefined ->
undefined;
Text ->
case location(Anno) of
{Line, Column} ->
end_location(Text, Line, Column);
Line ->
end_location(Text, Line)
end
case text(Anno) of
undefined ->
undefined;
Text ->
case location(Anno) of
{Line, Column} ->
end_location(Text, Line, Column);
Line ->
end_location(Text, Line)
end
end;

Location ->
Location
end.

-doc """
Expand Down Expand Up @@ -403,6 +417,15 @@ set_location({L, C}=Loc, {Line, Column}) when ?ALINE(Line), ?ACOLUMN(Column),
set_location(Location, Anno) ->
set(location, Location, Anno).

-doc "Modifies the end location of the annotations Anno.".
-doc(#{since => <<"OTP 27.2">>}).
-spec set_end_location(Location, Anno) -> Anno when
Location :: location(),
Anno :: anno().

set_end_location(Location, Anno) ->
set(end_location, Location, Anno).

-doc "Modifies the record marker of the annotations Anno.".
-doc(#{since => <<"OTP 18.0">>}).
-spec set_record(Record, Anno) -> Anno when
Expand Down Expand Up @@ -508,6 +531,10 @@ is_settable(location, Line) when ?LLINE(Line) ->
true;
is_settable(location, {Line, Column}) when ?LLINE(Line), ?LCOLUMN(Column) ->
true;
is_settable(end_location, Line) when ?LLINE(Line) ->
true;
is_settable(end_location, {Line, Column}) when ?LLINE(Line), ?LCOLUMN(Column) ->
true;
is_settable(record, Boolean) when Boolean; not Boolean ->
true;
is_settable(text, Text) ->
Expand Down
26 changes: 25 additions & 1 deletion lib/stdlib/test/erl_anno_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ end_location(_Config) ->
test(1, [{text, "TEXT", [{end_location, 1}, {length, 4}]},
{text, "TEXT\n", [{end_location, 2}, {length, 5}]},
{text, "TEXT\ntxt", [{end_location, 2}, {length, 8}]}]),
test({1, 17}, [{end_location, {1, 21}, [{end_location, {1, 21}}]},
{end_location, {2, 1}, [{end_location, {2, 1}}]}]),
test(1, [{end_location, 1, [{end_location, 1}]},
{end_location, 2, [{end_location, 2}]},
{end_location, {1, 2}, [{end_location, {1, 2}}]}]),
ok.

%% Test 'file'.
Expand Down Expand Up @@ -396,6 +401,8 @@ anno_set(line, Value, Anno) ->
erl_anno:set_line(Value, Anno);
anno_set(location, Value, Anno) ->
erl_anno:set_location(Value, Anno);
anno_set(end_location, Value, Anno) ->
erl_anno:set_end_location(Value, Anno);
anno_set(record, Value, Anno) ->
erl_anno:set_record(Value, Anno);
anno_set(text, Value, Anno) ->
Expand Down Expand Up @@ -463,6 +470,23 @@ primary_value(line, State) ->
Line ->
Line
end};
primary_value(end_location, State) ->
case lists:keyfind(end_location, 1, State) of
false ->
case lists:keyfind(text, 1, State) of
false ->
undefined;
{text, Text} ->
case lists:keyfind(location, 1, State) of
false ->
undefined;
{location, Location} ->
{end_location, erl_anno:end_location(erl_anno:set_text(Text, erl_anno:new(Location)))}
end
end;
Tuple ->
Tuple
end;
primary_value(Item, State) ->
case lists:keyfind(Item, 1, State) of
false ->
Expand All @@ -475,7 +499,7 @@ default() ->
[{Tag, default_value(Tag)} || Tag <- default_items()].

primary_items() ->
[file, generated, line, location, record, text].
[file, generated, line, location, end_location, record, text].

secondary_items() ->
%% 'length' has not been implemented
Expand Down

0 comments on commit 119b3ff

Please sign in to comment.