Generate a default signature for functions/types when docs are not available (#2751)

This commit is contained in:
Jonatan Kłosko 2024-08-18 08:35:20 +02:00 committed by GitHub
parent 246d692fb7
commit 4783cfbd2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 9 deletions

View file

@ -256,7 +256,7 @@ defmodule Livebook.Intellisense do
documentation: documentation:
join_with_newlines([ join_with_newlines([
format_documentation(documentation, :short), format_documentation(documentation, :short),
code(format_signatures(signatures, module)) code(format_signatures(signatures, module, name, arity))
]), ]),
insert_text: insert_text:
cond do cond do
@ -494,7 +494,7 @@ defmodule Livebook.Intellisense do
meta: meta meta: meta
}) do }) do
join_with_divider([ join_with_divider([
format_signatures(signatures, module) |> code(), format_signatures(signatures, module, name, arity) |> code(),
join_with_middle_dot([ join_with_middle_dot([
format_docs_link(module, {:function, name, arity}), format_docs_link(module, {:function, name, arity}),
format_meta(:since, meta) format_meta(:since, meta)
@ -514,7 +514,7 @@ defmodule Livebook.Intellisense do
type_spec: type_spec type_spec: type_spec
}) do }) do
join_with_divider([ join_with_divider([
format_type_signature(type_spec, module) |> code(), format_type_signature(type_spec, module, name, arity) |> code(),
format_docs_link(module, {:type, name, arity}), format_docs_link(module, {:type, name, arity}),
format_type_spec(type_spec, @extended_line_length) |> code(), format_type_spec(type_spec, @extended_line_length) |> code(),
format_documentation(documentation, :all) format_documentation(documentation, :all)
@ -623,9 +623,11 @@ defmodule Livebook.Intellisense do
end end
end end
defp format_signatures([], _module), do: nil defp format_signatures([], module, name, arity) do
signature_fallback(module, name, arity)
end
defp format_signatures(signatures, module) do defp format_signatures(signatures, module, _name, _arity) do
signatures_string = Enum.join(signatures, "\n") signatures_string = Enum.join(signatures, "\n")
# Don't add module prefix to operator signatures # Don't add module prefix to operator signatures
@ -636,13 +638,20 @@ defmodule Livebook.Intellisense do
end end
end end
defp format_type_signature(nil, _module), do: nil defp format_type_signature(nil, module, name, arity) do
signature_fallback(module, name, arity)
end
defp format_type_signature({_type_kind, type}, module) do defp format_type_signature({_type_kind, type}, module, _name, _arity) do
{:"::", _env, [lhs, _rhs]} = Code.Typespec.type_to_quoted(type) {:"::", _env, [lhs, _rhs]} = Code.Typespec.type_to_quoted(type)
inspect(module) <> "." <> Macro.to_string(lhs) inspect(module) <> "." <> Macro.to_string(lhs)
end end
defp signature_fallback(module, name, arity) do
args = Enum.map_join(1..arity//1, ", ", fn n -> "arg#{n}" end)
"#{inspect(module)}.#{name}(#{args})"
end
defp format_meta(:deprecated, %{deprecated: deprecated}) do defp format_meta(:deprecated, %{deprecated: deprecated}) do
"**Deprecated**. " <> deprecated "**Deprecated**. " <> deprecated
end end

View file

@ -2024,7 +2024,13 @@ defmodule Livebook.IntellisenseTest do
assert %{ assert %{
label: "hello/1", label: "hello/1",
kind: :function, kind: :function,
documentation: "No documentation available", documentation: """
No documentation available
```
RemoteModule.hello(arg1)
```\
""",
insert_text: "hello(${})" insert_text: "hello(${})"
} in Intellisense.get_completion_items("RemoteModule.hel", context, node) } in Intellisense.get_completion_items("RemoteModule.hel", context, node)
end end
@ -2037,7 +2043,13 @@ defmodule Livebook.IntellisenseTest do
%{ %{
label: "unsubscribe/1", label: "unsubscribe/1",
kind: :function, kind: :function,
documentation: "No documentation available", documentation: """
No documentation available
```
:mnesia.unsubscribe(arg1)
```\
""",
insert_text: "unsubscribe(${})" insert_text: "unsubscribe(${})"
} }
] = Intellisense.get_completion_items(":mnesia.unsub", context, node) ] = Intellisense.get_completion_items(":mnesia.unsub", context, node)