🚨 Format code

This commit is contained in:
2025-09-24 21:44:42 +08:00
parent 1462d8a753
commit f5bfff80b7
76 changed files with 839 additions and 863 deletions

View File

@@ -1,5 +1,5 @@
import type { Plugin, SupportLanguage, Parser, Printer, SupportOption } from 'prettier'
import dockerfileInit, { format } from './docker_fmt_vite.js'
import type { Plugin, SupportLanguage, Parser, Printer, SupportOption } from 'prettier';
import dockerfileInit, { format } from './docker_fmt_vite.js';
// Language configuration for Dockerfile
const languages: SupportLanguage[] = [
@@ -11,7 +11,7 @@ const languages: SupportLanguage[] = [
linguistLanguageId: 99,
vscodeLanguageIds: ['dockerfile'],
},
]
];
// Parser configuration
const parsers: Record<string, Parser<any>> = {
@@ -19,69 +19,60 @@ const parsers: Record<string, Parser<any>> = {
parse: (text: string) => {
// For Dockerfile, we don't need complex parsing, just return the text
// The formatting will be handled by the print function
return { type: 'dockerfile', value: text }
return { type: 'dockerfile', value: text };
},
astFormat: 'dockerfile',
locStart: () => 0,
locEnd: () => 0,
},
}
};
// Printer configuration
const printers: Record<string, Printer<any>> = {
dockerfile: {
// @ts-expect-error -- Support async printer like shell plugin
async print(path: any, options: any) {
await ensureInitialized()
const text = path.getValue().value || path.getValue()
await ensureInitialized();
const text = path.getValue().value || path.getValue();
try {
const formatted = format(text, {
indent: options.tabWidth || 2,
trailingNewline: true,
spaceRedirects: options.spaceRedirects !== false,
})
return formatted
});
return formatted;
} catch (error) {
console.warn('Dockerfile formatting error:', error)
return text
console.warn('Dockerfile formatting error:', error);
return text;
}
},
},
}
};
// WASM initialization
let isInitialized = false
let initPromise: Promise<void> | null = null
let isInitialized = false;
let initPromise: Promise<void> | null = null;
async function ensureInitialized(): Promise<void> {
if (isInitialized) {
return Promise.resolve()
return Promise.resolve();
}
if (!initPromise) {
initPromise = (async () => {
try {
await dockerfileInit()
isInitialized = true
await dockerfileInit();
isInitialized = true;
} catch (error) {
console.warn('Failed to initialize Dockerfile WASM module:', error)
initPromise = null
throw error
console.warn('Failed to initialize Dockerfile WASM module:', error);
initPromise = null;
throw error;
}
})()
})();
}
return initPromise
}
// Configuration mapping function
function mapOptionsToConfig(options: any) {
return {
indent: options.tabWidth || 2,
trailingNewline: options.insertFinalNewline !== false,
spaceRedirects: options.spaceRedirects !== false,
}
return initPromise;
}
// Plugin options
@@ -92,7 +83,7 @@ const options: Record<string, SupportOption> = {
default: true,
description: 'Add spaces around redirect operators',
},
}
};
// Plugin definition
const plugin: Plugin = {
@@ -105,7 +96,7 @@ const plugin: Plugin = {
useTabs: false,
spaceRedirects: true,
},
}
};
export default plugin
export { languages, parsers, printers, options }
export default plugin;
export { languages, parsers, printers, options };