]> git.lizzy.rs Git - micro.git/commitdiff
Improve comment plugin
authorZachary Yedidia <zyedidia@gmail.com>
Sun, 5 Jul 2020 19:48:49 +0000 (15:48 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Sun, 5 Jul 2020 19:48:49 +0000 (15:48 -0400)
When commenting a selection, the plugin won't just toggle each
line individually but will only uncomment the block if it is all
comments.

The comment plugin also now takes into account any number of spaces
between the comment character and the text. For example '//comment' will
be uncommented properly, as well as '//      comment'.

Fixes #1758

internal/config/runtime.go
runtime/plugins/comment/comment.lua

index b2ed70707a22310d5861addf07202dc13c96c329..3a7a094916cfac9b9151d32272a7999bd1c7e689 100644 (file)
@@ -1083,7 +1083,7 @@ func runtimePluginsAutocloseAutocloseLua() (*asset, error) {
        return a, nil
 }
 
-var _runtimePluginsCommentCommentLua = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x58\x6f\x6f\xdb\xb6\x13\x7e\xef\x4f\xc1\x9f\x5a\x01\x92\x63\xcb\x71\x7e\xef\xbc\x79\x45\x1b\x6c\xed\xb0\xac\x05\x9c\xac\x73\x61\x78\x00\x2d\x9d\x2c\xd6\x14\xa9\x91\x54\x53\x37\xe8\x3e\xfb\x40\xea\xbf\x28\x7b\x09\x06\x6c\x6f\x16\x20\xb1\xf9\xf0\xee\xb9\x23\xef\x39\x92\xc8\xfb\xef\x57\xb7\x3f\xbe\x7b\x8b\x96\xc8\x99\x07\x97\xc1\xa5\x33\x1a\x51\x1e\x62\x8a\x72\x45\x28\x5a\x22\x92\x66\x5c\x28\xcf\x49\x49\x28\xf8\x4c\x83\x8e\x5f\x5a\x84\x9c\xc5\x64\x6f\xd9\x14\x70\x6d\xb5\xcb\xe3\x18\x84\x65\x55\xc0\x8e\x5f\x85\x8b\x15\x5a\xa2\x87\xaf\xa3\x51\xac\x36\x0e\xce\x70\x98\x80\x26\x72\xb6\x3a\xb3\x67\xc8\x95\x8e\x99\xd9\x61\x55\x40\x8b\x45\x8d\x85\x05\x32\x9b\x35\xc8\xc5\x85\x8d\xa5\xf8\x00\x7d\xba\xa1\x10\xa1\x38\x4a\x85\xa9\x05\x4b\x59\x72\x8e\x91\x2b\xd1\x78\x56\xc0\x91\x15\x28\xc2\x42\xd9\x20\x0f\x0f\x20\x62\x42\xad\x14\x80\xa6\x05\x34\x9d\xd6\x58\x4c\x64\xd2\xb7\xdb\x47\x32\x14\x24\x53\x16\x4e\x25\xb5\xc2\xed\xb9\x05\x25\x58\x1e\x80\x52\x2b\x56\xa2\xd2\x12\xfc\xf6\x7f\x06\x46\xd3\xe9\x77\xc5\x14\x61\xa4\x98\xf9\xa6\xb6\xfe\x88\x3f\x61\x8b\x5a\x83\xed\xe4\xda\x53\x84\x7d\xc4\x57\x05\xfc\xa0\x93\x46\xcf\xbe\x96\x33\x39\x25\xb8\xbf\x98\x03\x57\x94\x30\x8b\x85\xe6\xd8\xca\x3b\xc5\xe2\x10\xf1\x7b\x76\x22\x77\xb6\x27\xec\x73\x9f\x9e\x91\xb4\x0f\xf1\xdd\x47\x5b\x3f\x19\x96\x61\xa5\x80\x07\x4d\x5b\xe6\x9c\x81\xb0\x74\x91\x25\x99\xed\xcf\xd9\x71\x00\xbc\x07\x21\x93\xba\x06\x2d\x0a\xc1\x95\x5d\xb0\xec\xa8\x12\xce\x2c\x5b\x83\xfe\xbf\x0f\x8b\x7c\x77\xb4\x31\x69\x17\x44\x2f\xcc\xae\xe0\x60\x5a\xf2\x77\x5b\x2d\xf2\x9e\xc4\x36\xa9\x82\x72\xab\xdd\x06\xe2\xa9\x45\xa8\xee\xc9\x7e\x48\x0a\x9f\x2c\xc2\xcf\x27\x35\x79\xc4\x36\xef\x97\x8a\xb6\xc5\xf0\xe5\x84\x20\xbf\x74\x3b\x6b\x14\xe7\x2c\x54\x84\x33\xc4\xd9\x2b\x73\x24\xbd\xcb\x80\x79\xbb\x3c\xf6\x47\x08\x21\x44\x62\x7d\x80\x05\xb7\xa0\x14\x61\x7b\xa9\x4f\x8c\x34\x05\xa6\xd4\x31\x33\x9d\xbc\x44\x8c\x50\xa4\x12\x60\xc6\xbc\x74\x89\xd5\xa6\xeb\xa5\x1b\xbf\x70\xd9\xa2\x3f\x06\x7c\xf4\xcf\xd9\x38\x67\x29\x6b\x1a\xa0\x12\x9e\xc0\x59\xee\x41\xed\xcd\xa2\x51\xf5\xa9\x7f\x9b\xbd\x29\x1d\x6f\x08\x03\x6f\x97\x4d\x10\x25\x0c\xde\x16\x1b\x54\x9c\xde\x1a\x40\x4b\xb4\xcb\x82\x57\x79\xbc\x30\x76\x96\x4d\x49\x72\x77\xcc\x1a\xd3\x53\xc9\xd9\x6e\x2b\xd8\xc3\x67\x9d\xf3\x6f\xae\x1c\x3b\x28\x08\xda\x7c\x8b\xbd\xcc\x77\x9e\xe3\xba\xce\xc4\x71\x5d\xd7\x75\xfc\x0a\x79\xae\x91\xe7\xcd\xd8\xd7\x63\xbf\x19\x7b\x7a\xec\x35\xe3\x17\x7a\xfc\xa2\x19\x8f\x9d\x09\xd2\x7f\x6b\x60\x6a\x80\x69\x03\x04\x06\x08\x1a\xe0\xc2\x00\x17\x0d\xb0\x35\xc0\xb6\x01\x36\x06\xd8\x34\x80\xeb\xba\x52\x63\x5e\x30\xf6\x9d\xf6\x9e\x49\xd0\x77\xf0\x74\x97\x05\xd7\xb9\x90\x5c\xe8\x8f\x5b\xa0\x60\xca\xd2\xde\xa4\x5c\x64\x5c\x76\x4d\x6f\x78\xd8\xb2\x20\x2c\x32\xfb\x27\x95\x20\x6c\x1f\xc4\x84\x45\x5e\x6b\x07\x75\x42\xae\x74\x7c\x34\x45\xf3\x4a\xf8\xa5\x69\x8a\x55\x98\x98\x72\x4e\x3a\xc5\xf0\xbb\x12\xce\x59\x39\x09\xd1\x4d\xa1\x86\xbf\xf2\xaf\x5d\x4b\xd9\xac\x20\xa3\x38\x04\xaf\x78\x1a\xe8\xfc\xbd\xcb\x4a\x6b\x13\xd4\x42\x9f\x15\x64\xd5\x8c\x7e\x94\x04\xaf\x41\xdd\x00\x8e\x08\xdb\xff\x9a\x10\x05\x32\xd3\x4c\xda\xc2\xd7\x5a\xe9\x25\xe7\xb7\x9b\xb5\xde\xb1\xc5\x1b\x2c\xeb\xcd\xf5\xfc\x81\x0e\x1d\x2c\xc3\x66\xbe\x0d\x3e\xe8\xe5\x02\x35\x5f\x1f\xe3\x72\xd5\xb8\x5c\x3d\xd2\x65\xbe\x0d\xd6\x4d\x94\xf5\x23\xa3\xac\x9b\x28\xeb\x33\xc7\x44\xed\xaf\xed\x0b\x31\x05\x6b\x34\x2d\x54\x73\xc2\xf4\x43\x63\xfa\xc1\x3e\x43\xda\x21\x3a\x8d\x5c\xcb\x63\xa8\x7f\x75\x17\x28\x41\xd2\xa2\x72\xff\xa4\x42\xfe\xd3\xc7\xd3\xf5\x71\xf1\x37\xf4\x51\x7e\x36\x9b\xbb\x02\x2d\x13\x05\x9e\xdf\x9d\x08\x6e\xb0\x54\xef\x89\xcc\x31\x5d\x17\xf7\x46\xe9\xf0\x1a\x2a\xd8\xf3\x87\xef\xab\xa6\x58\xfa\xd2\x92\x0a\x0b\x73\x85\x4d\x74\xf4\xa6\xcc\x31\x17\xd5\xfd\x65\x9b\xa0\x88\xd7\xb9\x0f\xdd\x82\xfe\xf9\x1b\xd3\xd8\x61\xb1\x97\xcd\x6b\xe2\x51\x6a\x6a\x1b\xf6\xcb\xbc\x78\x2d\x00\x2b\x10\x77\x09\x66\xde\x89\xab\x61\x73\xb5\x1d\xd0\x67\xd1\x87\xd5\xca\x96\xe7\xf4\xda\xf1\x3b\x97\x8d\x56\xc4\x12\x5d\xda\xc1\xca\x7d\x29\x43\x55\xdf\xaa\xeb\xa5\xaf\x87\xde\x1e\x77\x2b\x77\xa6\x49\x7a\xb5\x1c\x54\xef\x23\x97\x6d\x35\xda\xe9\x65\x5f\xfd\x9b\xcb\x9e\x9f\x58\xf6\xd0\xc9\xdb\x97\x6c\xab\x3b\x4f\x29\xd7\x1c\xbf\xb2\xfd\x08\xd1\x48\x0a\x91\xee\x8f\xf2\xa0\xd6\x6f\x30\x2f\x98\xfa\xae\x1c\x3f\x37\x6f\x99\x79\xeb\x2d\x53\xbf\xc1\x0c\x85\x00\x95\x0b\x56\x71\xf4\x62\x95\x2f\x04\xd3\x76\xd2\xbb\x35\xa3\xc9\xad\x1e\x75\x9c\x2b\xb3\x7c\x57\xd9\xcc\x27\x25\x46\x81\x79\x85\x83\xbf\x5c\x9a\x2f\xbd\x10\x84\x11\x55\x1e\x29\xc5\x3f\x26\x82\x9f\xf1\x01\xae\x79\x9a\x62\x16\x79\xd5\xab\xd3\xa9\x9f\x26\x93\xca\xec\x2d\xbf\xe6\x69\x46\x41\x41\xc7\xfb\x4e\x1c\x5f\x11\x16\xfd\x04\x47\xcf\x79\x49\xd5\x74\xa6\xd7\x4f\x73\xbc\x28\xfd\x83\x86\x31\xc6\x54\x9e\x76\xbe\x56\x82\xfe\xc2\x22\x10\x32\xe4\x02\x9e\xc2\xf2\x32\x8a\x56\x39\x53\x24\x85\x1f\x08\x85\xee\x1a\x8c\xc1\xea\xee\x0d\xd0\x6c\x82\x9c\x04\x68\x36\xab\x28\xd3\xc8\x29\xce\xc9\x3f\x03\x00\x00\xff\xff\xcf\xe8\x24\xaf\xf3\x11\x00\x00"
+var _runtimePluginsCommentCommentLua = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xd4\x58\x6f\x6f\xdb\xb6\x13\x7e\xef\x4f\xc1\x9f\x5a\x03\x92\x63\x2b\x71\x7e\xef\xbc\x79\x45\x1b\x6c\xed\xb0\xac\x05\x92\xac\x4b\x11\x78\x00\x2d\x9d\x64\x36\x14\xa9\x91\x54\x13\xb7\xe8\x3e\xfb\x40\x52\xff\x29\x3b\x4e\xb0\x62\x98\x81\x22\xe6\xc3\xbb\x87\xc7\xe3\x3d\x47\xba\xef\x7f\xbc\xb8\xfc\xf9\xdd\x5b\xb4\x44\xde\x3c\x3c\x09\x4f\xbc\xd1\x88\xf2\x08\x53\x54\x28\x42\xd1\x12\x91\x2c\xe7\x42\xf9\x5e\x46\x22\xc1\x8f\x35\xe8\x05\xa5\x45\xc4\x59\x42\x52\xc7\xc6\xc2\xb5\xd5\xba\x48\x12\x10\x8e\x95\x85\xbd\xa0\x5a\x2e\x51\x68\x89\xbe\x7c\x1d\x8d\x12\x75\xe3\xe1\x1c\x47\x1b\xd0\x44\xde\x4a\x47\xf6\x0c\x8d\xa5\x67\x66\xd6\x58\x59\x68\xb1\xa8\xb1\xc8\x22\xc7\xc7\x0d\x72\x74\xe4\x62\x19\xbe\x85\x3e\xdd\xd0\x12\x91\xd8\x4a\x85\xa9\x03\x4b\x59\x72\x4e\xd0\x58\xa2\xc9\xb1\x85\x63\x67\xa1\x18\x0b\xe5\x82\x3c\xba\x05\x91\x10\xea\x84\x00\x34\xb3\xd0\x6c\x56\x63\x09\x91\x9b\xbe\x5d\x1a\xcb\x48\x90\x5c\x39\x38\x95\xd4\x59\x2e\xe5\x0e\xb4\xc1\xf2\x16\x28\x75\xd6\xda\xa8\xac\x04\xbf\xff\x9f\x81\xd1\x6c\xf6\x83\x9d\x22\x8c\xd8\x99\xef\x6a\xeb\x8f\xf8\x13\x76\xa8\x35\xd8\x0e\xae\x3d\x45\xd8\x47\x7c\x6a\xe1\x2f\x3a\x68\xf4\xec\x6b\x39\x53\x50\x82\xfb\x9b\xb9\xe5\x8a\x12\xe6\xb0\xd0\x02\x3b\x71\x67\x58\xdc\xc6\xfc\x8e\xed\x88\x9d\xa5\x84\xdd\xf7\xe9\x19\xc9\xfa\x10\x5f\x7f\x74\xeb\x27\xc7\x32\xaa\x2a\xe0\x8b\xa6\x2d\x63\xce\x41\x38\x75\x91\x6f\x72\xd7\x9f\xb3\xed\x00\x78\x07\x42\x6e\xea\x33\x68\x51\x08\xae\xdc\x03\xcb\xb7\x6a\xc3\x99\x63\x6b\xd0\xff\xf7\x61\x51\xac\xb7\x2e\x26\xdd\x03\xd1\x1b\x73\x4f\x70\x30\x2c\xf9\xa7\x5b\x2d\xf2\x8e\x24\x2e\xa9\x82\x32\xd5\xe3\x06\xe2\x99\x43\xa8\xee\x48\x3a\x54\x0a\x9f\x1c\xc2\xfb\x9d\x35\xb9\xc5\x2e\xef\xe7\x8a\xb6\xc5\xf0\x79\x47\x41\x7e\xee\x2a\x6b\x94\x14\x2c\x52\x84\x33\xc4\xd9\x2b\xd3\x92\xde\xe5\xc0\xfc\x75\x91\x04\x23\x84\x10\x22\x89\x6e\x60\xe1\x25\x28\x45\x58\x2a\x75\xc7\xc8\x32\x60\x4a\x6d\x73\xa3\xe4\x25\x62\x84\x22\xb5\x01\x66\xcc\x4b\x97\x44\xdd\x74\xbd\xb4\xf0\xad\xcb\x0a\xfd\x35\xe0\xa3\x3f\x7b\xd7\xd9\x4b\x59\xd3\x00\x95\xf0\x08\xce\x32\x07\xb5\x37\x8b\x47\xd5\x5f\xfd\xaf\xc9\x0d\x91\x67\xd6\x15\x62\x7f\x9d\x4f\x11\x25\x0c\xde\x4e\x51\xc9\x77\x01\x29\xdc\xdb\x74\xd9\x5e\xae\xa7\xd1\x12\xad\xf3\xf0\x55\x91\x2c\xce\x09\x03\xdf\x78\xd4\x29\x95\x4a\x10\x96\x86\x19\x56\xd1\xc6\x4c\xf5\xb8\xba\xc9\x11\xa0\x0a\xc1\x90\x12\x05\x8c\xda\x71\x96\x78\x82\xf5\xae\xbb\x01\x97\x6c\x66\xe9\x3a\xe0\xc3\x43\xac\xee\x37\x43\x72\xb5\xcd\x1b\xd3\x5d\xd9\x6c\xb9\x49\xd0\xf7\xe6\x6c\x9d\x87\x67\x85\x90\x5c\xe8\x3f\x97\x40\xc1\x44\xd6\xa6\x2f\x44\xce\x65\xd7\xf4\x9c\x47\x2d\x0b\xc2\x62\xb8\x47\xcb\x2a\x5d\x09\x61\xb1\xdf\x0a\x6a\x8a\xbc\xf1\x58\x7a\x01\x9a\xa1\xb9\x1b\x36\xc4\xe7\x76\x8f\x2d\x8f\x45\x2a\x8b\xb5\x6f\xbc\xa6\x48\x09\x92\x99\x4d\x07\x76\xd3\x65\x2e\x2e\x20\xa7\x38\x02\xdf\x5e\xd0\x3a\x22\xff\xa4\x4a\xe0\x14\xb5\xd0\x67\xf6\xe0\xaa\x19\xfd\x34\x08\x5f\x83\x3a\x07\x1c\x13\x96\xfe\xbe\x21\x0a\x64\xae\x99\xcc\x1a\x28\x0c\xbb\x91\x35\xfa\xaa\x76\xbf\x78\x83\x65\x9d\x28\xbf\x57\x04\xc3\xe9\xbc\x99\xaf\xc2\x0f\x3a\x43\x40\xcd\xd7\x87\xcc\x4f\x1b\xf3\xd3\x03\xcc\xe7\xab\xf0\xba\x61\xbf\x3e\x80\xfd\xba\x61\xb7\xe6\x1d\x49\x36\x7e\xda\xce\x16\x40\x78\x8d\x8e\xec\x49\x0f\x98\x7d\x68\xcc\x3e\x74\x6a\xbf\x49\xda\x05\xe8\x63\x57\xe0\x07\xdd\x89\xf0\x1c\x4b\xf5\x9e\xc8\x02\xd3\x6b\x5b\xbe\xa5\xc3\x6b\xa8\x60\x3f\xe8\xc9\xa6\x60\x83\xc2\x79\xaa\xd2\xff\xa3\x32\x7a\x74\x83\xaa\xd3\x56\x6b\xee\x21\xff\xf6\x51\x7f\x63\xd1\xf5\x82\x0b\xda\xf7\xd4\x61\xd2\x7b\x82\xfc\x9e\x20\xc1\x27\xc8\xf0\x91\x52\x1c\xbe\x21\x87\x25\x39\xeb\x49\xf2\x01\x59\xa2\xde\xf5\xf9\xed\x24\xaa\x78\x9a\x52\x38\x3b\x54\xa6\x24\x39\xe8\xf2\xde\x51\xcf\x0f\xf3\x77\xf2\xb9\xe7\xd6\x75\xdf\x14\x9d\x8d\x34\xb5\xa7\xfd\xa4\xc2\xc2\xb0\x4c\xb5\xdf\xf9\x0e\xf9\x58\x71\x63\x4a\x4b\x0e\xdd\x03\xea\x37\x42\xc2\x45\xd5\x9c\x5c\x36\x14\xf3\xb6\x0a\x18\x57\x83\x49\xda\x9b\x23\xfd\xe9\xae\x6d\xdf\x21\x43\xd5\xf0\xe8\x88\xda\xc4\xce\xaa\xc3\xa7\xb3\xab\xc1\x38\x05\x3f\xe4\x1c\x1c\xf0\x06\x2c\xfd\x8c\x0f\x16\xa9\xfc\x07\x5a\x7c\x3b\x62\xfd\x1c\xfd\x63\x2c\x27\x5e\xeb\xa1\xd0\x79\xb2\x78\x53\x6f\x3c\x1e\x8f\xbd\xa0\x42\x9e\x6b\xe4\x79\x33\x0e\xf4\x38\x68\xc6\xbe\x1e\xfb\xcd\xf8\x85\x1e\xbf\x68\xc6\x13\x4f\x77\xfe\x49\x03\xcc\x0c\x30\x6b\x80\xd0\x00\x61\x03\x1c\x19\xe0\xa8\x01\x56\x06\x58\x35\xc0\x8d\x01\x6e\x1a\x60\x6c\x1f\x5b\x9e\x1f\x4e\x5a\xb1\x49\xcb\x24\x27\x5e\x30\x7a\xdc\x3b\xa8\x6d\xd8\xef\x92\x8b\xd7\x02\xb0\x02\x71\xb5\xc1\xcc\xdf\x71\x73\xde\x9c\xae\x06\x2a\xd9\x9e\x47\x55\x8c\xcb\x7d\xed\xbe\xe3\xb7\x2f\x1a\xdd\x50\x97\xe8\xc4\x5d\xac\xac\xaf\x72\xa9\xea\x5b\x75\xfb\xf6\x2b\xb1\xfa\xec\xe9\x17\x7b\x6e\x9a\xbd\x1d\x64\x50\x1f\x07\xa6\xc2\xb9\xbb\x76\xa7\xe2\xf4\xdf\x4f\xc5\xfc\xa0\x54\x54\xda\x6f\xa7\x64\xf8\xbe\x69\x5d\x85\x83\xb7\x82\xdb\xed\xf5\x8f\x8d\x4e\xcf\xd0\x48\x06\xb1\x6e\x87\xa5\x2a\xb4\xfc\xfd\x70\x16\x8c\xe5\xe4\xb9\x91\xc7\xbc\x25\xa3\x5a\xfe\xed\x9f\x7e\x25\x47\x6f\xad\xf2\xf1\x65\xba\xac\xf4\x2f\xcd\x68\x7a\xa9\x47\x1d\xe7\xca\xac\x58\x57\x36\xf3\x69\x89\x51\x60\xbe\x75\x08\x96\x4b\xf3\xa5\xff\x83\x98\x11\x55\xde\xe6\xf6\xbf\x3b\xc3\x5f\xf1\xad\xc9\x12\x66\xb1\x5f\x35\x3c\xaf\xce\xcd\xb4\x32\x7b\xcb\xcf\x78\x96\x53\x50\xd0\xf1\xbe\x12\xdb\x57\x84\xc5\xbf\xc0\xd6\xf7\x5e\x52\x35\x3b\xd6\xfb\xa7\x05\x5e\x94\xfe\x61\xc3\x68\xae\x99\x9d\xce\x67\x4a\xd0\xdf\x58\x0c\x42\x46\x5c\xc0\x63\x58\x5e\xc6\xf1\x45\xc1\x14\xc9\xe0\x27\x42\xa1\xbb\x07\x63\x70\x71\xf5\x06\x68\x3e\x45\xde\x06\x68\x7e\x5c\x51\x66\xb1\x67\x9f\x28\x7f\x07\x00\x00\xff\xff\x96\xe8\xc2\xe7\x49\x16\x00\x00"
 
 func runtimePluginsCommentCommentLuaBytes() ([]byte, error) {
        return bindataRead(
index 04e27261a11f65070433cd928abbaa9cf2092cf1..b77066b82d1f3db1f2f2a52402cd8c034db12fb8 100644 (file)
@@ -69,10 +69,38 @@ function onBufferOpen(buf)
     end
 end
 
+function isCommented(bp, lineN, commentRegex)
+    local line = bp.Buf:Line(lineN)
+    if string.match(line, commentRegex) then
+        return true
+    end
+    return false
+end
+
 function commentLine(bp, lineN)
     local line = bp.Buf:Line(lineN)
     local commentType = bp.Buf.Settings["commenttype"]
-    local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)")
+    local sel = -bp.Cursor.CurSelection
+    local curpos = -bp.Cursor.Loc
+    local index = string.find(commentType, "%%s") - 1
+    local commentedLine = commentType:gsub("%%s", trim(line))
+    bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine)
+    if bp.Cursor:HasSelection() then
+        bp.Cursor.CurSelection[1].Y = sel[1].Y
+        bp.Cursor.CurSelection[2].Y = sel[2].Y
+        bp.Cursor.CurSelection[1].X = sel[1].X
+        bp.Cursor.CurSelection[2].X = sel[2].X
+    else
+        bp.Cursor.X = curpos.X + index
+        bp.Cursor.Y = curpos.Y
+    end
+    bp.Cursor:Relocate()
+    bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
+end
+
+function uncommentLine(bp, lineN, commentRegex)
+    local line = bp.Buf:Line(lineN)
+    local commentType = bp.Buf.Settings["commenttype"]
     local sel = -bp.Cursor.CurSelection
     local curpos = -bp.Cursor.Loc
     local index = string.find(commentType, "%%s") - 1
@@ -88,46 +116,56 @@ function commentLine(bp, lineN)
             bp.Cursor.X = curpos.X - index
             bp.Cursor.Y = curpos.Y
         end
-    else
-        local commentedLine = commentType:gsub("%%s", trim(line))
-        bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine)
-        if bp.Cursor:HasSelection() then
-            bp.Cursor.CurSelection[1].Y = sel[1].Y
-            bp.Cursor.CurSelection[2].Y = sel[2].Y
-            bp.Cursor.CurSelection[1].X = sel[1].X
-            bp.Cursor.CurSelection[2].X = sel[2].X
-        else
-            bp.Cursor.X = curpos.X + index
-            bp.Cursor.Y = curpos.Y
-        end
     end
     bp.Cursor:Relocate()
     bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
 end
 
