Code.cursor_context
cursor_context
, go back to Code module for more information.
Specs
cursor_context(List.Chars.t(), keyword()) :: {:alias, charlist()} | {:dot, inside_dot, charlist()} | {:dot_arity, inside_dot, charlist()} | {:dot_call, inside_dot, charlist()} | :expr | {:local_or_var, charlist()} | {:local_arity, charlist()} | {:local_call, charlist()} | {:module_attribute, charlist()} | :none | {:unquoted_atom, charlist()} when inside_dot: {:alias, charlist()} | {:dot, inside_dot, charlist()} | {:module_attribute, charlist()} | {:unquoted_atom, charlist()} | {:var, charlist()}
Receives a string and returns the cursor context.
This function receives a string with incomplete Elixir code, representing a cursor position, and based on the string, it provides contextual information about said position. The return of this function can then be used to provide tips, suggestions, and autocompletion functionality.
This function provides a best-effort detection and may not be accurate under certain circumstances. See the "Limitations" section below.
Consider adding a catch-all clause when handling the return type of this function as new cursor information may be added in future releases.
Examples
iex> Code.cursor_context("")
:expr
iex> Code.cursor_context("hello_wor")
{:local_or_var, 'hello_wor'}
Return values
{:alias, charlist}
- the context is an alias, potentially a nested one, such asHello.Wor
orHelloWor
{:dot, inside_dot, charlist}
- the context is a dot whereinside_dot
is either a{:var, charlist}
,{:alias, charlist}
,{:module_attribute, charlist}
,{:unquoted_atom, charlist}
or adot
itself. If a var is given, this may either be a remote call or a map field access. Examples areHello.wor
,:hello.wor
,hello.wor
,Hello.nested.wor
,hello.nested.wor
, and@hello.world
{:dot_arity, inside_dot, charlist}
- the context is a dot arity whereinside_dot
is either a{:var, charlist}
,{:alias, charlist}
,{:module_attribute, charlist}
,{:unquoted_atom, charlist}
or adot
itself. If a var is given, it must be a remote arity. Examples areHello.world/
,:hello.world/
,hello.world/2
, and@hello.world/2
{:dot_call, inside_dot, charlist}
- the context is a dot call. This means parentheses or space have been added after the expression. whereinside_dot
is either a{:var, charlist}
,{:alias, charlist}
,{:module_attribute, charlist}
,{:unquoted_atom, charlist}
or adot
itself. If a var is given, it must be a remote call. Examples areHello.world(
,:hello.world(
,Hello.world
,hello.world(
,hello.world
, and@hello.world(
:expr
- may be any expression. Autocompletion may suggest an alias, local or var{:local_or_var, charlist}
- the context is a variable or a local (import or local) call, such ashello_wor
{:local_arity, charlist}
- the context is a local (import or local) call, such ashello_world/
{:local_call, charlist}
- the context is a local (import or local) call, such ashello_world(
andhello_world
{:module_attribute, charlist}
- the context is a module attribute, such as@hello_wor
:none
- no context possible:unquoted_atom
- the context is an unquoted atom. This can be either previous atoms or all available:erlang
modules
Limitations
- There is no context for operators
- The current algorithm only considers the last line of the input
- Context does not yet track strings, sigils, etc.
- Arguments of functions calls are not currently recognized