|
10 | 10 | %% Includes
|
11 | 11 | %%==============================================================================
|
12 | 12 | -include("els_lsp.hrl").
|
| 13 | +-include_lib("kernel/include/logger.hrl"). |
13 | 14 |
|
14 | 15 | %%==============================================================================
|
15 | 16 | %% Macro Definitions
|
16 | 17 | %%==============================================================================
|
17 | 18 | -define(DEFAULT_SUB_INDENT, 2).
|
| 19 | +-type formatter_name() :: |
| 20 | + sr_formatter |
| 21 | + | erlfmt_formatter |
| 22 | + | otp_formatter |
| 23 | + | default_formatter. |
18 | 24 |
|
19 | 25 | %%==============================================================================
|
20 | 26 | %% els_provider functions
|
@@ -118,19 +124,40 @@ format_document_local(
|
118 | 124 | <<"tabSize">> := TabSize
|
119 | 125 | } = Options
|
120 | 126 | ) ->
|
121 |
| - SubIndent = maps:get(<<"subIndent">>, Options, ?DEFAULT_SUB_INDENT), |
122 |
| - Opts = #{ |
| 127 | + SubIndent = get_sub_indent(Options), |
| 128 | + Opts0 = #{ |
123 | 129 | remove_tabs => InsertSpaces,
|
124 | 130 | break_indent => TabSize,
|
125 | 131 | sub_indent => SubIndent,
|
126 | 132 | output_dir => Dir
|
127 | 133 | },
|
128 | 134 | Config = els_config:get(formatting),
|
129 | 135 | 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]), |
130 | 138 | Formatter = rebar3_formatter:new(FormatterName, Opts, unused),
|
131 | 139 | rebar3_formatter:format_file(RelativePath, Formatter),
|
132 | 140 | ok.
|
133 | 141 |
|
| 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(), formatting_options(), 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 | + |
134 | 161 | -spec rangeformat_document(uri(), map(), range(), formatting_options()) ->
|
135 | 162 | {ok, [text_edit()]}.
|
136 | 163 | rangeformat_document(_Uri, _Document, _Range, _Options) ->
|
@@ -170,7 +197,7 @@ expand_glob(RootPath, Glob) ->
|
170 | 197 | [unicode:characters_to_binary(Path) || Path <- filelib:wildcard(Wildcard)].
|
171 | 198 |
|
172 | 199 | -spec get_formatter_name(map() | undefined) ->
|
173 |
| - sr_formatter | erlfmt_formatter | otp_formatter | default_formatter. |
| 200 | + formatter_name(). |
174 | 201 | get_formatter_name(undefined) ->
|
175 | 202 | default_formatter;
|
176 | 203 | get_formatter_name(Config) ->
|
|
0 commit comments