Skip to content

Commit

Permalink
Allow prettier getter async
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 13, 2024
1 parent 4e54e05 commit ab0b18d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/prettier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function create(
/**
* Prettier instance or getter to use.
*/
prettierInstanceOrGetter: typeof import('prettier') | ((context: ServiceContext) => typeof import('prettier') | undefined),
prettierInstanceOrGetter: typeof import('prettier') | ((context: ServiceContext) => Result<typeof import('prettier') | undefined>),
{
html,
documentSelector = ['html', 'css', 'scss', 'typescript', 'javascript'],
Expand Down Expand Up @@ -56,20 +56,23 @@ export function create(
name: 'prettier',
create(context): ServicePluginInstance {

const prettier = typeof prettierInstanceOrGetter === 'function'
? prettierInstanceOrGetter(context)
: prettierInstanceOrGetter;

if (!prettier) {
return {};
}
let prettierInstanceOrPromise: Result<typeof import('prettier') | undefined>;

return {
async provideDocumentFormattingEdits(document, _, formatOptions) {
if (!matchDocument(documentSelector, document)) {
return;
}

prettierInstanceOrPromise ??= typeof prettierInstanceOrGetter === 'function'
? prettierInstanceOrGetter(context)
: prettierInstanceOrGetter;

const prettier = await prettierInstanceOrPromise;
if (!prettier) {
return;
}

if (!isFormattingEnabled(prettier, document, context)) {
return;
}
Expand Down

0 comments on commit ab0b18d

Please sign in to comment.