Skip to content

Commit 62160f9

Browse files
committed
Add possiblity to configure formatting width
1 parent e1d04e2 commit 62160f9

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

apps/els_lsp/src/els_formatting_provider.erl

+29-4
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,39 @@ 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) -> map().
147+
maybe_set_width(erlfmt_formatter, Opts, Width) when is_integer(Width) ->
148+
Opts#{print_width => Width};
149+
maybe_set_width(default_formatter, Opts, Width) when is_integer(Width) ->
150+
Opts#{paper => Width};
151+
maybe_set_width(otp_formatter, Opts, Width) when is_integer(Width) ->
152+
Opts#{paper => Width};
153+
maybe_set_width(_Formatter, Opts, _) ->
154+
Opts.
155+
156+
-spec get_width(map()) -> integer() | undefined.
157+
get_width(Config) ->
158+
maps:get("width", Config, undefined).
159+
134160
-spec rangeformat_document(uri(), map(), range(), formatting_options()) ->
135161
{ok, [text_edit()]}.
136162
rangeformat_document(_Uri, _Document, _Range, _Options) ->
@@ -169,8 +195,7 @@ expand_glob(RootPath, Glob) ->
169195
Wildcard = unicode:characters_to_list(filename:join(RootPath, Glob), utf8),
170196
[unicode:characters_to_binary(Path) || Path <- filelib:wildcard(Wildcard)].
171197

172-
-spec get_formatter_name(map() | undefined) ->
173-
sr_formatter | erlfmt_formatter | otp_formatter | default_formatter.
198+
-spec get_formatter_name(map() | undefined) -> formatter_name().
174199
get_formatter_name(undefined) ->
175200
default_formatter;
176201
get_formatter_name(Config) ->

0 commit comments

Comments
 (0)