import type { AutocompletionContext } from '@mongodb-js/mongodb-ts-autocomplete'; import type { TypeSignature } from '@mongosh/shell-api'; import { signatures as shellSignatures } from '@mongosh/shell-api'; type TypeSignatureAttributes = { [key: string]: TypeSignature }; export async function directCommandCompleter( context: AutocompletionContext, line: string ): Promise { const SHELL_COMPLETIONS = shellSignatures.ShellApi .attributes as TypeSignatureAttributes; // Split at space-to-non-space transitions when looking at this as a command, // because multiple spaces (e.g. 'show collections') are valid in commands. // This split keeps the spaces intact so we can join them back later. const splitLineWhitespace = line.split(/(? item.trim()) )) || []; // Adjust to full input, because `completer` only completed the last item // in the line, e.g. ['profile'] -> ['show profile'] const fullLineHits = hits.map((hit) => [...splitLineWhitespace.slice(0, -1), hit].join('') ); return fullLineHits; }