Skip to content

Commit

Permalink
Add init callback to diagnostics behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
vkatsuba committed Dec 9, 2021
1 parent 630156c commit 652bc58
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
24 changes: 12 additions & 12 deletions apps/els_lsp/src/els_diagnostics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
-callback run(uri()) -> [diagnostic()].
-callback source() -> binary().
-callback on_complete(uri(), [diagnostic()]) -> ok.
-optional_callbacks([ on_complete/2 ]).
-callback init() -> ok.
-optional_callbacks([ on_complete/2, init/0 ]).

%%==============================================================================
%% API
Expand Down Expand Up @@ -79,9 +80,7 @@ enabled_diagnostics() ->
Default = default_diagnostics(),
Enabled = maps:get("enabled", Config, []),
Disabled = maps:get("disabled", Config, []),
Diagnostics = lists:usort((Default ++ valid(Enabled)) -- valid(Disabled)),
ok = extra(Diagnostics),
Diagnostics.
lists:usort((Default ++ valid(Enabled)) -- valid(Disabled)).

-spec make_diagnostic(range(), binary(), severity(), binary()) -> diagnostic().
make_diagnostic(Range, Message, Severity, Source) ->
Expand Down Expand Up @@ -119,6 +118,7 @@ run_diagnostic(Uri, Id) ->
els_diagnostics_provider:notify(Diagnostics, self())
end
},
ok = init_diagnostic(CbModule),
{ok, Pid} = els_background_job:new(Config),
Pid.

Expand Down Expand Up @@ -150,11 +150,11 @@ valid(Ids0) ->
end,
Valid.

-spec extra(list()) -> ok.
extra([]) ->
ok;
extra([<<"sheldon">>|_]) ->
{ok, _} = application:ensure_all_started(sheldon),
ok;
extra([_|T]) ->
extra(T).
-spec init_diagnostic(atom()) -> ok.
init_diagnostic(CbModule) ->
case erlang:function_exported(CbModule, init, 0) of
true ->
CbModule:init();
false ->
ok
end.
19 changes: 15 additions & 4 deletions apps/els_lsp/src/els_sheldon_diagnostics.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%==============================================================================
%% Compiler diagnostics
%% Sheldon diagnostics
%%==============================================================================

-module(els_sheldon_diagnostics).
Expand All @@ -14,7 +14,8 @@
%% Exports
%%==============================================================================

-export([ is_default/0
-export([ init/0
, is_default/0
, run/1
, source/0
]).
Expand All @@ -30,6 +31,14 @@
%% Callback Functions
%%==============================================================================

-spec init() -> ok.
init() ->
%% By default "sheldon" is not started by reason that he spend few seconds
%% to prepare and load dictionary. The "sheldon" shoulld be load only once
%% when diagnostic is enabled.
application:ensure_all_started(sheldon),
ok.

-spec is_default() -> boolean().
is_default() ->
false.
Expand All @@ -45,8 +54,10 @@ run(Uri) ->
of
[] -> [];
Problems -> format_diagnostics(Problems)
catch _:Err ->
?LOG_WARNING("Sheldon error.[Err=~p] ", [Err]),
catch Type:Error:Stacktrace ->
?LOG_WARNING( "Sheldon error: [type=~p] [error=~p] [stacktrace=~p]"
, [Type, Error, Stacktrace]
),
[]
end
end.
Expand Down

0 comments on commit 652bc58

Please sign in to comment.