codemaid
codemaid copied to clipboard
Cleaning: Automatically insert brackets around one line statement blocks (SA1503)
Migrated from https://trello.com/c/OYUlarhQ with 18 votes
Would love to have a clean up feature that added { } brackets to one-line if statements.
if (x == y)
Do();
would be
if (x == y)
{
Do();
}
NOTE: This feature is dependent on a conversion to the Roslyn APIs.
The reverse would also be great. :)
For reference adding the { and } is SA1503.
Is there a way to write / add a custom rule to perform this action in the mean time?
@AdjutantML Unfortunately no
+1.
Another +1 for this enhancement.
The latest update to Visual Studio 2017 includes some experimental formatting options, including one titled "Add/remove braces for single-line control statements". Its still experimental to Microsoft, but check it out.
I prefer the opposite clean-up in most cases, because extra braces are often not needed for simple single line conditions (with no comment), e.g. validation of parameters, break/continue out of loops. Of course when a comment is between the condition and "single line" statement that is effectively a 2 line "block" and so must have braces to avoid mistakes, i.e. use all line types except blank lines in the calculation.
If this feature is added to CodeMaid, please ensure that both extents of this clean-up preference are respected. I find the "add/remove" setting names ambiguous. All these types of setting which some people prefer one way or the other should be tri-state (true = do it, false = do the opposite, null/unspecified = leave alone).
Taken further, some people may also wish to pull-back single line condition statements to the same line (as the condition) or enforce new line, either always or if they do not hit the word-wrap limit.
I would love it if the reverse for this issue could be implemented, aka:
Have this:
if (x == y)
{
Do();
}
Turn into this after format:
if (x == y)
Do();
The Add/remove braces option shown above works in VS 2022 only for adding braces, but I can't find any setting that inverts this behavior. Roslynator has a refactor option for this, but that doesn't work with formatting an entire document in 1 click.
It would be nice to get a table of which features are supported natively and what the EditorConfig setting names are compared to CodeMaid settings. I don't use CodeMaid anymore since most the formatting became built-in to VS2019 updates and are also applied automatically via other VS extensions such as "format on save".
I would consider using CodeMaid again, but only if it added significant value and had a cross platform option, i.e. it's clear everything is moving to VS Code as the only "Visual Studio" which is processor agnostic. Sure, VS2022 finally supports AMD64 and VS for Mac adds that platform. However, when producing new products with any Visual Studio development environment people lean towards VS Code due to accessibility. Professionally I still use the full VS but prefer to select extensions which are available with the same settings (i.e. EditorConfig alone) in both development environments.
Taken positively, I suppose this is a feature request to align all settings with EditorConfig including clear documentation (table of settings/map), remove built-in features and compile/package as a VS Code extension too.