Packages
surface
0.7.4
0.12.3
0.12.2
0.12.1
0.12.0
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.1
0.1.0
0.1.0-alpha.2
0.1.0-alpha.1
0.1.0-alpha.0
A component based library for Phoenix LiveView
Current section
Files
Jump to
Current section
Files
lib/surface/formatter/phases/block_exceptions.ex
defmodule Surface.Formatter.Phases.BlockExceptions do
@moduledoc """
Handle exceptional case for blocks.
"""
@behaviour Surface.Formatter.Phase
alias Surface.Formatter.Phase
def run(nodes, _opts) do
Phase.transform_elements_and_descendants(nodes, &transform_block/1)
end
def transform_block({:block, "case", expr, sub_blocks, %{has_sub_blocks?: true} = meta}) do
# "case" is a special case because its sub blocks are indented, unlike `if`
# and `for`; therefore, the last `:indent_one_less` needs to be removed from
# the last sub-block and moved to the outer block.
#
# This code is a bit hacky but kept the rest of the architecture simpler.
reversed_sub_blocks = Enum.reverse(sub_blocks)
[last_sub_block | rest] = reversed_sub_blocks
{:block, "match", match_expr, children, last_meta} = last_sub_block
modified_sub_blocks = [
{:block, "match", match_expr, Enum.slice(children, 0..-2), last_meta} | rest
]
modified_sub_blocks = Enum.reverse(modified_sub_blocks)
{:block, "case", expr, [:newline, :indent | modified_sub_blocks] ++ [:indent_one_less], meta}
end
def transform_block(node), do: node
end