|
1 | 1 | defmodule ElixirSense.Core.BuiltinFunctions do |
2 | 2 | @moduledoc false |
| 3 | + require ElixirSense.Core.Introspection, as: Introspection |
3 | 4 |
|
4 | 5 | @functions %{ |
5 | 6 | {:exception, 1} => %{specs: [quote(do: exception(term) :: Exception.t())], args: ["msg"]}, |
@@ -210,6 +211,43 @@ defmodule ElixirSense.Core.BuiltinFunctions do |
210 | 211 |
|
211 | 212 | @all Map.keys(@functions) |
212 | 213 |
|
| 214 | + @doc """ |
| 215 | + Returns a list of all builtin functions matching the given function name and arity. |
| 216 | +
|
| 217 | + ## Parameters |
| 218 | + - function: The function name (atom or string) |
| 219 | + - arity: The arity (integer) or :any for all arities |
| 220 | + |
| 221 | + ## Returns |
| 222 | + A list of tuples {function_name, arity, doc, specs} for all matching functions. |
| 223 | + |
| 224 | + ## Examples |
| 225 | + |
| 226 | + iex> get_builtin_functions_doc(:module_info, :any) |
| 227 | + [{"module_info", 0, "The `module_info/0` function...", ["@spec module_info() :: ..."]}, |
| 228 | + {"module_info", 1, "The call `module_info(Key)`...", ["@spec module_info(:module) :: ..."]}] |
| 229 | + |
| 230 | + iex> get_builtin_functions_doc(:module_info, 1) |
| 231 | + [{"module_info", 1, "The call `module_info(Key)`...", ["@spec module_info(:module) :: ..."]}] |
| 232 | + |
| 233 | + iex> get_builtin_functions_doc(:module_info, 0) |
| 234 | + [{"module_info", 0, "The `module_info/0` function...", ["@spec module_info() :: ..."]}] |
| 235 | + """ |
| 236 | + def get_builtin_functions_doc(function, arity) do |
| 237 | + function_atom = if is_atom(function), do: function, else: String.to_atom(to_string(function)) |
| 238 | + |
| 239 | + @functions |
| 240 | + |> Enum.filter(fn {{f, a}, _value} -> |
| 241 | + f == function_atom and Introspection.matches_arity?(a, arity) |
| 242 | + end) |
| 243 | + |> Enum.map(fn {{f, a}, %{specs: specs}} -> |
| 244 | + doc = Map.get(@docs, {f, a}, "") |
| 245 | + formatted_specs = specs |> Enum.map(&"@spec #{Macro.to_string(&1)}") |
| 246 | + {to_string(f), a, doc, formatted_specs} |
| 247 | + end) |
| 248 | + |> Enum.sort_by(fn {_name, arity, _doc, _specs} -> arity end) |
| 249 | + end |
| 250 | + |
213 | 251 | for {{f, a}, %{specs: specs}} <- @functions do |
214 | 252 | def get_specs({unquote(f), unquote(a)}), |
215 | 253 | do: unquote(specs |> Enum.map(&"@spec #{Macro.to_string(&1)}")) |
|
0 commit comments