defmodule LiveDebuggerWeb.Components.ElixirDisplay do
@moduledoc """
This module provides a component to display a tree of terms.
Check LiveDebugger.Utils.TermParser.
"""
use LiveDebuggerWeb, :component
@max_auto_expand_size 6
@doc """
Returns a tree of terms.
"""
attr(:id, :string, required: true)
attr(:node, :any, required: true)
attr(:level, :integer, required: true)
def term(assigns) do
assigns =
assigns
|> assign(:expanded?, auto_expand?(assigns.node, assigns.level))
|> assign(:has_children?, has_children?(assigns.node))
~H"""
<.text_items :if={!@has_children?} items={@node.content} />
<.collapsible
:if={@has_children?}
id={@id <> "collapsible"}
open={@expanded?}
icon="icon-chevron-right"
label_class="max-w-max"
chevron_class="text-code-2 m-auto w-[2ch] h-[2ch]"
>
<:label>
<.text_items items={@node.expanded_before} />
<.text_items items={@node.content} />
<%= for {child, index} <- Enum.with_index(@node.children) do %>
-
<.term id={@id <> "-#{index}"} node={child} level={@level + 1} />
<% end %>
<.text_items items={@node.expanded_after} />
"""
end
attr(:items, :list, required: true)
defp text_items(assigns) do
~H"""
<%= for item <- @items do %>
<%= item.text %>
<% end %>
"""
end
defp text_item_color_class(item) do
if item.color, do: "#{item.color}", else: ""
end
defp auto_expand?(_node, 1), do: true
defp auto_expand?(node, _level) do
node.kind == "tuple" and children_number(node) <= @max_auto_expand_size
end
defp has_children?(%{children: nil} = _node), do: false
defp has_children?(_node), do: true
defp children_number(%{children: nil} = _node), do: 0
defp children_number(%{children: children}), do: length(children)
end