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

Make formatting use in-memory text instead of file content. #1555

Merged
merged 1 commit into from
Oct 1, 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
33 changes: 23 additions & 10 deletions apps/els_lsp/src/els_formatting_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ handle_request({document_formatting, Params}) ->
<<"textDocument">> := #{<<"uri">> := Uri}
} = Params,
Path = els_uri:path(Uri),
{ok, Document} = els_utils:lookup_document(Uri),
case els_utils:project_relative(Uri) of
{error, not_relative} ->
{response, []};
RelativePath ->
format_document(Path, RelativePath, Options)
format_document(Path, Document, RelativePath, Options)
end;
handle_request({document_rangeformatting, Params}) ->
#{
Expand Down Expand Up @@ -79,17 +80,17 @@ handle_request({document_ontypeformatting, Params}) ->
%%==============================================================================
%% Internal functions
%%==============================================================================
-spec format_document(binary(), string(), formatting_options()) ->
-spec format_document(binary(), els_dt_document:item(), string(), formatting_options()) ->
{response, [text_edit()]}.
format_document(Path, RelativePath, Options) ->
format_document(Path, Document, RelativePath, Options) ->
Config = els_config:get(formatting),
case {get_formatter_files(Config), get_formatter_exclude_files(Config)} of
{all, ExcludeFiles} ->
case lists:member(Path, ExcludeFiles) of
true ->
{response, []};
false ->
do_format_document(Path, RelativePath, Options)
do_format_document(Document, RelativePath, Options)
end;
{Files, ExcludeFiles} ->
case lists:member(Path, Files) of
Expand All @@ -98,20 +99,32 @@ format_document(Path, RelativePath, Options) ->
true ->
{response, []};
false ->
do_format_document(Path, RelativePath, Options)
do_format_document(Document, RelativePath, Options)
end;
false ->
{response, []}
end
end.

-spec do_format_document(binary(), string(), formatting_options()) ->
-spec do_format_document(els_dt_document:item(), string(), formatting_options()) ->
{response, [text_edit()]}.
do_format_document(Path, RelativePath, Options) ->
do_format_document(#{text := Text}, RelativePath, Options) ->
Fun = fun(Dir) ->
format_document_local(Dir, RelativePath, Options),
Outfile = filename:join(Dir, RelativePath),
{response, els_text_edit:diff_files(Path, Outfile)}
{ok, OldCwd} = file:get_cwd(),
ok = file:set_cwd(Dir),
try
Basename = filename:basename(RelativePath),
InFile = filename:join(Dir, Basename),
OutDir = filename:join([Dir, "formatted"]),
OutFile = filename:join(OutDir, Basename),
ok = filelib:ensure_dir(OutFile),
ok = file:write_file(InFile, Text),
ok = format_document_local(OutDir, Basename, Options),
Diff = els_text_edit:diff_files(InFile, OutFile),
{response, Diff}
after
ok = file:set_cwd(OldCwd)
end
end,
tempdir:mktmp(Fun).

Expand Down
1 change: 1 addition & 0 deletions apps/els_lsp/test/els_formatter_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ format_doc(Config) ->
try
file:set_cwd(RootPath),
Uri = ?config(format_input_uri, Config),
ok = els_config:set(formatting, #{}),
#{result := Result} = els_client:document_formatting(Uri, 8, true),
?assertEqual(
[
Expand Down
Loading