completionCommand method

  1. @override
String completionCommand(
  1. String word, {
  2. required bool isCommand,
})
override

A command, in this dialect, that prints TAB-completion candidates for word — one per line, each a full replacement for the typed word (so it includes whatever was typed), with directories suffixed / so the editor can avoid appending a trailing space.

When isCommand is true and word has no path separator, the word is in command position and completes against executables on $PATH (plus the shell's own builtins/cmdlets where natural); otherwise it globs paths.

Run as a one-off exec on the node, in the matching shell family (see resolveShellInvocation's shellFamily hint), so the snippet's syntax is understood and $PATH/cwd match the interactive session.

Implementation

@override
String completionCommand(String word, {required bool isCommand}) {
  final w = _posixSingleQuote(word);
  // Glob `<word>*`, marking directories with a trailing slash. `"$w"*` keeps
  // the typed prefix literal (handles spaces/special chars) while globbing.
  const fileGlob =
      r'for p in "$w"*; do [ -e "$p" ] || continue; '
      r'if [ -d "$p" ]; then printf "%s/\n" "$p"; '
      r'else printf "%s\n" "$p"; fi; done';
  if (!isCommand) {
    return 'w=$w; $fileGlob';
  }
  // Command position: a word with a slash is a path; otherwise scan $PATH for
  // executables and print their basenames, sorted and de-duplicated.
  const pathScan =
      r'IFS=:; for d in $PATH; do [ -d "$d" ] || continue; '
      r'for p in "$d"/"$w"*; do [ -f "$p" ] && [ -x "$p" ] && '
      r'printf "%s\n" "${p##*/}"; done; done | sort -u';
  return 'w=$w; case "\$w" in */*) $fileGlob ;; *) $pathScan ;; esac';
}