Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add erl_anno:set_end_location/2 #8966

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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().
jonatanklosko marked this conversation as resolved.
Show resolved Hide resolved

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)))}
Copy link
Contributor Author

@jonatanklosko jonatanklosko Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end_location is a bit tricky in the current tests setup, because it's both primary and secondary part of anno. In order to accommodate this, here we need to infer the value here (similarly to the clause for line above). Duplicating the whole text scan logic from the actual implementation sounds weird, so I call the inference directly.

I am happy to revise the tests, if another approach is preferable :)

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