feat(completion): suggest mongo operator-fitting values

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-10-02 09:09:09 +08:00
1 parent 0cdd634d20
commit 472f70b218
3 files changed
+146 -5

No files matched your search

+17 -3
View File
@@ -332,6 +332,11 @@ const STAGE_OPTION_VALUE_MODES: Record<string, Record<string, MongoCompletionMod
$fill: { partitionBy: "fieldRef" },
};
/** Enum value key inside a stage's option object, by stage and option key. */
const STAGE_OPTION_VALUE_ENUMS: Record<string, Record<string, string>> = {
$merge: { whenMatched: "whenMatched", whenNotMatched: "whenNotMatched" },
};
export function getMongoCompletionContext(text: string, cursor: number): MongoCompletionContext {
const safeCursor = Math.max(0, Math.min(cursor, text.length));
const beforeCursor = text.slice(0, safeCursor);
@@ -877,11 +882,17 @@ function innermost(scan: MongoCallScan): MongoContainer | undefined {
/** Query operators whose value is a document with its own keys rather than a field's constraint object. */
const SUB_DOCUMENT_QUERY_OPERATORS = new Set(["$text", "$geoWithin", "$geoIntersects", "$near", "$nearSphere", "$geometry"]);
/** Value positions in a filter that take a fixed set of strings, by the key and the operator that holds it. */
/** Value positions in a filter that take a fixed set of values, by the key and the operator that holds it. */
function filterValueEnum(operator: string | null, key: string | null): string | undefined {
if (key === "$type") return "$type";
if (key === "$options") return "$options";
if (key === "type" && operator === "$geometry") return "geometryType";
if (key === "$exists") return "boolean";
if (operator === "$text") {
if (key === "$caseSensitive" || key === "$diacriticSensitive") return "boolean";
if (key === "$language") return "$language";
}
if (key === "$regex") return "$regex";
return undefined;
}
@@ -901,6 +912,7 @@ function classifyFilter(scan: MongoCallScan, rootIndex: number): MongoCursorClas
}
if (inner.kind !== "object") return { mode: "none" };
if (scan.inValue) {
if (scan.valueKey === "$size" || scan.valueKey === "$mod") return { mode: "none" };
const enumKey = filterValueEnum(inner.key, scan.valueKey);
if (enumKey) return { mode: "enumValue", enumKey };
return { mode: scan.inString ? "none" : "value" };
@@ -1027,7 +1039,7 @@ const FILTER_OPTION_KEYS = new Set(["partialFilterExpression", "validator"]);
/** Option keys whose value is a document with its own fixed keys, and the value sets inside it. */
const SUB_DOCUMENT_OPTION_KEYS: Record<string, Record<string, string>> = {
collation: { caseFirst: "caseFirst", alternate: "alternate", maxVariable: "maxVariable", strength: "strength" },
collation: { locale: "locale", caseFirst: "caseFirst", alternate: "alternate", maxVariable: "maxVariable", strength: "strength" },
timeseries: { granularity: "granularity" },
clusteredIndex: {},
};
@@ -1251,6 +1263,8 @@ function classifyStageOptions(stage: string, scan: MongoCallScan, bodyIndex: num
if (depth === 0) {
if (scan.inValue) {
const enumKey = STAGE_OPTION_VALUE_ENUMS[stage]?.[scan.valueKey ?? ""];
if (enumKey) return { mode: "enumValue", enumKey, stage };
const mode = STAGE_OPTION_VALUE_MODES[stage]?.[scan.valueKey ?? ""] ?? "none";
const isJoinedField = (stage === "$lookup" && scan.valueKey === "foreignField") || (stage === "$graphLookup" && (scan.valueKey === "connectToField" || scan.valueKey === "connectFromField"));
const collection = isJoinedField ? stageBody?.stringValues?.["from"] : undefined;
@@ -1533,7 +1547,7 @@ function enumValueItems(prefix: string, values: readonly MongoOperatorSpec[], ca
.filter((value) => !quoted || value.apply.startsWith('"'))
.map((value) => ({
label: value.label,
type: "keyword" as const,
type: mongoOperatorItemType(value.apply),
detail: value.detail,
info: `${category} value`,
apply: quoted ? quoteMongoString(value.label, prefix) : value.apply,
@@ -569,6 +569,69 @@ export const ENUM_VALUES: Record<string, MongoOperatorSpec[]> = {
["linear", "Linear interpolation between surrounding values", '"linear"'],
["locf", "Last observation carried forward", '"locf"'],
]),
boolean: specs([
["true", "Boolean true", "true"],
["false", "Boolean false", "false"],
]),
$regex: specs([["/pattern/", "Regular expression literal", "/${pattern}/"]]),
$language: specs([
["none", "No language-specific rules", '"none"'],
["da", "Danish", '"da"'],
["de", "German", '"de"'],
["en", "English", '"en"'],
["es", "Spanish", '"es"'],
["fi", "Finnish", '"fi"'],
["fr", "French", '"fr"'],
["hu", "Hungarian", '"hu"'],
["it", "Italian", '"it"'],
["nb", "Norwegian", '"nb"'],
["nl", "Dutch", '"nl"'],
["pt", "Portuguese", '"pt"'],
["ro", "Romanian", '"ro"'],
["ru", "Russian", '"ru"'],
["sv", "Swedish", '"sv"'],
["tr", "Turkish", '"tr"'],
["danish", "Danish", '"danish"'],
["dutch", "Dutch", '"dutch"'],
["english", "English", '"english"'],
["finnish", "Finnish", '"finnish"'],
["french", "French", '"french"'],
["german", "German", '"german"'],
["hungarian", "Hungarian", '"hungarian"'],
["italian", "Italian", '"italian"'],
["norwegian", "Norwegian", '"norwegian"'],
["portuguese", "Portuguese", '"portuguese"'],
["romanian", "Romanian", '"romanian"'],
["russian", "Russian", '"russian"'],
["spanish", "Spanish", '"spanish"'],
["swedish", "Swedish", '"swedish"'],
["turkish", "Turkish", '"turkish"'],
]),
locale: specs([
["simple", "Simple binary comparison", '"simple"'],
["en", "English", '"en"'],
["fr", "French", '"fr"'],
["de", "German", '"de"'],
["es", "Spanish", '"es"'],
["pt", "Portuguese", '"pt"'],
["it", "Italian", '"it"'],
["ru", "Russian", '"ru"'],
["zh", "Chinese", '"zh"'],
["ja", "Japanese", '"ja"'],
["ko", "Korean", '"ko"'],
["ar", "Arabic", '"ar"'],
]),
whenMatched: specs([
["replace", "Replace the existing document", '"replace"'],
["keepExisting", "Keep the existing document", '"keepExisting"'],
["merge", "Merge the matching documents", '"merge"'],
["fail", "Fail the aggregation operation", '"fail"'],
]),
whenNotMatched: specs([
["insert", "Insert the document", '"insert"'],
["discard", "Discard the document", '"discard"'],
["fail", "Fail the aggregation operation", '"fail"'],
]),
};
export const BULK_WRITE_OPERATIONS: MongoOperatorSpec[] = specs([