Skip to content

Commit 9c0d48e

Browse files
authored
Add config option to include / exclude files for formatting. (#1546)
1 parent 79eab37 commit 9c0d48e

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

apps/els_lsp/src/els_formatting_provider.erl

+48-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,34 @@ handle_request({document_ontypeformatting, Params}) ->
7474
%% Internal functions
7575
%%==============================================================================
7676
-spec format_document(binary(), string(), formatting_options()) ->
77-
{[text_edit()]}.
77+
{response, [text_edit()]}.
7878
format_document(Path, RelativePath, Options) ->
79+
Config = els_config:get(formatting),
80+
case {get_formatter_files(Config), get_formatter_exclude_files(Config)} of
81+
{all, ExcludeFiles} ->
82+
case lists:member(Path, ExcludeFiles) of
83+
true ->
84+
{response, []};
85+
false ->
86+
do_format_document(Path, RelativePath, Options)
87+
end;
88+
{Files, ExcludeFiles} ->
89+
case lists:member(Path, Files) of
90+
true ->
91+
case lists:member(Path, ExcludeFiles) of
92+
true ->
93+
{response, []};
94+
false ->
95+
do_format_document(Path, RelativePath, Options)
96+
end;
97+
false ->
98+
{response, []}
99+
end
100+
end.
101+
102+
-spec do_format_document(binary(), string(), formatting_options()) ->
103+
{response, [text_edit()]}.
104+
do_format_document(Path, RelativePath, Options) ->
79105
Fun = fun(Dir) ->
80106
format_document_local(Dir, RelativePath, Options),
81107
Outfile = filename:join(Dir, RelativePath),
@@ -122,6 +148,27 @@ rangeformat_document(_Uri, _Document, _Range, _Options) ->
122148
ontypeformat_document(_Uri, _Document, _Line, _Col, _Char, _Options) ->
123149
{ok, []}.
124150

151+
-spec get_formatter_files(map()) -> [binary()] | all.
152+
get_formatter_files(Config) ->
153+
RootPath = els_uri:path(els_config:get(root_uri)),
154+
case maps:get("files", Config, all) of
155+
all ->
156+
all;
157+
Globs ->
158+
lists:flatten([expand_glob(RootPath, Glob) || Glob <- Globs])
159+
end.
160+
161+
-spec get_formatter_exclude_files(map()) -> [binary()].
162+
get_formatter_exclude_files(Config) ->
163+
RootPath = els_uri:path(els_config:get(root_uri)),
164+
Globs = maps:get("exclude_files", Config, []),
165+
lists:flatten([expand_glob(RootPath, Glob) || Glob <- Globs]).
166+
167+
-spec expand_glob(binary(), binary()) -> [binary()].
168+
expand_glob(RootPath, Glob) ->
169+
Wildcard = unicode:characters_to_list(filename:join(RootPath, Glob), utf8),
170+
[unicode:characters_to_binary(Path) || Path <- filelib:wildcard(Wildcard)].
171+
125172
-spec get_formatter_name(map() | undefined) ->
126173
sr_formatter | erlfmt_formatter | otp_formatter | default_formatter.
127174
get_formatter_name(undefined) ->

0 commit comments

Comments
 (0)