diff --git a/apps/els_lsp/src/els_diagnostics.erl b/apps/els_lsp/src/els_diagnostics.erl index 58bd3043f..d066a767e 100644 --- a/apps/els_lsp/src/els_diagnostics.erl +++ b/apps/els_lsp/src/els_diagnostics.erl @@ -45,7 +45,8 @@ %%============================================================================== %% API %%============================================================================== --export([ available_diagnostics/0 +-export([ init/0 + , available_diagnostics/0 , default_diagnostics/0 , enabled_diagnostics/0 , make_diagnostic/4 @@ -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">> @@ -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. @@ -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). diff --git a/apps/els_lsp/src/els_diagnostics_provider.erl b/apps/els_lsp/src/els_diagnostics_provider.erl index 240d9556c..c185d5e90 100644 --- a/apps/els_lsp/src/els_diagnostics_provider.erl +++ b/apps/els_lsp/src/els_diagnostics_provider.erl @@ -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 diff --git a/apps/els_lsp/test/els_diagnostics_SUITE.erl b/apps/els_lsp/test/els_diagnostics_SUITE.erl index 2642fb926..e57b1b9d4 100644 --- a/apps/els_lsp/test/els_diagnostics_SUITE.erl +++ b/apps/els_lsp/test/els_diagnostics_SUITE.erl @@ -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). @@ -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