Skip to content

Commit 1acd487

Browse files
committed
Make formatting use in-memory text instead of file content.
This will make document formatting use the unsaved state of the file.
1 parent 5afd375 commit 1acd487

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

apps/els_lsp/src/els_formatting_provider.erl

+23-10
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ handle_request({document_formatting, Params}) ->
3232
<<"textDocument">> := #{<<"uri">> := Uri}
3333
} = Params,
3434
Path = els_uri:path(Uri),
35+
{ok, Document} = els_utils:lookup_document(Uri),
3536
case els_utils:project_relative(Uri) of
3637
{error, not_relative} ->
3738
{response, []};
3839
RelativePath ->
39-
format_document(Path, RelativePath, Options)
40+
format_document(Path, Document, RelativePath, Options)
4041
end;
4142
handle_request({document_rangeformatting, Params}) ->
4243
#{
@@ -79,17 +80,17 @@ handle_request({document_ontypeformatting, Params}) ->
7980
%%==============================================================================
8081
%% Internal functions
8182
%%==============================================================================
82-
-spec format_document(binary(), string(), formatting_options()) ->
83+
-spec format_document(binary(), els_dt_document:item(), string(), formatting_options()) ->
8384
{response, [text_edit()]}.
84-
format_document(Path, RelativePath, Options) ->
85+
format_document(Path, Document, RelativePath, Options) ->
8586
Config = els_config:get(formatting),
8687
case {get_formatter_files(Config), get_formatter_exclude_files(Config)} of
8788
{all, ExcludeFiles} ->
8889
case lists:member(Path, ExcludeFiles) of
8990
true ->
9091
{response, []};
9192
false ->
92-
do_format_document(Path, RelativePath, Options)
93+
do_format_document(Document, RelativePath, Options)
9394
end;
9495
{Files, ExcludeFiles} ->
9596
case lists:member(Path, Files) of
@@ -98,20 +99,32 @@ format_document(Path, RelativePath, Options) ->
9899
true ->
99100
{response, []};
100101
false ->
101-
do_format_document(Path, RelativePath, Options)
102+
do_format_document(Document, RelativePath, Options)
102103
end;
103104
false ->
104105
{response, []}
105106
end
106107
end.
107108

108-
-spec do_format_document(binary(), string(), formatting_options()) ->
109+
-spec do_format_document(els_dt_document:item(), string(), formatting_options()) ->
109110
{response, [text_edit()]}.
110-
do_format_document(Path, RelativePath, Options) ->
111+
do_format_document(#{text := Text}, RelativePath, Options) ->
111112
Fun = fun(Dir) ->
112-
format_document_local(Dir, RelativePath, Options),
113-
Outfile = filename:join(Dir, RelativePath),
114-
{response, els_text_edit:diff_files(Path, Outfile)}
113+
{ok, OldCwd} = file:get_cwd(),
114+
ok = file:set_cwd(Dir),
115+
try
116+
Basename = filename:basename(RelativePath),
117+
InFile = filename:join(Dir, Basename),
118+
OutDir = filename:join([Dir, "formatted"]),
119+
OutFile = filename:join(OutDir, Basename),
120+
ok = filelib:ensure_dir(OutFile),
121+
ok = file:write_file(InFile, Text),
122+
ok = format_document_local(OutDir, Basename, Options),
123+
Diff = els_text_edit:diff_files(InFile, OutFile),
124+
{response, Diff}
125+
after
126+
ok = file:set_cwd(OldCwd)
127+
end
115128
end,
116129
tempdir:mktmp(Fun).
117130

apps/els_lsp/test/els_formatter_SUITE.erl

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ format_doc(Config) ->
7373
try
7474
file:set_cwd(RootPath),
7575
Uri = ?config(format_input_uri, Config),
76+
ok = els_config:set(formatting, #{}),
7677
#{result := Result} = els_client:document_formatting(Uri, 8, true),
7778
?assertEqual(
7879
[

0 commit comments

Comments
 (0)