Elixir语言中 Code.Fragment.cursor_context 相关用法介绍如下。
用法:
cursor_context(fragment, opts \\ [])
(从 1.13.0 开始)
@spec 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()}
| {:operator, charlist()}
| {:operator_arity, charlist()}
| {:operator_call, charlist()}
| :none
| {:sigil, charlist()}
| {:struct, charlist()}
| {:unquoted_atom, charlist()}
when inside_dot:
{:alias, charlist()}
| {:dot, inside_dot, charlist()}
| {:module_attribute, charlist()}
| {:unquoted_atom, charlist()}
| {:var, charlist()}
接收一个字符串并返回游标上下文。
此函数接收带有 Elixir 代码片段的字符串,表示光标位置,并基于该字符串提供有关所述位置的上下文信息。然后可以使用此函数的返返回提供提示、建议和自动完成函数。
此函数提供了尽力而为的检测,并且可能并非在所有情况下都准确。请参阅下面的"Limitations" 部分。
考虑在处理此函数的返回类型时添加 catch-all 子句,因为新的游标信息可能会在未来版本中添加。
例子
iex> Code.Fragment.cursor_context("")
:expr
iex> Code.Fragment.cursor_context("hello_wor")
{:local_or_var, 'hello_wor'}
返回值
-
{:alias, charlist}- 上下文是别名,可能是嵌套的,例如Hello.Wor或HelloWor -
{:dot, inside_dot, charlist}- 上下文是一个点,其中inside_dot是{:var, charlist}、{:alias, charlist}、{:module_attribute, charlist}、{:unquoted_atom, charlist}或dot本身。如果给出了 var,这可能是远程调用或映射字段访问。例如Hello.wor、:hello.wor、hello.wor、Hello.nested.wor、hello.nested.wor和@hello.world -
{:dot_arity, inside_dot, charlist}- 上下文是一个点,其中inside_dot是{:var, charlist}、{:alias, charlist}、{:module_attribute, charlist}、{:unquoted_atom, charlist}或dot本身。如果给出了一个 var,它必须是一个远程的 arity。例如Hello.world/、:hello.world/、hello.world/2和@hello.world/2 -
{:dot_call, inside_dot, charlist}- 上下文是一个点调用。这意味着在表达式之后添加了括号或空格。其中inside_dot是{:var, charlist}、{:alias, charlist}、{:module_attribute, charlist}、{:unquoted_atom, charlist}或dot本身。如果给出了 var,则它必须是远程调用。例如Hello.world(、:hello.world(、Hello.world、hello.world(、hello.world和@hello.world( -
:expr- 可以是任何表达式。自动完成可能会建议一个别名、本地或 var -
{:local_or_var, charlist}- 上下文是变量或本地(导入或本地)调用,例如hello_wor -
{:local_arity, charlist}- 上下文是本地(导入或本地)arity,例如hello_world/ -
{:local_call, charlist}- 上下文是本地(导入或本地)调用,例如hello_world(和hello_world -
{:module_attribute, charlist}- 上下文是模块属性,例如@hello_wor -
{:operator, charlist}- 上下文是一个运算符,例如+或==。请注意文本运算符,例如when不会显示为运算符,而是显示为:local_or_var。@永远不是:operator并且始终是:module_attribute -
{:operator_arity, charlist}- 上下文是一个运算符,它是一个后跟 /的运算符,例如+/、not/或when/ -
{:operator_call, charlist}- 上下文是一个运算符调用,它是一个运算符后跟空格,例如left +、not或x when -
:none- 没有上下文可能 -
{:sigil, charlist}- 上下文是一个印记。它可以是符号的开头,例如~或~s,也可以是以~开头的运算符,例如~>和~>> -
{:struct, charlist}- 上下文是一个结构,例如%、%UR或%URI -
{:unquoted_atom, charlist}- 上下文是一个未引用的原子。这可以是任何原子或表示模块的原子
限制
当前算法只考虑输入的最后一行。这意味着它还将在字符串、heredocs 等中显示建议,这是有意的,因为它有助于文档测试、引用等。
相关用法
- Elixir Code.Fragment.container_cursor_to_quoted用法及代码示例
- Elixir Code.Fragment.surround_context用法及代码示例
- Elixir Code.prepend_path用法及代码示例
- Elixir Code.compiler_options用法及代码示例
- Elixir Code.quoted_to_algebra用法及代码示例
- Elixir Code.put_compiler_option用法及代码示例
- Elixir Code.ensure_compiled用法及代码示例
- Elixir Code.format_string!用法及代码示例
- Elixir Code.required_files用法及代码示例
- Elixir Code.get_compiler_option用法及代码示例
- Elixir Code.available_compiler_options用法及代码示例
- Elixir Code.ensure_loaded?用法及代码示例
- Elixir Code.eval_quoted用法及代码示例
- Elixir Code.require_file用法及代码示例
- Elixir Code.delete_path用法及代码示例
- Elixir Code.append_path用法及代码示例
- Elixir Code.ensure_loaded用法及代码示例
- Elixir Code.unrequire_files用法及代码示例
- Elixir Code.fetch_docs用法及代码示例
- Elixir Code.string_to_quoted_with_comments用法及代码示例
- Elixir Code.eval_string用法及代码示例
- Elixir Code用法及代码示例
- Elixir Config.config_env用法及代码示例
- Elixir Config.config用法及代码示例
- Elixir Config.Reader用法及代码示例
注:本文由纯净天空筛选整理自elixir-lang.org大神的英文原创作品 Code.Fragment.cursor_context(fragment, opts \\ [])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
