View Source Sourceror.Code.Common (Sourceror v1.11.0)

General purpose utilities for working with Sourceror.Zipper.

Summary

Functions

Adds the provided code to the zipper.

Returns a list of zippers to each node that satisfies the predicate function, or an empty list if none are found.

Enters a block, and moves to the first child, or returns the zipper unmodified.

Enters a block with a single child, and moves to that child, or returns the zipper unmodified.

Moves a zipper to the left.

Moves nextwards (depth-first), until the provided predicate returns true.

Moves a zipper to the right.

Moves to the first node that matches the predicate.

Matches and moves to the location of a __cursor__ in provided source code.

Moves to the cursor that matches the provided pattern or one of the provided patterns, in the current scope.

Moves to a do block for the current call.

Moves to the last node that matches the predicate.

Moves to the next node that matches the given pattern.

Moves to the next zipper that matches the predicate.

Moves a zipper upwards.

Moves to the last node before the node that matches the predicate, going upwards.

Returns true if the current node matches the given pattern.

Checks if two nodes are equal.

Removes any nodes matching the provided pattern, until there are no matches left.

Removes all nodes matching the given predicate with the given function.

Replaces code with new code.

Moves the zipper all the way to the right, potentially entering a single value block.

Updates all nodes matching the given predicate with the given function.

Returns true if the node represents a variable assignment.

Runs the function fun on the subtree of the currently focused node and returns the updated zipper.

Functions

Link to this function

add_code(zipper, new_code, opts \\ [])

View Source
@spec add_code(Sourceror.Zipper.t(), String.t() | Macro.t(), keyword()) ::
  Sourceror.Zipper.t()

Adds the provided code to the zipper.

Options

  • :placement - :after | :before. Determines if the code goes :after or :before the current node. Defaults to :after.

Link to this function

add_comment(zipper, comment, opts \\ [])

View Source
Link to this function

find_all(zipper, predicate)

View Source
@spec find_all(Sourceror.Zipper.t(), predicate :: (Sourceror.Zipper.t() -> boolean())) ::
  [
    Sourceror.Zipper.t()
  ]

Returns a list of zippers to each node that satisfies the predicate function, or an empty list if none are found.

The optional second parameters specifies the direction, defaults to :next.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> zippers = Sourceror.Code.Common.find_all(zipper, fn z -> match?({:__block__, _, [_]}, z.node) end)
iex> length(zippers) >= 3
true

See also move_to/2.

Link to this function

maybe_move_to_block(zipper)

View Source
@spec maybe_move_to_block(Sourceror.Zipper.t()) :: Sourceror.Zipper.t()

Enters a block, and moves to the first child, or returns the zipper unmodified.

Examples

iex> zipper = Sourceror.parse_string!("(1 + 2)") |> Sourceror.Zipper.zip()
iex> zipper = Sourceror.Code.Common.maybe_move_to_block(zipper)
iex> match?({:+, _, _}, zipper.node)
true

See also maybe_move_to_single_child_block/1.

Link to this function

maybe_move_to_single_child_block(zipper)

View Source
@spec maybe_move_to_single_child_block(Sourceror.Zipper.t()) :: Sourceror.Zipper.t()

Enters a block with a single child, and moves to that child, or returns the zipper unmodified.

Examples

iex> zipper = Sourceror.parse_string!("(1)") |> Sourceror.Zipper.zip()
iex> zipper = Sourceror.Code.Common.maybe_move_to_single_child_block(zipper)
iex> match?({:__block__, _, [1]}, zipper.node)
true

See also maybe_move_to_block/1.

Link to this function

move_left(zipper, pred_or_n)

View Source
@spec move_left(
  Sourceror.Zipper.t(),
  non_neg_integer() | (Sourceror.Zipper.t() -> boolean())
) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves a zipper to the left.

If the second argument is a predicate function, it will be called on the zipper and then move leftwards until the predicate returns true. This function will automatically enter and exit blocks.

If the second argument is a non-negative integer, it will move left that many times if possible, returning :error otherwise.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Common.move_to(zipper, fn z -> match?({:__block__, _, [3]}, z.node) end)
iex> {:ok, zipper} = Sourceror.Code.Common.move_left(zipper, 1)
iex> match?({:__block__, _, [2]}, zipper.node)
true

See also move_right/2.

@spec move_next(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> boolean())) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves nextwards (depth-first), until the provided predicate returns true.

Returns :error if the end is reached without finding a match.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Common.move_next(zipper, fn z -> match?({:__block__, _, [2]}, z.node) end)
iex> match?({:__block__, _, [2]}, result.node)
true

See also move_to/2.

Link to this function

move_right(zipper, pred_or_n)

