當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Elixir Code.Fragment.cursor_context用法及代碼示例

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.WorHelloWor

  • {:dot, inside_dot, charlist} - 上下文是一個點,其中 inside_dot{:var, charlist}{:alias, charlist}{:module_attribute, charlist}{:unquoted_atom, charlist}dot 本身。如果給出了 var,這可能是遠程調用或映射字段訪問。例如 Hello.wor:hello.worhello.worHello.nested.worhello.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.worldhello.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 +notx when

  • :none - 沒有上下文可能

  • {:sigil, charlist} - 上下文是一個印記。它可以是符號的開頭,例如 ~~s ,也可以是以 ~ 開頭的運算符,例如 ~>~>>

  • {:struct, charlist} - 上下文是一個結構,例如 %%UR%URI

  • {:unquoted_atom, charlist} - 上下文是一個未引用的原子。這可以是任何原子或表示模塊的原子

限製

當前算法隻考慮輸入的最後一行。這意味著它還將在字符串、heredocs 等中顯示建議,這是有意的,因為它有助於文檔測試、引用等。

相關用法


注:本文由純淨天空篩選整理自elixir-lang.org大神的英文原創作品 Code.Fragment.cursor_context(fragment, opts \\ [])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。