fix(mongodb): keep one colon when a key completion lands before an existing one

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-09-29 13:41:11 +08:00
1 parent 6ffc462790
commit 43f502332e
3 files changed
+41 -6

No files matched your search

@@ -255,6 +255,26 @@ describe("QueryEditor completion provider ownership", () => {
expect(await pending).toBeNull();
});
it("does not double the separator when a Mongo key is re-picked in front of an existing colon", async () => {
const accept = async (doc: string, cursor: number) => {
const { store, provide, currentView } = createHarness({ databaseType: "mongodb", modelValue: doc });
store.listMongoCompletionFields.mockResolvedValue([{ name: "name", type: "string" }]);
currentView.dispatch({ selection: { anchor: cursor } });
const result = await provide();
const option = result?.options.find((candidate) => candidate.label === "name");
expect(option, "the field is offered").toBeDefined();
(option!.apply as (view: EditorView, completion: unknown, from: number, to: number) => void)(currentView, option, result!.from, cursor);
return { doc: currentView.state.doc.toString(), cursor: currentView.state.selection.main.head };
};
// Editing the key of an existing entry: the colon already there is kept, not doubled.
expect(await accept("db.users.find({ na: 1 })", 18)).toEqual({ doc: "db.users.find({ name: 1 })", cursor: 20 });
expect(await accept("db.users.find({ na : 1 })", 18)).toEqual({ doc: "db.users.find({ name : 1 })", cursor: 20 });
// A fresh key still gets its separator.
expect(await accept("db.users.find({ na", 18)).toEqual({ doc: "db.users.find({ name: ", cursor: 22 });
expect(await accept("db.users.find({ na })", 18)).toEqual({ doc: "db.users.find({ name: })", cursor: 22 });
});
it.each(["redis", "mongodb", "mysql"] as const)("does not query %s metadata without a connection", async (databaseType) => {
const { store, provide } = createHarness({ databaseType, connectionId: undefined });
expect(await provide()).toBeNull();
@@ -20,7 +20,7 @@ import {
import { originForSqlCompletionProvider, originForTypedSqlCompletionStart, shouldAllowSqlCompletionTrigger, type SqlCompletionTriggerFacts, type SqlCompletionTriggerOrigin } from "@/lib/sql/sqlCompletionTriggerPolicy";
import { driverProfileHasCompletionCandidates } from "@/lib/database/driverProfileExtensions";
import { buildElasticsearchCompletionItemsFromContext, elasticsearchCompletionNeedsFields, getElasticsearchCompletionContext, getElasticsearchCompletionResultValidFor, shouldAutoOpenElasticsearchCompletion, type ElasticsearchCompletionField } from "@/lib/elasticsearch/elasticsearchCompletion";
import { buildMongoCompletionItemsFromContext, getMongoCompletionContext, getMongoCompletionResultValidFor, mongoCompletionNeedsCollections, mongoCompletionNeedsDatabases, mongoCompletionNeedsFields, shouldAutoOpenMongoCompletion } from "@/lib/mongo/mongoCompletion";
import { buildMongoCompletionItemsFromContext, foldMongoKeySeparator, getMongoCompletionContext, getMongoCompletionResultValidFor, mongoCompletionNeedsCollections, mongoCompletionNeedsDatabases, mongoCompletionNeedsFields, shouldAutoOpenMongoCompletion } from "@/lib/mongo/mongoCompletion";
import { buildSoqlCompletionItems, getSoqlCompletionContext, getSoqlCompletionResultValidFor, resolveSoqlFieldCandidates, resolveSoqlValueField, shouldAutoOpenSoqlCompletion, soqlCompletionNeedsObjects, type SoqlCompletionField, type SoqlCompletionObject } from "@/lib/soql/soqlCompletion";
import {
buildSqlServerUseDatabaseCompletionItems,
@@ -466,11 +466,15 @@ export function useQueryEditorCompletion(options: QueryEditorCompletionOptions)
replaceClosingQuote: "replaceClosingQuote" in item ? item.replaceClosingQuote : undefined,
replaceSelectWildcard: "replaceSelectWildcard" in item ? item.replaceSelectWildcard : undefined,
});
const insert = appendSqlCompletionSpace(item.apply ?? item.label, {
let insert = appendSqlCompletionSpace(item.apply ?? item.label, {
enabled: ("appendSpace" in item && item.appendSpace === true) || (shouldInsertSqlCompletionSpace() && settingsStore.editorSettings.insertSpaceAfterCompletion),
itemType: item.type,
nextCharacter: view.state.sliceDoc(replaceTo, replaceTo + 1),
});
if (props.databaseType === "mongodb") {
// A key completion writes its own `: `; re-picking a key in front of an existing one must not double it.
insert = foldMongoKeySeparator(insert, view.state.sliceDoc(replaceTo, view.state.doc.lineAt(replaceTo).to));
}
if (runtime.codeMirrorInsertCompletionText) {
view.dispatch(runtime.codeMirrorInsertCompletionText(view.state, insert, from, replaceTo));
} else {