I’m developing a VSCode extension to format files with the default formatter via the context menu.
To do this, I used the following code:
document = await vscode.workspace.openTextDocument(uri.fsPath);
const edits: vscode.TextEdit[] = await vscode.commands.executeCommand('vscode.executeFormatDocumentProvider', document.uri, document.languageId);
However, it only works if I set a timeout after the openTextDocument
or if I’ve previously opened any file with the same language as the document. Otherwise, the executeCommand
return undefined. I suppose this happens because the format document provider is not ready yet.
How can I fix this? Is there any way to await the format document provider’s startup?
Vinícius Iancovski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.