From 4031bbe82ea75ad20d87e38df38bf68642399102 Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Sun, 5 Jul 2026 01:17:06 -0600 Subject: [PATCH] update --- src/extensions/autocompletion.ts | 41 +++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/extensions/autocompletion.ts b/src/extensions/autocompletion.ts index d3bc2d4..767a101 100644 --- a/src/extensions/autocompletion.ts +++ b/src/extensions/autocompletion.ts @@ -6,6 +6,8 @@ import type { } from "@codemirror/autocomplete" import type { EditorView } from "@codemirror/view" import { LANGUAGE } from "../types" +import { selectedTableId } from "../composables/sqlTable" +import { sqlTables } from "../data/sqlTables" import { cpp } from "./cpp" import { c } from "./c" import { python } from "./python" @@ -26,6 +28,38 @@ const chineseAnnotations: Record = { sql, } +// 数据类型只取基础部分(TEXT NOT NULL -> TEXT) +function baseType(type: string): string { + return type.trim().split(/\s+/)[0] +} + +// 根据当前选中的数据表,动态生成表名与列名补全 +function buildSqlDynamicCompletions(): ChineseCompletion[] { + const tableCompletions: ChineseCompletion[] = sqlTables.map((table) => ({ + label: table.tableName, + detail: table.label, + type: "type", + info: `数据表 ${table.label},包含列:${table.columns + .map((column) => column.name) + .join("、")}`, + boost: table.id === selectedTableId.value ? 95 : 40, + apply: `${table.tableName} `, + })) + const activeTable = + sqlTables.find((table) => table.id === selectedTableId.value) ?? + sqlTables[0] + const columnCompletions: ChineseCompletion[] = activeTable.columns.map( + (column) => ({ + label: column.name, + detail: baseType(column.type), + type: "variable", + info: `${activeTable.label} 的列,类型 ${baseType(column.type)}`, + boost: 93, + }), + ) + return [...tableCompletions, ...columnCompletions] +} + export function enhanceCompletion(language: LANGUAGE): CompletionSource { return async function ( context: CompletionContext, @@ -33,7 +67,12 @@ export function enhanceCompletion(language: LANGUAGE): CompletionSource { const word = context.matchBefore(/\w+/) if (!word) return null - const completions: Completion[] = (chineseAnnotations[language] || []).map( + const source: ChineseCompletion[] = [ + ...(language === "sql" ? buildSqlDynamicCompletions() : []), + ...(chineseAnnotations[language] || []), + ] + + const completions: Completion[] = source.map( (completion) => { const insertText = typeof completion.apply === "string"