Skip to content

Commit bfc2312

Browse files
committed
return metadata on signatures
1 parent a6fd799 commit bfc2312

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

lib/elixir_sense/core/introspection.ex

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ defmodule ElixirSense.Core.Introspection do
150150
for {f, a} <- BuiltinFunctions.all(), f == fun do
151151
spec = BuiltinFunctions.get_specs({f, a}) |> Enum.join("\n")
152152
params = BuiltinFunctions.get_args({f, a})
153-
%{name: Atom.to_string(fun), params: params, documentation: "Built-in function", spec: spec}
153+
154+
%{
155+
name: Atom.to_string(fun),
156+
params: params,
157+
documentation: "Built-in function",
158+
spec: spec,
159+
metadata: %{builtin: true}
160+
}
154161
end
155162
end
156163

@@ -165,7 +172,7 @@ defmodule ElixirSense.Core.Introspection do
165172
doc = extract_summary_from_docs(text)
166173

167174
spec = get_spec_as_string(mod, fun, arity, kind, metadata)
168-
%{name: fun_str, params: fun_args, documentation: doc, spec: spec}
175+
%{name: fun_str, params: fun_args, documentation: doc, spec: spec, metadata: metadata}
169176
end
170177

171178
case results do
@@ -185,6 +192,7 @@ defmodule ElixirSense.Core.Introspection do
185192
defp get_spec_from_typespec(mod, fun) do
186193
# TypeInfo.get_function_specs does fallback to behaviours
187194
function_specs = TypeInfo.get_function_specs(mod, fun, :any)
195+
app = ElixirSense.Core.Applications.get_application(mod)
188196

189197
results =
190198
for {behaviour, specs} <- function_specs, {{_name, _arity}, [params | _]} = spec <- specs do
@@ -194,7 +202,8 @@ defmodule ElixirSense.Core.Introspection do
194202
name: Atom.to_string(fun),
195203
params: params,
196204
documentation: "",
197-
spec: spec |> spec_to_string(if(behaviour, do: :callback, else: :spec))
205+
spec: spec |> spec_to_string(if(behaviour, do: :callback, else: :spec)),
206+
metadata: if(behaviour, do: %{implementing: behaviour, app: app}, else: %{app: app})
198207
}
199208
end
200209

@@ -216,11 +225,20 @@ defmodule ElixirSense.Core.Introspection do
216225
f == fun do
217226
dummy_params = if a == 0, do: [], else: Enum.map(1..a, fn _ -> "term" end)
218227

228+
metadata =
229+
if {f, a} in BuiltinFunctions.erlang_builtin_functions(mod) do
230+
%{builtin: true, app: :erts}
231+
else
232+
app = ElixirSense.Core.Applications.get_application(mod)
233+
%{app: app}
234+
end
235+
219236
%{
220237
name: Atom.to_string(fun),
221238
params: dummy_params,
222239
documentation: "",
223-
spec: ""
240+
spec: "",
241+
metadata: metadata
224242
}
225243
end
226244
end

lib/elixir_sense/core/metadata.ex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ defmodule ElixirSense.Core.Metadata do
8888

8989
if Version.match?(System.version(), "< 1.15.0-dev") do
9090
# return early if cursor env already found by parser replacing line
91-
# this helps on < 1.15 and braks tests on later versions
91+
# this helps on < 1.15 and breaks tests on later versions
9292
def get_cursor_env(%__MODULE__{cursor_env: {_, env}}, _position, _surround) do
9393
env
9494
end
@@ -429,7 +429,8 @@ defmodule ElixirSense.Core.Metadata do
429429
name: Atom.to_string(function),
430430
params: args,
431431
documentation: Introspection.extract_summary_from_docs(docs),
432-
spec: spec
432+
spec: spec,
433+
metadata: %{builtin: true}
433434
}
434435
end
435436
end
@@ -459,7 +460,8 @@ defmodule ElixirSense.Core.Metadata do
459460
name: Atom.to_string(function),
460461
params: params |> Enum.with_index() |> Enum.map(&Introspection.param_to_var/1),
461462
documentation: Introspection.extract_summary_from_docs(function_info.doc),
462-
spec: spec
463+
spec: spec,
464+
metadata: function_info.meta
463465
}
464466
end)
465467
end
@@ -485,7 +487,8 @@ defmodule ElixirSense.Core.Metadata do
485487
name: Atom.to_string(type),
486488
params: type_info.args |> List.last(),
487489
documentation: Introspection.extract_summary_from_docs(type_info.doc),
488-
spec: spec
490+
spec: spec,
491+
metadata: type_info.meta
489492
}
490493
end)
491494
end
@@ -495,17 +498,17 @@ defmodule ElixirSense.Core.Metadata do
495498
NormalizedCode.callback_documentation(behaviour)
496499
|> Map.new()
497500

