PhpStorm Tips: Aligning Comment Slashes with Code for Better Formatting

PhpStorm by default adds the comment slashes at the very beginning of the line. This means if you were to comment out a section the slashes would be the first characters of those lines.

This becomes a bit ugly when you format the file through your IDE or some other formatting tool which will format the lines to push the slashes to be inline with the rest of the code, something similar to this:

Notice the extra spacing between the slashes and the actual code? I guess since you are here, it is bugging you too huh? In my case, I have a Github Action that automatically formats the code on push and whenever I was pushing comments at the beginning of the line, I was getting unnecessary fix formatting commits just for this.

The Solution

Thankfully, PhpStorm is fully customizable in most places. To fix that, go to settings and search for “Line comment at first column“. It should be located in “Preferences | Editor | Code Style | PHP | Code Generation” and similarly for other languages too.

Converting a line into a comment will now add the slashes where the code would normally start so the formatter wouldn’t need to update it later.

Leave a Reply

Your email address will not be published. Required fields are marked *