View Source Sourceror.Zipper.Inspect (Sourceror v0.13.0)

Provides Sourceror.Zipper's implementation for the Inspect protocol.

When inspecting a zipper, the default representation shows the current node and provides indicators of the surrounding context without displaying the full path, which can be very verbose for large ASTs.

iex> alias Sourceror.Zipper, as: Z
Sourceror.Zipper

iex> code = """
...> def my_function do
...>   :ok
...> end\
...> """

iex> zipper = code |> Code.string_to_quoted!() |> Z.zip()
#Sourceror.Zipper<
  #root
  {:def, [line: 1], [{:my_function, [line: 1], nil}, [do: :ok]]}
>

iex> zipper |> Z.next()
#Sourceror.Zipper<
  {:my_function, [line: 1], nil}
  #...
>

This representation can be changed using default_inspect_as/1 to set a global default or by using :custom_options with the :zippers key when inspecting. (See Inspect.Opts for more about inspect options.)

Zippers can be inspected in these formats:

  • :as_ast (default, seen above) - display the current node as an AST
  • :as_code - display the current node formatted as code
  • :raw - display the raw %Sourceror.Zipper{} struct including the :path.

Using the zipper defined above as an example:

iex> zipper |> inspect(custom_options: [zipper: :as_code]) |> IO.puts()
#Sourceror.Zipper<
  #root
  def my_function do
    :ok
  end
>

iex> zipper |> Z.next() |> inspect(custom_options: [zipper: :as_code]) |> IO.puts()
#Sourceror.Zipper<
  my_function
  #...
>

iex> zipper |> Z.next() |> inspect(custom_options: [zipper: :raw], pretty: true) |> IO.puts()
%Sourceror.Zipper{
  node: {:my_function, [line: 1], nil},
  path: %{
    parent: %Sourceror.Zipper{
      node: {:def, [line: 1], [{:my_function, [line: 1], nil}, [do: :ok]]},
      path: nil
    },
    left: nil,
    right: [[do: :ok]]
  }
}

Link to this section Summary

Types

Inspection formats for zippers.

Functions

Sets the default inspection format for zippers.

Link to this section Types

@type inspect_as() :: :as_ast | :as_code | :raw

Inspection formats for zippers.

Link to this section Functions

Link to this function

default_inspect_as(inspect_as)

View Source
@spec default_inspect_as(inspect_as()) :: :ok

Sets the default inspection format for zippers.

examples

Examples

Consider the following zipper:

iex> zipper
#Sourceror.Zipper<
  #root
  {:def, [line: 1], [{:my_function, [line: 1], nil}, [do: :ok]]}
>

iex> Sourceror.Zipper.Inspect.default_inspect_as(:as_code)
:ok
iex> zipper
#Sourceror.Zipper<
  #root
  def my_function do
    :ok
  end
>

iex> Sourceror.Zipper.Inspect.default_inspect_as(:raw)
:ok
iex> zipper
%Sourceror.Zipper{
  node: {:def, [line: 1], [{:my_function, [line: 1], nil}, [do: :ok]]},
  path: nil
}