View Source
@spec move_right(
  Sourceror.Zipper.t(),
  non_neg_integer() | (Sourceror.Zipper.t() -> boolean())
) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves a zipper to the right.

If the second argument is a predicate function, it will be called on the zipper and then move rightwards until the predicate returns true. This function will automatically enter and exit blocks.

If the second argument is a non-negative integer, it will move right that many times if possible, returning :error otherwise.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Common.move_to(zipper, fn z -> match?({:__block__, _, [1]}, z.node) end)
iex> {:ok, zipper} = Sourceror.Code.Common.move_right(zipper, 1)
iex> match?({:__block__, _, [2]}, zipper.node)
true

See also move_left/2.

@spec move_to(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> boolean())) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves to the first node that matches the predicate.

Examples

iex> zipper = Sourceror.parse_string!("foo = 1") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Common.move_to(zipper, fn z -> match?({:=, _, _}, z.node) end)
iex> match?({:=, _, _}, result.node)
true

See also move_to_last/2.

Link to this function

move_to_cursor(zipper, pattern)

View Source
@spec move_to_cursor(Sourceror.Zipper.t(), Sourceror.Zipper.t() | String.t()) ::
  {:ok, Sourceror.Zipper.t()} | :error

Matches and moves to the location of a __cursor__ in provided source code.

Use __cursor__() to match a cursor in the provided source code. Use __ to skip any code at a point.

For example:

zipper =
  """
  if true do
    10
  end
  """
  |> Sourceror.Zipper.zip()

pattern =
  """
  if __ do
    __cursor__()
  end
  """

zipper
|> Sourceror.Code.Common.move_to_cursor(pattern)
|> Zipper.node()
# => 10
Link to this function

move_to_cursor_match_in_scope(zipper, patterns)

View Source
@spec move_to_cursor_match_in_scope(Sourceror.Zipper.t(), String.t() | [String.t()]) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves to the cursor that matches the provided pattern or one of the provided patterns, in the current scope.

See move_to_cursor/2 for an example of a pattern.

Link to this function

move_to_do_block(zipper)

View Source
@spec move_to_do_block(Sourceror.Zipper.t()) :: {:ok, Sourceror.Zipper.t()} | :error

Moves to a do block for the current call.

For example, at a node like:

foo do
  10
end

You would get a zipper back at 10.

See also move_upwards/2.

Link to this function

move_to_last(zipper, pred)

View Source
@spec move_to_last(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> boolean())) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves to the last node that matches the predicate.

Similar to move_to/2 but it doesn't stop at the first match, for example a zipper for the following code:

port = 4000
port = 4001

With a match for port = _ as {:=, _, [{:port, _, _}, _]}, will return the second port variable.

Link to this macro

move_to_pattern(zipper, pattern)

View Source (macro)

Moves to the next node that matches the given pattern.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Common.move_to_pattern(zipper, {:__block__, _, [2]})
iex> match?({:__block__, _, [2]}, result.node)
true

See also node_matches_pattern?/2.

Link to this function

move_to_zipper(zipper, pred)

View Source
@spec move_to_zipper(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> boolean())) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves to the next zipper that matches the predicate.

Examples

iex> zipper = Sourceror.parse_string!("foo = 1") |> Sourceror.Zipper.zip()
iex> {:ok, result} = Sourceror.Code.Common.move_to_zipper(zipper, fn z -> match?({:=, _, _}, z.node) end)
iex> match?({:=, _, _}, result.node)
true

See also move_to/2.

Link to this function

move_upwards(zipper, pred_or_n)

View Source
@spec move_upwards(
  Sourceror.Zipper.t(),
  non_neg_integer() | (Sourceror.Zipper.t() -> boolean())
) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves a zipper upwards.

If the second argument is a predicate function, it will be called on the zipper and then move upwards until the predicate returns true.

If the second argument is a non-negative integer, it will move upwards that many times if possible, returning :error otherwise.

Examples

iex> zipper = Sourceror.parse_string!("defmodule Foo do\ndef bar do\n1\nend\nend") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Function.move_to_def(zipper, :bar, 0)
iex> {:ok, zipper} = Sourceror.Code.Common.move_upwards(zipper, &match?({:defmodule, _, _}, &1.node))
iex> match?({:defmodule, _, _}, zipper.node)
true

See also move_upwards_until/2.

Link to this function

move_upwards_until(zipper, pred)

View Source
@spec move_upwards_until(Sourceror.Zipper.t(), (Sourceror.Zipper.t() -> boolean())) ::
  {:ok, Sourceror.Zipper.t()} | :error

Moves to the last node before the node that matches the predicate, going upwards.

Examples

