feat(grid): improve WHERE condition completion
This commit is contained in:
10 files changed
+576
-56
No files matched your search
+176
-1
@@ -1,6 +1,6 @@
|
||||
import { effectScope, nextTick, ref } from "vue";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { useDataGridConditionEditor } from "@/composables/useDataGridConditionEditor";
|
||||
import { completeDataGridConditionQuote, useDataGridConditionEditor } from "@/composables/useDataGridConditionEditor";
|
||||
import { rememberDataGridConditionHistory } from "@/lib/dataGrid/dataGridConditionHistory";
|
||||
|
||||
const storage = new Map<string, string>();
|
||||
@@ -41,6 +41,155 @@ describe("useDataGridConditionEditor", () => {
|
||||
expect(value.value).toBe("status = customer_name");
|
||||
});
|
||||
|
||||
it("builds and applies suggestions at the current caret instead of the value end", async () => {
|
||||
const value = ref("");
|
||||
const selectionStart = ref(0);
|
||||
const selectionEnd = ref(0);
|
||||
const editor = useDataGridConditionEditor({
|
||||
kind: "where",
|
||||
value,
|
||||
selectionStart,
|
||||
selectionEnd,
|
||||
columns: ["customer_id", "customer_name"],
|
||||
historyScope: {},
|
||||
});
|
||||
|
||||
value.value = "status = cus AND enabled = 1";
|
||||
selectionStart.value = 12;
|
||||
selectionEnd.value = 12;
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["customer_id", "customer_name"]));
|
||||
|
||||
expect(editor.accept(0)).toBe(true);
|
||||
expect(value.value).toBe("status = customer_id AND enabled = 1");
|
||||
expect(selectionStart.value).toBe(20);
|
||||
expect(selectionEnd.value).toBe(20);
|
||||
});
|
||||
|
||||
it("uses the current selection as an explicit replacement range", async () => {
|
||||
const value = ref("");
|
||||
const selectionStart = ref(0);
|
||||
const selectionEnd = ref(0);
|
||||
const editor = useDataGridConditionEditor({ kind: "where", value, selectionStart, selectionEnd, columns: ["old_value"], historyScope: {} });
|
||||
|
||||
value.value = "status = old AND enabled = 1";
|
||||
selectionStart.value = 9;
|
||||
selectionEnd.value = 12;
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["old_value"]));
|
||||
|
||||
expect(editor.accept()).toBe(true);
|
||||
expect(value.value).toBe("status = old_value AND enabled = 1");
|
||||
});
|
||||
|
||||
it("suggests WHERE connectors after a completed expression", async () => {
|
||||
const value = ref("");
|
||||
const editor = useDataGridConditionEditor({ kind: "where", value, columns: ["account_id", "owner_id"], historyScope: {} });
|
||||
|
||||
value.value = "owner_id = 1 a";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value).toEqual([{ value: "AND", kind: "keyword" }]));
|
||||
expect(editor.accept()).toBe(true);
|
||||
expect(value.value).toBe("owner_id = 1 AND");
|
||||
await nextTick();
|
||||
|
||||
value.value = "owner_id = 1 o";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value).toEqual([{ value: "OR", kind: "keyword" }]));
|
||||
});
|
||||
|
||||
it("suggests fields after a WHERE connector even when the active token is empty", async () => {
|
||||
const value = ref("");
|
||||
const editor = useDataGridConditionEditor({ kind: "where", value, columns: ["account_id", "owner_id"], historyScope: {} });
|
||||
|
||||
value.value = "status = 'active' AND ";
|
||||
await nextTick();
|
||||
await vi.waitFor(() =>
|
||||
expect(editor.suggestions.value).toEqual([
|
||||
{ value: "account_id", kind: "column" },
|
||||
{ value: "owner_id", kind: "column" },
|
||||
]),
|
||||
);
|
||||
expect(editor.accept(1)).toBe(true);
|
||||
expect(value.value).toBe("status = 'active' AND owner_id");
|
||||
});
|
||||
|
||||
it("does not offer connectors inside quoted values or in ORDER BY", async () => {
|
||||
const whereValue = ref("");
|
||||
const whereEditor = useDataGridConditionEditor({ kind: "where", value: whereValue, columns: ["name"], historyScope: {} });
|
||||
whereValue.value = "name = 'Alice a";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(whereEditor.suggestions.value).toEqual([]));
|
||||
|
||||
const orderByValue = ref("");
|
||||
const orderByEditor = useDataGridConditionEditor({ kind: "orderBy", value: orderByValue, columns: ["amount"], historyScope: {} });
|
||||
orderByValue.value = "created_at a";
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(orderByEditor.suggestions.value).toEqual([]));
|
||||
});
|
||||
|
||||
it.each(["deleted_at IS ", "deleted_at IS a", "deleted_at IS NOT o", "name LIKE o", "id IN a", "score BETWEEN o"])("does not offer connectors while the keyword operator is incomplete: %s", async (condition) => {
|
||||
const value = ref("");
|
||||
const editor = useDataGridConditionEditor({ kind: "where", value, columns: ["account_id"], historyScope: {}, suggestionDebounceMs: 1 });
|
||||
|
||||
value.value = condition;
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
expect(editor.suggestions.value).toEqual([]);
|
||||
});
|
||||
|
||||
it("completes inside dialect quoted identifiers without treating them as strings", async () => {
|
||||
const value = ref("");
|
||||
const selectionStart = ref(0);
|
||||
const selectionEnd = ref(0);
|
||||
const editor = useDataGridConditionEditor({
|
||||
kind: "where",
|
||||
value,
|
||||
selectionStart,
|
||||
selectionEnd,
|
||||
identifierQuote: '"',
|
||||
columns: [{ name: "name", insertText: '"name"' }],
|
||||
historyScope: {},
|
||||
});
|
||||
|
||||
value.value = '"na" = 1';
|
||||
selectionStart.value = 3;
|
||||
selectionEnd.value = 3;
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(editor.suggestions.value.map((item) => item.value)).toEqual(["name"]));
|
||||
expect(editor.accept()).toBe(true);
|
||||
expect(value.value).toBe('"name" = 1');
|
||||
});
|
||||
|
||||
it("keeps double quotes as string delimiters when the dialect uses another identifier quote", async () => {
|
||||
const value = ref("");
|
||||
const selectionStart = ref(0);
|
||||
const selectionEnd = ref(0);
|
||||
const editor = useDataGridConditionEditor({
|
||||
kind: "where",
|
||||
value,
|
||||
selectionStart,
|
||||
selectionEnd,
|
||||
identifierQuote: "`",
|
||||
columns: ["name"],
|
||||
historyScope: {},
|
||||
suggestionDebounceMs: 1,
|
||||
});
|
||||
|
||||
value.value = '"na" = 1';
|
||||
selectionStart.value = 3;
|
||||
selectionEnd.value = 3;
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
expect(editor.suggestions.value).toEqual([]);
|
||||
});
|
||||
|
||||
it("pairs WHERE quotes, wraps selections, and skips an existing closing quote", () => {
|
||||
expect(completeDataGridConditionQuote("id = ", 5, 5, "'")).toEqual({ value: "id = ''", selectionStart: 6, selectionEnd: 6 });
|
||||
expect(completeDataGridConditionQuote("name", 0, 4, '"')).toEqual({ value: '"name"', selectionStart: 1, selectionEnd: 5 });
|
||||
expect(completeDataGridConditionQuote("id = ''", 6, 6, "'")).toEqual({ value: "id = ''", selectionStart: 7, selectionEnd: 7 });
|
||||
});
|
||||
|
||||
it("reuses column comments for field suggestions without adding them to history", async () => {
|
||||
const scope = { connectionId: "connection", database: "db", tableName: "users" };
|
||||
rememberDataGridConditionHistory("where", scope, "customer_id = 1");
|
||||
@@ -161,6 +310,32 @@ describe("useDataGridConditionEditor", () => {
|
||||
expect(editor.suggestions.value.map((item) => item.value)).toEqual(["order_id"]);
|
||||
});
|
||||
|
||||
it("passes the cursor text and replacement range to asynchronous providers", async () => {
|
||||
const value = ref("");
|
||||
const selectionStart = ref(0);
|
||||
const selectionEnd = ref(0);
|
||||
const suggestionProvider = vi.fn(() => ["customer_id"]);
|
||||
useDataGridConditionEditor({ kind: "where", value, selectionStart, selectionEnd, historyScope: {}, suggestionProvider });
|
||||
|
||||
value.value = "status = cus AND enabled = 1";
|
||||
selectionStart.value = 12;
|
||||
selectionEnd.value = 12;
|
||||
await nextTick();
|
||||
await vi.waitFor(() => expect(suggestionProvider).toHaveBeenCalledOnce());
|
||||
|
||||
expect(suggestionProvider).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
value: "status = cus AND enabled = 1",
|
||||
valueBeforeCursor: "status = cus",
|
||||
token: "cus",
|
||||
from: 9,
|
||||
to: 12,
|
||||
selectionStart: 12,
|
||||
selectionEnd: 12,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("loads, filters, accepts, and deletes scoped history", () => {
|
||||
const scope = { connectionId: "connection", database: "db", tableName: "users" };
|
||||
rememberDataGridConditionHistory("where", scope, "status = 'active'");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { computed, getCurrentScope, onScopeDispose, ref, toValue, watch, type MaybeRefOrGetter, type Ref } from "vue";
|
||||
import { forgetDataGridConditionHistory, loadDataGridConditionHistory, rememberDataGridConditionHistory, type DataGridConditionHistoryKind, type DataGridConditionHistoryScope } from "@/lib/dataGrid/dataGridConditionHistory";
|
||||
|
||||
export type DataGridConditionSuggestionKind = "column" | "history";
|
||||
export type DataGridConditionSuggestionKind = "column" | "keyword" | "history";
|
||||
|
||||
export interface DataGridConditionColumnSuggestion {
|
||||
name: string;
|
||||
@@ -21,7 +21,12 @@ export interface DataGridConditionSuggestion {
|
||||
export interface DataGridConditionSuggestionContext {
|
||||
kind: DataGridConditionHistoryKind;
|
||||
value: string;
|
||||
valueBeforeCursor: string;
|
||||
token: string;
|
||||
from: number;
|
||||
to: number;
|
||||
selectionStart: number;
|
||||
selectionEnd: number;
|
||||
signal: AbortSignal;
|
||||
}
|
||||
|
||||
@@ -30,6 +35,9 @@ export type DataGridConditionSuggestionProvider = (context: DataGridConditionSug
|
||||
export interface UseDataGridConditionEditorOptions {
|
||||
kind: DataGridConditionHistoryKind;
|
||||
value: Ref<string>;
|
||||
selectionStart?: Ref<number>;
|
||||
selectionEnd?: Ref<number>;
|
||||
identifierQuote?: MaybeRefOrGetter<string | undefined>;
|
||||
columns?: MaybeRefOrGetter<readonly DataGridConditionColumnOption[] | undefined>;
|
||||
historyScope: MaybeRefOrGetter<DataGridConditionHistoryScope>;
|
||||
suggestionProvider?: DataGridConditionSuggestionProvider;
|
||||
@@ -39,25 +47,152 @@ export interface UseDataGridConditionEditorOptions {
|
||||
|
||||
const WHERE_TOKEN_PATTERN = /([^\s,()><=!&|]+)$/;
|
||||
const ORDER_BY_TOKEN_PATTERN = /([^\s,()]+)$/;
|
||||
const WHERE_TOKEN_FORWARD_PATTERN = /^([^\s,()><=!&|]+)/;
|
||||
const ORDER_BY_TOKEN_FORWARD_PATTERN = /^([^\s,()]+)/;
|
||||
const WHERE_CONNECTOR_KEYWORDS = ["AND", "OR"] as const;
|
||||
const WHERE_VALUE_OPERATOR_PATTERN = /(?:^|[\s(])(?:IS(?:\s+NOT)?|(?:NOT\s+)?(?:LIKE|ILIKE|IN|BETWEEN)|SIMILAR\s+TO|REGEXP|RLIKE|GLOB|MATCH)\s*$/i;
|
||||
|
||||
interface DataGridConditionCompletionTarget {
|
||||
value: string;
|
||||
valueBeforeCursor: string;
|
||||
token: string;
|
||||
from: number;
|
||||
to: number;
|
||||
selectionStart: number;
|
||||
selectionEnd: number;
|
||||
quotedIdentifier: boolean;
|
||||
insideString: boolean;
|
||||
}
|
||||
|
||||
interface ActiveQuote {
|
||||
kind: "identifier" | "string";
|
||||
close: string;
|
||||
contentStart: number;
|
||||
}
|
||||
|
||||
function normalizedColumnComment(column: DataGridConditionColumnOption): string | undefined {
|
||||
if (typeof column === "string" || typeof column.comment !== "string") return undefined;
|
||||
return column.comment.trim() || undefined;
|
||||
}
|
||||
|
||||
function activeToken(kind: DataGridConditionHistoryKind, value: string): string {
|
||||
return (
|
||||
value
|
||||
.trim()
|
||||
.split(kind === "where" ? /[\s,()><=!&|]+/ : /[\s,()]+/)
|
||||
.pop() ?? ""
|
||||
);
|
||||
function normalizedIdentifierQuote(identifierQuote: string | undefined): string | undefined {
|
||||
const quote = identifierQuote?.trim();
|
||||
return quote && quote !== "'" ? quote : undefined;
|
||||
}
|
||||
|
||||
function replaceActiveToken(kind: DataGridConditionHistoryKind, value: string, replacement: string): string {
|
||||
const match = value.match(kind === "where" ? WHERE_TOKEN_PATTERN : ORDER_BY_TOKEN_PATTERN);
|
||||
if (!match) return value;
|
||||
return `${value.slice(0, -match[1].length)}${replacement}`;
|
||||
function identifierCloseQuote(open: string): string {
|
||||
return open === "[" ? "]" : open;
|
||||
}
|
||||
|
||||
function activeQuoteAt(value: string, cursor: number, identifierQuote: string | undefined): ActiveQuote | undefined {
|
||||
const identifierOpen = normalizedIdentifierQuote(identifierQuote);
|
||||
const identifierClose = identifierOpen ? identifierCloseQuote(identifierOpen) : undefined;
|
||||
let active: ActiveQuote | undefined;
|
||||
for (let index = 0; index < cursor; index += 1) {
|
||||
const character = value[index];
|
||||
if (!active) {
|
||||
if (identifierOpen && value.startsWith(identifierOpen, index)) {
|
||||
active = { kind: "identifier", close: identifierClose!, contentStart: index + identifierOpen.length };
|
||||
index += identifierOpen.length - 1;
|
||||
} else if (character === "'" || (character === '"' && identifierOpen !== '"')) {
|
||||
active = { kind: "string", close: character, contentStart: index + 1 };
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (active.kind === "string" && character === "\\") {
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (!value.startsWith(active.close, index)) continue;
|
||||
if (value.startsWith(active.close + active.close, index) && index + active.close.length * 2 <= cursor) {
|
||||
index += active.close.length * 2 - 1;
|
||||
continue;
|
||||
}
|
||||
index += active.close.length - 1;
|
||||
active = undefined;
|
||||
}
|
||||
return active;
|
||||
}
|
||||
|
||||
function clampedSelection(value: string, selectionStart: number | undefined, selectionEnd: number | undefined): { start: number; end: number } {
|
||||
const start = Math.min(Math.max(selectionStart ?? value.length, 0), value.length);
|
||||
const end = Math.min(Math.max(selectionEnd ?? start, start), value.length);
|
||||
return { start, end };
|
||||
}
|
||||
|
||||
function conditionCompletionTarget(kind: DataGridConditionHistoryKind, value: string, selectionStart: number | undefined, selectionEnd: number | undefined, identifierQuote: string | undefined): DataGridConditionCompletionTarget {
|
||||
const selection = clampedSelection(value, selectionStart, selectionEnd);
|
||||
const valueBeforeCursor = value.slice(0, selection.start);
|
||||
const quote = kind === "where" ? activeQuoteAt(value, selection.start, identifierQuote) : undefined;
|
||||
if (quote?.kind === "string") {
|
||||
return { value, valueBeforeCursor, token: "", from: selection.start, to: selection.end, selectionStart: selection.start, selectionEnd: selection.end, quotedIdentifier: false, insideString: true };
|
||||
}
|
||||
if (quote?.kind === "identifier") {
|
||||
const closeIndex = selection.start === selection.end ? value.indexOf(quote.close, selection.start) : -1;
|
||||
return {
|
||||
value,
|
||||
valueBeforeCursor,
|
||||
token: selection.start === selection.end ? value.slice(quote.contentStart, selection.start) : value.slice(selection.start, selection.end),
|
||||
from: selection.start === selection.end ? quote.contentStart : selection.start,
|
||||
to: selection.start === selection.end && closeIndex >= 0 ? closeIndex : selection.end,
|
||||
selectionStart: selection.start,
|
||||
selectionEnd: selection.end,
|
||||
quotedIdentifier: true,
|
||||
insideString: false,
|
||||
};
|
||||
}
|
||||
if (selection.start !== selection.end) {
|
||||
return {
|
||||
value,
|
||||
valueBeforeCursor,
|
||||
token: value.slice(selection.start, selection.end),
|
||||
from: selection.start,
|
||||
to: selection.end,
|
||||
selectionStart: selection.start,
|
||||
selectionEnd: selection.end,
|
||||
quotedIdentifier: false,
|
||||
insideString: false,
|
||||
};
|
||||
}
|
||||
const beforeMatch = valueBeforeCursor.match(kind === "where" ? WHERE_TOKEN_PATTERN : ORDER_BY_TOKEN_PATTERN);
|
||||
const token = beforeMatch?.[1] ?? "";
|
||||
const afterMatch = value.slice(selection.start).match(kind === "where" ? WHERE_TOKEN_FORWARD_PATTERN : ORDER_BY_TOKEN_FORWARD_PATTERN);
|
||||
return {
|
||||
value,
|
||||
valueBeforeCursor,
|
||||
token,
|
||||
from: selection.start - token.length,
|
||||
to: selection.start + (afterMatch?.[1].length ?? 0),
|
||||
selectionStart: selection.start,
|
||||
selectionEnd: selection.end,
|
||||
quotedIdentifier: false,
|
||||
insideString: false,
|
||||
};
|
||||
}
|
||||
|
||||
function whereSuggestionRole(target: DataGridConditionCompletionTarget): "field" | "connector" | "none" {
|
||||
if (target.insideString) return "none";
|
||||
if (target.quotedIdentifier) return "field";
|
||||
const prefix = target.value.slice(0, target.from).trimEnd();
|
||||
if (WHERE_VALUE_OPERATOR_PATTERN.test(prefix)) return "none";
|
||||
if (!prefix || /(?:^|\s)(?:AND|OR|NOT)$/i.test(prefix) || /[,(<>=!~+\-*/]$/.test(prefix)) return "field";
|
||||
return "connector";
|
||||
}
|
||||
|
||||
export interface DataGridConditionQuoteCompletion {
|
||||
value: string;
|
||||
selectionStart: number;
|
||||
selectionEnd: number;
|
||||
}
|
||||
|
||||
export function completeDataGridConditionQuote(value: string, selectionStart: number, selectionEnd: number, quote: "'" | '"'): DataGridConditionQuoteCompletion {
|
||||
if (selectionStart === selectionEnd && value[selectionStart] === quote) {
|
||||
return { value, selectionStart: selectionStart + 1, selectionEnd: selectionStart + 1 };
|
||||
}
|
||||
const selected = value.slice(selectionStart, selectionEnd);
|
||||
const nextValue = `${value.slice(0, selectionStart)}${quote}${selected}${quote}${value.slice(selectionEnd)}`;
|
||||
if (selected) return { value: nextValue, selectionStart: selectionStart + 1, selectionEnd: selectionEnd + 1 };
|
||||
return { value: nextValue, selectionStart: selectionStart + 1, selectionEnd: selectionStart + 1 };
|
||||
}
|
||||
|
||||
export function useDataGridConditionEditor(options: UseDataGridConditionEditorOptions) {
|
||||
@@ -65,6 +200,7 @@ export function useDataGridConditionEditor(options: UseDataGridConditionEditorOp
|
||||
const highlightedIndex = ref(-1);
|
||||
const historyOpen = ref(false);
|
||||
const suggestionsLoading = ref(false);
|
||||
const replacementRange = ref<{ from: number; to: number }>();
|
||||
let suggestionTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
let suggestionRequestId = 0;
|
||||
let suggestionAbortController: AbortController | undefined;
|
||||
@@ -86,35 +222,48 @@ export function useDataGridConditionEditor(options: UseDataGridConditionEditorOp
|
||||
suggestions.value = [];
|
||||
highlightedIndex.value = -1;
|
||||
historyOpen.value = false;
|
||||
replacementRange.value = undefined;
|
||||
}
|
||||
|
||||
function defaultSuggestions(token: string): DataGridConditionSuggestion[] {
|
||||
const normalizedToken = token.toLowerCase();
|
||||
if (!normalizedToken) return [];
|
||||
function defaultSuggestions(target: DataGridConditionCompletionTarget): DataGridConditionSuggestion[] {
|
||||
const role = options.kind === "where" ? whereSuggestionRole(target) : "field";
|
||||
if (role === "none") return [];
|
||||
const normalizedToken = target.token.toLowerCase();
|
||||
const seen = new Set<string>();
|
||||
const suggestions: DataGridConditionSuggestion[] = [];
|
||||
for (const column of toValue(options.columns) ?? []) {
|
||||
const value = typeof column === "string" ? column : column.name;
|
||||
const normalizedValue = value.toLowerCase();
|
||||
if (!normalizedValue.startsWith(normalizedToken) || normalizedValue === normalizedToken || seen.has(value)) continue;
|
||||
seen.add(value);
|
||||
const comment = normalizedColumnComment(column);
|
||||
const insertText = typeof column === "string" ? value : column.insertText;
|
||||
suggestions.push({ value, kind: "column", ...(insertText !== undefined && insertText !== value ? { insertText } : {}), ...(comment ? { comment } : {}) });
|
||||
if (role === "field") {
|
||||
for (const column of toValue(options.columns) ?? []) {
|
||||
const columnValue = typeof column === "string" ? column : column.name;
|
||||
const normalizedValue = columnValue.toLowerCase();
|
||||
if ((normalizedToken && (!normalizedValue.startsWith(normalizedToken) || normalizedValue === normalizedToken)) || seen.has(columnValue)) continue;
|
||||
seen.add(columnValue);
|
||||
const comment = normalizedColumnComment(column);
|
||||
const insertText = target.quotedIdentifier ? columnValue : typeof column === "string" ? columnValue : column.insertText;
|
||||
suggestions.push({ value: columnValue, kind: "column", ...(insertText !== undefined && insertText !== columnValue ? { insertText } : {}), ...(comment ? { comment } : {}) });
|
||||
}
|
||||
} else {
|
||||
for (const keyword of WHERE_CONNECTOR_KEYWORDS) {
|
||||
if (!keyword.toLowerCase().startsWith(normalizedToken) || keyword.toLowerCase() === normalizedToken) continue;
|
||||
suggestions.push({ value: keyword, kind: "keyword" });
|
||||
}
|
||||
}
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
async function loadSuggestions(value: string, requestId: number, controller: AbortController) {
|
||||
const token = activeToken(options.kind, value);
|
||||
if (!token) return;
|
||||
async function loadSuggestions(target: DataGridConditionCompletionTarget, requestId: number, controller: AbortController) {
|
||||
if (!target.token && (options.kind !== "where" || !target.value.trim())) return;
|
||||
suggestionsLoading.value = true;
|
||||
try {
|
||||
const values = options.suggestionProvider ? await options.suggestionProvider({ kind: options.kind, value, token, signal: controller.signal }) : undefined;
|
||||
const role = options.kind === "where" ? whereSuggestionRole(target) : "field";
|
||||
const values =
|
||||
options.suggestionProvider && target.token && role === "field"
|
||||
? await options.suggestionProvider({ kind: options.kind, value: target.value, valueBeforeCursor: target.valueBeforeCursor, token: target.token, from: target.from, to: target.to, selectionStart: target.selectionStart, selectionEnd: target.selectionEnd, signal: controller.signal })
|
||||
: undefined;
|
||||
// A slower request must never replace suggestions for a newer editor value.
|
||||
if (controller.signal.aborted || requestId !== suggestionRequestId || options.value.value !== value || historyOpen.value) return;
|
||||
if (controller.signal.aborted || requestId !== suggestionRequestId || options.value.value !== target.value || historyOpen.value) return;
|
||||
const limit = options.suggestionLimit ?? 8;
|
||||
suggestions.value = values ? [...new Set(values)].slice(0, limit).map((suggestion) => ({ value: suggestion, kind: "column" })) : defaultSuggestions(token).slice(0, limit);
|
||||
suggestions.value = values ? [...new Set(values)].slice(0, limit).map((suggestion) => ({ value: suggestion, kind: "column" })) : defaultSuggestions(target).slice(0, limit);
|
||||
replacementRange.value = { from: target.from, to: target.to };
|
||||
highlightedIndex.value = suggestions.value.length > 0 ? 0 : -1;
|
||||
} catch (error) {
|
||||
if (!controller.signal.aborted && requestId === suggestionRequestId) {
|
||||
@@ -127,19 +276,21 @@ export function useDataGridConditionEditor(options: UseDataGridConditionEditorOp
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleSuggestions(value: string) {
|
||||
function scheduleSuggestions(value: string, selectionStart = options.selectionStart?.value, selectionEnd = options.selectionEnd?.value) {
|
||||
cancelSuggestionRequest();
|
||||
suggestions.value = [];
|
||||
highlightedIndex.value = -1;
|
||||
historyOpen.value = false;
|
||||
if (!value.trim()) return;
|
||||
|
||||
const target = conditionCompletionTarget(options.kind, value, selectionStart, selectionEnd, toValue(options.identifierQuote));
|
||||
|
||||
const requestId = suggestionRequestId;
|
||||
const controller = new AbortController();
|
||||
suggestionAbortController = controller;
|
||||
suggestionTimer = setTimeout(() => {
|
||||
suggestionTimer = undefined;
|
||||
void loadSuggestions(value, requestId, controller);
|
||||
void loadSuggestions(target, requestId, controller);
|
||||
}, options.suggestionDebounceMs ?? 0);
|
||||
}
|
||||
|
||||
@@ -150,6 +301,7 @@ export function useDataGridConditionEditor(options: UseDataGridConditionEditorOp
|
||||
return;
|
||||
}
|
||||
historyOpen.value = true;
|
||||
replacementRange.value = undefined;
|
||||
suggestions.value = loadDataGridConditionHistory(options.kind, toValue(options.historyScope), options.value.value).map((value) => ({ value, kind: "history" }));
|
||||
highlightedIndex.value = -1;
|
||||
}
|
||||
@@ -179,9 +331,22 @@ export function useDataGridConditionEditor(options: UseDataGridConditionEditorOp
|
||||
function accept(index = highlightedIndex.value) {
|
||||
const suggestion = suggestions.value[index];
|
||||
if (!suggestion) return false;
|
||||
suppressNextValueSuggestion = true;
|
||||
// History is already executable SQL; only fresh column suggestions apply dialect quoting.
|
||||
options.value.value = suggestion.kind === "history" ? suggestion.value : replaceActiveToken(options.kind, options.value.value, suggestion.insertText ?? suggestion.value);
|
||||
let caret: number;
|
||||
if (suggestion.kind === "history") {
|
||||
suppressNextValueSuggestion = true;
|
||||
options.value.value = suggestion.value;
|
||||
caret = suggestion.value.length;
|
||||
} else {
|
||||
const range = replacementRange.value;
|
||||
const currentTarget = conditionCompletionTarget(options.kind, options.value.value, options.selectionStart?.value, options.selectionEnd?.value, toValue(options.identifierQuote));
|
||||
if (!range || currentTarget.from !== range.from || currentTarget.to !== range.to) return false;
|
||||
const replacement = suggestion.insertText ?? suggestion.value;
|
||||
suppressNextValueSuggestion = true;
|
||||
options.value.value = `${options.value.value.slice(0, range.from)}${replacement}${options.value.value.slice(range.to)}`;
|
||||
caret = range.from + replacement.length;
|
||||
}
|
||||
if (options.selectionStart) options.selectionStart.value = caret;
|
||||
if (options.selectionEnd) options.selectionEnd.value = caret;
|
||||
dismiss();
|
||||
return true;
|
||||
}
|
||||
@@ -199,28 +364,30 @@ export function useDataGridConditionEditor(options: UseDataGridConditionEditorOp
|
||||
return "navigate";
|
||||
}
|
||||
if (suggestions.value.length > 0 && event.key === "Tab") {
|
||||
if (!accept()) return undefined;
|
||||
event.preventDefault();
|
||||
accept();
|
||||
return "accept";
|
||||
}
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
if (suggestions.value.length > 0 && highlightedIndex.value >= 0) {
|
||||
accept();
|
||||
return "accept";
|
||||
if (accept()) return "accept";
|
||||
}
|
||||
return "apply";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
watch(options.value, (value) => {
|
||||
if (suppressNextValueSuggestion) {
|
||||
suppressNextValueSuggestion = false;
|
||||
return;
|
||||
}
|
||||
scheduleSuggestions(value);
|
||||
});
|
||||
watch(
|
||||
() => [options.value.value, options.selectionStart?.value, options.selectionEnd?.value] as const,
|
||||
([value, selectionStart, selectionEnd]) => {
|
||||
if (suppressNextValueSuggestion) {
|
||||
suppressNextValueSuggestion = false;
|
||||
return;
|
||||
}
|
||||
scheduleSuggestions(value, selectionStart, selectionEnd);
|
||||
},
|
||||
);
|
||||
if (getCurrentScope()) onScopeDispose(cancelSuggestionRequest);
|
||||
|
||||
return {
|
||||
@@ -228,6 +395,7 @@ export function useDataGridConditionEditor(options: UseDataGridConditionEditorOp
|
||||
highlightedIndex,
|
||||
historyOpen,
|
||||
suggestionsLoading,
|
||||
replacementRange,
|
||||
dropdownOpen,
|
||||
scheduleSuggestions,
|
||||
openHistory,
|
||||
|
||||
Reference in new issue
Block a user