Skip to content

Improve singleline selection commenting

Waqar Ahmed requested to merge work/commentingSelections into master

Currently, if you have a selection and "afterwhitespace" turned on for the language that you are using and you do a selection and comment it out it leads to a really bad result with languages like C/C++. Following code:

for (auto x : list) {
    if (x > 1)
    	printf("%d", x);
}

will become

// for (auto x : list) {
    // if (x > 1)
    	// printf("%d", x);
// }

Which makes zero sense. If the formatter runs over the above commented code it will just destroy all the indentation and make the code unreadable.

With this change, the comment marker will be placed at the smallest indent found in the selection, which can be 0. For the above code, the result will be:

// for (auto x : list) {
//    if (x > 1)
//    	printf("%d", x);
// }

Comments are nicely aligned, and we keep the indentation.

This behaviour is consistent with other editors for e.g., vscode, sublime. Its also compatible with what for e.g clang-format or prettier do, i.e., align the comments according to indentation setting.

CCBUG: 438744 CCBUG: 456819

Edited by Waqar Ahmed

Merge request reports