Skip to content

Commit

Permalink
Move init diagnostics into init of els_diagnostics_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
vkatsuba committed Dec 10, 2021
1 parent 652bc58 commit 6c0a23b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
23 changes: 18 additions & 5 deletions apps/els_lsp/src/els_diagnostics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
%%==============================================================================
%% API
%%==============================================================================
-export([ available_diagnostics/0
-export([ init/0
, available_diagnostics/0
, default_diagnostics/0
, enabled_diagnostics/0
, make_diagnostic/4
Expand All @@ -56,6 +57,15 @@
%% API
%%==============================================================================

-spec init() -> ok.
init() ->
case els_config:get(diagnostics) of
undefined ->
ok;
_ ->
init_diagnostics(enabled_diagnostics())
end.

-spec available_diagnostics() -> [diagnostic_id()].
available_diagnostics() ->
[ <<"bound_var_in_pattern">>
Expand Down Expand Up @@ -118,7 +128,6 @@ 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 +159,15 @@ valid(Ids0) ->
end,
Valid.

-spec init_diagnostic(atom()) -> ok.
init_diagnostic(CbModule) ->
-spec init_diagnostics(binary()) -> ok.
init_diagnostics([]) ->
ok;
init_diagnostics([Id|T]) ->
CbModule = cb_module(Id),
case erlang:function_exported(CbModule, init, 0) of
true ->
CbModule:init();
false ->
ok
end.
end,
init_diagnostics(T).
1 change: 1 addition & 0 deletions apps/els_lsp/src/els_diagnostics_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ options() ->

-spec init() -> state().
init() ->
ok = els_diagnostics:init(),
#{ in_progress => [] }.

%% LSP 3.15 introduce versioning for diagnostics. Until all clients
Expand Down
80 changes: 44 additions & 36 deletions apps/els_lsp/test/els_diagnostics_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,23 @@ init_per_testcase(TestCase, Config) when TestCase =:= gradualizer ->
els_mock_diagnostics:setup(),
els_test_utils:init_per_testcase(TestCase, Config);
init_per_testcase(TestCase, Config) when TestCase =:= sheldon ->
meck:new(els_sheldon_diagnostics, [passthrough, no_link]),
meck:expect(els_sheldon_diagnostics, is_default, 0, true),
els_mock_diagnostics:setup(),
els_test_utils:init_per_testcase(TestCase, Config);
case list_to_integer(erlang:system_info(otp_release)) >= 23 of
true ->
meck:new(els_sheldon_diagnostics, [passthrough, no_link]),
meck:expect(els_sheldon_diagnostics, is_default, 0, true),
els_mock_diagnostics:setup(),
meck:expect( els_diagnostics_provider
, init
, fun() ->
DiagnosticsConfig = #{"enabled" => [<<"sheldon">>]},
els_config:set(diagnostics, DiagnosticsConfig),
meck:passthrough([])
end
),
els_test_utils:init_per_testcase(TestCase, Config);
false ->
{skip, "Sheldon diagnostics should run on OTP23+"}
end;
init_per_testcase(TestCase, Config) ->
els_mock_diagnostics:setup(),
els_test_utils:init_per_testcase(TestCase, Config).
Expand Down Expand Up @@ -667,38 +680,33 @@ gradualizer(_Config) ->

-spec sheldon(config()) -> ok.
sheldon(_Config) ->
case list_to_integer(erlang:system_info(otp_release)) >= 23 of
true ->
{ok, Cwd} = file:get_cwd(),
RootPath = els_test_utils:root_path(),
try
file:set_cwd(RootPath),
Path = src_path("diagnostics_sheldon.erl"),
Source = <<"Sheldon">>,
Errors = [],
Warnings = [ #{ code => spellcheck
, message => <<"The word \"sheldon\" in "
"comment is unknown. "
"Maybe you wanted to use \"Sheldon\"?">>
, range => {{2, 0}, {3, 0}}
, relatedInformation => []
}
, #{ code => spellcheck
, message => <<"The word \"somestrange\" in comment is "
"unknown.">>
, range => {{0, 0}, {1, 0}}
, relatedInformation => []
}
],
Hints = [],
els_test:run_diagnostics_test(Path, Source, Errors, Warnings, Hints)
catch _Err ->
file:set_cwd(Cwd)
end,
ok;
false ->
{skipped, "Sheldon diagnostics should run on OTP23+"}
end.
{ok, Cwd} = file:get_cwd(),
RootPath = els_test_utils:root_path(),
try
file:set_cwd(RootPath),
Path = src_path("diagnostics_sheldon.erl"),
Source = <<"Sheldon">>,
Errors = [],
Warnings = [ #{ code => spellcheck
, message => <<"The word \"sheldon\" in "
"comment is unknown. "
"Maybe you wanted to use \"Sheldon\"?">>
, range => {{2, 0}, {3, 0}}
, relatedInformation => []
}
, #{ code => spellcheck
, message => <<"The word \"somestrange\" in comment is "
"unknown.">>
, range => {{0, 0}, {1, 0}}
, relatedInformation => []
}
],
Hints = [],
els_test:run_diagnostics_test(Path, Source, Errors, Warnings, Hints)
catch _Err ->
file:set_cwd(Cwd)
end,
ok.

%%==============================================================================
%% Internal Functions
Expand Down

0 comments on commit 6c0a23b

Please sign in to comment.