✨ Added sql prettier plugin
This commit is contained in:
1180
frontend/src/utils/prettier/plugins/sql/detect.mjs
Normal file
1180
frontend/src/utils/prettier/plugins/sql/detect.mjs
Normal file
File diff suppressed because it is too large
Load Diff
11
frontend/src/utils/prettier/plugins/sql/sql.d.ts
vendored
Normal file
11
frontend/src/utils/prettier/plugins/sql/sql.d.ts
vendored
Normal 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;
|
||||
60
frontend/src/utils/prettier/plugins/sql/sql.mjs
Normal file
60
frontend/src/utils/prettier/plugins/sql/sql.mjs
Normal 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
|
||||
};
|
||||
Reference in New Issue
Block a user