Skip to content

Commit

Permalink
[#60] Add support and docs for a YML erlang_ls.config file
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoaloi committed Sep 20, 2019
1 parent ebfdef1 commit cd76473
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 15 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ Code completion is available for the following elements:
* Atom names
* Module attributes

## Customization

It is possible to customize the behaviour of the `erlang_ls` server by
adding a `erlang_ls.config` file to the root of your projects.

A sample `erlang_ls.config` file would look like the following:

otp_path: "/path/to/otp/lib/erlang"

Currently, the following customizations are possible:

| Parameter | Description |
|-----------|------------------------------|
| otp_path | Path to the OTP installation |
| | |

## Troubleshooting

It is possible to compile and start the language server in _debug mode_:
Expand Down
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
, {cowlib, "2.3.0"}
, {redbug, "1.2.1"}
, {lager, "3.6.8"}
, {yamerl, "0.7.0"}
]
}.

Expand Down
6 changes: 4 additions & 2 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
{<<"jsx">>,{pkg,<<"jsx">>,<<"2.9.0">>},0},
{<<"lager">>,{pkg,<<"lager">>,<<"3.6.8">>},0},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.5.0">>},0},
{<<"redbug">>,{pkg,<<"redbug">>,<<"1.2.1">>},0}]}.
{<<"redbug">>,{pkg,<<"redbug">>,<<"1.2.1">>},0},
{<<"yamerl">>,{pkg,<<"yamerl">>,<<"0.7.0">>},0}]}.
[
{pkg_hash,[
{<<"cowlib">>, <<"BBD58EF537904E4F7C1DD62E6AA8BC831C8183CE4EFA9BD1150164FE15BE4CAA">>},
{<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>},
{<<"jsx">>, <<"D2F6E5F069C00266CAD52FB15D87C428579EA4D7D73A33669E12679E203329DD">>},
{<<"lager">>, <<"897EFC7679BB82383448646C96768CDC4E747464DD18B999C7AACA485686B0DA">>},
{<<"ranch">>, <<"F04166F456790FEE2AC1AA05A02745CC75783C2BFB26D39FAF6AEFC9A3D3A58A">>},
{<<"redbug">>, <<"9153EE50E42C39CE3F6EFA65EE746F4A52896DA66862CFB59E7C0F838B7B8414">>}]}
{<<"redbug">>, <<"9153EE50E42C39CE3F6EFA65EE746F4A52896DA66862CFB59E7C0F838B7B8414">>},
{<<"yamerl">>, <<"E51DBA652DCE74C20A88294130B48051EBBBB0BE7D76F22DE064F0F3CCF0AAF5">>}]}
].
1 change: 1 addition & 0 deletions src/erlang_ls.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
, jsx
, cowlib
, lager
, yamerl
]},
{env,[]},
{modules, []},
Expand Down
41 changes: 28 additions & 13 deletions src/erlang_ls_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
%%==============================================================================
-type state() :: #state{}.

%%==============================================================================
%% Macros
%%==============================================================================
-define(DEFAULT_CONFIG_PATH, "erlang_ls.config").

%%==============================================================================
%% ranch_protocol callbacks
%%==============================================================================
Expand Down Expand Up @@ -127,10 +132,12 @@ handle_method(<<"initialize">>, Params) ->
#{ <<"rootUri">> := RootUri
, <<"initializationOptions">> := InitOptions
} = Params,
Config = erlang_config(InitOptions, RootUri),
OtpPath = proplists:get_value(otp_path, Config, code:root_dir()),
ok = erlang_ls_buffer_server:set_otp_path(OtpPath),
ok = erlang_ls_buffer_server:set_root_uri(RootUri),
Config = consult_config(filename:join([ erlang_ls_uri:path(RootUri)
, config_path(InitOptions)
])),
OtpPath = maps:get(<<"otp_path">>, Config, code:root_dir()),
ok = erlang_ls_buffer_server:set_otp_path(OtpPath),
Result = #{ capabilities =>
#{ hoverProvider => false
, completionProvider =>
Expand Down Expand Up @@ -217,13 +224,21 @@ send_notification(Socket, Method, Params) ->
lager:debug("[SERVER] Sending notification [notification=~p]", [Notification]),
gen_tcp:send(Socket, Notification).

-spec erlang_config(map(), binary()) -> [term()].
erlang_config(#{<<"erlang">> := #{<<"config">> := Filename}}, RootUri) ->
RootPath = erlang_ls_uri:path(RootUri),
Path = filename:join([RootPath, Filename]),
lager:info("Reading config file from ~p", [Path]),
%% TODO: check the file exits
{ok, Terms} = file:consult(Path),
Terms;
erlang_config(_, _) ->
[].
-spec config_path(map()) -> erlang_ls_uri:path().
config_path(#{<<"erlang">> := #{<<"config_path">> := ConfigPath}}) ->
ConfigPath;
config_path(_) ->
?DEFAULT_CONFIG_PATH.

-spec consult_config(erlang_ls_uri:path()) -> [any()].
consult_config(Path) ->
lager:info("Reading config file. path=~p", [Path]),
Options = [{map_node_format, map}],
try yamerl:decode_file(Path, Options) of
[Config] -> Config
catch
Class:Error ->
lager:warning( "Error reading config file. path=~p class=~p error=~p"
, [Path, Class, Error]),
#{}
end.
1 change: 1 addition & 0 deletions test/prop_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ setup() ->
meck:expect(erlang_ls_compiler_diagnostics, diagnostics, 1, []),
meck:expect(erlang_ls_dialyzer_diagnostics, diagnostics, 1, []),
application:ensure_all_started(erlang_ls),
file:write_file("/tmp/erlang_ls.config", <<"">>),
lager:set_loglevel(lager_console_backend, warning),
ok.

Expand Down

0 comments on commit cd76473

Please sign in to comment.