Skip to content

Commit

Permalink
[#22] Convert indexer into gen_server
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoaloi committed Jul 16, 2019
1 parent d7e934c commit 09b5058
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
87 changes: 82 additions & 5 deletions src/erlang_ls_indexer.erl
@@ -1,16 +1,92 @@
%%==============================================================================
%% The Buffer Server
%%==============================================================================
-module(erlang_ls_indexer).

-export([ index/1 ]).
%%==============================================================================
%% Behaviours
%%==============================================================================
-behaviour(gen_server).

-spec index(string()) -> map().
index(Path) ->
{ok, IoDevice} = file:open(Path, [read]),
%%==============================================================================
%% Exports
%%==============================================================================
%% API
-export([ start_link/0
, index/1
, stop/0
]).

%% gen_server callbacks
-export([ init/1
, handle_call/3
, handle_cast/2
]).

%%==============================================================================
%% Includes
%%==============================================================================
-include("erlang_ls.hrl").

%%==============================================================================
%% Defines
%%==============================================================================
-define(SERVER, ?MODULE).

%%==============================================================================
%% Record Definitions
%%==============================================================================
-record(state, {}).

%%==============================================================================
%% Type Definitions
%%==============================================================================
-type state() :: #state{}.

%%%=============================================================================
%%% API
%%%=============================================================================
-spec start_link() -> {ok, pid()}.
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, {}, []).

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

-spec stop() -> ok.
stop() ->
gen_server:stop(?SERVER).

%%==============================================================================
%% gen_server Callback Functions
%%==============================================================================
-spec init({}) -> {ok, state()}.
init({}) ->
{ok, #state{}}.

-spec handle_call(any(), any(), state()) -> {reply, ok, state()}.
handle_call({index, Uri}, _From, State) ->
ok = do_index(Uri),
{reply, ok, State}.

-spec handle_cast(any(), state()) -> {noreply, state()}.
handle_cast(_Msg, State) -> {noreply, State}.

%%==============================================================================
%% Internal Functions
%%==============================================================================
-spec do_index(uri()) -> ok.
do_index(Uri) ->
{ok, IoDevice} = file:open(Uri, [read]),
{ok, Forms} = epp_dodger:parse(IoDevice, {1, 1}),
erl_syntax_lib:mapfold(fun analyze_form/2, #{ module => undefined
, macros => []
, variables => []
, applications => []
}, erl_syntax:form_list(Forms)).
}, erl_syntax:form_list(Forms)),
ok = file:close(IoDevice),
ok.

-spec analyze_form(erl_syntax:syntax_tree(), map()) -> map().
analyze_form(Form, Acc) ->
Expand Down Expand Up @@ -71,3 +147,4 @@ analyze_tree(_Form, _Type, Acc) ->
%% TODO: Assume OTP structure on init
%% TODO: Function to append to map field
%% TODO: We should input binaries, not files. Could we patch epp?
%% TODO: Avoid epp parsing twice
4 changes: 4 additions & 0 deletions src/erlang_ls_sup.erl
Expand Up @@ -47,5 +47,9 @@ init([]) ->
, start => {erlang_ls_buffer_sup, start_link, []}
, shutdown => brutal_kill
}
, #{ id => erlang_ls_indexer
, start => {erlang_ls_indexer, start_link, []}
, shutdown => brutal_kill
}
],
{ok, {SupFlags, ChildSpecs}}.

0 comments on commit 09b5058

Please sign in to comment.