View Source merlin_bindings (merlin v3.0.0)

Module for manipulating bindings, more commonly known as variables.

Summary

Types

Represents the current set of bindings for a node.

Represents a set of bindings.

Represents the state a binding can be in.

Represents a variable binding.

Functions

Returns the given node annotated with detailed information about its bindings.

Returns the given node annotated with detailed information about its bindings using the given bindings as a starting point.

See also: erl_syntax_lib:annotate_bindings/2.

Returns the state() for given binding in the given node.

Returns the bindings for the given node in the given state.

Returns true if the given node has been annotated with its bindings.
Returns true if the given node has a binding with the given name.
Returns true if the given node has a binding with the given name in the given state.

Returns a new variable binding guaranteed to be unique with respect to the current set of bindings in the given node.

Returns new variable(s) binding according to the given options.

Types

-type binding_mapping() :: #{env := bindings(), bound := bindings(), free := bindings()}.
Represents the current set of bindings for a node.
-type bindings() :: ordsets:ordset(atom()).

Represents a set of bindings.

Each binding is its name as an atom.
-type state() :: env | bound | free.

Represents the state a binding can be in.

env
Bindings from the surrounding scope, i.e. a fun's closure.
bound
Bindings with value.
free
Bindings without a value. It's a compile time error to try to access them.
-type variable() :: merlin:ast().
Represents a variable binding.

Functions

-spec annotate(merlin:ast()) -> merlin:ast().

Returns the given node annotated with detailed information about its bindings.

Fetches the starting (env) bindings from the node itself, defaulting to an empty set if none are found. This is in contrast to erl_syntax_lib:annotate_bindings/1 that crashes if no such bindings can be found.

See also: annotate/1, annotate/2.

Link to this function

annotate(Nodes, EnvBindings)

View Source
-spec annotate(merlin:ast(), bindings()) -> merlin:ast().
Returns the given node annotated with detailed information about its bindings using the given bindings as a starting point.

See also: erl_syntax_lib:annotate_bindings/2.

-spec get(merlin:ast(), Binding) -> state() | unbound when Binding :: atom() | string().

Returns the state() for given binding in the given node.

Prefers bound over env over free over unbound. The latter means that the given binding does not occur in the given node, unlike free which means that the binding will occur later.

The given node, or one of its ancestors, must have been annotated with its bindings.
Link to this function

get_by_state(Node, State)

View Source
-spec get_by_state(merlin:ast(), state()) -> bindings().

Returns the bindings for the given node in the given state.

The given node, or one of its ancestors, must have been annotated with its bindings.
-spec has(merlin:ast()) -> boolean().
Returns true if the given node has been annotated with its bindings.
-spec has(merlin:ast(), Binding) -> boolean() when Binding :: atom() | string().
Returns true if the given node has a binding with the given name.
Link to this function

has(Node, Binding, State)

View Source
-spec has(merlin:ast(), Binding, state()) -> boolean() when Binding :: atom() | string().
Returns true if the given node has a binding with the given name in the given state.
-spec new(Node) -> {Node, variable()} when Node :: merlin:ast().

Returns a new variable binding guaranteed to be unique with respect to the current set of bindings in the given node.

The variable will have its attributes copied from the given node. This means both user annotations, position information, and comments are taken from the given node. In practice it means that stacktraces will point to a more correct location rather then the top of the file.

The given node's bindings will be updated to reflect the new variable.
-spec new(Node, Format | Total | Options) -> {Node, variable() | [variable()]}
       when
           Node :: merlin:ast(),
           Format :: io:format(),
           Total :: pos_integer(),
           Options ::
               #{file => string(),
                 format => Format,
                 location => erl_anno:location(),
                 total => Total}.

Returns new variable(s) binding according to the given options.

The options may specify a format string, which is used to generate the variable name. The format string must contain a single ~tp which is used to ensure uniqueness.

You may also specify a total option, which is used to generate multiple variables.

Both the format string and the total option can be passed directly as a short hand.

See also: new/1.