feat(editor): format SQL files on save
This commit is contained in:
1 parent
249896dddf
commit
876f850e2d
17 files changed
+148
-7
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { formatSqlSnapshotForSave } from "@/lib/sql/sqlFormatOnSave";
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
const promise = new Promise<T>((resolvePromise) => {
|
||||
resolve = resolvePromise;
|
||||
});
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
describe("formatSqlSnapshotForSave", () => {
|
||||
it("does not overwrite edits made while formatting loads", async () => {
|
||||
const formatting = deferred<string>();
|
||||
let sql = "select 1";
|
||||
const pending = formatSqlSnapshotForSave(
|
||||
sql,
|
||||
() => sql,
|
||||
() => formatting.promise,
|
||||
(formatted) => {
|
||||
sql = formatted;
|
||||
},
|
||||
);
|
||||
|
||||
sql = "select 2";
|
||||
formatting.resolve("SELECT 1");
|
||||
|
||||
await expect(pending).resolves.toBe("select 2");
|
||||
expect(sql).toBe("select 2");
|
||||
});
|
||||
|
||||
it("applies and returns formatted SQL when the snapshot is unchanged", async () => {
|
||||
let sql = "select 1";
|
||||
await expect(
|
||||
formatSqlSnapshotForSave(
|
||||
sql,
|
||||
() => sql,
|
||||
async () => "SELECT 1",
|
||||
(formatted) => {
|
||||
sql = formatted;
|
||||
},
|
||||
),
|
||||
).resolves.toBe("SELECT 1");
|
||||
expect(sql).toBe("SELECT 1");
|
||||
});
|
||||
});
|
||||
@@ -227,6 +227,7 @@ describe("settings search", () => {
|
||||
{ titleKey: "settings.autoAliasTables", category: "editor", targetId: "editor" },
|
||||
{ titleKey: "settings.clickTableNavigationTarget", category: "editor", targetId: "editor" },
|
||||
{ titleKey: "settings.generateSqlIncludeDatabaseName", category: "editor", targetId: "editor" },
|
||||
{ titleKey: "settings.formatSqlOnSqlFileSave", category: "editor", targetId: "editor" },
|
||||
{ titleKey: "settings.sqlFormatterKeywordCase", category: "formatter", targetId: "formatter" },
|
||||
{ titleKey: "settings.sqlFormatterFunctionCase", category: "formatter", targetId: "formatter" },
|
||||
{ titleKey: "settings.sqlFormatterDataTypeCase", category: "formatter", targetId: "formatter" },
|
||||
|
||||
@@ -68,6 +68,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
|
||||
"openDataTabsNextToActive",
|
||||
"prefillNewQueryWithSelect",
|
||||
"generateSqlIncludeDatabaseName",
|
||||
"formatSqlOnSqlFileSave",
|
||||
"updateNotificationsEnabled",
|
||||
"sidebarObjectInfoMode",
|
||||
"sidebarAllowHorizontalScroll",
|
||||
|
||||
@@ -122,6 +122,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
|
||||
{ id: "editor-app-close-unsaved-tabs", category: "editor", titleKey: "settings.appCloseUnsavedTabsMode", descriptionKey: "settings.appCloseUnsavedTabsModeDescription", targetId: "editor" },
|
||||
{ id: "editor-prefill-query", category: "editor", titleKey: "settings.prefillNewQueryWithSelect", descriptionKey: "settings.prefillNewQueryWithSelectDescription", targetId: "editor" },
|
||||
{ id: "editor-generate-sql-include-database", category: "editor", titleKey: "settings.generateSqlIncludeDatabaseName", descriptionKey: "settings.generateSqlIncludeDatabaseNameDescription", targetId: "editor" },
|
||||
{ id: "editor-format-sql-on-sql-file-save", category: "editor", titleKey: "settings.formatSqlOnSqlFileSave", descriptionKey: "settings.formatSqlOnSqlFileSaveDescription", targetId: "editor" },
|
||||
{ id: "editor-diagnostics", category: "editor", titleKey: "settings.sqlSemanticDiagnosticsEnabled", descriptionKey: "settings.sqlSemanticDiagnosticsEnabledDescription", targetId: "editor" },
|
||||
{ id: "editor-sql-variables", category: "editor", titleKey: "settings.sqlVariableSyntax", descriptionKey: "settings.sqlVariableSyntaxDescription", targetId: "editor" },
|
||||
{ id: "editor-saved-sql-target", category: "editor", titleKey: "settings.savedSqlOpenTarget", targetId: "editor" },
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export async function formatSqlSnapshotForSave(sqlSnapshot: string, currentSql: () => string, format: (sql: string) => Promise<string>, applyFormatted: (sql: string) => void): Promise<string> {
|
||||
const formatted = await format(sqlSnapshot);
|
||||
if (currentSql() !== sqlSnapshot) return currentSql();
|
||||
if (formatted !== sqlSnapshot) applyFormatted(formatted);
|
||||
return formatted;
|
||||
}
|
||||
Reference in new issue
Block a user