Skip to content

Commit f31cb17

Browse files
committed
Allow indentation of multiple files
1 parent 87e3cfe commit f31cb17

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

apps/erlang_ls/src/erlang_ls.erl

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
main(Args) ->
88
case getopt:parse(cli_options(), Args) of
9-
{ok, {Opts, _Other}} ->
9+
{ok, {Opts, Other}} ->
1010
Dump = proplists:get_value(dump, Opts),
1111
Indent = proplists:get_value(indent, Opts),
1212
if Dump =:= undefined, Indent =:= undefined ->
1313
start_server(Opts);
1414
is_list(Indent) ->
15-
indent(Indent);
15+
indent([Indent|Other], proplists:get_value(verbose, Opts, 0));
1616
is_list(Dump) ->
1717
dump_file(Dump)
1818
end;
@@ -24,9 +24,9 @@ main(Args) ->
2424
cli_options() ->
2525
[
2626
{dump, $d, "dump", string, "Dump sourcer db for file"},
27-
{indent, $i, "indent", string, "Indent file and exit"},
2827
{port, $p, "port", integer, "LSP server port"},
29-
{verbose, $v, "verbose", integer, "Verbosity level"}
28+
{verbose, $v, "verbose", integer, "Verbosity level"},
29+
{indent, $i, "indent", string, "Indent file(s) and exit"}
3030
].
3131

3232
start_server(Opts) ->
@@ -64,19 +64,28 @@ dump_file(File) ->
6464
io:format("Analyze: ~.6wms~n", [AT div 1000]),
6565
ok.
6666

67-
indent(File) ->
68-
case file:read_file(File) of
69-
{ok, BinSrc} ->
70-
Enc = encoding(BinSrc),
71-
Src = unicode:characters_to_list(BinSrc, Enc),
72-
{ST,Indented} = timer:tc(fun() -> sourcer_indent:lines(Src) end),
73-
ok = file:write_file(File, unicode:characters_to_binary(Indented, utf8, Enc)),
74-
io:format("Indent: ~.6wms~n", [ST div 1000]),
75-
ok;
76-
{error, Error} ->
77-
io:format("Could not read file: ~s\n Reason: ~p~n",[File, Error]),
67+
indent([File|Files], Verbose) ->
68+
try case file:read_file(File) of
69+
{ok, BinSrc} ->
70+
Enc = encoding(BinSrc),
71+
Src = unicode:characters_to_list(BinSrc, Enc),
72+
{ST,Indented} = timer:tc(fun() -> sourcer_indent:lines(Src) end),
73+
ok = file:write_file(File, unicode:characters_to_binary(Indented, utf8, Enc)),
74+
Verbose > 0 andalso io:format("Indent: ~.6wms ~s~n", [ST div 1000, File]),
75+
indent(Files, Verbose);
76+
{error, Error} ->
77+
Str = io_lib:format("Could not read file: ~ts\n Reason: ~p~n", [File, Error]),
78+
throw({error,Str})
79+
end
80+
catch throw:{error, Desc} ->
81+
io:format("~ts", [Desc]),
82+
erlang:halt(1);
83+
error:_What ->
84+
io:format("Error could not indent file: ~ts\n", [File]),
7885
erlang:halt(1)
79-
end.
86+
end;
87+
indent([], _) ->
88+
ok.
8089

8190
encoding(Bin) ->
8291
case epp:read_encoding_from_binary(Bin) of

rebar.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@
4747

4848
{escript_main_app, erlang_ls}.
4949
{escript_comment, "%% v0.2.0\n"}.
50+
51+
{provider_hooks, [{post, [{compile, escriptize}]}]}.

0 commit comments

Comments
 (0)