@@ -32,11 +32,12 @@ handle_request({document_formatting, Params}) ->
32
32
<<" textDocument" >> := #{<<" uri" >> := Uri }
33
33
} = Params ,
34
34
Path = els_uri :path (Uri ),
35
+ {ok , Document } = els_utils :lookup_document (Uri ),
35
36
case els_utils :project_relative (Uri ) of
36
37
{error , not_relative } ->
37
38
{response , []};
38
39
RelativePath ->
39
- format_document (Path , RelativePath , Options )
40
+ format_document (Path , Document , RelativePath , Options )
40
41
end ;
41
42
handle_request ({document_rangeformatting , Params }) ->
42
43
#{
@@ -79,17 +80,17 @@ handle_request({document_ontypeformatting, Params}) ->
79
80
% %==============================================================================
80
81
% % Internal functions
81
82
% %==============================================================================
82
- -spec format_document (binary (), string (), formatting_options ()) ->
83
+ -spec format_document (binary (), els_dt_document : item (), string (), formatting_options ()) ->
83
84
{response , [text_edit ()]}.
84
- format_document (Path , RelativePath , Options ) ->
85
+ format_document (Path , Document , RelativePath , Options ) ->
85
86
Config = els_config :get (formatting ),
86
87
case {get_formatter_files (Config ), get_formatter_exclude_files (Config )} of
87
88
{all , ExcludeFiles } ->
88
89
case lists :member (Path , ExcludeFiles ) of
89
90
true ->
90
91
{response , []};
91
92
false ->
92
- do_format_document (Path , RelativePath , Options )
93
+ do_format_document (Document , RelativePath , Options )
93
94
end ;
94
95
{Files , ExcludeFiles } ->
95
96
case lists :member (Path , Files ) of
@@ -98,20 +99,32 @@ format_document(Path, RelativePath, Options) ->
98
99
true ->
99
100
{response , []};
100
101
false ->
101
- do_format_document (Path , RelativePath , Options )
102
+ do_format_document (Document , RelativePath , Options )
102
103
end ;
103
104
false ->
104
105
{response , []}
105
106
end
106
107
end .
107
108
108
- -spec do_format_document (binary (), string (), formatting_options ()) ->
109
+ -spec do_format_document (els_dt_document : item (), string (), formatting_options ()) ->
109
110
{response , [text_edit ()]}.
110
- do_format_document (Path , RelativePath , Options ) ->
111
+ do_format_document (#{ text : = Text } , RelativePath , Options ) ->
111
112
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
115
128
end ,
116
129
tempdir :mktmp (Fun ).
117
130
0 commit comments