Skip to content

Commit

Permalink
Refactor Kino.Tree to build consistent data
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanchrobot committed Feb 18, 2024
1 parent 9b4272e commit a92b5de
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
84 changes: 42 additions & 42 deletions lib/kino/tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Kino.Tree do
end

defp to_node(string, suffix) when is_binary(string) do
%{kind: "binary", content: [green(inspect(string)) | suffix], children: nil}
leaf_node("binary", [green(inspect(string)) | suffix])
end

defp to_node(atom, suffix) when is_atom(atom) do
Expand All @@ -54,32 +54,25 @@ defmodule Kino.Tree do
blue(inspect(atom))
end

%{kind: "atom", content: [span | suffix], children: nil}
leaf_node("atom", [span | suffix])
end

defp to_node(number, suffix) when is_number(number) do
%{kind: "number", content: [blue(inspect(number)) | suffix], children: nil}
leaf_node("number", [blue(inspect(number)) | suffix])
end

defp to_node({}, suffix) do
%{kind: "tuple", content: [black("{}") | suffix], children: nil}
leaf_node("tuple", [black("{}") | suffix])
end

defp to_node(tuple, suffix) when is_tuple(tuple) do
size = tuple_size(tuple)
children = tuple |> Tuple.to_list() |> to_children(size)

%{
kind: "tuple",
content: [black("{...}") | suffix],
children: children,
expanded_before: [black("{")],
expanded_after: [black("}") | suffix]
}
branch_node("tuple", [black("{...}") | suffix], children, [black("{")], [black("}") | suffix])
end

defp to_node([], suffix) do
%{kind: "list", content: [black("[]") | suffix], children: nil}
leaf_node("list", [black("[]") | suffix])
end

defp to_node(list, suffix) when is_list(list) do
Expand All @@ -92,56 +85,43 @@ defmodule Kino.Tree do
to_children(list, size)
end

%{
kind: "list",
content: [black("[...]") | suffix],
children: children,
expanded_before: [black("[")],
expanded_after: [black("]") | suffix]
}
branch_node("list", [black("[...]") | suffix], children, [black("[")], [black("]") | suffix])
end

defp to_node(%Regex{} = regex, suffix) do
%{kind: "regex", content: [red(inspect(regex)) | suffix], children: nil}
leaf_node("regex", [red(inspect(regex)) | suffix])
end

defp to_node(%module{} = struct, suffix) when is_struct(struct) do
if Inspect.impl_for(struct) != Inspect.Any do
%{kind: "struct", content: [black(inspect(struct)) | suffix], children: nil}
leaf_node("struct", [black(inspect(struct)) | suffix])
else
map = Map.from_struct(struct)
size = map_size(map)
children = to_key_value_children(map, size)

%{
kind: "struct",
content: [black("%"), blue(inspect(module)), black("{...}") | suffix],
children: children,
expanded_before: [black("%"), blue(inspect(module)), black("{")],
expanded_after: [black("}") | suffix]
}
branch_node(
"struct",
[black("%"), blue(inspect(module)), black("{...}") | suffix],
children,
[black("%"), blue(inspect(module)), black("{")],
[black("}") | suffix]
)
end
end

defp to_node(%{} = map, suffix) when map_size(map) == 0 do
%{kind: "map", content: [black("%{}") | suffix], children: nil}
leaf_node("map", [black("%{}") | suffix])
end

defp to_node(map, suffix) when is_map(map) do
size = map_size(map)
children = map |> Enum.sort() |> to_key_value_children(size)

%{
kind: "map",
content: [black("%{...}") | suffix],
children: children,
expanded_before: [black("%{")],
expanded_after: [black("}") | suffix]
}
branch_node("map", [black("%{...}") | suffix], children, [black("%{")], [black("}") | suffix])
end

defp to_node(other, suffix) do
%{kind: "other", content: [black(inspect(other)) | suffix], children: nil}
leaf_node("other", [black(inspect(other)) | suffix])
end

defp to_key_value_node({key, value}, suffix) do
Expand All @@ -155,15 +135,15 @@ defmodule Kino.Tree do
end

case to_node(value, suffix) do
%{content: content, children: nil} = node ->
%{node | content: [key_span, sep_span | content]}

%{content: content, expanded_before: expanded_before} = node ->
%{
node
| content: [key_span, sep_span | content],
expanded_before: [key_span, sep_span | expanded_before]
}

%{content: content} = node ->
%{node | content: [key_span, sep_span | content]}
end
end

Expand All @@ -187,6 +167,26 @@ defmodule Kino.Tree do
end
end

defp leaf_node(kind, content) do
%{
kind: kind,
content: content,
children: nil,
expanded_before: nil,
expanded_after: nil
}
end

defp branch_node(kind, content, children, expanded_before, expanded_after) do
%{
kind: kind,
content: content,
children: children,
expanded_before: expanded_before,
expanded_after: expanded_after
}
end

defp black(text), do: %{text: text, color: nil}
defp red(text), do: %{text: text, color: "var(--ansi-color-red)"}
defp green(text), do: %{text: text, color: "var(--ansi-color-green)"}
Expand Down
8 changes: 4 additions & 4 deletions test/kino/tree_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ defmodule Kino.TreeTest do
Enum.map(list, &plaintext/1)
end

defp plaintext(%{content: content, children: nil} = node) do
%{node | content: text_of(content)}
end

defp plaintext(
%{
content: content,
Expand All @@ -251,10 +255,6 @@ defmodule Kino.TreeTest do
}
end

defp plaintext(%{content: content, children: nil} = node) do
%{node | content: text_of(content)}
end

defp text_of(list) when is_list(list) do
list |> Enum.map(& &1.text) |> Enum.join()
end
Expand Down

0 comments on commit a92b5de

Please sign in to comment.