feat(editor): format SQL files on save

This commit is contained in:
lizzzzz777 authored and GitHub committed 2026-08-25 14:46:25 +08:00
1 parent 249896dddf
commit 876f850e2d
17 files changed
+148 -7

No files matched your search

+50 -7
View File
@@ -119,7 +119,11 @@ import { initSavedSqlEditorPositions } from "@/lib/app/savedSqlEditorPosition";
import { hasTreeNodeDatabaseContext } from "@/lib/sidebar/treeNodeContext";
import { isSchemaAware, isSingleDatabase, usesTreeSchemaMode } from "@/lib/database/databaseFeatureSupport";
import { codeMirrorSqlDialect, connectionUsesDatabaseObjectTreeMode, effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
import { sqlFormatDialectForDbType } from "@/lib/sql/sqlFormatter";
import { canFormatSqlForDatabaseType, formatSqlForEditing, sqlFormatDialectForDbType } from "@/lib/sql/sqlFormatter";
import { formatSqlSnapshotForSave } from "@/lib/sql/sqlFormatOnSave";
import { detectAndFormatStructured } from "@/lib/sql/autoFormat";
import { formatMongoShellText } from "@/lib/mongo/mongoFormatter";
import { detectAndFormatElasticsearchRequests } from "@/lib/elasticsearch/elasticsearchFormatter";
import { detectDatabaseFileType } from "@/lib/database/databaseFileDetection";
import { ensureJdbcxRuntimeDrivers } from "@/lib/database/jdbcxBuiltinDriver";
import { ensureRegisteredJdbcProductRuntimeDrivers } from "@/lib/database/jdbcProductProfiles";
@@ -1165,7 +1169,7 @@ function handleCloseActionPromptOpenChange(open: boolean) {
async function writeExternalSqlTab(tab: QueryTab, options: { closeAfterSave?: boolean; expectedContentHash?: string; expectedMissing?: boolean } = {}): Promise<"saved" | "retry" | "failed"> {
if (!tab.externalSqlPath || !isTauriRuntime()) return "failed";
try {
const result = await api.writeExternalSqlFile(tab.externalSqlPath, tab.sql, {
const result = await api.writeExternalSqlFile(tab.externalSqlPath, await formattedSqlForSave(tab), {
expectedContentHash: options.expectedContentHash,
expectedMissing: options.expectedMissing,
});
@@ -1211,6 +1215,45 @@ function savedSqlTargetForSave(tab: QueryTab) {
});
}
/**
* Applies the "format SQL when saving SQL files" editor setting: returns the
* formatted SQL that should be written to disk / the SQL library, mirroring the
* editor's own formatting logic (Mongo shell, Elasticsearch, structured JSON/XML
* and SQL) so a save never corrupts non-SQL content. When formatting changes the
* SQL, the tab content is also updated so the editor reflects exactly what was
* saved and stays clean if `markTabClean` runs afterwards.
*/
async function formattedSqlForSave(tab: QueryTab): Promise<string> {
if (!settingsStore.editorSettings.formatSqlOnSqlFileSave) return tab.sql;
const sqlSnapshot = tab.sql;
if (!sqlSnapshot.trim()) return sqlSnapshot;
const connection = connectionStore.getConfig(tab.connectionId);
const databaseType = effectiveDatabaseTypeForConnection(connection) ?? connection?.db_type;
if (!canFormatSqlForDatabaseType(databaseType)) return sqlSnapshot;
try {
return await formatSqlSnapshotForSave(
sqlSnapshot,
() => tab.sql,
async (sql) => {
if (databaseType === "mongodb") return formatMongoShellText(sql, settingsStore.editorSettings.sqlFormatter);
const esRequest = detectAndFormatElasticsearchRequests(sql, databaseType, settingsStore.editorSettings.sqlFormatter.tabWidth);
if (esRequest.kind === "elasticsearch") return esRequest.formatted;
if (esRequest.kind === "unsupported") return sql;
const structured = detectAndFormatStructured(sql, {
indentSize: settingsStore.editorSettings.sqlFormatter.tabWidth,
useTabs: settingsStore.editorSettings.sqlFormatter.useTabs,
});
if (structured.kind === "json" || structured.kind === "xml") return structured.formatted;
if (structured.kind === "unsupported") return sql;
return formatSqlForEditing(sql, sqlFormatDialectForDbType(databaseType), settingsStore.editorSettings.sqlFormatter);
},
(formatted) => queryStore.updateSql(tab.id, formatted),
);
} catch {
return tab.sql;
}
}
async function saveTabForCloseAll(tabId: string): Promise<boolean> {
const tab = queryStore.tabs.find((t) => t.id === tabId);
if (!tab) return true;
@@ -1237,7 +1280,7 @@ async function saveTabForCloseAll(tabId: string): Promise<boolean> {
database: target.database,
catalog: target.catalog,
schema: target.schema,
sql: tab.sql,
sql: await formattedSqlForSave(tab),
});
queryStore.linkSavedSql(tab.id, saved.id, saved.name);
queryStore.markTabClean(tab);
@@ -1305,7 +1348,7 @@ async function handleSaveTab(tabId: string) {
database: target.database,
catalog: target.catalog,
schema: target.schema,
sql: tab.sql,
sql: await formattedSqlForSave(tab),
});
queryStore.linkSavedSql(tab.id, updated.id, updated.name);
queryStore.markTabClean(tab);
@@ -1347,7 +1390,7 @@ async function openSaveSqlDialog() {
database: target.database,
catalog: target.catalog,
schema: target.schema,
sql: tab.sql,
sql: await formattedSqlForSave(tab),
});
queryStore.linkSavedSql(tab.id, updated.id, updated.name);
queryStore.markTabClean(tab);
@@ -1446,7 +1489,7 @@ async function confirmSaveSqlToLibrary() {
database: target.database,
catalog: target.catalog,
schema: target.schema,
sql: tab.sql,
sql: await formattedSqlForSave(tab),
});
queryStore.linkSavedSql(tab.id, saved.id, saved.name);
queryStore.markTabClean(tab);
@@ -1461,7 +1504,7 @@ async function confirmSaveSqlToLibrary() {
async function saveExternalSqlTabAs(tab: QueryTab): Promise<boolean> {
if (!canSaveSqlTab(tab) || !isTauriRuntime()) return false;
try {
const saved = await api.saveExternalSqlFile(defaultSavedSqlName(tab.title), tab.sql);
const saved = await api.saveExternalSqlFile(defaultSavedSqlName(tab.title), await formattedSqlForSave(tab));
if (!saved) return false;
queryStore.linkExternalSqlPath(tab.id, saved.path, sqlFileTitleFromPath(saved.path), saved.version);
rememberExternalSqlFileTarget(saved.path, { connectionId: tab.connectionId, database: tab.database, catalog: tab.catalog });