Visual Studio Marketplace: https://marketplace.visualstudio.com/items?itemName=OlegShilo.CodeMaidPlus
This simple extension (see GitHub home page) is an attempt to address some shortcomings of rather excellent code formatting extension CodeMaid. The best outcome is achieved when it is used (integrated) with CodeMaid even though it can be used on its own.
I hope at some stage this work will become a part of CodeMaid. If you wanter why it's not the case right now read this thread https://github.com/oleg-shilo/CodeMaidPlus/issues/1#event-2608741029
One of the most popular and versatile coding assistance solution, ReSharper, has a fundamental flaw. Its outdated formatting strategy is extremely aggressive and very often stays on the way of being productive. Thus it does not support some modern syntax paradigms nor allow applying styles in a less aggressive way. Basically when it comes to formatting this great product becomes rather stubborn.
The problem has been consistently reported to (and acknowledged by) JetBrains since as early as 10-15 years ago. Though their support always reject any need for improvement. If you google you will be able to find the threads where ReSHarper support simply advise the users to stop using ReSharper formatting if they don't like it as is.
For people who don't want to give up on ReSharper and yet demand more than its can do for formatting the solution can be to use it in conjunction with CodeMaid. CodeMaid is specifically developed as a versatile formatting solution thus it can be used in conjunction with ReSharper. CodeMaid for formatting and ReSharper for everything else.
This approach usually works very well. However due to the more liberal nature of the formatting algorithm (something that makes CodMaid so great) it can leave some formatting artefacts that need to be addressed manually.
This extension is an attempt to address these CodeMaid shortcomings by extending CodeMaid functionality with additional formatting algorithms that are automatically invoked during CodeMaid cleaning/formatting execution. You just need to configure CodeMaid to invoke CodeMaid+ during cleanup (see Installation section)
The following formatting actions are performed during CodeMaid+ execution:
Sorting using statements
CodeMaid invokes Visual Studio own 'remove and sort using statements'. If it is done on saving the document it can lead to the accidental loss of some of them. The problem is that VS does both removal and sorting. CodeMaid.Plus fixes this problem by not removing unused 'usings' and still sorting and removing duplicates.
All 'usings' are grouped and all four groups are ordered as below. And all items within a given group are sorted alphabetically:
using System.* using Microsoft.* using <aliases> using <statics>
Fluent is not aligned
CodeMaid/VS more relaxed indentation for fluent API is great but it may get accidentally misaligned and not even gain the first level of indent. CodeMaid+ fixes it by ensuring at least one level of indentation:
Limitations Currently each line being aligned is handled independently by aligning its indent to the nearest anchor point.
The alignment anchor points for a line are:
- total indent of the previous line
- total indent of the previous line + extra single indent
- start of the special tokens in the previous line:
- '.' character
- '(' character
In the future releases the alignment adjustments applied to the text above the line being aligned will be incorporated. The will lead to more harmonious alignment outcome.
XML Documentation block may contain trailing blank line
A simple problem that is just overlooked by CodeMaid.
Install the extension from Visual Studio Marketplace and configure it to be invoked during CodeMaid cleanup by placing the command
Tools.CM+Format in the CodeMaid > Options > Reorganizing > ThirdParty > Other Cleaning Commands:
Alternatively you can open extension setting dialog and press 'Integrate' button. The dialog can be accessed via View > Other Windows > CM+ Settings:
- Currently the extension processes only the files that are part of a solution.
- The formatting is based on the canonical C# bracket style. If you are using "Egyptian brackets" it will interfere with the extension Align Indents feature.
- The indent size is assumed to be 4 spaces.
It will be read from the VS settings in the future.
- May not be invoked automatically on-save for very large files.
This is actually a CodeMaid defect when it fails to invoke third-party extensions on-save. You will need to invoke the extension manually in such cases.
This solution is an open end effort and it can grow in additional functionality if the need arises. On the other hand, it may be short lived if CodeMaid address its problems natively.