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}) {
  // cmd has no robust quoting for the mixed `for`-set / `where` contexts
  // below, so drop the few characters that would break the command line.
  // Best-effort, matching this dialect's degraded marker (no rich git/priv);
  // words with spaces or cmd metacharacters may not complete under cmd.
  final w = word.replaceAll(RegExp(r'[\"%&|<>^]'), '');
  final treatAsPath = !isCommand || word.contains('/') || word.contains(r'\');
  if (treatAsPath) {
    // Directories first (suffixed `/`), then files (skipped by the dir test).
    // `for` keeps the directory prefix from the typed pattern.
    return 'for /d %A in ($w*) do @echo %A/'
        ' & for %A in ($w*) do @if not exist "%A\\" @echo %A';
  }
  // Command position: matching executables on %PATH% (and cwd), basenames
  // only (`%~nxA`); `where` honours %PATHEXT%.
  return 'for /f "delims=" %A in (\'where "$w*" 2^>nul\') do @echo %~nxA';
}