Added sql prettier plugin

This commit is contained in:
2025-09-12 00:52:19 +08:00
parent b745329e26
commit 41afb834ae
6 changed files with 1342 additions and 4 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
import { Parser, Plugin } from "prettier";
export declare const languages: Plugin["languages"];
export declare const parsers: {
sql: Parser;
};
export declare const printers: Plugin["printers"];
export declare const options: Plugin["options"];
declare const plugin: Plugin;
export default plugin;

View File

@@ -0,0 +1,60 @@
import { format } from 'sql-formatter';
import { detectDialect } from './detect.mjs';
// Languages
export const languages = [{
name: "SQL",
parsers: ["sql"],
extensions: [".sql"]
}];
// Parsers
export const parsers = {
sql: {
parse: (text) => text,
astFormat: "sql-format",
locStart: (node) => 0,
locEnd: (node) => node.length
}
};
// Printers
export const printers = {
"sql-format": {
print: (path) => {
const text = path.getValue();
if (!text || typeof text !== 'string') {
return text;
}
try {
// 自动检测SQL方言
const dialect = detectDialect(text);
// 格式化配置 - 使用固定的最佳实践配置
const formatOptions = {
language: dialect,
tabWidth: 2,
useTabs: true,
keywordCase: 'upper',
dataTypeCase: 'upper',
functionCase: 'upper',
identifierCase: 'preserve'
};
return format(text, formatOptions);
} catch (error) {
return text;
}
}
}
};
// Default export
export default {
languages,
parsers,
printers
};