Sublime Forum

How can I comment codes including HTML comments?

#1

For example, this is my code:

I want to comment everything, but when I hit comment short cut, it does this:

Please tell me the fix; thanks!

0 Likes

#2

The comment keyboard shortcut will comment out all of the selected text if there is any or just the current line if there’s not.

So to comment out more lines, either comment them out one at a time or select them first and then hit the key shortcut.

You can use Selection > Expand selection to tag or the keyboard shortcut bound to it to possibly help you select what you want easier than doing it manually.

0 Likes

#3

It does not work when there is HTMl comment within selected codes, such as:
code

code

How can I comment whole section ignoring in between comments? Thanks!

1 Like

#4

Ahh I see; yes, in this case no matter what you select, if any of it is a comment, the command will toggle that comment. If you press the comment key twice in that case, the second one will comment all of the selected text, but that destructively removes the original comments.

I don’t think there’s a way around that except to modify the internal command that’s doing the commenting or create one of your own, though.

1 Like

#5

It’s not that hard to do.
Highlight the area that wish to comment including the previously commented code.
In many cases you will get the previously commented area/code double commented.
In the instance that the included comment is toggled, then you just recomment it.

I bit of a pain but most of the time, I see double comments.

Hope that helps => cmd+/

1 Like

#6

In many cases you will get the previously commented area/code double commented.

You won’t get the previously commented code uncommented.
It will just double comment the entire block selected.

<!--<!-- ......................................
..............................................
...........<!-- .......a previously commented section ...................--> ............
......................................................................
..................................................
..................................................--> -->

This thing is a nuisance.
In HTML you’d often have sections of the page that are commented out pending final decisions on the page layout, e.g. webp images.
If you have to fix layout issues with the element this means having to uncomment individual commented blocks within it before commenting out the entire block.

1 Like

#9

I wasn’t able to post this text normally the text area recognizes some parts as some sort of code so I had to take picture of explanation of issue, so i have an issue about the issue hot damn :crazy_face:

0 Likes

#10

If you are commenting a code block containing comments then it will only comment till the contained closing comment tag.

Neither will uncommenting the block work properly.

I don’t think there is any workaround for this.

Except do not comment except where absolutely necessary.

0 Likes

#11

> You gotta be joking? Wouldnt a fix be simple? In the form of detect first <!-- if <!-- and --> detected (consider it as nested) ignore until ---> detected? A simple count of even amounts of <!-- and --> to find end of comment block?

0 Likes

#12

Okay.

You draft the Python plugin to implement this.

I am happy to submit test code blocks.

0 Likes

#13

HTML doesn’t support nested comments, see https://stackoverflow.com/questions/18145886/html-nested-comments.

So even if you would write a plugin to change the comment toggle behavior in HTML files, it wouldn’t make much sense to do that and when you open the file in a browser, it would not work as expected.

The reason why the second <!-- is highlighted as an error, is because it is not allowed within the commented text: https://html.spec.whatwg.org/multipage/syntax.html#syntax-comments

0 Likes

#14

Or do you suggest to uncomment all of the HTML comments when you select a block and use “toggle comment”? I think that could be possible with a few adjustments to the built-in Default/comment.py file. It would probably need a special case for the HTML syntax, because in general you would need the information whether the language does or does not support nested block comments.

0 Likes