Visual Studio 2019实现保存时自动格式化(类VS Code Prettier功能)
Great question! I totally get why you’d want that seamless auto-formatting workflow from VS Code’s Prettier in Visual Studio 2019—consistent code style saves so much time and avoids nitpicks during reviews. Let’s break down your options:
Native VS2019 Solution (No Extensions Needed)
Yes, you can set up VS2019 to automatically run that "wrap, indent, and align parameters" functionality when you save a file. Here’s how:
Configure your formatting rules first
OpenTools > Options > Text Editor > [Your Language (e.g., C#)] > Code Style > Formatting > Wrapping. Look for the settings under "Parameters" (like "Wrap parameters" and "Align parameters") and tweak them to match your preferred style (e.g., wrap when lines get too long, align parameters vertically).Enable "Format Document on Save"
Head back toTools > Options > Text Editor > [Your Language] > Code Style > General, then check the box for "Format document on save".This will trigger the full document formatter every time you save, which includes applying those parameter wrapping/indent rules you set up. It’s not exactly Prettier’s targeted formatting, but it’s built-in and free.
Free Extensions for More Control
If you want more granular control or an experience closer to Prettier, these free extensions work great in VS2019:
CodeMaid
A fan-favorite, fully free extension that goes beyond basic formatting. You can set it to auto-format on save, customize every aspect of parameter wrapping/indentation, plus it cleans up redundant code, organizes using statements, and more. It’s highly customizable and integrates seamlessly with VS2019.Roslynator
While primarily a code analysis tool, Roslynator includes enhanced formatting rules that you can enable, and it works with VS’s native "Format on Save" setting. It adds more flexibility to parameter alignment and wrapping than the default formatter.
Either way, you can absolutely replicate that Prettier-like auto-formatting workflow in VS2019—whether using built-in settings or a free extension.
内容的提问来源于stack exchange,提问作者Ian G

