View Source Styler.Style behaviour (Styler v0.6.1)

A Style takes AST and returns a transformed version of that AST.

Because these transformations involve traversing trees (the "T" in "AST"), we wrap the AST in a structure called a Zipper to facilitate walking the trees.

Link to this section Summary

Callbacks

run will be used with Zipper.traverse_while/3, meaning it will be executed on every node of the AST.

Functions

Set the line of all comments with line in range_start..range_end to instead have line range_start

Deletes :line and newlines from the node's meta

Ensure the parent node can have multiple children.

Sets :line, :closing, and :last to all be on line and deletes :newlines

Change the line of all comments with line in range by adding delta to it. A positive delta will move the lines further down a file, while a negative delta will move them up.

Traverses an ast node, updating all nodes' meta with meta_fun

Link to this section Types

@type context() :: %{comment: [map()], file: :stdin | String.t()}

Link to this section Callbacks

run will be used with Zipper.traverse_while/3, meaning it will be executed on every node of the AST.

You can skip traversing parts of the tree by returning a Zipper that's further along in the traversal, for example by calling Zipper.skip(zipper) to skip an entire subtree you know is of no interest to your Style.

Link to this section Functions

Link to this function

displace_comments(comments, range)

View Source

Set the line of all comments with line in range_start..range_end to instead have line range_start

Link to this function

drop_line_meta(ast_node)

View Source

Deletes :line and newlines from the node's meta

If you expected {:foo, foo_meta, [bar, baz, bop] to give you a a single line like

foo(bar, baz, bop)

but instead got

foo(

bar,
baz,
bop

)

then it's likely that at least one of bar, baz, and/or bop have :line meta that's confusing the formatter and causing the multilining.

This function fixes that problem.

# => foo(bar, baz, bop)

Link to this function

ensure_block_parent(zipper)

View Source

Ensure the parent node can have multiple children.

If a context-changing node (a do end block or an -> arrow block) is encountered the child is wrapped in a :__block__

Other nodes (pipes, assignments) can only have a fixed number of children. This function will recursively traverse up the zipper until it's found the parents of those nodes.

Link to this function

set_line_meta_to_line(ast_node, line)

View Source

Sets :line, :closing, and :last to all be on line and deletes :newlines

Link to this function

shift_comments(comments, range, delta)

View Source

Change the line of all comments with line in range by adding delta to it. A positive delta will move the lines further down a file, while a negative delta will move them up.

Link to this function

update_all_meta(node, meta_fun)

View Source

Traverses an ast node, updating all nodes' meta with meta_fun