Skip to content

Commit

Permalink
Allows custom setting the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
lafirest committed Jan 8, 2024
1 parent b9e854a commit e364271
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions apps/els_core/src/els_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
| refactorerl
| wrangler
| edoc_custom_tags
| providers.
| providers
| formatting.

-type path() :: file:filename().
-type state() :: #{
Expand All @@ -83,7 +84,8 @@
compiler_telemetry_enabled => boolean(),
wrangler => map() | 'notconfigured',
refactorerl => map() | 'notconfigured',
providers => map()
providers => map(),
formatting => map()
}.

-type error_reporting() :: lsp_notification | log.
Expand Down Expand Up @@ -167,6 +169,7 @@ do_initialize(RootUri, Capabilities, InitOptions, {ConfigPath, Config}) ->
RefactorErl = maps:get("refactorerl", Config, notconfigured),
Providers = maps:get("providers", Config, #{}),
EdocParseEnabled = maps:get("edoc_parse_enabled", Config, true),
Formatting = maps:get("formatting", Config, #{}),

%% Initialize and start Wrangler
case maps:get("wrangler", Config, notconfigured) of
Expand Down Expand Up @@ -269,6 +272,7 @@ do_initialize(RootUri, Capabilities, InitOptions, {ConfigPath, Config}) ->
ok = set(indexing_enabled, IndexingEnabled),

ok = set(refactorerl, RefactorErl),
ok = set(formatting, Formatting),
ok.

-spec start_link() -> {ok, pid()}.
Expand Down
20 changes: 19 additions & 1 deletion apps/els_lsp/src/els_formatting_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ format_document_local(
sub_indent => SubIndent,
output_dir => Dir
},
Formatter = rebar3_formatter:new(default_formatter, Opts, unused),
Config = els_config:get(formatting),
FormatterName = get_formatter_name(Config),
Formatter = rebar3_formatter:new(FormatterName, Opts, unused),
rebar3_formatter:format_file(RelativePath, Formatter),
ok.

Expand All @@ -119,3 +121,19 @@ rangeformat_document(_Uri, _Document, _Range, _Options) ->
{ok, [text_edit()]}.
ontypeformat_document(_Uri, _Document, _Line, _Col, _Char, _Options) ->
{ok, []}.

-spec get_formatter_name(map() | undefined) ->
sr_formatter | erlfmt_formatter | otp_formatter | default_formatter.
get_formatter_name(undefined) ->
default_formatter;
get_formatter_name(Config) ->
case maps:get("formatter", Config, undefined) of
"sr" ->
sr_formatter;
"erlfmt" ->
erlfmt_formatter;
"otp" ->
otp_formatter;
_ ->
default_formatter
end.

0 comments on commit e364271

Please sign in to comment.