Behaviours: rebar3_ast_formatter, rebar3_formatter.
Rebar3 Pretty Printing of abstract Erlang syntax trees, based on original erl_prettypr.
It was taken verbatim from erl_prettypr and it was modified just to fix some bugs.
This module is a front end to the pretty-printing library moduleprettypr
, for text formatting of abstract syntax trees defined by
the module erl_syntax
.
clause_t() = case_expr | fun_expr | if_expr | receive_expr | try_expr | {function, prettypr:document()} | spec
context() = #ctxt{prec = integer(), sub_indent = non_neg_integer(), break_indent = non_neg_integer(), clause = clause_t() | undefined, hook = hook(), paper = integer(), ribbon = integer(), user = term(), encoding = epp:source_encoding(), empty_lines = sets:set(integer())}
hook() = none | fun((erl_syntax:syntaxTree(), term(), term()) -> prettypr:document())
syntaxTree() = erl_syntax:syntaxTree()
An abstract syntax tree. See the erl_syntax
module for
details.
best/1 | Equivalent to best(Tree, []). |
best/2 | Creates a fixed "best" abstract layout for a syntax tree. |
format/1 | Equivalent to format(Tree, []). |
format/3 | Pretty-prints/formats an abstract Erlang syntax tree as text. |
format_file/3 | Format a file. |
get_ctxt_hook/1 | Returns the hook function field of the pretty-printer context. |
get_ctxt_linewidth/1 | Returns the line width field of the pretty-printer context. |
get_ctxt_paperwidth/1 | Returns the paper width field of the pretty-printer context. |
get_ctxt_precedence/1 | Returns the operator precedence field of the pretty-printer context. |
get_ctxt_user/1 | Returns the user data field of the pretty-printer context. |
init/2 | Initialize the formatter and generate a state that will be passed in when calling other callbacks. |
layout/1 | Equivalent to layout(Tree, []). |
layout/2 | Creates an abstract document layout for a syntax tree. |
set_ctxt_hook/2 | Updates the hook function field of the pretty-printer context. |
set_ctxt_linewidth/2 | Updates the line width field of the pretty-printer context. |
set_ctxt_paperwidth/2 | Updates the paper width field of the pretty-printer context. |
set_ctxt_precedence/2 | Updates the operator precedence field of the pretty-printer context. |
set_ctxt_user/2 | Updates the user data field of the pretty-printer context. |
best(Node::erl_syntax:syntaxTree()) -> empty | prettypr:document()
Equivalent to best(Tree, []).
best(Node::erl_syntax:syntaxTree(), Options::[term()]) -> empty | prettypr:document()
Creates a fixed "best" abstract layout for a syntax tree. This
is similar to the layout/2
function, except that here, the final
layout has been selected with respect to the given options. The atom
empty
is returned if no such layout could be produced. For
information on the options, see the format/2
function.
See also: best/1, format/2, layout/2, prettypr:best/3.
format(Node::erl_syntax:syntaxTree()) -> string()
Equivalent to format(Tree, []).
format(Node::erl_syntax:syntaxTree(), EmptyLines::[pos_integer()], Options::rebar3_formatter:opts()) -> string()
Pretty-prints/formats an abstract Erlang syntax tree as text. For
example, if you have a .beam
file that has been compiled with
debug_info
, the following should print the source code for the
module (as it looks in the debug info representation):
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks("myfile.beam",[abstract_code]), io:put_chars(otp_formatter:format(erl_syntax:form_list(AC), [], #{}))Available options:
hook()
}none
, the given function is called
for each node whose list of annotations is not empty; see below
for details. The default value is none
.undefined
.hook()
type) is passed the current
syntax tree node, the context, and a continuation. The context can be
examined and manipulated by functions such as get_ctxt_user/1
and
set_ctxt_user/2
. The hook must return a "document" data structure
(see layout/2
and best/2
); this may be constructed in
part or in whole by applying the continuation function. For example,
the following is a trivial hook:
fun (Node, Ctxt, Cont) -> Cont(Node, Ctxt) endwhich yields the same result as if no hook was given. The following, however:
fun (Node, Ctxt, Cont) -> Doc = Cont(Node, Ctxt), prettypr:beside(prettypr:text("<b>"), prettypr:beside(Doc, prettypr:text("</b>"))) endwill place the text of any annotated node (regardless of the annotation data) between HTML "boldface begin" and "boldface end" tags.
See also: erl_syntax, best/2, format/1, get_ctxt_user/1, layout/2, set_ctxt_user/2.
format_file(File::file:filename_all(), X2::nostate, Opts::rebar3_formatter:opts()) -> rebar3_formatter:result()
Format a file.
Apply formatting rules to a file containing erlang code.
Use Opts
to configure the formatter.
Returns the hook function field of the pretty-printer context.
See also: set_ctxt_hook/2.
get_ctxt_linewidth(Ctxt::context()) -> integer()
Returns the line width field of the pretty-printer context.
See also: set_ctxt_linewidth/2.
get_ctxt_paperwidth(Ctxt::context()) -> integer()
Returns the paper width field of the pretty-printer context.
See also: set_ctxt_paperwidth/2.
get_ctxt_precedence(Ctxt::context()) -> integer()
Returns the operator precedence field of the pretty-printer context.
See also: set_ctxt_precedence/2.
get_ctxt_user(Ctxt::context()) -> term()
Returns the user data field of the pretty-printer context.
See also: set_ctxt_user/2.
init(X1::rebar3_formatter:opts(), X2::undefined | rebar_state:t()) -> nostate
Initialize the formatter and generate a state that will be passed in when calling other callbacks.
layout(Node::erl_syntax:syntaxTree()) -> prettypr:document()
Equivalent to layout(Tree, []).
layout(Node::erl_syntax:syntaxTree(), Options::[term()]) -> prettypr:document()
Creates an abstract document layout for a syntax tree. The
result represents a set of possible layouts (cf. module prettypr
).
For information on the options, see format/2
; note, however,
that the paper
and ribbon
options are ignored by this function.
prettypr
module, or used in a hook
function (see format/2
for details).
See also: prettypr, format/2, layout/1.
Updates the hook function field of the pretty-printer context.
See also: get_ctxt_hook/1.
Updates the line width field of the pretty-printer context.
Note: changing this value (and passing the resulting context to a continuation function) does not affect the normal formatting, but may affect user-defined behaviour in hook functions.See also: get_ctxt_linewidth/1.
Updates the paper width field of the pretty-printer context.
Note: changing this value (and passing the resulting context to a continuation function) does not affect the normal formatting, but may affect user-defined behaviour in hook functions.See also: get_ctxt_paperwidth/1.
Updates the operator precedence field of the pretty-printer
context. See the //stdlib/erl_parse
module for operator precedences.
See also: //stdlib/erl_parse, get_ctxt_precedence/1.
Updates the user data field of the pretty-printer context.
See also: get_ctxt_user/1.
Generated by EDoc