Text of sourceror.hexdocs.pm › Sourceror.Code.Common.html as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Sourceror.Code.Common — Sourceror v1.13.0

View Source Sourceror.Code.Common (Sourceror v1.13.0) General purpose utilities for working with Sourceror.Zipper . Summary Functions add_code(zipper, new_code, opts \\ []) Adds the provided code to the zipper. add_comment(zipper, comment, opts \\ []) extendable_block?(arg1) find_all(zipper, predicate) Returns a list of zippers to each node that satisfies the predicate function, or an empty list if none are found. maybe_move_to_block(zipper) Enters a block, and moves to the first child, or returns the zipper unmodified. maybe_move_to_single_child_block(zipper) Enters a block with a single child, and moves to that child, or returns the zipper unmodified. move_left(zipper, pred_or_n) Moves a zipper to the left. move_next(zipper, pred) Moves nextwards (depth-first), until the provided predicate returns true . move_right(zipper, pred_or_n) Moves a zipper to the right. move_to(zipper, pred) Moves to the first node that matches the predicate. move_to_cursor(zipper, pattern) Matches and moves to the location of a __cursor__ in provided source code. move_to_cursor_match_in_scope(zipper, patterns) Moves to the cursor that matches the provided pattern or one of the provided patterns, in the current scope. move_to_do_block(zipper) Moves to a do block for the current call. move_to_last(zipper, pred) Moves to the last node that matches the predicate. move_to_pattern(zipper, pattern) Moves to the next node that matches the given pattern. move_to_zipper(zipper, pred) Moves to the next zipper that matches the predicate. move_upwards(zipper, pred_or_n) Moves a zipper upwards. move_upwards_until(zipper, pred) Moves to the last node before the node that matches the predicate, going upwards. node_matches_pattern?(zipper, pattern) Returns true if the current node matches the given pattern. nodes_equal?(left, right) Checks if two nodes are equal. parse_to_zipper!(string) remove(zipper, pred) Removes any nodes matching the provided pattern, until there are no matches left. remove_all_matches(zipper, pred) Removes all nodes matching the given predicate with the given function. replace_code(zipper, code) Replaces code with new code. rightmost(zipper) Moves the zipper all the way to the right, potentially entering a single value block. single_child_block?(zipper) update_all_matches(zipper, pred, fun) Updates all nodes matching the given predicate with the given function. variable_assignment?(arg1, name) Returns true if the node represents a variable assignment. within(top_zipper, fun) 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 extendable_block?(arg1) 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 .

Link to this function move_next(zipper, pred) View Source @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 .

Link to this function move_to(zipper, pred) View Source @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_

The rest of this page is on the live site.