fix(mongo): suggest extended json values in completion
This commit is contained in:
1 parent
32e070f387
commit
074216ed0f
4 files changed
+189
-12
No files matched your search
@@ -1,5 +1,19 @@
|
||||
import { mongoExtendedJsonValueType } from "@/lib/mongo/mongoDocumentValues";
|
||||
import { ACCUMULATORS, COMMON_OPERATORS, EXPRESSION_OPERATORS, PIPELINE_STAGES, PUSH_MODIFIERS, QUERY_OPERATORS, STAGE_OPTION_KEYS, UPDATE_OPERATORS, UPDATE_OPERATOR_LABELS, VALUE_SNIPPETS, mongoOperatorItemType, type MongoOperatorSpec } from "@/lib/mongo/mongoCompletionTables";
|
||||
import {
|
||||
ACCUMULATORS,
|
||||
COMMON_OPERATORS,
|
||||
EXPRESSION_OPERATORS,
|
||||
EXTENDED_JSON_VALUES,
|
||||
PIPELINE_STAGES,
|
||||
PUSH_MODIFIERS,
|
||||
QUERY_OPERATORS,
|
||||
STAGE_OPTION_KEYS,
|
||||
UPDATE_OPERATORS,
|
||||
UPDATE_OPERATOR_LABELS,
|
||||
VALUE_SNIPPETS,
|
||||
mongoOperatorItemType,
|
||||
type MongoOperatorSpec,
|
||||
} from "@/lib/mongo/mongoCompletionTables";
|
||||
|
||||
/**
|
||||
* What the cursor may usefully be completed with. Each mode maps to exactly one
|
||||
@@ -11,7 +25,26 @@ import { ACCUMULATORS, COMMON_OPERATORS, EXPRESSION_OPERATORS, PIPELINE_STAGES,
|
||||
* the editor turns into "no popup" — deliberately better than falling back to
|
||||
* `root` and showing unrelated `db.…` snippets mid-document.
|
||||
*/
|
||||
export type MongoCompletionMode = "none" | "root" | "collection" | "collectionOrMethod" | "collectionRef" | "method" | "cursorMethod" | "field" | "fieldPath" | "fieldRef" | "value" | "queryOperator" | "updateOperator" | "pushModifier" | "expression" | "accumulator" | "stage" | "stageOption";
|
||||
export type MongoCompletionMode =
|
||||
| "none"
|
||||
| "root"
|
||||
| "collection"
|
||||
| "collectionOrMethod"
|
||||
| "collectionRef"
|
||||
| "method"
|
||||
| "cursorMethod"
|
||||
| "field"
|
||||
| "fieldPath"
|
||||
| "fieldRef"
|
||||
| "value"
|
||||
| "valueWrapper"
|
||||
| "queryOperator"
|
||||
| "updateOperator"
|
||||
| "pushModifier"
|
||||
| "expression"
|
||||
| "accumulator"
|
||||
| "stage"
|
||||
| "stageOption";
|
||||
|
||||
export interface MongoCompletionField {
|
||||
name: string;
|
||||
@@ -157,6 +190,12 @@ const METHOD_ARG_ROLES: Record<string, readonly MongoArgRole[]> = {
|
||||
sort: ["sortKeys"],
|
||||
};
|
||||
|
||||
/** `{ $oid: "..." }` for a bare value position, where the user has not typed the braces yet. */
|
||||
const BRACED_EXTENDED_JSON_VALUES: MongoOperatorSpec[] = EXTENDED_JSON_VALUES.map((spec) => ({ ...spec, apply: `{ ${spec.apply} }` }));
|
||||
|
||||
/** Query operators whose array holds plain values, unlike `$and` / `$or` / `$nor` which hold sub-filters. */
|
||||
const VALUE_ARRAY_OPERATORS = new Set(["$in", "$nin", "$all"]);
|
||||
|
||||
const CALL_METHOD_PATTERN = new RegExp(`\\.(${Object.keys(METHOD_ARG_ROLES).join("|")})\\s*\\(`, "g");
|
||||
|
||||
/** Modes reached by walking the `db.collection.method` chain, where a `.` switches item source. */
|
||||
@@ -278,10 +317,15 @@ export function buildMongoCompletionItemsFromContext(context: MongoCompletionCon
|
||||
items = fieldRefItems(prefix, fields);
|
||||
break;
|
||||
case "value":
|
||||
items = specItems(VALUE_SNIPPETS, prefix, "value", 100);
|
||||
// Shell constructors first; the extended JSON spellings need their own braces here.
|
||||
items = [...specItems(VALUE_SNIPPETS, prefix, "value", 100), ...specItems(BRACED_EXTENDED_JSON_VALUES, prefix, "extended JSON value", 90)];
|
||||
break;
|
||||
case "valueWrapper":
|
||||
items = specItems(EXTENDED_JSON_VALUES, prefix, "extended JSON value", 100);
|
||||
break;
|
||||
case "queryOperator":
|
||||
items = specItems(QUERY_OPERATORS, prefix, "query operator", 100);
|
||||
// `{ _id: { $oid: ... } }` is as valid here as `{ _id: { $gt: ... } }`.
|
||||
items = [...specItems(QUERY_OPERATORS, prefix, "query operator", 100), ...specItems(EXTENDED_JSON_VALUES, prefix, "extended JSON value", 90)];
|
||||
break;
|
||||
case "updateOperator":
|
||||
items = specItems(UPDATE_OPERATORS, prefix, "update operator", 100);
|
||||
@@ -496,14 +540,20 @@ function innermost(scan: MongoCallScan): MongoContainer | undefined {
|
||||
function classifyFilter(scan: MongoCallScan, rootIndex: number): MongoCompletionMode {
|
||||
const inner = innermost(scan);
|
||||
if (!inner || innerDepth(scan, rootIndex) < 0) return "none";
|
||||
// Elements of `$in: [...]` are values, so `{ _id: { $in: [ObjectId(...)] } }` completes like any value.
|
||||
if (inner.kind === "array") return VALUE_ARRAY_OPERATORS.has(inner.key ?? "") && !scan.inString ? "value" : "none";
|
||||
if (inner.kind !== "object") return "none";
|
||||
if (scan.inValue) return scan.inString ? "none" : "value";
|
||||
if (innerDepth(scan, rootIndex) === 0) return "field";
|
||||
|
||||
// Inside a nested object: whose value is it?
|
||||
switch (inner.key) {
|
||||
case null:
|
||||
return "field"; // an element of `$and` / `$or` / `$nor`
|
||||
case null: {
|
||||
// An object inside an array: a sub-filter under `$and` / `$or` / `$nor`,
|
||||
// or an extended JSON wrapper such as `{ $oid: ... }` under `$in`.
|
||||
const parent = scan.stack[scan.stack.length - 2];
|
||||
return parent?.kind === "array" && VALUE_ARRAY_OPERATORS.has(parent.key ?? "") ? "valueWrapper" : "field";
|
||||
}
|
||||
case "$elemMatch":
|
||||
return "field";
|
||||
case "$expr":
|
||||
|
||||
@@ -353,11 +353,38 @@ export const VALUE_SNIPPETS: MongoOperatorSpec[] = specs([
|
||||
["ISODate", "MongoDB ISODate value", 'ISODate("${date}")'],
|
||||
["new Date", "JavaScript date value", 'new Date("${date}")'],
|
||||
["NumberLong", "64-bit integer value", 'NumberLong("${value}")'],
|
||||
["NumberInt", "32-bit integer value", "NumberInt(${value})"],
|
||||
["NumberDecimal", "128-bit decimal value", 'NumberDecimal("${value}")'],
|
||||
["UUID", "UUID value", 'UUID("${uuid}")'],
|
||||
["BinData", "Binary value", 'BinData(0, "${base64}")'],
|
||||
["Timestamp", "Internal timestamp value", "Timestamp(${seconds}, ${ordinal})"],
|
||||
["MinKey", "Sorts before every other value", "MinKey"],
|
||||
["MaxKey", "Sorts after every other value", "MaxKey"],
|
||||
["null", "Null value", "null"],
|
||||
["true", "Boolean true", "true"],
|
||||
["false", "Boolean false", "false"],
|
||||
]);
|
||||
|
||||
/**
|
||||
* Extended JSON spellings of the same values, as the driver returns them and
|
||||
* as they may be typed directly. Each entry is the wrapper's body; in a bare
|
||||
* value position the completion adds the surrounding braces.
|
||||
*/
|
||||
export const EXTENDED_JSON_VALUES: MongoOperatorSpec[] = specs([
|
||||
["$oid", "ObjectId as extended JSON", '$oid: "${id}"'],
|
||||
["$date", "Date as extended JSON", '$date: "${date}"'],
|
||||
["$numberLong", "64-bit integer as extended JSON", '$numberLong: "${value}"'],
|
||||
["$numberInt", "32-bit integer as extended JSON", '$numberInt: "${value}"'],
|
||||
["$numberDouble", "Double as extended JSON", '$numberDouble: "${value}"'],
|
||||
["$numberDecimal", "128-bit decimal as extended JSON", '$numberDecimal: "${value}"'],
|
||||
["$uuid", "UUID as extended JSON", '$uuid: "${uuid}"'],
|
||||
["$binary", "Binary as extended JSON", '$binary: { base64: "${base64}", subType: "00" }'],
|
||||
["$timestamp", "Internal timestamp as extended JSON", "$timestamp: { t: ${seconds}, i: ${ordinal} }"],
|
||||
["$regularExpression", "Regular expression as extended JSON", '$regularExpression: { pattern: "${pattern}", options: "" }'],
|
||||
["$minKey", "MinKey as extended JSON", "$minKey: 1"],
|
||||
["$maxKey", "MaxKey as extended JSON", "$maxKey: 1"],
|
||||
]);
|
||||
|
||||
/**
|
||||
* Everyday operators, floated above the long tail. Without this the lists sort
|
||||
* alphabetically once the user has typed only `$`, which buries `$eq` and
|
||||
@@ -382,6 +409,9 @@ export const COMMON_OPERATORS: ReadonlySet<string> = new Set([
|
||||
"$regex",
|
||||
"$elemMatch",
|
||||
"$expr",
|
||||
// extended JSON values
|
||||
"$oid",
|
||||
"$date",
|
||||
// update
|
||||
"$set",
|
||||
"$unset",
|
||||
|
||||
Reference in new issue
Block a user