-function commentSelection(bp, startLine, endLine)
+function toggleCommentLine(bp, lineN, commentRegex)
+    if isCommented(bp, lineN, commentRegex) then
+        uncommentLine(bp, lineN, commentRegex)
+    else
+        commentLine(bp, lineN)
+    end
+end
+
+function toggleCommentSelection(bp, startLine, endLine, commentRegex)
+    local allComments = true
     for line = startLine, endLine do
-        commentLine(bp, line)
+        if not isCommented(bp, line, commentRegex) then
+            allComments = false
+        end
+    end
+
+    for line = startLine, endLine do
+        if allComments then
+            uncommentLine(bp, line, commentRegex)
+        else
+            commentLine(bp, line)
+        end
     end
 end
 
 function comment(bp, args)
+    local commentType = bp.Buf.Settings["commenttype"]
+    local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)"):gsub("%s+", "%s*")
+
     if bp.Cursor:HasSelection() then
         if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then
             local endLine = bp.Cursor.CurSelection[1].Y
             if bp.Cursor.CurSelection[1].X == 0 then
                 endLine = endLine - 1
             end
-            commentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine)
+            toggleCommentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine, commentRegex)
         else
             local endLine = bp.Cursor.CurSelection[2].Y
             if bp.Cursor.CurSelection[2].X == 0 then
                 endLine = endLine - 1
             end
-            commentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine)
+            toggleCommentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine, commentRegex)
         end
     else
-        commentLine(bp, bp.Cursor.Y)
+        toggleCommentLine(bp, bp.Cursor.Y, commentRegex)
     end
 end