开发者

Visual Studio - automatically add necessary spaces to comments

When I'm writing comments in my code, I often forget to add the initial space after the comment identifier.

'this is a comment

when really it is supposed to be

' this is a comment

I realize this is quite trivial, and you could simply say "just add the damn space you idiot", but I'd really like to automate this so that I just don't have to worry about it.

Can anyone point me in the right direction of an elegant way to add the comment space?

note I do realize that a catch all string replace or regex replace could screw up other things ... IE:

Dim something As String = "I'm a nerd"

would actually come out

Dim something As String = "I' m a nerd"

So the way I see this being resolved is if it's only on a line by it's 开发者_如何学Goself and is not followed by a second single quote... IE: '' would not trigger the replacement.


You could always get a copy of resharper and one of the rules in there is what you are describing. Once you finish with your code you can do a format on the whole file or even solution and it will globally fix that rule for you.


This would be a pretty good case for an editor extension. You can detect when a line is whitespace, apostrophe, not white space and either insert the space or put a decoration so you will learn to follow the pattern. If you've moved to 2010, consider it - they are really pretty easy to write.


I had and have similiar problems. This is a habit which you can change. Yet, sometimes your brain is just wired to make the same mistakes. For instance, no matter what I do, I always type data instead of date.

You can change your behavior. Find some method that you find helpful for changing habits. Create a "personal code review" checklist and add this item. After a few months, you will find the space comes naturally.

If this is one of the "hard-wired" brain goofs, then create a *Visual Studio" macro that visit's CodeDom. Through the CodeDom namespace it is easy to find comments and make changes as needed. Why use regular-expressions?

If you want to use regular-expressions, because you are familiar and it is easier, then create a better expression to avoid potential errors.

Refactoring VB.NET code with regular expressions at http://www.vbmigration.com/Blog/post/2008/07/Refactoring-VB%2cNET-code-with-regular-expressions.aspx should be helpful in creating better regular-expressions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