Skip to content

Commit 9839af8

Browse files
committed
Move init diagnostics into init of els_diagnostics_provider
1 parent 652bc58 commit 9839af8

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

apps/els_lsp/src/els_diagnostics.erl

+18-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
%%==============================================================================
4646
%% API
4747
%%==============================================================================
48-
-export([ available_diagnostics/0
48+
-export([ init/0
49+
, available_diagnostics/0
4950
, default_diagnostics/0
5051
, enabled_diagnostics/0
5152
, make_diagnostic/4
@@ -56,6 +57,15 @@
5657
%% API
5758
%%==============================================================================
5859

60+
-spec init() -> ok.
61+
init() ->
62+
case els_config:get(diagnostics) of
63+
undefined ->
64+
ok;
65+
_ ->
66+
init_diagnostics(enabled_diagnostics())
67+
end.
68+
5969
-spec available_diagnostics() -> [diagnostic_id()].
6070
available_diagnostics() ->
6171
[ <<"bound_var_in_pattern">>
@@ -118,7 +128,6 @@ run_diagnostic(Uri, Id) ->
118128
els_diagnostics_provider:notify(Diagnostics, self())
119129
end
120130
},
121-
ok = init_diagnostic(CbModule),
122131
{ok, Pid} = els_background_job:new(Config),
123132
Pid.
124133

@@ -150,11 +159,15 @@ valid(Ids0) ->
150159
end,
151160
Valid.
152161

153-
-spec init_diagnostic(atom()) -> ok.
154-
init_diagnostic(CbModule) ->
162+
-spec init_diagnostics(binary()) -> ok.
163+
init_diagnostics([]) ->
164+
ok;
165+
init_diagnostics([Id|T]) ->
166+
CbModule = cb_module(Id),
155167
case erlang:function_exported(CbModule, init, 0) of
156168
true ->
157169
CbModule:init();
158170
false ->
159171
ok
160-
end.
172+
end,
173+
init_diagnostics(T).

apps/els_lsp/src/els_diagnostics_provider.erl

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ options() ->
4242

4343
-spec init() -> state().
4444
init() ->
45+
ok = els_diagnostics:init(),
4546
#{ in_progress => [] }.
4647

4748
%% LSP 3.15 introduce versioning for diagnostics. Until all clients

apps/els_lsp/test/els_diagnostics_SUITE.erl

+43-35
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,22 @@ init_per_testcase(TestCase, Config) when TestCase =:= gradualizer ->
126126
els_test_utils:init_per_testcase(TestCase, Config);
127127
init_per_testcase(TestCase, Config) when TestCase =:= sheldon ->
128128
meck:new(els_sheldon_diagnostics, [passthrough, no_link]),
129-
meck:expect(els_sheldon_diagnostics, is_default, 0, true),
130-
els_mock_diagnostics:setup(),
131-
els_test_utils:init_per_testcase(TestCase, Config);
129+
case list_to_integer(erlang:system_info(otp_release)) >= 23 of
130+
true ->
131+
meck:expect(els_sheldon_diagnostics, is_default, 0, true),
132+
els_mock_diagnostics:setup(),
133+
meck:expect( els_diagnostics_provider
134+
, init
135+
, fun() ->
136+
DiagnosticsConfig = #{"enabled" => [<<"sheldon">>]},
137+
els_config:set(diagnostics, DiagnosticsConfig),
138+
meck:passthrough([])
139+
end
140+
),
141+
els_test_utils:init_per_testcase(TestCase, Config);
142+
false ->
143+
{skip, "Sheldon diagnostics should run on OTP23+"}
144+
end;
132145
init_per_testcase(TestCase, Config) ->
133146
els_mock_diagnostics:setup(),
134147
els_test_utils:init_per_testcase(TestCase, Config).
@@ -667,38 +680,33 @@ gradualizer(_Config) ->
667680

668681
-spec sheldon(config()) -> ok.
669682
sheldon(_Config) ->
670-
case list_to_integer(erlang:system_info(otp_release)) >= 23 of
671-
true ->
672-
{ok, Cwd} = file:get_cwd(),
673-
RootPath = els_test_utils:root_path(),
674-
try
675-
file:set_cwd(RootPath),
676-
Path = src_path("diagnostics_sheldon.erl"),
677-
Source = <<"Sheldon">>,
678-
Errors = [],
679-
Warnings = [ #{ code => spellcheck
680-
, message => <<"The word \"sheldon\" in "
681-
"comment is unknown. "
682-
"Maybe you wanted to use \"Sheldon\"?">>
683-
, range => {{2, 0}, {3, 0}}
684-
, relatedInformation => []
685-
}
686-
, #{ code => spellcheck
687-
, message => <<"The word \"somestrange\" in comment is "
688-
"unknown.">>
689-
, range => {{0, 0}, {1, 0}}
690-
, relatedInformation => []
691-
}
692-
],
693-
Hints = [],
694-
els_test:run_diagnostics_test(Path, Source, Errors, Warnings, Hints)
695-
catch _Err ->
696-
file:set_cwd(Cwd)
697-
end,
698-
ok;
699-
false ->
700-
{skipped, "Sheldon diagnostics should run on OTP23+"}
701-
end.
683+
{ok, Cwd} = file:get_cwd(),
684+
RootPath = els_test_utils:root_path(),
685+
try
686+
file:set_cwd(RootPath),
687+
Path = src_path("diagnostics_sheldon.erl"),
688+
Source = <<"Sheldon">>,
689+
Errors = [],
690+
Warnings = [ #{ code => spellcheck
691+
, message => <<"The word \"sheldon\" in "
692+
"comment is unknown. "
693+
"Maybe you wanted to use \"Sheldon\"?">>
694+
, range => {{2, 0}, {3, 0}}
695+
, relatedInformation => []
696+
}
697+
, #{ code => spellcheck
698+
, message => <<"The word \"somestrange\" in comment is "
699+
"unknown.">>
700+
, range => {{0, 0}, {1, 0}}
701+
, relatedInformation => []
702+
}
703+
],
704+
Hints = [],
705+
els_test:run_diagnostics_test(Path, Source, Errors, Warnings, Hints)
706+
catch _Err ->
707+
file:set_cwd(Cwd)
708+
end,
709+
ok.
702710

703711
%%==============================================================================
704712
%% Internal Functions

0 commit comments

Comments
 (0)