Skip to content

Commit 56c7ced

Browse files
committed
Add possiblity to configure formatting width
1 parent e1d04e2 commit 56c7ced

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

apps/els_lsp/src/els_formatting_provider.erl

+30-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@
1010
%% Includes
1111
%%==============================================================================
1212
-include("els_lsp.hrl").
13+
-include_lib("kernel/include/logger.hrl").
1314

1415
%%==============================================================================
1516
%% Macro Definitions
1617
%%==============================================================================
1718
-define(DEFAULT_SUB_INDENT, 2).
19+
-type formatter_name() ::
20+
sr_formatter
21+
| erlfmt_formatter
22+
| otp_formatter
23+
| default_formatter.
1824

1925
%%==============================================================================
2026
%% els_provider functions
@@ -118,19 +124,40 @@ format_document_local(
118124
<<"tabSize">> := TabSize
119125
} = Options
120126
) ->
121-
SubIndent = maps:get(<<"subIndent">>, Options, ?DEFAULT_SUB_INDENT),
122-
Opts = #{
127+
SubIndent = get_sub_indent(Options),
128+
Opts0 = #{
123129
remove_tabs => InsertSpaces,
124130
break_indent => TabSize,
125131
sub_indent => SubIndent,
126132
output_dir => Dir
127133
},
128134
Config = els_config:get(formatting),
129135
FormatterName = get_formatter_name(Config),
136+
Opts = maybe_set_width(FormatterName, Opts0, get_width(Config)),
137+
?LOG_INFO("Format using ~p with options: ~p", [FormatterName, Opts]),
130138
Formatter = rebar3_formatter:new(FormatterName, Opts, unused),
131139
rebar3_formatter:format_file(RelativePath, Formatter),
132140
ok.
133141

142+
-spec get_sub_indent(map()) -> integer().
143+
get_sub_indent(Options) ->
144+
maps:get("subIndent", Options, ?DEFAULT_SUB_INDENT).
145+
146+
-spec maybe_set_width(formatter_name(), map(), integer() | undefined) ->
147+
formatting_options().
148+
maybe_set_width(erlfmt_formatter, Opts, Width) when is_integer(Width) ->
149+
Opts#{print_width => Width};
150+
maybe_set_width(default_formatter, Opts, Width) when is_integer(Width) ->
151+
Opts#{paper => Width};
152+
maybe_set_width(otp_formatter, Opts, Width) when is_integer(Width) ->
153+
Opts#{paper => Width};
154+
maybe_set_width(_Formatter, Opts, _) ->
155+
Opts.
156+
157+
-spec get_width(map()) -> integer() | undefined.
158+
get_width(Config) ->
159+
maps:get("width", Config, undefined).
160+
134161
-spec rangeformat_document(uri(), map(), range(), formatting_options()) ->
135162
{ok, [text_edit()]}.
136163
rangeformat_document(_Uri, _Document, _Range, _Options) ->
@@ -170,7 +197,7 @@ expand_glob(RootPath, Glob) ->
170197
[unicode:characters_to_binary(Path) || Path <- filelib:wildcard(Wildcard)].
171198

172199
-spec get_formatter_name(map() | undefined) ->
173-
sr_formatter | erlfmt_formatter | otp_formatter | default_formatter.
200+
formatter_name().
174201
get_formatter_name(undefined) ->
175202
default_formatter;
176203
get_formatter_name(Config) ->

0 commit comments

Comments
 (0)