feat(editor): select all and deselect all in completion column picker

This commit is contained in:
zipg authored and GitHub committed 2026-09-19 10:55:21 +08:00
1 parent 257e023120
commit ffb915061f
14 files changed
+230 -22

No files matched your search

@@ -4253,12 +4253,14 @@ interface BatchColumnSelectionActionItem {
detail: string;
boost: number;
batchColumnSelectionAction: true;
/** Toggles every candidate checkbox instead of inserting the selection. */
batchColumnSelectionToggleAll?: true;
sessionKey: string;
}
type QueryCompletionOption = Completion & {
dbxBatchColumnSelection?: { sessionKey: string; candidateKey: string };
dbxBatchColumnSelectionAction?: { sessionKey: string };
dbxBatchColumnSelectionAction?: { sessionKey: string; toggleAll?: true };
};
let batchColumnSelectionSession: BatchColumnSelectionSession | null = null;
@@ -4279,7 +4281,7 @@ interface BatchColumnSelectionDragState {
}
const batchColumnSelectionCheckboxMarkers = new WeakMap<HTMLElement, BatchColumnSelectionCheckboxMarker>();
const batchColumnSelectionActionMarkers = new WeakMap<HTMLElement, string>();
const batchColumnSelectionActionMarkers = new WeakMap<HTMLElement, { sessionKey: string; toggleAll: boolean }>();
const batchColumnSelectionTooltipParents = new WeakMap<EditorViewType, HTMLElement>();
let batchColumnSelectionDragState: BatchColumnSelectionDragState | null = null;
let batchColumnSelectionRefreshCleanup: (() => void) | null = null;
@@ -4314,19 +4316,37 @@ function setBatchColumnSelectionValue(sessionKey: string, candidateKey: string,
if (checkbox) checkbox.checked = selected;
}
function batchColumnSelectionAllSelected(session: BatchColumnSelectionSession): boolean {
return session.candidates.length > 0 && session.selectedKeys.size === session.candidates.length;
}
function updateBatchColumnSelectionActionLabel(view: EditorViewType, sessionKey: string) {
const session = batchColumnSelectionSession;
if (!session || session.key !== sessionKey) return;
const label = t("editor.completion.insertSelectedColumns", { count: session.selectedKeys.size });
const insertLabel = t("editor.completion.insertSelectedColumns", { count: session.selectedKeys.size });
const toggleAllLabel = batchColumnSelectionAllSelected(session) ? t("editor.completion.deselectAllColumns") : t("editor.completion.selectAllColumns");
const tooltipParent = batchColumnSelectionTooltipParents.get(view) ?? view.dom;
tooltipParent.querySelectorAll<HTMLElement>(".cm-batch-column-selection-action-marker").forEach((marker) => {
if (batchColumnSelectionActionMarkers.get(marker) !== sessionKey) return;
const markerState = batchColumnSelectionActionMarkers.get(marker);
if (!markerState || markerState.sessionKey !== sessionKey) return;
const element = marker.closest("li")?.querySelector<HTMLElement>(".cm-completionLabel");
if (!element) return;
const label = markerState.toggleAll ? toggleAllLabel : insertLabel;
if (element.textContent !== label) element.textContent = label;
});
}
/** Check or clear every field checkbox of the active batch selection. */
function toggleAllBatchColumnSelection(view: EditorViewType, sessionKey: string) {
const session = batchColumnSelectionSession;
if (!session || session.key !== sessionKey) return;
const selectAll = !batchColumnSelectionAllSelected(session);
session.selectedKeys = selectAll ? new Set(session.candidates.map((candidate) => candidate.key)) : new Set();
updateBatchColumnSelectionActionLabel(view, sessionKey);
// Reopen the list so every rendered checkbox and both action rows reflect the new state.
scheduleBatchColumnSelectionRefresh(view, sessionKey, session.candidates[0]?.key ?? "");
}
function scheduleBatchColumnSelectionRefresh(view: EditorViewType, sessionKey: string, focusCandidateKey: string, scrollState?: { element: HTMLElement | null; top: number; left: number }) {
cancelBatchColumnSelectionRefresh();
const preservedScroll =
@@ -4684,7 +4704,7 @@ function renderBatchColumnSelectionActionMarker(completion: Completion): Node |
const marker = document.createElement("span");
marker.className = "cm-batch-column-selection-action-marker";
marker.hidden = true;
batchColumnSelectionActionMarkers.set(marker, action.sessionKey);
batchColumnSelectionActionMarkers.set(marker, { sessionKey: action.sessionKey, toggleAll: action.toggleAll === true });
return marker;
}
@@ -4806,19 +4826,32 @@ function buildCompletionResult(items: Array<QueryCompletionItem | BatchColumnSel
function buildSqlCompletionResult(items: SqlCompletionItem[], completionContext: SqlCompletionContext, fullDoc: string, position: number) {
const replacement = prepareSqlCompletionReplacement(fullDoc, position, completionContext, items);
const session = prepareBatchColumnSelectionSession(replacement.items, fullDoc, replacement.from, position);
const action: BatchColumnSelectionActionItem | undefined = session
? {
label: t("editor.completion.insertSelectedColumns", { count: session.selectedKeys.size }),
filterText: completionContext.prefix,
type: "text",
detail: t("editor.completion.insertSelectedColumnsDetail"),
// Keep ordinary single-field completion as the default Enter target.
boost: -1000,
batchColumnSelectionAction: true,
sessionKey: session.key,
}
: undefined;
return buildCompletionResult(action ? [...replacement.items, action] : replacement.items, replacement.from, getSqlCompletionResultValidFor(fullDoc, position), completionContext.prefix);
const actions: BatchColumnSelectionActionItem[] = session
? [
{
label: batchColumnSelectionAllSelected(session) ? t("editor.completion.deselectAllColumns") : t("editor.completion.selectAllColumns"),
filterText: completionContext.prefix,
type: "text",
detail: t("editor.completion.selectAllColumnsDetail"),
// Keep ordinary single-field completion as the default Enter target.
boost: -1000,
batchColumnSelectionAction: true,
batchColumnSelectionToggleAll: true,
sessionKey: session.key,
},
{
label: t("editor.completion.insertSelectedColumns", { count: session.selectedKeys.size }),
filterText: completionContext.prefix,
type: "text",
detail: t("editor.completion.insertSelectedColumnsDetail"),
// Keep ordinary single-field completion as the default Enter target.
boost: -1000,
batchColumnSelectionAction: true,
sessionKey: session.key,
},
]
: [];
return buildCompletionResult(actions.length > 0 ? [...replacement.items, ...actions] : replacement.items, replacement.from, getSqlCompletionResultValidFor(fullDoc, position), completionContext.prefix);
}
// CodeMirror's built-in matcher only matches single-character queries against
@@ -4885,11 +4918,17 @@ function completionOptionForItem(item: QueryCompletionItem | BatchColumnSelectio
return {
...labelPresentation,
...(sortText ? { sortText } : {}),
dbxBatchColumnSelectionAction: { sessionKey: item.sessionKey },
dbxBatchColumnSelectionAction: { sessionKey: item.sessionKey, ...(item.batchColumnSelectionToggleAll ? { toggleAll: true as const } : {}) },
type: item.type,
detail: item.detail,
boost: item.boost,
apply(view: EditorViewType, _completionItem: unknown, from: number, to: number) {
if (item.batchColumnSelectionToggleAll) {
// Toggling the selection never edits the document; the checkout rows
// (and the insert action) are refreshed in place instead.
toggleAllBatchColumnSelection(view, item.sessionKey);
return;
}
applyBatchColumnSelection(view, item, from, to);
},
};
@@ -163,7 +163,7 @@ describe("QueryEditor batch column selection", () => {
});
it("marks the insertion action so the completion menu can keep it sticky", () => {
expect(source).toContain("dbxBatchColumnSelectionAction: { sessionKey: item.sessionKey }");
expect(source).toContain("dbxBatchColumnSelectionAction: { sessionKey: item.sessionKey, ...(item.batchColumnSelectionToggleAll ? { toggleAll: true as const } : {}) }");
expect(source).toContain('optionClass: (completion) => ((completion as QueryCompletionOption).dbxBatchColumnSelectionAction ? "cm-batch-column-selection-action" : "")');
});
@@ -175,8 +175,8 @@ describe("QueryEditor batch column selection", () => {
expect(source.slice(dragUpdateStart, dragUpdateEnd)).toContain("updateBatchColumnSelectionActionLabel(state.view, state.sessionKey);");
expect(source).toContain("const batchColumnSelectionTooltipParents = new WeakMap<EditorViewType, HTMLElement>();");
expect(source).toContain("batchColumnSelectionTooltipParents.set(view.value, tooltipParent);");
expect(source).toContain("const batchColumnSelectionActionMarkers = new WeakMap<HTMLElement, string>();");
expect(source).toContain("batchColumnSelectionActionMarkers.get(marker) !== sessionKey");
expect(source).toContain("const batchColumnSelectionActionMarkers = new WeakMap<HTMLElement, { sessionKey: string; toggleAll: boolean }>();");
expect(source).toContain("!markerState || markerState.sessionKey !== sessionKey");
expect(source).toContain("renderBatchColumnSelectionActionMarker");
});