-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathels_crossref_diagnostics.erl
242 lines (224 loc) · 8.05 KB
/
els_crossref_diagnostics.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
%%==============================================================================
%% Crossref diagnostics
%% Like xref, but using the internal indexing results
%%==============================================================================
-module(els_crossref_diagnostics).
%%==============================================================================
%% Behaviours
%%==============================================================================
-behaviour(els_diagnostics).
%%==============================================================================
%% Exports
%%==============================================================================
-export([
is_default/0,
run/1,
source/0
]).
-type missing_reason() :: module | function | export.
%%==============================================================================
%% Includes
%%==============================================================================
-include("els_lsp.hrl").
-include_lib("kernel/include/logger.hrl").
%%==============================================================================
%% Callback Functions
%%==============================================================================
-spec is_default() -> boolean().
is_default() ->
false.
-spec run(uri()) -> [els_diagnostics:diagnostic()].
run(Uri) ->
EnabledDiagnostics = els_diagnostics:enabled_diagnostics(),
CompilerEnabled = lists:member(<<"compiler">>, EnabledDiagnostics),
Start = erlang:monotonic_time(millisecond),
Res =
case els_utils:lookup_document(Uri) of
{error, _Error} ->
[];
{ok, Document} ->
POIs = els_dt_document:pois(Document, kinds()),
Opts = #{
compiler_enabled => CompilerEnabled
},
{Diags, _Cache} =
lists:mapfoldl(
fun(#{id := Id} = POI, Cache) ->
case find_in_cache(Id, Cache) of
{ok, HasDef} ->
{[make_diagnostic(HasDef, POI)], Cache};
error ->
HasDef = has_definition(POI, Document, Opts),
{
make_diagnostic(HasDef, POI),
update_cache(HasDef, Id, Cache)
}
end
end,
#{},
POIs
),
lists:flatten(Diags)
end,
End = erlang:monotonic_time(millisecond),
Duration = End - Start,
?LOG_DEBUG("Crossref done for ~p [duration: ~p ms]", [els_uri:module(Uri), Duration]),
Res.
-spec source() -> binary().
source() ->
<<"CrossRef">>.
%%==============================================================================
%% Internal Functions
%%==============================================================================
-spec update_cache(true | {missing, function | module}, els_poi:poi_id(), map()) -> map().
update_cache({missing, module}, {M, _F, _A}, Cache) ->
%% Cache missing module to avoid repeated lookups
Cache#{M => missing};
update_cache(HasDef, Id, Cache) ->
Cache#{Id => HasDef}.
-spec find_in_cache(els_poi:poi_id(), map()) -> _.
find_in_cache({M, _F, _A}, Cache) when is_map_key(M, Cache) ->
{ok, {missing, module}};
find_in_cache(Id, Cache) ->
maps:find(Id, Cache).
-spec kinds() -> [els_poi:poi_kind()].
kinds() ->
[
application,
implicit_fun,
import_entry,
export_entry,
nifs_entry
].
-spec make_diagnostic(_, els_poi:poi()) -> [els_diagnostics:diagnostic()].
make_diagnostic({missing, Kind}, #{id := Id} = POI) ->
Message = error_msg(Kind, Id),
Severity = ?DIAGNOSTIC_ERROR,
[
els_diagnostics:make_diagnostic(
els_protocol:range(range(Kind, POI)),
Message,
Severity,
source()
)
];
make_diagnostic(true, _) ->
[].
-spec range(missing_reason(), els_poi:poi()) -> els_poi:poi_range().
range(module, #{data := #{mod_range := Range}}) ->
Range;
range(function, #{data := #{name_range := Range}}) ->
Range;
range(export, #{data := #{name_range := Range}}) ->
Range;
range(_, #{range := Range}) ->
Range.
-spec error_msg(missing_reason(), els_poi:poi_id()) -> binary().
error_msg(module, {M, _F, _A}) ->
els_utils:to_binary(io_lib:format("Cannot find module ~p", [M]));
error_msg(function, Id) ->
els_utils:to_binary(io_lib:format("Cannot find definition for function ~s", [id_str(Id)]));
error_msg(export, Id) ->
els_utils:to_binary(io_lib:format("Function ~s is not exported.", [id_str(Id)])).
-spec id_str(els_poi:poi_id()) -> string().
id_str(Id) ->
case Id of
{F, A} -> lists:flatten(io_lib:format("~p/~p", [F, A]));
{M, F, A} -> lists:flatten(io_lib:format("~p:~p/~p", [M, F, A]))
end.
-spec has_definition(els_poi:poi(), els_dt_document:item(), _) ->
true | {missing, missing_reason()}.
has_definition(#{data := #{imported := true}}, _Document, _Opts) ->
%% Call to a bif
true;
has_definition(#{id := {module_info, 0}}, _, _) ->
true;
has_definition(#{id := {module_info, 1}}, _, _) ->
true;
has_definition(#{data := #{mod_is_variable := true}}, _, _) ->
true;
has_definition(#{data := #{fun_is_variable := true}}, _, _) ->
true;
has_definition(#{id := {Module, module_info, Arity}}, _, _) when Arity =:= 0; Arity =:= 1 ->
case els_dt_document_index:lookup(Module) of
{ok, []} ->
{missing, module};
{ok, _} ->
true
end;
has_definition(#{id := {record_info, 2}}, _, _) ->
true;
has_definition(#{id := {behaviour_info, 1}}, _, _) ->
true;
has_definition(#{id := {lager, Level, Arity}}, _, _) ->
lager_definition(Level, Arity);
has_definition(#{id := {lists, append, 1}}, _, _) ->
%% lists:append/1 isn't indexed for some reason
true;
has_definition(
#{id := {F, A}} = POI,
Document,
#{
%% Compiler already checks local function calls
compiler_enabled := false
}
) ->
Uri = els_dt_document:uri(Document),
MFA = {els_uri:module(Uri), F, A},
case function_lookup(MFA) of
true ->
true;
{missing, export} ->
true;
{missing, function} ->
case els_code_navigation:goto_definition(Uri, POI) of
{ok, _Defs} ->
true;
{error, _Error} ->
{missing, function}
end
end;
has_definition(#{id := {M, _F, _A} = MFA} = POI, _Document, _Opts) ->
case function_lookup(MFA) of
true ->
true;
{missing, export} ->
{missing, export};
{missing, function} ->
case els_utils:find_module(M) of
{ok, Uri} ->
case els_code_navigation:goto_definition(Uri, POI) of
{ok, _Defs} ->
function_lookup(MFA);
{error, _Error} ->
{missing, function}
end;
{error, _} ->
{missing, module}
end
end;
has_definition(_POI, #{uri := _Uri}, _Opts) ->
true.
-spec function_lookup(mfa()) -> true | {missing, missing_reason()}.
function_lookup(MFA) ->
case els_dt_functions:lookup(MFA) of
{ok, []} ->
{missing, function};
{ok, [#{is_exported := true}]} ->
true;
{ok, [#{is_exported := false}]} ->
{missing, export}
end.
-spec lager_definition(atom(), integer()) -> boolean().
lager_definition(Level, Arity) when Arity =:= 1 orelse Arity =:= 2 ->
case lists:member(Level, lager_levels()) of
true ->
true;
false ->
{missing, function}
end;
lager_definition(_, _) ->
{missing, function}.
-spec lager_levels() -> [atom()].
lager_levels() ->
[debug, debug_unsafe, info, notice, warning, error, critical, alert, emergency].