feat(completion): complete mongo aggregation field references
This commit is contained in:
1 parent
b1d068cd97
commit
9089131184
3 files changed
+291
-4
No files matched your search
@@ -117,6 +117,8 @@ export interface MongoCompletionContext {
|
||||
operator?: string;
|
||||
/** Which fixed value set the cursor's value position accepts (`$type`, `explain`, `caseFirst`). */
|
||||
enumKey?: string;
|
||||
/** User variables in scope for aggregation expressions. */
|
||||
variables?: string[];
|
||||
}
|
||||
|
||||
export interface MongoCompletionInput {
|
||||
@@ -434,11 +436,13 @@ export function getMongoCompletionContext(text: string, cursor: number): MongoCo
|
||||
if (!scan) return isInsideCallArguments(beforeCursor) || isAfterUseKeyword(beforeCursor) || isAfterShowKeyword(beforeCursor) ? at("none") : at("root");
|
||||
|
||||
const classified = classifyCursorInCall(call.method, scan);
|
||||
const variables = classified.mode === "fieldRef" || classified.mode === "expression" ? collectScopeVariables(scan) : undefined;
|
||||
return {
|
||||
...at(classified.mode, classified.stage, classified.method, classified.bulkWriteOperation, classified.keyMap),
|
||||
...(classified.operator ? { operator: classified.operator } : {}),
|
||||
...(classified.enumKey ? { enumKey: classified.enumKey } : {}),
|
||||
...(classified.pipelineKind ? { pipelineKind: classified.pipelineKind } : {}),
|
||||
...(variables ? { variables } : {}),
|
||||
collection: classified.collection ?? collection,
|
||||
};
|
||||
}
|
||||
@@ -496,7 +500,7 @@ export function buildMongoCompletionItemsFromContext(context: MongoCompletionCon
|
||||
items = fieldPathItems(prefix, fields);
|
||||
break;
|
||||
case "fieldRef":
|
||||
items = fieldRefItems(prefix, fields);
|
||||
items = isVariablePrefix(prefix) ? variableItems(prefix, context.variables, context.stage) : fieldRefItems(prefix, fields);
|
||||
break;
|
||||
case "indexName":
|
||||
items = indexNameItems(prefix, input.indexes ?? [], context.method);
|
||||
@@ -522,7 +526,7 @@ export function buildMongoCompletionItemsFromContext(context: MongoCompletionCon
|
||||
items = specItems(PROJECTION_OPERATORS, prefix, "projection operator", 100);
|
||||
break;
|
||||
case "expression":
|
||||
items = [...specItems(EXPRESSION_OPERATORS, prefix, "aggregation expression", 100), ...fieldRefItems(prefix, fields, 80)];
|
||||
items = isVariablePrefix(prefix) ? variableItems(prefix, context.variables, context.stage) : [...specItems(EXPRESSION_OPERATORS, prefix, "aggregation expression", 100), ...fieldRefItems(prefix, fields, 80)];
|
||||
break;
|
||||
case "accumulator":
|
||||
items = specItems(ACCUMULATORS, prefix, "accumulator", 100);
|
||||
@@ -633,7 +637,8 @@ export function getMongoDocumentQueryCompletionContext(text: string, cursor: num
|
||||
if (classified.mode === "none") return nothing;
|
||||
|
||||
const { prefix, from } = readMongoPropertyPrefix(text, safeCursor);
|
||||
return { ...classified, prefix, from, replaceClosingQuote: closingQuoteAtCursor(prefix, text, safeCursor) };
|
||||
const variables = classified.mode === "fieldRef" || classified.mode === "expression" ? collectScopeVariables(scan) : undefined;
|
||||
return { ...classified, prefix, from, replaceClosingQuote: closingQuoteAtCursor(prefix, text, safeCursor), ...(variables ? { variables } : {}) };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -755,6 +760,10 @@ interface MongoContainer {
|
||||
key: string | null;
|
||||
/** Closed string literal values in this object container, e.g. `{ from: "orders" }`. */
|
||||
stringValues?: Record<string, string>;
|
||||
/** Property keys defined in this object container. */
|
||||
keys?: string[];
|
||||
/** Variables defined by an enclosing child `vars: { … }` or `let: { … }`. */
|
||||
letVars?: string[];
|
||||
}
|
||||
|
||||
interface MongoCallScan {
|
||||
@@ -778,6 +787,7 @@ interface MongoCursorClass {
|
||||
keyMap?: string;
|
||||
operator?: string;
|
||||
enumKey?: string;
|
||||
variables?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -846,7 +856,13 @@ function scanMongoCallArguments(text: string, start: number, cursor: number): Mo
|
||||
}
|
||||
if (char === "}" || char === "]" || char === ")") {
|
||||
if (stack.length === 0) return null;
|
||||
stack.pop();
|
||||
const popped = stack.pop();
|
||||
if (popped && popped.kind === "object" && (popped.key === "vars" || popped.key === "let")) {
|
||||
const parent = stack[stack.length - 1];
|
||||
if (parent && parent.kind === "object") {
|
||||
parent.letVars = [...(parent.letVars ?? []), ...(popped.keys ?? [])];
|
||||
}
|
||||
}
|
||||
token = "";
|
||||
valueKey = null;
|
||||
inValue = false;
|
||||
@@ -856,6 +872,16 @@ function scanMongoCallArguments(text: string, start: number, cursor: number): Mo
|
||||
valueKey = token.trim() || valueKey;
|
||||
token = "";
|
||||
inValue = true;
|
||||
if (valueKey) {
|
||||
const inner = stack[stack.length - 1];
|
||||
if (inner && inner.kind === "object") {
|
||||
const cleanKey = valueKey.replace(/^["']|["']$/g, "");
|
||||
inner.keys ??= [];
|
||||
if (!inner.keys.includes(cleanKey)) {
|
||||
inner.keys.push(cleanKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (char === ",") {
|
||||
@@ -871,6 +897,54 @@ function scanMongoCallArguments(text: string, start: number, cursor: number): Mo
|
||||
return { argIndex, stack, valueKey, inValue, inString: quote !== null };
|
||||
}
|
||||
|
||||
function isExpressionArray(scan: MongoCallScan): boolean {
|
||||
const inner = innermost(scan);
|
||||
if (inner?.kind !== "array") return false;
|
||||
if (inner.key?.startsWith("$")) return true;
|
||||
for (let i = scan.stack.length - 1; i >= 0; i--) {
|
||||
const container = scan.stack[i];
|
||||
if (container.key?.startsWith("$")) return true;
|
||||
if (container.kind !== "array") break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function collectScopeVariables(scan: MongoCallScan): string[] {
|
||||
const vars = new Set<string>();
|
||||
|
||||
for (let i = scan.stack.length - 1; i >= 0; i--) {
|
||||
const container = scan.stack[i];
|
||||
if (!container || container.kind !== "object") continue;
|
||||
|
||||
const childKey = i === scan.stack.length - 1 ? scan.valueKey : scan.stack[i + 1]?.key;
|
||||
|
||||
if (container.key === "$map") {
|
||||
if (childKey === "in") {
|
||||
vars.add(container.stringValues?.["as"] || "this");
|
||||
}
|
||||
} else if (container.key === "$filter") {
|
||||
if (childKey === "cond") {
|
||||
vars.add(container.stringValues?.["as"] || "this");
|
||||
}
|
||||
} else if (container.key === "$reduce") {
|
||||
if (childKey === "in") {
|
||||
vars.add("value");
|
||||
vars.add("this");
|
||||
}
|
||||
} else if (container.key === "$let") {
|
||||
if (childKey === "in" && container.letVars) {
|
||||
for (const v of container.letVars) vars.add(v);
|
||||
}
|
||||
} else if (container.key === "$lookup") {
|
||||
if (childKey === "pipeline" && container.letVars) {
|
||||
for (const v of container.letVars) vars.add(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [...vars];
|
||||
}
|
||||
|
||||
function classifyCursorInCall(method: string, scan: MongoCallScan): MongoCursorClass {
|
||||
const role = METHOD_ARG_ROLES[method]?.[scan.argIndex];
|
||||
if (!role) return { mode: "none" };
|
||||
@@ -952,6 +1026,15 @@ function classifyFilter(scan: MongoCallScan, rootIndex: number): MongoCursorClas
|
||||
const schemaIndex = findContainerIndex(scan, rootIndex, "$jsonSchema");
|
||||
if (schemaIndex >= 0) return classifyJsonSchema(scan, schemaIndex);
|
||||
|
||||
// Under `$expr` the vocabulary is aggregation expressions.
|
||||
const exprIndex = findContainerIndex(scan, rootIndex, "$expr");
|
||||
if (exprIndex >= 0) {
|
||||
if (isExpressionArray(scan)) return { mode: "fieldRef" };
|
||||
if (scan.inValue) return { mode: "fieldRef" };
|
||||
if (inner.kind === "object") return { mode: "expression" };
|
||||
return { mode: "none" };
|
||||
}
|
||||
|
||||
if (inner.kind === "array") {
|
||||
// `$type: ["string", "null"]` lists BSON types.
|
||||
if (inner.key === "$type") return { mode: "enumValue", enumKey: "$type" };
|
||||
@@ -960,6 +1043,7 @@ function classifyFilter(scan: MongoCallScan, rootIndex: number): MongoCursorClas
|
||||
}
|
||||
if (inner.kind !== "object") return { mode: "none" };
|
||||
if (scan.inValue) {
|
||||
if (scan.valueKey === "$expr") return { mode: "fieldRef" };
|
||||
if (scan.valueKey === "$size" || scan.valueKey === "$mod") return { mode: "none" };
|
||||
const enumKey = filterValueEnum(inner.key, scan.valueKey);
|
||||
if (enumKey) return { mode: "enumValue", enumKey };
|
||||
@@ -1331,6 +1415,7 @@ function classifyGroup(scan: MongoCallScan, bodyIndex: number): MongoCompletionM
|
||||
if (scan.inValue) return "fieldRef";
|
||||
|
||||
const inner = innermost(scan);
|
||||
if (isExpressionArray(scan)) return "fieldRef";
|
||||
if (inner?.kind !== "object") return "none";
|
||||
// One level in: `_id: { … }` builds a compound key, anything else is an accumulator.
|
||||
if (depth === 1) return inner.key === "_id" ? "expression" : "accumulator";
|
||||
@@ -1343,6 +1428,7 @@ function classifyProjection(scan: MongoCallScan, bodyIndex: number): MongoComple
|
||||
if (scan.inValue) return "fieldRef";
|
||||
|
||||
const inner = innermost(scan);
|
||||
if (isExpressionArray(scan)) return "fieldRef";
|
||||
if (inner?.kind !== "object") return "none";
|
||||
return depth === 0 ? "field" : "expression";
|
||||
}
|
||||
@@ -1397,6 +1483,7 @@ function classifyStageOptions(stage: string, scan: MongoCallScan, bodyIndex: num
|
||||
return { mode: "fieldRef", stage };
|
||||
}
|
||||
if (scan.inValue) return { mode: "fieldRef", stage };
|
||||
if (isExpressionArray(scan)) return { mode: "fieldRef", stage };
|
||||
return { mode: innermost(scan)?.kind === "object" ? "expression" : "none", stage };
|
||||
}
|
||||
|
||||
@@ -1430,6 +1517,7 @@ function classifyStageOptions(stage: string, scan: MongoCallScan, bodyIndex: num
|
||||
}
|
||||
|
||||
if (scan.inValue) return { mode: "fieldRef", stage };
|
||||
if (isExpressionArray(scan)) return { mode: "fieldRef", stage };
|
||||
return { mode: innermost(scan)?.kind === "object" ? "expression" : "none", stage };
|
||||
}
|
||||
|
||||
@@ -1646,6 +1734,52 @@ function fieldRefItems(prefix: string, fields: MongoCompletionField[], baseBoost
|
||||
);
|
||||
}
|
||||
|
||||
const SYSTEM_VARIABLES: MongoOperatorSpec[] = [
|
||||
{ label: "$$ROOT", detail: "Root document of the pipeline", apply: "$$ROOT" },
|
||||
{ label: "$$CURRENT", detail: "Current document being processed", apply: "$$CURRENT" },
|
||||
{ label: "$$NOW", detail: "Current date and time", apply: "$$NOW" },
|
||||
{ label: "$$CLUSTER_TIME", detail: "Current cluster timestamp", apply: "$$CLUSTER_TIME" },
|
||||
{ label: "$$REMOVE", detail: "Excludes a field from the document", apply: "$$REMOVE" },
|
||||
{ label: "$$DESCEND", detail: "Descends to subdocuments in $redact", apply: "$$DESCEND" },
|
||||
{ label: "$$PRUNE", detail: "Excludes all fields at current level in $redact", apply: "$$PRUNE" },
|
||||
{ label: "$$KEEP", detail: "Includes all fields at current level in $redact", apply: "$$KEEP" },
|
||||
];
|
||||
|
||||
const REDACT_VARIABLES = new Set(["$$DESCEND", "$$PRUNE", "$$KEEP"]);
|
||||
|
||||
function isVariablePrefix(prefix: string): boolean {
|
||||
return normalizeMongoKeyPrefix(prefix).startsWith("$$");
|
||||
}
|
||||
|
||||
function variableItems(prefix: string, userVariables: string[] = [], stage?: string): MongoCompletionItem[] {
|
||||
const normalizedPrefix = normalizeMongoKeyPrefix(prefix);
|
||||
|
||||
const userItems: MongoCompletionItem[] = userVariables
|
||||
.map((name) => `$$${name}`)
|
||||
.filter((label) => matchesFuzzyPrefix(label, normalizedPrefix))
|
||||
.map((label) => ({
|
||||
label,
|
||||
type: "keyword" as const,
|
||||
detail: "variable in scope",
|
||||
apply: quoteMongoString(label, prefix),
|
||||
boost: startsWithPrefix(label, normalizedPrefix) ? 140 : 110,
|
||||
}));
|
||||
|
||||
const systemItems: MongoCompletionItem[] = SYSTEM_VARIABLES.filter((v) => matchesFuzzyPrefix(v.label, normalizedPrefix)).map((v) => {
|
||||
const isRedact = REDACT_VARIABLES.has(v.label);
|
||||
const baseBoost = isRedact && stage !== "$redact" ? 60 : 100;
|
||||
return {
|
||||
label: v.label,
|
||||
type: "keyword" as const,
|
||||
detail: v.detail,
|
||||
apply: quoteMongoString(v.label, prefix),
|
||||
boost: startsWithPrefix(v.label, normalizedPrefix) ? baseBoost + 20 : baseBoost - 15,
|
||||
};
|
||||
});
|
||||
|
||||
return dedupeAndSort([...userItems, ...systemItems]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Values from a fixed set, in or out of quotes: `$type: "` and `$type: ` both offer
|
||||
* `string`. Inside a quote the item supplies only the bare value and consumes the
|
||||
|
||||
@@ -233,6 +233,12 @@ export const EXPRESSION_OPERATORS: MongoOperatorSpec[] = specs([
|
||||
["$multiply", "Multiplies numbers", "$multiply: [${}, ${}]"],
|
||||
["$divide", "Divides two numbers", "$divide: [${}, ${}]"],
|
||||
["$mod", "Returns the remainder of a division", "$mod: [${}, ${}]"],
|
||||
["$sum", "Sums values", "$sum: [${}, ${}]"],
|
||||
["$avg", "Averages values", "$avg: [${}, ${}]"],
|
||||
["$min", "Returns the lowest value", "$min: [${}, ${}]"],
|
||||
["$max", "Returns the highest value", "$max: [${}, ${}]"],
|
||||
["$stdDevPop", "Population standard deviation", "$stdDevPop: [${}, ${}]"],
|
||||
["$stdDevSamp", "Sample standard deviation", "$stdDevSamp: [${}, ${}]"],
|
||||
["$abs", "Absolute value", "$abs: ${}"],
|
||||
["$ceil", "Rounds up to the next integer", "$ceil: ${}"],
|
||||
["$floor", "Rounds down to the previous integer", "$floor: ${}"],
|
||||
|
||||
Reference in new issue
Block a user