fix(mongodb): stay quiet in unmodelled argument positions

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-09-28 13:50:32 +08:00
1 parent 52eaf32233
commit c571c1ddc7
2 files changed
+56 -2

No files matched your search

+26 -2
View File
@@ -303,10 +303,13 @@ export function getMongoCompletionContext(text: string, cursor: number): MongoCo
} }
const call = findInnermostMongoCall(beforeCursor); const call = findInnermostMongoCall(beforeCursor);
if (!call) return at("root"); // Top-level snippets belong at the start of a command. Inside an argument list — of a method
// this engine does not model (`limit(`, `drop(`, `dropIndex("`, `runCommand({`, …) or after a
// `use` — they are noise: `db.collection.find` is not something you can type there.
if (!call) return isInsideCallArguments(beforeCursor) || isAfterUseKeyword(beforeCursor) ? at("none") : at("root");
const scan = scanMongoCallArguments(text, call.openParenIndex + 1, safeCursor); const scan = scanMongoCallArguments(text, call.openParenIndex + 1, safeCursor);
if (!scan) return at("root"); if (!scan) return isInsideCallArguments(beforeCursor) || isAfterUseKeyword(beforeCursor) ? at("none") : at("root");
const classified = classifyCursorInCall(call.method, scan); const classified = classifyCursorInCall(call.method, scan);
return { return {
@@ -1253,6 +1256,27 @@ function skipMongoStringOrComment(text: string, i: number, end: number): number
return i; return i;
} }
/**
* Whether the cursor sits inside an unclosed `(` of the current command. Literals and comments
* are masked first so a parenthesis inside a string does not count, and the depth resets at `;`
* because an unclosed call cannot span two commands.
*/
function isInsideCallArguments(beforeCursor: string): boolean {
const masked = maskMongoLiterals(beforeCursor);
let depth = 0;
for (const char of masked) {
if (char === "(") depth++;
else if (char === ")") depth = Math.max(0, depth - 1);
else if (char === ";") depth = 0;
}
return depth > 0;
}
/** `use <database>` takes a database name, which this engine has no list of, so it stays quiet. */
function isAfterUseKeyword(beforeCursor: string): boolean {
return /(?:^|[\s;])use\s+[\w$-]*$/.test(maskMongoLiterals(beforeCursor));
}
/** Blank out string/comment CONTENT (preserving length, so offsets stay valid) before pattern matching. */ /** Blank out string/comment CONTENT (preserving length, so offsets stay valid) before pattern matching. */
function maskMongoLiterals(text: string): string { function maskMongoLiterals(text: string): string {
const chars = [...text]; const chars = [...text];
+30
View File
@@ -589,6 +589,36 @@ test("every suggested option key parses on the method that offers it", () => {
} }
}); });
test("offers nothing rather than top-level snippets inside an unmodelled argument", () => {
// `db.collection.find` is not something that can be typed inside these parentheses, so the
// top-level snippets are noise there; the engine stays quiet until the argument is modelled.
for (const text of [
"db.users.find({}).limit(",
"db.users.find({}).skip(",
"db.users.find({}).explain(",
"db.users.find({}).collation({ ",
"db.users.drop(",
"db.users.renameCollection(",
'db.users.dropIndex("',
"db.users.estimatedDocumentCount(",
"db.runCommand({ ",
'db.createCollection("x", { ',
"use ",
"use ord",
// A modelled call followed by `use` takes the scan-failure path, not the unmodelled-call one.
"db.users.find({}); use ",
]) {
assert.deepEqual(labels(text, { fields, collections }), [], text);
}
// A command still starts with the top-level snippets, including after one has finished.
assert.ok(labels("").includes("db.collection.find"));
assert.ok(labels("db.users.find({});\n").includes("db.collection.find"));
assert.ok(labels("db.users.find({})\n").includes("db.collection.find"));
// A parenthesis inside a string does not count as an open argument list.
assert.ok(labels('db.users.find({ name: "(" });\n').includes("db.collection.find"));
});
test("completes commands addressed to another database through getSiblingDB", () => { test("completes commands addressed to another database through getSiblingDB", () => {
// The parser accepts `db.getSiblingDB("x").coll.find(…)`, so the editor has to complete it the // The parser accepts `db.getSiblingDB("x").coll.find(…)`, so the editor has to complete it the
// same way it completes `db.coll.find(…)` — including resolving the collection, which is what // same way it completes `db.coll.find(…)` — including resolving the collection, which is what