Skip to content

Commit b622534

Browse files
committed
chore(cli-repl): clean autocomplete implementation up a bit
Best viewed as a whitespace-diff-disabled change, doesn't include any functional work.
1 parent cbc264a commit b622534

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -388,52 +388,59 @@ class MongoshNodeRepl implements EvaluationListener {
388388
null,
389389
instanceState.getAutocompleteParameters()
390390
);
391-
(repl as Mutable<typeof repl>).completer = callbackify(
392-
async (text: string): Promise<[string[], string]> => {
393-
this.insideAutoCompleteOrGetPrompt = true;
394-
try {
395-
// Merge the results from the repl completer and the mongosh completer.
396-
const [
397-
[replResults, replOrig],
398-
[mongoshResults, , mongoshResultsExclusive],
399-
] = await Promise.all([
400-
(async () => (await origReplCompleter(text)) || [[]])(),
401-
(async () => await mongoshCompleter(text))(),
402-
]);
403-
this.bus.emit('mongosh:autocompletion-complete'); // For testing.
404-
405-
// Sometimes the mongosh completion knows that what it is doing is right,
406-
// and that autocompletion based on inspecting the actual objects that
407-
// are being accessed will not be helpful, e.g. in `use a<tab>`, we know
408-
// that we want *only* database names and not e.g. `assert`.
409-
if (mongoshResultsExclusive) {
410-
return [mongoshResults, text];
411-
}
391+
const innerCompleter = async (
392+
text: string
393+
): Promise<[string[], string]> => {
394+
// Merge the results from the repl completer and the mongosh completer.
395+
const [
396+
[replResults, replOrig],
397+
[mongoshResults, , mongoshResultsExclusive],
398+
] = await Promise.all([
399+
(async () => (await origReplCompleter(text)) || [[]])(),
400+
(async () => await mongoshCompleter(text))(),
401+
]);
402+
this.bus.emit('mongosh:autocompletion-complete'); // For testing.
403+
404+
// Sometimes the mongosh completion knows that what it is doing is right,
405+
// and that autocompletion based on inspecting the actual objects that
406+
// are being accessed will not be helpful, e.g. in `use a<tab>`, we know
407+
// that we want *only* database names and not e.g. `assert`.
408+
if (mongoshResultsExclusive) {
409+
return [mongoshResults, text];
410+
}
412411

413-
// The REPL completer may not complete the entire string; for example,
414-
// when completing ".ed" to ".editor", it reports as having completed
415-
// only the last piece ("ed"), or when completing "{ $g", it completes
416-
// only "$g" and not the entire result.
417-
// The mongosh completer always completes on the entire string.
418-
// In order to align them, we always extend the REPL results to include
419-
// the full string prefix.
420-
const replResultPrefix = replOrig
421-
? text.substr(0, text.lastIndexOf(replOrig))
422-
: '';
423-
const longReplResults = replResults.map(
424-
(result: string) => replResultPrefix + result
425-
);
412+
// The REPL completer may not complete the entire string; for example,
413+
// when completing ".ed" to ".editor", it reports as having completed
414+
// only the last piece ("ed"), or when completing "{ $g", it completes
415+
// only "$g" and not the entire result.
416+
// The mongosh completer always completes on the entire string.
417+
// In order to align them, we always extend the REPL results to include
418+
// the full string prefix.
419+
const replResultPrefix = replOrig
420+
? text.substr(0, text.lastIndexOf(replOrig))
421+
: '';
422+
const longReplResults = replResults.map(
423+
(result: string) => replResultPrefix + result
424+
);
425+
426+
// Remove duplicates, because shell API methods might otherwise show
427+
// up in both completions.
428+
const deduped = [...new Set([...longReplResults, ...mongoshResults])];
426429

427-
// Remove duplicates, because shell API methods might otherwise show
428-
// up in both completions.
429-
const deduped = [...new Set([...longReplResults, ...mongoshResults])];
430+
return [deduped, text];
431+
};
430432

431-
return [deduped, text];
433+
const nodeReplCompleter = callbackify(
434+
async (text: string): Promise<[string[], string]> => {
435+
this.insideAutoCompleteOrGetPrompt = true;
436+
try {
437+
return await innerCompleter(text);
432438
} finally {
433439
this.insideAutoCompleteOrGetPrompt = false;
434440
}
435441
}
436442
);
443+
(repl as Mutable<typeof repl>).completer = nodeReplCompleter;
437444

438445
// This is used below for multiline history manipulation.
439446
let originalHistory: string[] | null = null;

0 commit comments

Comments
 (0)