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

[Format range] Introduce functions to format enclosing range. #295

Merged
merged 3 commits into from
May 26, 2021
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
36 changes: 32 additions & 4 deletions src/erlfmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,52 @@ replace_pragma_comment_block(_Prefix, [("%" ++ _) = Head | Tail]) ->
replace_pragma_comment_block(Prefix, [Head | Tail]) ->
[Head | replace_pragma_comment_block(Prefix, Tail)].

% Format the minimum number of top-level forms
% that cover the passed range.
% Rationale: top-level forms is the smallest
% granularity we support now.
-spec format_file_range(
file:name_all(),
erlfmt_scan:location(),
erlfmt_scan:location(),
[{print_width, pos_integer()}]
) ->
{ok, string(), [error_info()]}
| {error, error_info()}
| {options, [{erlfmt_scan:location(), erlfmt_scan:location()}]}.
| {error, error_info()}.
format_file_range(FileName, StartLocation, EndLocation, Options) ->
{ok, Nodes, Warnings} = file_read_nodes(FileName, ignore),
format_range(FileName, StartLocation, EndLocation, Options, Nodes, Warnings).
format_enclosing_range(FileName, StartLocation, EndLocation, Options, Nodes, Warnings).

-spec format_string_range(
string(),
erlfmt_scan:location(),
erlfmt_scan:location(),
[{print_width, pos_integer()}]
) ->
{ok, string(), [error_info()]}
| {error, error_info()}.
format_string_range(String, StartLocation, EndLocation, Options) ->
FileName = "nofile",
Pragma = proplists:get_value(pragma, Options, ignore),
{ok, Nodes, Warnings} = read_nodes_string(FileName, String, Pragma),
format_range(FileName, StartLocation, EndLocation, Options, Nodes, Warnings).
format_enclosing_range(FileName, StartLocation, EndLocation, Options, Nodes, Warnings).

format_enclosing_range(FileName, StartLocation, EndLocation, Options, Nodes, Warnings) ->
case format_range(FileName, StartLocation, EndLocation, Options, Nodes, Warnings) of
{options, PossibleRanges} ->
% Pick the largest range, so all intersected forms are covered.
{Starts, Ends} = lists:unzip(PossibleRanges),
Start = lists:min(Starts),
End = lists:max(Ends),
Res = format_range(FileName, Start, End, Options, Nodes, Warnings),
% Poor man's assert (to avoid including assert.hrl)
% This time we must have the formatted result.
{ok, _, _} = Res,
Res;
X ->
% Already ok or error: pass as is.
X
end.

format_range(FileName, StartLocation, EndLocation, Options, Nodes, Warnings) ->
PrintWidth = proplists:get_value(print_width, Options, ?DEFAULT_WIDTH),
Expand Down
71 changes: 67 additions & 4 deletions test/erlfmt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
broken_range/1,
snapshot_range_whole_comments/1,
snapshot_range_partial/1,
snapshot_range_partial2/1,
snapshot_enclosing_range/1,
snapshot_enclosing_range2/1,
snapshot_enclosing_range_no_leak/1,
contains_pragma/1,
insert_pragma/1,
overlong_warning/1,
Expand Down Expand Up @@ -179,7 +183,11 @@ groups() ->
simple_comments_range,
broken_range,
snapshot_range_whole_comments,
snapshot_range_partial
snapshot_range_partial,
snapshot_range_partial2,
snapshot_enclosing_range,
snapshot_enclosing_range2,
snapshot_enclosing_range_no_leak
]},
{pragma_tests, [parallel], [
contains_pragma,
Expand Down Expand Up @@ -1259,13 +1267,68 @@ snapshot_range_whole_comments(Config) ->
snapshot_range_partial(_) ->
% Check only the specified form is formatted.
Original =
"x() ->"
"0.\n"
"x()->0.\n"
"y() -> 1.\n"
"z() -> 2.\n",
% Only x() should be touched.
Reference = "x() -> 0.\n",
Output = erlfmt:format_string_range(Original, {1, 1}, {1, 9}, []),
Output = erlfmt:format_string_range(Original, {1, 1}, {1, 8}, []),
assert_snapshot_match(list_to_binary(Reference), Output).

snapshot_range_partial2(_) ->
% Check only the specified form is formatted, too,
% with a range spanning on two lines.
Original =
"x()->\n"
"0.\n"
"y() -> 1.\n",
% Only x() should be touched.
Reference =
"x() ->\n"
" 0.",
Output = erlfmt:format_string_range(Original, {1, 1}, {2, 3}, []),
assert_snapshot_match(list_to_binary(Reference), Output).

snapshot_enclosing_range(_) ->
% Check we pick format the whole top level form covering passed range.
% Same Original and reference than snapshot_range_partial2.
Original =
"x()->\n"
"0.\n"
"y() -> 1.\n",
Reference =
"x() ->\n"
" 0.",
% Range is just the first char (mimic e.g. function renaming).
Output = erlfmt:format_string_range(Original, {1, 1}, {1, 2}, []),
assert_snapshot_match(list_to_binary(Reference), Output).

snapshot_enclosing_range2(_) ->
% Same test when picking second part of the form:
% passed range doesn't intersect 'top level'.
Original =
"x()->\n"
" 0.\n"
"y() -> 1.\n",
Reference =
"x() ->\n"
" 0.",
Output = erlfmt:format_string_range(Original, {2, 2}, {2, 3}, []),
assert_snapshot_match(list_to_binary(Reference), Output).

snapshot_enclosing_range_no_leak(_) ->
% Check only the specified form is formatted.
Original =
"x()->\n"
" 0.\n"
"\n"
"y() -> 1.\n",
Reference =
"x() ->\n"
" 0.",
% End point overshots line 3, but doesn't reach line 4:
% Only x() must be formatted.
Output = erlfmt:format_string_range(Original, {1, 1}, {3, 3}, []),
assert_snapshot_match(list_to_binary(Reference), Output).

contains_pragma(Config) when is_list(Config) ->
Expand Down