iex> zipper = Sourceror.parse_string!("defmodule Foo do\ndef bar do\n1\nend\nend") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Function.move_to_def(zipper, :bar, 0)
iex> {:ok, zipper} = Sourceror.Code.Common.move_upwards_until(zipper, &match?({:defmodule, _, _}, &1.node))
iex> match?({:defmodule, _, _}, zipper.node)
true

See also move_upwards/2.

Link to this macro

node_matches_pattern?(zipper, pattern)

View Source (macro)

Returns true if the current node matches the given pattern.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.Common.node_matches_pattern?(zipper, value when is_list(value))
true

See also move_to_pattern/2.

Link to this function

nodes_equal?(left, right)

View Source
@spec nodes_equal?(Sourceror.Zipper.t() | Macro.t(), Macro.t()) :: boolean()

Checks if two nodes are equal.

Examples

iex> zipper = Sourceror.parse_string!("foo = 1") |> Sourceror.Zipper.zip()
iex> node = Sourceror.Zipper.node(zipper)
iex> Sourceror.Code.Common.nodes_equal?(zipper, node)
true
Link to this function

parse_to_zipper!(string)

View Source

Removes any nodes matching the provided pattern, until there are no matches left.

See also remove_all_matches/2.

Link to this function

remove_all_matches(zipper, pred)

View Source
@spec remove_all_matches(
  Sourceror.Zipper.t(),
  (Sourceror.Zipper.t() -> boolean())
) :: Sourceror.Zipper.t()

Removes all nodes matching the given predicate with the given function.

Recurses until the predicate no longer returns false.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> zipper = Sourceror.Code.Common.remove_all_matches(zipper, 
...>   fn z -> match?({:__block__, _, [2]}, z.node) end)
iex> Sourceror.to_string(zipper.node)
"[1, 3]"

See also update_all_matches/3.

Link to this function

replace_code(zipper, code)

View Source

Replaces code with new code.

Examples

iex> zipper = Sourceror.parse_string!("foo = 1") |> Sourceror.Zipper.zip()
iex> zipper = Sourceror.Code.Common.move_to_zipper(zipper, fn z -> match?({:=, _, _}, z.node) end) |> elem(1)
iex> zipper = Sourceror.Code.Common.replace_code(zipper, "bar = 2")
iex> Sourceror.to_string(zipper.node)
"bar = 2"

See also add_code/3.

@spec rightmost(Sourceror.Zipper.t()) :: Sourceror.Zipper.t()

Moves the zipper all the way to the right, potentially entering a single value block.

Link to this function

single_child_block?(zipper)

View Source
@spec single_child_block?(Sourceror.Zipper.t()) :: boolean()
Link to this function

update_all_matches(zipper, pred, fun)

View Source
@spec update_all_matches(
  Sourceror.Zipper.t(),
  (Sourceror.Zipper.t() -> boolean()),
  (Sourceror.Zipper.t() ->
     {:ok, Sourceror.Zipper.t() | {:code, term()}} | {:warning | :error, term()})
) :: {:ok, Sourceror.Zipper.t()} | {:warning | :error, term()}

Updates all nodes matching the given predicate with the given function.

Recurses until the predicate no longer returns false.

Examples

iex> zipper = Sourceror.parse_string!("foo = 1") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Common.update_all_matches(zipper, 
...>   fn z -> match?({:=, _, _}, z.node) end,
...>   fn z -> {:ok, Sourceror.Code.Common.replace_code(z, "bar = 2")} end)
iex> Sourceror.to_string(zipper.node) |> String.contains?("bar = 2")
true

See also remove_all_matches/2.

Link to this function

variable_assignment?(arg1, name)

View Source
@spec variable_assignment?(zipper :: Sourceror.Zipper.t(), name :: atom()) ::
  boolean()

Returns true if the node represents a variable assignment.

Examples

iex> zipper = Sourceror.parse_string!("foo = 1") |> Sourceror.Zipper.zip()
iex> Sourceror.Code.Common.variable_assignment?(zipper, :foo)
true
iex> Sourceror.Code.Common.variable_assignment?(zipper, :bar)
false

Runs the function fun on the subtree of the currently focused node and returns the updated zipper.

fun must return {:ok, zipper} or :error, which may be positioned at the top of the subtree.

Examples

iex> zipper = Sourceror.parse_string!("[1, 2, 3]") |> Sourceror.Zipper.zip()
iex> {:ok, zipper} = Sourceror.Code.Common.within(zipper, fn z -> 
...>   {:ok, Sourceror.Code.Common.replace_code(z, "[4, 5, 6]")}
...> end)
iex> Sourceror.to_string(zipper.node)
"[4, 5, 6]"