6

Is there a way to stop Visual Studio text editor (2015 or 2017) from auto moving the comment lines when pressing Ctrl+K,D or relevant key stroke (Ctrl+E,D - format entire document) ?

I would like my comments to stay at the position placed, regardless of the code position above or below.

For example, when writing a comment on a new line starting say at col 60 as below:

code line  
|-------- white space ----------| //... comment ..  <-- comment is auto shifted left  
code line

so that it doesn't interfere with the code below or above, when pressing Ctrl+K,D the comment is shifted left, to align with the surrounding code.

On the other hand, when a comment is placed following a code line, as below:

|--code line --|- white space -| //..1st comment line..  
|------white space ------------| //..2nd comment line.. <-- comment is properly auto aligned

an eventual 2nd comment on a new line, remains aligned with the one above, which indicates that the editor can adjust the position of comment lines based on certain criteria.

I should clarify that I would like to preserve the rest of the functionality of Ctrl+K,D.

Edit: I'm afraid that all the guidelines mentioned in Managing Code Formatting provided below by Seth, simply force the application of the instructions already set in text editor for every specific language (and in particular c#) about intending text.

So, even if some tabs or spaces are inserted in front of the comment, they are automatically wiped out if Ctrl+K,D is applied, simply because there is no relevant instruction about the handling of spaces in certain constructs, and so the general rule is applied.

I believe that a simple instruction added to text editor, such as "preserve spaces or tabs in front of comments", would solve the problem (as is indeed the case but only when a comment follows code).
It would be just an improvement to an already existing, albeit undocumented, behavior (as far as I have searched).

Dave
  • 25,297
  • 10
  • 57
  • 69
john_m
  • 161
  • 1
  • 6
  • [Managing Code Formatting](https://msdn.microsoft.com/en-us/library/999b219z(v=vs.71).aspx) might be relevant. Other than that it's probably not easy (at least). After all what would happen if the source is formatted so poorly if it's indented all the extra spaces? Suddenly they would be "interfering" again. – Seth Nov 13 '17 at 11:17
  • @Seth - Thank you, but pls see my Edit – john_m Nov 13 '17 at 12:29
  • Related topic: [Disable auto-indent of comments with Visual Studio 2015](https://stackoverflow.com/questions/43899505/disable-auto-indent-of-comments-with-visual-studio-2015) – john_m Jan 05 '18 at 07:27

2 Answers2

1

If you have ReSharper (a great tool to have for lots of reasons) its formatting engine does not have this issue. Go to VS > Tools > Customize > Keyboard... > Show Commands Containing "FormatDocument", and remove all key mappings. Now Show Commands Containing "Reformat", add shortcut Ctrl-KD to the ReSharper command.

Voila, unrelated comments don't get lined up just because they are on consecutive lines. You can also just use the R# command Ctrl-Alt-Enter instead of Ctrl-KD but the latter is what my fingers automatically do after years of practice so overriding this key combination is my preferred solution.

If you want a comment to have extra indentation, you can use "// // Comment..." which might not be exactly what you want but isn't too bad.

Mick Bruno
  • 119
  • 1
  • 2
    Thank you Mick for your answer, but unfortunately I don't use ReSharper. Therefore, I don't know if this answer could be marked as the correct one for the question posed. Perhaps a moderator could assist here.... – john_m Oct 12 '18 at 15:46
  • 2
    Yeah, didn't mean for this to be "the" answer, just a possible workaround. But as somebody who does not work for ReSharper but has been an avid user for a decade, try to get your employer (if you work as a programmer) to buy it for you. There are some great cost/benefit docs on their web site and it has genuinely made me a better programmer. – Mick Bruno Oct 15 '18 at 01:49
  • 1
    "IF"... I don't use any Resharper, and the problem is with Visual Studio Code as well... @john_m, how did you finally solve the problem? – serge Nov 16 '20 at 12:57
  • 2
    @Serge I didn't :) - and I'm really disappointed to hear about lots of "improvements" in code quality in VS, but still not doing anything about this tiny bit of code readability.. – john_m Nov 17 '20 at 17:40
-1

This is a "half solution", but I found nothing better. In many cases the comments alignment follows an user action. In theses case CTRL-Z (undo) put the comments in the position they had before the alignment. To undo the user action another CTRL-Z is required. As I said, it's not a solution, anyway it's the fastest way to keep the code as we want. Hope MS will had an option to disable comments alignment.