Skip to content

Commit c265498

Browse files
authored
fix(cli-repl): fix query operator completion MONGOSH-793 (#911)
My previous fix for REPL command completion in 720728d was wrong, this fixes the integraion of the Node.js and mongosh autocompleters properly.
1 parent 489ffdb commit c265498

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,13 @@ describe('CliRepl', () => {
11771177
await waitCompletion(cliRepl.bus);
11781178
expect(output).to.include('use admin');
11791179
});
1180+
1181+
it('completes query operators', async() => {
1182+
input.write('db.movies.find({year: {$g');
1183+
await tabtab();
1184+
await waitCompletion(cliRepl.bus);
1185+
expect(output).to.include('db.movies.find({year: {$gte');
1186+
});
11801187
});
11811188
}
11821189
});

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,22 @@ class MongoshNodeRepl implements EvaluationListener {
204204
if (mongoshResultsExclusive) {
205205
return [mongoshResults, text];
206206
}
207+
208+
// The REPL completer may not complete the entire string; for example,
209+
// when completing ".ed" to ".editor", it reports as having completed
210+
// only the last piece ("ed"), or when completing "{ $g", it completes
211+
// only "$g" and not the entire result.
212+
// The mongosh completer always completes on the entire string.
213+
// In order to align them, we always extend the REPL results to include
214+
// the full string prefix.
215+
const replResultPrefix = replOrig ? text.substr(0, text.lastIndexOf(replOrig)) : '';
216+
const longReplResults = replResults.map((result: string) => replResultPrefix + result);
217+
207218
// Remove duplicates, because shell API methods might otherwise show
208219
// up in both completions.
209-
const deduped = [...new Set([...replResults, ...mongoshResults])];
220+
const deduped = [...new Set([...longReplResults, ...mongoshResults])];
210221

211-
// Use the REPL completer's original text when available, because that
212-
// makes a difference for completion of REPL commands like `.editor`.
213-
return [deduped, replOrig ?? text];
222+
return [deduped, text];
214223
} finally {
215224
this.insideAutoCompleteOrGetPrompt = false;
216225
}

0 commit comments

Comments
 (0)