Prettier-vscode: How to format current file automatically without saving manually?

Created on 24 Jun 2018  路  9Comments  路  Source: prettier/prettier-vscode

I want Prettier to format everything I type in supported languages without manually saving or unfocusing editors. formatOnType appears to be unsupported. formatOnSave only works if I set autoSave to onWindowChange or onFocusChange, but I want to be able to write large amounts of code in the current file keeping it formatted without manually saving, and afterDelay does not trigger formatOnSave.

Is there a supported configuration for this use case, or would formatOnType or afterDelay need to be implemented?

enhancement locked

Most helpful comment

I understand part of the issue is with VSCode and not the Prettier extension, but what is the best answer to my question? I was hoping to find a recommended solution that solves these issues, even if it doesn't involve changes to the Prettier extension.

All 9 comments

I think that formatOnType is almost impossible because if you type your code it gets restructured and your cursor position won't work probably.

Question: Why is hitting cmd+s not an option? Or why is autoSave on window change not an option?

Autosave in VSCode

"editor.formatOnSave": true,
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,

@RobinMalfait I believe we can use prettier.formatWithCursor for this. I want to be able to auto save with Prettier formatting. onWindowChange is close to what I want, but I'll still have to save or switch windows/editors to format the file when typing large changes, which is not ideal compared to using auto save or formatOnType.

@tree4pl Prettier formatting does not support afterDelay. After one second the file will be auto saved, but it will not be formatted until it is manually saved.

There are other issues with formatOnType so I'll close that one.
The autoSave and formatOnSave combination is a limitation of vscode.

I understand part of the issue is with VSCode and not the Prettier extension, but what is the best answer to my question? I was hoping to find a recommended solution that solves these issues, even if it doesn't involve changes to the Prettier extension.

What you proposing is quite amazing feature, instead of pressing ctrl + s to format the code on autosave will be quite easy and amazing at the same time. I hope they made this feature available anytime soon.

I am still having this issue today. Looks like it has not been resolved.

The same for me. Looking for this feature to be available.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings