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 = _psSingleQuote(word);
  final treatAsPath = !isCommand || word.contains('/') || word.contains(r'\');
  // `$w` holds the typed word. Split off any directory prefix on the last
  // separator (`/` or `\`) so each candidate keeps the prefix the user typed;
  // list the parent dir, filter by the leaf prefix, suffix `/` for containers.
  const pathBody =
      r"$i=[Math]::Max($w.LastIndexOf('/'),$w.LastIndexOf('\'));"
      r"if($i -ge 0){$pre=$w.Substring(0,$i+1);$leaf=$w.Substring($i+1)}"
      r"else{$pre='';$leaf=$w};$base=if($pre){$pre}else{'.'};"
      r"Get-ChildItem -Force -LiteralPath $base -ErrorAction SilentlyContinue|"
      r"Where-Object{$_.Name -like ($leaf+'*')}|"
      r'ForEach-Object{$p=$pre+$_.Name;if($_.PSIsContainer){"$p/"}else{"$p"}}';
  // Command position: every command whose name starts with the word —
  // applications on $PATH plus cmdlets/functions/aliases — by name, deduped.
  const cmdBody =
      r"Get-Command -All -CommandType Application,Cmdlet,Function,Alias "
      r"-Name ($w+'*') -ErrorAction SilentlyContinue|"
      r'ForEach-Object{$_.Name}|Sort-Object -Unique';
  return '\$w=$w;${treatAsPath ? pathBody : cmdBody}';
}