Skip to content

Commit

Permalink
Make formatting use in-memory text instead of file content.
Browse files Browse the repository at this point in the history
This will make document formatting use the unsaved state of the file.
  • Loading branch information
plux committed Oct 1, 2024
1 parent 5afd375 commit 1acd487
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
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

0 comments on commit 1acd487

Please sign in to comment.