Skip to content

Commit

Permalink
feat: implemented provideDocumentFormattingEdits
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 9, 2022
1 parent 1d37f55 commit 73f046d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/monaco/ls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,20 @@ export async function setupLs(modelsMap: Ref<Map<string, monaco.editor.ITextMode
return moncaoResult;
},
}),
monaco.languages.registerDocumentFormattingEditProvider(lang, {
provideDocumentFormattingEdits: async (model, options) => {
const document = documents.get(model);
if (document) {
const codeResult = await ds.format(
document,
monaco2code.asFormattingOptions(options),
);
if (codeResult) {
return codeResult.map(code2monaco.asTextEdit);
}
}
},
}),
monaco.languages.registerCompletionItemProvider(lang, {
// https://github.com/johnsoncodehk/volar/blob/2f786182250d27e99cc3714fbfc7d209616e2289/packages/vue-language-server/src/registers/registerlanguageFeatures.ts#L57
triggerCharacters: '!@#$%^&*()_+-=`~{}|[]\:";\'<>?,./ '.split(''),
Expand Down
7 changes: 7 additions & 0 deletions src/monaco/monaco2code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ export function asTriggerKind(kind: monaco.languages.CompletionTriggerKind): vsc
return vscode.CompletionTriggerKind.TriggerForIncompleteCompletions;
}
}

export function asFormattingOptions(options: monaco.languages.FormattingOptions): vscode.FormattingOptions {
return {
tabSize: options.tabSize,
insertSpaces: options.insertSpaces,
};
}

0 comments on commit 73f046d

Please sign in to comment.