Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(mongodb): stay quiet in unmodelled argument positions

Loc Nguyenc571c1ddc73c
diff --git a/apps/desktop/src/lib/mongo/mongoCompletion.ts b/apps/desktop/src/lib/mongo/mongoCompletion.ts
index 4fe6ee6efd..b663657f3c 100644
--- a/apps/desktop/src/lib/mongo/mongoCompletion.ts
+++ b/apps/desktop/src/lib/mongo/mongoCompletion.ts
@@ -303,10 +303,13 @@ export function getMongoCompletionContext(text: string, cursor: number): MongoCo
}
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);
- if (!scan) return at("root");
+ if (!scan) return isInsideCallArguments(beforeCursor) || isAfterUseKeyword(beforeCursor) ? at("none") : at("root");
const classified = classifyCursorInCall(call.method, scan);
return {
@@ -1253,6 +1256,27 @@ function skipMongoStringOrComment(text: string, i: number, end: number): number
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. */
function maskMongoLiterals(text: string): string {
const chars = [...text];
diff --git a/packages/app-tests/mongoCompletion.test.ts b/packages/app-tests/mongoCompletion.test.ts
index 9519d37f87..cdf9d3aa7f 100644
--- a/packages/app-tests/mongoCompletion.test.ts
+++ b/packages/app-tests/mongoCompletion.test.ts
@@ -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", () => {
// 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