Skip to content

Commit

Permalink
[#60] Add support for deps_dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoaloi committed Sep 20, 2019
1 parent cd76473 commit bf893be
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,15 @@ 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"
deps_dirs:
- "lib/*"

Currently, the following customizations are possible:

| Parameter | Description |
|-----------|------------------------------|
| otp_path | Path to the OTP installation |
| | |
| Parameter | Description |
|------------|---------------------------------------------------------------------|
| otp\_path | Path to the OTP installation |
| deps\_dirs | List of directories containing dependencies. It supports wildcards. |

## Troubleshooting

Expand Down
50 changes: 34 additions & 16 deletions src/erlang_ls_buffer_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
-export([ start_link/0
, add_buffer/2
, get_buffer/1
, get_root_uri/0
, get_deps_dirs/0
, get_otp_path/0
, get_root_uri/0
, remove_buffer/1
, set_root_uri/1
, set_deps_dirs/1
, set_otp_path/1
, set_root_uri/1
, stop/0
]).

Expand All @@ -45,7 +47,11 @@
%%==============================================================================
%% Record Definitions
%%==============================================================================
-record(state, { buffers, root_uri, otp_path }).
-record(state, { buffers
, deps_dirs = []
, root_uri
, otp_path
}).

%%==============================================================================
%% Type Definitions
Expand All @@ -69,27 +75,34 @@ add_buffer(Uri, Buffer) ->
get_buffer(Uri) ->
gen_server:call(?SERVER, {get_buffer, Uri}).

-spec get_root_uri() -> {ok, erlang_ls_uri:uri() | undefined}.
get_root_uri() ->
gen_server:call(?SERVER, {get_root_uri}).
-spec get_deps_dirs() -> {ok, [path()]}.
get_deps_dirs() ->
gen_server:call(?SERVER, {get_deps_dirs}).

-spec get_otp_path() -> {ok, path() | undefined}.
get_otp_path() ->
gen_server:call(?SERVER, {get_otp_path}).

-spec get_root_uri() -> {ok, erlang_ls_uri:uri() | undefined}.
get_root_uri() ->
gen_server:call(?SERVER, {get_root_uri}).

-spec remove_buffer(erlang_ls_uri:uri()) -> ok.
remove_buffer(Uri) ->
gen_server:call(?SERVER, {remove_buffer, Uri}).

-spec set_root_uri(erlang_ls_uri:uri()) -> ok.
set_root_uri(Uri) ->
gen_server:call(?SERVER, {set_root_uri, Uri}).
-spec set_deps_dirs([erlang_ls_uri:path()]) -> ok.
set_deps_dirs(DepsDirs) ->
gen_server:call(?SERVER, {set_deps_dirs, DepsDirs}).

%% TODO: move it to a separate config server
-spec set_otp_path(erlang_ls_uri:uri()) -> ok.
set_otp_path(Uri) ->
gen_server:call(?SERVER, {set_otp_path, Uri}).

-spec set_root_uri(erlang_ls_uri:uri()) -> ok.
set_root_uri(Uri) ->
gen_server:call(?SERVER, {set_root_uri, Uri}).

-spec stop() -> ok.
stop() ->
gen_server:stop(?SERVER).
Expand All @@ -107,19 +120,24 @@ handle_call({add_buffer, Uri, Buffer}, _From, State) ->
handle_call({get_buffer, Uri}, _From, State) ->
Buffer = proplists:get_value(Uri, State#state.buffers),
{reply, {ok, Buffer}, State};
handle_call({get_root_uri}, _From, State) ->
RootUri = State#state.root_uri,
{reply, {ok, RootUri}, State};
handle_call({get_deps_dirs}, _From, State) ->
DepsDirs = State#state.deps_dirs,
{reply, {ok, DepsDirs}, State};
handle_call({get_otp_path}, _From, State) ->
OtpPath = State#state.otp_path,
{reply, {ok, OtpPath}, State};
handle_call({get_root_uri}, _From, State) ->
RootUri = State#state.root_uri,
{reply, {ok, RootUri}, State};
handle_call({remove_buffer, Uri}, _From, State) ->
Buffers = proplists:delete(Uri, State#state.buffers),
{reply, ok, State#state{buffers = Buffers}};
handle_call({set_root_uri, Uri}, _From, State) ->
{reply, ok, State#state{root_uri = Uri}};
handle_call({set_deps_dirs, DepsDirs}, _From, State) ->
{reply, ok, State#state{deps_dirs = DepsDirs}};
handle_call({set_otp_path, Path}, _From, State) ->
{reply, ok, State#state{otp_path = Path}}.
{reply, ok, State#state{otp_path = Path}};
handle_call({set_root_uri, Uri}, _From, State) ->
{reply, ok, State#state{root_uri = Uri}}.

-spec handle_cast(any(), state()) -> {noreply, state()}.
handle_cast(_Msg, State) -> {noreply, State}.
Expand Down
20 changes: 16 additions & 4 deletions src/erlang_ls_code_navigation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
%% API
-export([ goto_definition/2 ]).

-export([ otp_path/0
]).

%%==============================================================================
%% Includes
%%==============================================================================
Expand Down Expand Up @@ -138,9 +135,24 @@ app_path() ->
, filename:join([RootPath, "include"])
].

-spec deps_path() -> [string()].
deps_path() ->
{ok, RootUri} = erlang_ls_buffer_server:get_root_uri(),
RootPath = binary_to_list(erlang_ls_uri:path(RootUri)),
{ok, Dirs} = erlang_ls_buffer_server:get_deps_dirs(),
lists:foldl(fun(Dir, Acc) ->
Sources = filename:join([RootPath, Dir, "src"]),
Includes = filename:join([RootPath, Dir, "include"]),
lists:append([ filelib:wildcard(Sources)
, filelib:wildcard(Includes)
, Acc
])
end
, [], Dirs).

-spec include_path() -> [string()].
include_path() ->
lists:append( [ app_path(), otp_path() ]).
lists:append( [ app_path(), otp_path(), deps_path() ]).

%% Look for a definition recursively in a file and its includes.
-spec search(binary(), [string()], any()) ->
Expand Down
9 changes: 6 additions & 3 deletions src/erlang_ls_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ handle_method(<<"initialize">>, Params) ->
Config = consult_config(filename:join([ erlang_ls_uri:path(RootUri)
, config_path(InitOptions)
])),
OtpPath = maps:get(<<"otp_path">>, Config, code:root_dir()),
OtpPath = maps:get("otp_path", Config, code:root_dir()),
DepsDirs = maps:get("deps_dirs", Config, []),
ok = erlang_ls_buffer_server:set_otp_path(OtpPath),
ok = erlang_ls_buffer_server:set_deps_dirs(DepsDirs),
Result = #{ capabilities =>
#{ hoverProvider => false
, completionProvider =>
Expand Down Expand Up @@ -230,12 +232,13 @@ config_path(#{<<"erlang">> := #{<<"config_path">> := ConfigPath}}) ->
config_path(_) ->
?DEFAULT_CONFIG_PATH.

-spec consult_config(erlang_ls_uri:path()) -> [any()].
-spec consult_config(erlang_ls_uri:path()) -> map().
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
[] -> #{};
[Config] -> Config
catch
Class:Error ->
lager:warning( "Error reading config file. path=~p class=~p error=~p"
Expand Down

0 comments on commit bf893be

Please sign in to comment.