feat(editor): auto-detect JSON and XML formatting
This commit is contained in:
1 parent
3e779d24d7
commit
0bf2475804
15 files changed
+407
-2
No files matched your search
@@ -1,9 +1,25 @@
|
||||
import { DEFAULT_SQL_FORMATTER_SETTINGS, sqlFormatterOptions, type SqlFormatterSettings } from "@/lib/sql/sqlFormatterConfig";
|
||||
import { looksLikeXml } from "@/lib/sql/autoFormat";
|
||||
|
||||
export type SqlFormatDialect = "mysql" | "postgres" | "sqlite" | "sqlserver" | "clickhouse" | "generic";
|
||||
|
||||
export const MAX_SQL_FORMAT_CHARS = 1_000_000;
|
||||
|
||||
/**
|
||||
* Thrown by {@link formatSqlText} when the input is XML-looking and must never
|
||||
* be run through the SQL formatter. sql-formatter silently rewrites well-formed
|
||||
* XML into corrupted output, so this guard keeps any caller (including future
|
||||
* ones) from corrupting structured text. Callers that can format XML should
|
||||
* route before calling (see {@link detectAndFormatStructured}); this is
|
||||
* defense-in-depth.
|
||||
*/
|
||||
export class UnsupportedStructuredInputError extends Error {
|
||||
constructor(readonly detectedType: "xml") {
|
||||
super(`Cannot format ${detectedType} content as SQL.`);
|
||||
this.name = "UnsupportedStructuredInputError";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a connection's database type to the SQL-formatter dialect to use.
|
||||
*
|
||||
@@ -65,6 +81,10 @@ export async function formatSqlText(sql: string, dialect: SqlFormatDialect = "ge
|
||||
throw new Error("SQL is too large to format safely.");
|
||||
}
|
||||
|
||||
if (looksLikeXml(sql)) {
|
||||
throw new UnsupportedStructuredInputError("xml");
|
||||
}
|
||||
|
||||
const { format } = await import("sql-formatter");
|
||||
const options = sqlFormatterOptions(settings);
|
||||
const language = formatterLanguage(dialect);
|
||||
|
||||
Reference in new issue
Block a user