Toggle comment blockwise


#1

All we know that there is a shortcut for commenting a line CTRL+/ on PCs and so on so forth.

Since many languages permit for commenting blocks of code with special delimiters, say %- and -%, (Multiline comments would fall into this category some times), I would like to know whether ATOM.IO is able to do that.

For instance CTRL+/ would toggle to comment all the highlighted lines, whereas what I aim for is to have a shortcut for commenting the blocks such that

x = 30 <this is my comment highlighted by <>>
<this is another comment> y = 2 <x,y are just variables>
assert(x + y == 32) <and this is a 
multiline highlighted comment
which I want to toggle to comment>

and after hitting, for instance CTRL+ALT+/

x = 30 %-this is my comment highlighted by <>-%
%-this is another comment-% y = 2 %-x,y are just variables-%
assert(x + y == 32) %-and this is a 
multiline highlighted comment
which I want to toggle to comment-%

I don’t see any particular difficulty on implementing this since special characters, such as (), {}, or $$ (in LaTeX), already do exactly when typed.


#2

If there is no delimiters like %- and -%, and the language only supports for commenting from, say % to the end of the line, I would be easily detected by the editor and the shortcut CTRL+ALT+/ could instead comment from the cursor or the beginning of the first highlighted block in the line until the end of the line.

This would result into

x = 30 % this is my comment highlighted by <>
% this is another comment y = 2 x,y are just variables
assert(x + y == 32) % and this is a 
% multiline highlighted comment
% which I want to toggle to comment

which is slightly different from the current CTRL+/ everyone is used to.


#3

Atom is able to do this, easily. You just use the API to pull the text of each selection and replace instances of < and >, then insert the finished product.


#4

The < and > is to indicate which part of the text is selected (or highlighted)


#5

Then don’t replace any characters. Just insert the delimiters at the beginning and end of each selection. If you want to build this to be language-independent, keep in mind that language packages have config settings for defining their own comment markers.