completer 函数的使用


¥Use of the completer function

completer 函数将用户输入的当前行作为参数,并返回包含 2 个条目的 Array

¥The completer function takes the current line entered by the user as an argument, and returns an Array with 2 entries:

  • 使用匹配条目的 Array 补全。

    ¥An Array with matching entries for the completion.

  • 用于匹配的子字符串。

    ¥The substring that was used for the matching.

例如:[[substr1, substr2, ...], originalsubstring]

¥For instance: [[substr1, substr2, ...], originalsubstring].

function completer(line) {
  const completions = '.help .error .exit .quit .q'.split(' ');
  const hits = completions.filter((c) => c.startsWith(line));
  // Show all completions if none found
  return [hits.length ? hits : completions, line];
} 

completer 函数也可以返回 <Promise>,或者是异步的:

¥The completer function can also return a <Promise>, or be asynchronous:

async function completer(linePartial) {
  await someAsyncWork();
  return [['123'], linePartial];
}