From 13af842c66df48ddc804d42ba4777458276581df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Tue, 11 Jun 2019 23:50:41 +0200 Subject: [PATCH] IEx: Sort Types in `t` helper (#9131) --- lib/iex/lib/iex/introspection.ex | 47 +++++++++++++++++++------------ lib/iex/test/iex/helpers_test.exs | 14 +++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/iex/lib/iex/introspection.ex b/lib/iex/lib/iex/introspection.ex index d054f92cb8..c48230081f 100644 --- a/lib/iex/lib/iex/introspection.ex +++ b/lib/iex/lib/iex/introspection.ex @@ -644,7 +644,9 @@ defmodule IEx.Introspection do types_not_found(inspect(module)) {:ok, types} -> - Enum.each(types, &(&1 |> format_type() |> IO.puts())) + types + |> Enum.sort_by(fn {_, {name, _, args}} -> {name, length(args)} end) + |> Enum.each(&(&1 |> format_type() |> IO.puts())) end dont_display_result() @@ -656,15 +658,19 @@ defmodule IEx.Introspection do no_beam(module) {:ok, types} -> - printed = - for {kind, {^type, _, args}} = typespec <- types, - kind != :typep do - type_doc(module, type, length(args), typespec) - |> print_typespec() - end + types + |> Enum.filter(&match?({kind, {^type, _, _}} when kind in [:type, :opaque], &1)) + |> Enum.sort_by(fn {_, {name, _, args}} -> {name, length(args)} end) + |> case do + [] -> + types_not_found_or_private("#{inspect(module)}.#{type}") - if printed == [] do - types_not_found_or_private("#{inspect(module)}.#{type}") + types -> + Enum.map(types, fn {_, {_, _, args}} = typespec -> + module + |> type_doc(type, length(args), typespec) + |> print_typespec() + end) end end @@ -677,16 +683,21 @@ defmodule IEx.Introspection do no_beam(module) {:ok, types} -> - printed = - for {kind, {^type, _, args}} = typespec <- types, - kind != :typep, - length(args) == arity do - type_doc(module, type, arity, typespec) - |> print_typespec() - end + types + |> Enum.find( + &match?( + {kind, {^type, _, args}} when kind in [:type, :opaque] and length(args) == arity, + &1 + ) + ) + |> case do + nil -> + types_not_found_or_private("#{inspect(module)}.#{type}") - if printed == [] do - types_not_found_or_private("#{inspect(module)}.#{type}") + typespec -> + module + |> type_doc(type, arity, typespec) + |> print_typespec() end end diff --git a/lib/iex/test/iex/helpers_test.exs b/lib/iex/test/iex/helpers_test.exs index 77400bbfeb..0ac3a9c3d9 100644 --- a/lib/iex/test/iex/helpers_test.exs +++ b/lib/iex/test/iex/helpers_test.exs @@ -824,6 +824,10 @@ defmodule IEx.HelpersTest do end) >= 2 end + test "prints private types" do + assert capture_io(fn -> t(Date.Range) end) =~ "@typep iso_days" + end + test "prints type information" do assert "@type t() ::" <> _ = capture_io(fn -> t(Enum.t()) end) assert capture_io(fn -> t(Enum.t()) end) == capture_io(fn -> t(Enum.t() / 0) end) @@ -831,6 +835,16 @@ defmodule IEx.HelpersTest do assert capture_io(fn -> t(URI.t()) end) == capture_io(fn -> t(URI.t() / 0) end) end + test "sorts types alphabetically" do + unsorted = + capture_io(fn -> t(Enum) end) + |> String.split("\n") + |> Enum.reject(&(&1 == "")) + |> Enum.map(&String.replace(&1, ~r/@(type|opaque) /, "")) + + assert unsorted == Enum.sort(unsorted) + end + test "prints type documentation" do content = """ defmodule TypeSample do