Skip to content

Commit

Permalink
feat(prettier): add getPrettier option
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 21, 2023
1 parent 43ee5f9 commit 6804af6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/prettier/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ServicePluginInstance, ServicePlugin } from '@volar/language-service';
import type { ServicePluginInstance, ServicePlugin, ServiceEnvironment } from '@volar/language-service';
import type { Options, ResolveConfigOptions } from 'prettier';

export function create(
Expand Down Expand Up @@ -42,6 +42,7 @@ export function create(
* This property is useful whenever you want to load a specific instance of Prettier (for instance, loading the Prettier version installed in the user's project)
*/
prettier?: typeof import('prettier') | undefined;
getPrettier?: (serviceEnv: ServiceEnvironment) => typeof import('prettier') | undefined,
} = {},
getPrettierConfig = async (filePath: string, prettier: typeof import('prettier'), config?: ResolveConfigOptions) => {
return await prettier.resolveConfig(filePath, config) ?? {};
Expand All @@ -53,7 +54,9 @@ export function create(

let prettier: typeof import('prettier');
try {
prettier = options.prettier ?? require('prettier');
prettier = options.prettier
?? options.getPrettier?.(context.env)
?? require('prettier');
} catch (e) {
throw new Error("Could not load Prettier: " + e);
}
Expand Down

0 comments on commit 6804af6

Please sign in to comment.