-
Notifications
You must be signed in to change notification settings - Fork 194
/
locator.ex
291 lines (251 loc) · 7.78 KB
/
locator.ex
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# This code has originally been a part of https://github.com/elixir-lsp/elixir_sense
# Copyright (c) 2017 Marlus Saraiva
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do
@moduledoc """
Provides a function to find out where symbols are defined.
Currently finds definition of modules, functions and macros,
typespecs, variables and attributes.
"""
alias ElixirSense.Core.Binding
alias ElixirSense.Core.Introspection
alias ElixirSense.Core.Metadata
alias ElixirSense.Core.State
alias ElixirSense.Core.State.ModFunInfo
alias ElixirSense.Core.State.TypeInfo
alias ElixirSense.Core.State.VarInfo
alias ElixirSense.Core.Source
alias ElixirSense.Core.SurroundContext
alias ElixirLS.LanguageServer.Location
alias ElixirSense.Core.Parser
alias ElixirLS.LanguageServer.Plugins.Phoenix.Scope
alias ElixirSense.Core.Normalized.Code, as: NormalizedCode
def definition(code, line, column, options \\ []) do
case NormalizedCode.Fragment.surround_context(code, {line, column}) do
:none ->
nil
context ->
metadata =
Keyword.get_lazy(options, :metadata, fn ->
Parser.parse_string(code, true, true, {line, column})
end)
env =
Metadata.get_env(metadata, {line, column})
|> Metadata.add_scope_vars(metadata, {line, column})
find(
context,
env,
metadata
)
end
end
@doc """
Finds out where a module, function, macro or variable was defined.
"""
@spec find(
any(),
State.Env.t(),
Metadata.t()
) :: %Location{} | nil
def find(
context,
%State.Env{
module: module,
vars: vars,
attributes: attributes
} = env,
metadata
) do
binding_env = Binding.from_env(env, metadata)
type = SurroundContext.to_binding(context.context, module)
case type do
nil ->
nil
{:keyword, _} ->
nil
{:variable, variable} ->
var_info =
vars
|> Enum.find(fn
%VarInfo{name: name, positions: positions} ->
name == variable and context.begin in positions
end)
if var_info != nil do
{definition_line, definition_column} = Enum.min(var_info.positions)
%Location{type: :variable, file: nil, line: definition_line, column: definition_column}
else
find_function_or_module(
{nil, variable},
context,
env,
metadata,
binding_env
)
end
{:attribute, attribute} ->
attribute_info =
Enum.find(attributes, fn
%State.AttributeInfo{name: name} -> name == attribute
end)
if attribute_info != nil do
%State.AttributeInfo{positions: [{line, column} | _]} = attribute_info
%Location{type: :attribute, file: nil, line: line, column: column}
end
{module, function} ->
find_function_or_module(
{module, function},
context,
env,
metadata,
binding_env
)
end
end
defp find_function_or_module(
target,
context,
env,
metadata,
binding_env,
visited \\ []
) do
unless target in visited do
do_find_function_or_module(
target,
context,
env,
metadata,
binding_env,
[target | visited]
)
end
end
defp do_find_function_or_module(
{{kind, _} = type, function},
context,
env,
metadata,
binding_env,
visited
)
when kind in [:attribute, :variable] do
case Binding.expand(binding_env, type) do
{:atom, module} ->
do_find_function_or_module(
{{:atom, module}, function},
context,
env,
metadata,
binding_env,
visited
)
_ ->
nil
end
end
defp do_find_function_or_module(
{nil, :super},
context,
%State.Env{function: {function, arity}, module: module} = env,
metadata,
binding_env,
visited
) do
case metadata.mods_funs_to_positions[{module, function, arity}] do
%ModFunInfo{overridable: {true, origin}} ->
# overridable function is most likely defined by __using__ macro
do_find_function_or_module(
{{:atom, origin}, :__using__},
context,
env,
metadata,
binding_env,
visited
)
_ ->
nil
end
end
defp do_find_function_or_module(
{module, function},
context,
env,
metadata,
_binding_env,
_visited
) do
m = get_module(module, context, env, metadata)
case {m, function}
|> Introspection.actual_mod_fun(
env,
metadata.mods_funs_to_positions,
metadata.types,
context.begin,
true
) do
{_, _, false, _} ->
nil
{mod, fun, true, :mod_fun} ->
{line, column} = context.end
call_arity = Metadata.get_call_arity(metadata, mod, fun, line, column) || :any
fn_definition =
Location.get_function_position_using_metadata(
mod,
fun,
call_arity,
metadata.mods_funs_to_positions
)
case fn_definition do
nil ->
Location.find_mod_fun_source(mod, fun, call_arity)
%ModFunInfo{positions: positions} = mi ->
# for simplicity take last position here as positions are reversed
{line, column} = positions |> Enum.at(-1)
%Location{
file: nil,
type: ModFunInfo.get_category(mi),
line: line,
column: column
}
end
{mod, fun, true, :type} ->
{line, column} = context.end
call_arity = Metadata.get_call_arity(metadata, mod, fun, line, column) || :any
type_definition =
Location.get_type_position_using_metadata(mod, fun, call_arity, metadata.types)
case type_definition do
nil ->
Location.find_type_source(mod, fun, call_arity)
%TypeInfo{positions: positions} ->
# for simplicity take last position here as positions are reversed
{line, column} = positions |> Enum.at(-1)
%Location{
file: nil,
type: :typespec,
line: line,
column: column
}
end
end
end
defp get_module(module, %{end: {line, col}}, env, metadata) do
with {true, module} <- get_phoenix_module(module, env),
true <- Introspection.elixir_module?(module) do
text_before = Source.text_before(metadata.source, line, col)
case Scope.within_scope(text_before) do
{false, _} ->
module
{true, scope_alias} ->
Module.concat(scope_alias, module)
end
end
end
defp get_phoenix_module(module, env) do
case {Phoenix.Router in env.requires, module} do
{true, {:atom, module}} -> {true, module}
{false, {:atom, module}} -> module
_ -> nil
end
end
end