feat(mongodb): complete bulkWrite operations in completions

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-09-27 10:27:16 +08:00
1 parent 26138a6236
commit 6ee12ab0a5
3 files changed
+148 -8

No files matched your search

+63 -5
View File
@@ -7,6 +7,8 @@ import {
FIELD_QUERY_OPERATORS,
PIPELINE_STAGES,
PUSH_MODIFIERS,
BULK_WRITE_OPERATION_FIELDS,
BULK_WRITE_OPERATIONS,
METHOD_OPTION_KEYS,
STAGE_OPTION_KEYS,
TOP_LEVEL_QUERY_OPERATORS,
@@ -48,7 +50,9 @@ export type MongoCompletionMode =
| "accumulator"
| "stage"
| "stageOption"
| "methodOption";
| "methodOption"
| "bulkWriteOperation"
| "bulkWriteField";
export interface MongoCompletionField {
name: string;
@@ -78,6 +82,8 @@ export interface MongoCompletionContext {
stage?: string;
/** Collection method whose options object the cursor sits in. */
method?: string;
/** bulkWrite operation (`updateOne`, `deleteMany`, …) whose body the cursor sits in. */
bulkWriteOperation?: string;
}
export interface MongoCompletionInput {
@@ -253,7 +259,7 @@ export function getMongoCompletionContext(text: string, cursor: number): MongoCo
const collection = extractActiveCollection(text, safeCursor);
const { prefix, from } = readMongoPropertyPrefix(text, safeCursor);
const replaceClosingQuote = closingQuoteAtCursor(prefix, text, safeCursor);
const at = (mode: MongoCompletionMode, stage?: string, method?: string): MongoCompletionContext => ({ mode, prefix, from, replaceClosingQuote, collection, stage, method });
const at = (mode: MongoCompletionMode, stage?: string, method?: string, bulkWriteOperation?: string): MongoCompletionContext => ({ mode, prefix, from, replaceClosingQuote, collection, stage, method, bulkWriteOperation });
if (isInsideMongoComment(text, safeCursor)) return { mode: "none", prefix: "", from: safeCursor };
@@ -298,7 +304,10 @@ export function getMongoCompletionContext(text: string, cursor: number): MongoCo
if (!scan) return at("root");
const classified = classifyCursorInCall(call.method, scan);
return { ...at(classified.mode, classified.stage, classified.method), collection: classified.collection ?? collection };
return {
...at(classified.mode, classified.stage, classified.method, classified.bulkWriteOperation),
collection: classified.collection ?? collection,
};
}
export function buildMongoCompletionItems(text: string, cursor: number, input: MongoCompletionInput = {}): MongoCompletionItem[] {
@@ -378,6 +387,12 @@ export function buildMongoCompletionItemsFromContext(context: MongoCompletionCon
case "methodOption":
items = specItems(METHOD_OPTION_KEYS[context.method ?? ""] ?? [], prefix, `${context.method}() option`, 100);
break;
case "bulkWriteOperation":
items = specItems(BULK_WRITE_OPERATIONS, prefix, "bulkWrite operation", 100);
break;
case "bulkWriteField":
items = specItems(BULK_WRITE_OPERATION_FIELDS[context.bulkWriteOperation ?? ""] ?? [], prefix, `${context.bulkWriteOperation} field`, 100);
break;
default:
items = [];
}
@@ -555,6 +570,7 @@ interface MongoCursorClass {
stage?: string;
collection?: string;
method?: string;
bulkWriteOperation?: string;
}
/**
@@ -655,9 +671,8 @@ function classifyCursorInCall(method: string, scan: MongoCallScan): MongoCursorC
return { mode: scan.stack.length === 0 ? "fieldPath" : "none" };
case "pipeline":
return classifyPipeline(scan);
// bulkWrite operations are `{ <op>: { filter, update, … } }` entries; completing inside them is a follow-up.
case "operations":
return { mode: "none" };
return classifyBulkWriteOperations(scan);
case "options":
return classifyMethodOptions(method, scan);
default:
@@ -745,6 +760,49 @@ function classifyMethodOptions(method: string, scan: MongoCallScan): MongoCursor
return { mode: inner.kind === "object" ? "methodOption" : "none", method };
}
/**
* `bulkWrite([{ <operation>: { <field>: … } }])`, which nests one level deeper than the other
* arguments: the array holds operation wrappers, each wrapper holds exactly one operation whose
* body carries the fields, and those fields are ordinary filters, updates and documents.
*/
function classifyBulkWriteOperations(scan: MongoCallScan): MongoCursorClass {
const arrayIndex = scan.stack.findIndex((container) => container.kind === "array");
if (arrayIndex !== 0) return { mode: "none" };
const wrapper = scan.stack[arrayIndex + 1];
if (!wrapper || wrapper.kind !== "object") return { mode: "none" };
// `[{ … }]` — naming the operation.
if (scan.stack.length - 1 === arrayIndex + 1) {
return { mode: scan.inValue ? "none" : "bulkWriteOperation" };
}
const operation = scan.stack[arrayIndex + 2]?.key ?? "";
const fields = BULK_WRITE_OPERATION_FIELDS[operation];
if (!fields) return { mode: "none" };
// `[{ updateOne: { … } }]` — naming a field of the operation.
if (scan.stack.length - 1 === arrayIndex + 2) {
return { mode: scan.inValue ? "value" : "bulkWriteField", bulkWriteOperation: operation };
}
// Inside a field's value, where the shapes are the ordinary ones.
const fieldIndex = arrayIndex + 3;
switch (scan.stack[fieldIndex]?.key ?? "") {
case "filter":
return { mode: classifyFilter(scan, fieldIndex), bulkWriteOperation: operation };
case "arrayFilters":
return { mode: classifyFilter(scan, fieldIndex + 1), bulkWriteOperation: operation };
case "update":
return { mode: classifyUpdate(scan, fieldIndex), bulkWriteOperation: operation };
case "document":
case "replacement":
return { mode: classifyDocument(scan, fieldIndex), bulkWriteOperation: operation };
default:
return { mode: "none" };
}
}
function classifyPipeline(scan: MongoCallScan): MongoCursorClass {
const pipelineIndex = findPipelineArrayIndex(scan.stack);
if (pipelineIndex < 0) return { mode: "none" };
@@ -294,6 +294,33 @@ export const EXPRESSION_OPERATORS: MongoOperatorSpec[] = specs([
["$rand", "Returns a random float between 0 and 1", "$rand: {}"],
]);
/**
* The operations a `bulkWrite()` array accepts, and the fields each one allows.
*
* Both lists are exactly what the shell parser accepts: it rejects an unknown operation key and
* an unknown field inside one, so anything extra here would complete into a command that fails.
*/
export const BULK_WRITE_OPERATIONS: MongoOperatorSpec[] = specs([
["insertOne", "Insert one document", "insertOne: { document: { ${} } }"],
["updateOne", "Update the first matching document", "updateOne: { filter: { ${} }, update: { $set: { ${} } } }"],
["updateMany", "Update every matching document", "updateMany: { filter: { ${} }, update: { $set: { ${} } } }"],
["replaceOne", "Replace the first matching document", "replaceOne: { filter: { ${} }, replacement: { ${} } }"],
["deleteOne", "Delete the first matching document", "deleteOne: { filter: { ${} } }"],
["deleteMany", "Delete every matching document", "deleteMany: { filter: { ${} } }"],
]);
const BULK_WRITE_FILTER: Spec = ["filter", "Documents the operation applies to", "filter: { ${} }"];
const BULK_WRITE_UPSERT: Spec = ["upsert", "Insert the document when the filter matches nothing", "upsert: true"];
export const BULK_WRITE_OPERATION_FIELDS: Record<string, MongoOperatorSpec[]> = {
insertOne: specs([["document", "Document to insert", "document: { ${} }"]]),
updateOne: specs([BULK_WRITE_FILTER, ["update", "Update operators or an aggregation pipeline", "update: { $set: { ${} } }"], BULK_WRITE_UPSERT, ["arrayFilters", "Conditions for the $[<identifier>] positional operator", 'arrayFilters: [{ "${elem}.${field}": ${} }]']]),
updateMany: specs([BULK_WRITE_FILTER, ["update", "Update operators or an aggregation pipeline", "update: { $set: { ${} } }"], BULK_WRITE_UPSERT, ["arrayFilters", "Conditions for the $[<identifier>] positional operator", 'arrayFilters: [{ "${elem}.${field}": ${} }]']]),
replaceOne: specs([BULK_WRITE_FILTER, ["replacement", "Whole document that replaces the match", "replacement: { ${} }"], BULK_WRITE_UPSERT]),
deleteOne: specs([BULK_WRITE_FILTER]),
deleteMany: specs([BULK_WRITE_FILTER]),
};
/**
* Option keys accepted by a collection method's trailing options argument.
*