Pregunta

I often comment out lines,

but if the line is indented (almost always) then it leaves ugly whitespace inside the comment, like so:

Headers.Add(new List<string>
{
    BondsMonth,
    BondsIdCode,
    //                BondsBondTaken,
    //                // BondsPayments0,
    BondsPaidBackStatus,
});
  • I looked through Resharper C# Code Editing options, but could not find an option to make code cleanup / reformatting remove those additional spaces?
  • Or is it possible to make the comment shortcut / function remove more than 1 space in the first place?
¿Fue útil?

Solución

I agree with what Christopher said, but if you do still want to get rid of excess spaces after a //, you can do so by doing a search and replace using regular expressions with the following target and replacement:

Target: ^*// * Replacement: // (note the trailing space)

(Using the Visual Studio IDE to do so.)

From your example it looks like you might also want to replace ^*// // with //

Otros consejos

This doesn't answer the specific question you asked, but it will result in more maintainable, readable code and it wouldn't fit in a comment where I wanted to put it.

Commenting out a line should almost always be with the intent of either

  1. Completely removing the line (ugly doesn't matter)
  2. Putting it back in when everything else is working correctly. (Ugly doesn't matter since you'd never ever commit it w/out everything working).

Please just delete the line. It should be in source control (Git, SVN, anything) so that you can always recover what you deleted anyway.

In either of the above cases it should be so temporary of a fix that you shouldn't worry about it.

Thanks to Alex for pointing out that the Visual Studio add-in StyleCop does it beautifully, and even integrates with Resharper Code Cleanup out-of-the-box!

The only undesirable side-effect is that cleanup has become much slower, probably due to the plethora of rules StyleCop evaluates / fixes.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top