Why does commenting a section of code mess up the format?


#1

When I use /* and */ to delineate a section of code as commented out, I’ve noticed that after I do a save, that section gets messed up. Are the characters I’m using not correct for commenting a section? That’d be a really nice right-click option to have: to either comment or uncomment a section.

If my original code looks like this:
for (counter = 0; counter < 12; counter++) {
newCount = counter + 1;
softAssertion.assertEquals(main.getLastFeatureSubList2Attribute(“innerText”,
String.valueOf(newCount)), MedvantxArray[counter], “Item " + MedvantxArray[newCount] + "
was not found.”);
}

My commented code then becomes this:
/*

  • for (counter = 0; counter < 12; counter++) { newCount = counter + 1;
  • softAssertion.assertEquals(main.getLastFeatureSubList2Attribute(“innerText”,
  • String.valueOf(newCount)), MedvantxArray[counter], "Item " +
  • MedvantxArray[newCount] + " was not found."); }
    */

For large sections that I’ve commented, that leaves me with a lot of time-wasting work to get that code back in it’s correct format.


#2

Hi @ohrnsk!

The auto-format on save feature was introduced in v0.20.0 (Release notes v0.20.0).
It is the default behavior, that when saving a file it is formatted (and comments are auto-formatted the way you described). However, you can disable this feature from the Main menu --> “Preferences” --> “User settings” --> “Editor” by adding the "formatOnSave": false parameter. After you disable that feature, your code won’t be formatted when you do a save.
If you want to only format your code (and not the comments), you could select the part which should be auto-formatted, right-click and choose “Format selection” from the menu.

Furthermore, there already exists a keyboard shortcut for commenting and uncommenting lines. :smiley: You can find all available shortcuts in the menu “Help” --> “Keyboard shortcuts”. You can change the shortcuts in there as you like.
The default shortcuts for commenting/uncommenting are as follows:

I guess this would resolve your need for a right-click option? Or would you prefer it as an option in the right-click menu?

Thanks a lot for your input!
Best regards.