498-
meta = %{implementing: behaviour}
499-
spec = Introspection.get_spec_as_string(nil, f, a, kind, meta)
500501
app = ElixirSense.Core.Applications.get_application(behaviour)
502+
meta = %{implementing: behaviour, implementing_module_app: app}
503+
504+
spec = Introspection.get_spec_as_string(nil, f, a, kind, meta)
501505

502506
case docs[{f, a}] do
503507
nil ->
504508
{spec, "", meta}
505509

506510
{_signatures, docs, callback_meta, mime_type} ->
507-
{spec, docs |> NormalizedCode.extract_docs(mime_type, behaviour, app),
508-
callback_meta |> Map.merge(meta)}
511+
{spec, docs |> NormalizedCode.extract_docs(mime_type, behaviour, app), callback_meta}
509512
end
510513
end
511514

lib/elixir_sense/core/type_info.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ defmodule ElixirSense.Core.TypeInfo do
6666
def get_signatures(mod, type, code_docs) when not is_nil(mod) and not is_nil(type) do
6767
case code_docs || NormalizedCode.get_docs(mod, :type_docs) do
6868
docs when is_list(docs) ->
69-
for {{t, arity}, _, _, text, _metadata} <- docs, t == type do
69+
for {{t, arity}, _, _, text, metadata} <- docs, t == type do
7070
{_kind, {_name, _def, args}} = get_type_spec(mod, type, arity)
7171
type_args = Enum.map(args, &(&1 |> elem(2) |> Atom.to_string()))
7272
type_str = Atom.to_string(type)
7373
doc = Introspection.extract_summary_from_docs(text)
7474
typedef = get_type_spec(mod, type, arity)
7575
spec = format_type_spec(typedef, line_length: @param_option_spec_line_length)
76-
%{name: type_str, params: type_args, documentation: doc, spec: spec}
76+
%{name: type_str, params: type_args, documentation: doc, spec: spec, metadata: metadata}
7777
end
7878

7979
nil ->
80+
app = ElixirSense.Core.Applications.get_application(mod)
81+
8082
for {kind, {name, _type, args}} = typedef <- Typespec.get_types(mod),
8183
name == type,
8284
kind in [:type, :opaque] do
@@ -86,7 +88,8 @@ defmodule ElixirSense.Core.TypeInfo do
8688
name: Atom.to_string(name),
8789
params: type_args,
8890
documentation: "",
89-
spec: format_type_spec(typedef)
91+
spec: format_type_spec(typedef),
92+
metadata: %{app: app}
9093
}
9194
end
9295
end
@@ -104,7 +107,8 @@ defmodule ElixirSense.Core.TypeInfo do
104107
%{spec: ast} -> spec_ast_to_string(ast)
105108
%{signature: signature} -> signature
106109
_ -> "#{type}"
107-
end
110+
end,
111+
metadata: %{builtin: true}
108112
}
109113
end
110114
end

test/elixir_sense/core/metadata_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ defmodule ElixirSense.Core.MetadataTest do
4040
|> Metadata.get_function_signatures(MyModule, :func)
4141

4242
assert signatures == [
43-
%{name: "func", params: ["par"], documentation: "", spec: ""},
43+
%{name: "func", params: ["par"], documentation: "", spec: "", metadata: %{}},
4444
%{
4545
name: "func",
4646
params: ["tuple", "optional \\\\ true"],
4747
documentation: "",
48-
spec: ""
48+
spec: "",
49+
metadata: %{}
4950
}
5051
]
5152
end

0 commit comments

Comments
 (0)