Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows custom setting the formatter #1473

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, #{}),
DocsMemo = maps:get("docs_memo", Config, false),

%% Initialize and start Wrangler
Expand Down Expand Up @@ -271,6 +274,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.
Loading