Sublime Forum

"Toggle comment" doesn't work correctly in HTML

#1

Take the following HTML:

    <script src="console.js"></script>
    <script src="lodash.js"></script>
    <script src="application.js"></script>

Let’s say you want to disable one of the scripts temporarily. So you click your cursor on (or next to) the relevant script tag, and press CMD+/ or CTRL+/.

Expected result:

    <script src="console.js"></script>
    <script src="lodash.js"></script>
    <!-- <script src="application.js"></script> -->

Actual result:

    <script src="scripts/safe-console.js"></script>
    <script src="scripts/lodash.js"></script>
    // <script src="scripts/application.js"></script>

(This happens no matter where on the line you put your cursor – even if you put it right at the beginning of the line, before the indentation.)

The same bug happens with tags, except it uses CSS comment syntax instead, e.g. body{color:blue} becomes /body{color:blue}/.

This bug has existed for as long as I have been using Sublime Text, and it still exists in ST3 (build 3012).

This bug is really annoying, because toggling individual script tags on and off is probably the most common use case for HTML comments when working on large JavaScript codebases (for me, anyway), and HTML’s comment syntax is particularly fiddly to type manually.

0 Likes

#2

This isn’t a fix for commenting but it might help in the short term to quickly disable a script. If you double tap CTRL + / it should do the following:

Original:

<script type="text/javascript"></script>

After the first CTRL + /

// <script type="text/javascript"></script>

After the second CTRL + /

<!-- // <script type="text/javascript"></script> -->

From this point on it seems like it just toggles the HTML comment.

0 Likes

#3

Another work around is to select the line, CTR/CMD + X to cut it, CTR/CMD + / to insert comment tags, and CTR/CMD + V to paste it.

0 Likes

#4

cut and paste works as a workaround but this should truly be fixed. It’s annoying.

0 Likes

#5

ST has trouble with nested languages. The comment applied to a particular line is based on the scopes at the cursor.

For example, given the following (with no leading spaces)

<script src="scripts/application.js"></script>

Placing the cursor at the beginning of the string has the following scope.

text.html.basic meta.tag.any.html punctuation.definition.tag.begin.html 

Now, insert a leading space then again place the cursor at the beginning of the line. It now has the following scope

text.html.basic source.js.embedded.html 

I’m not saying it’s not bothersome, but I think it’s worthwhile to understand why it “doesn’t work correctly”. You could try playing with the tmLanguage files to get things to work a little better, but that’s a pretty significant undertaking I think.

0 Likes

#6

i modified portable version and fixed bug
in HTML.tmLanguage on line 286 i replaced this
(?:^\s+)?(<)((?i:script))\b(?!^>]/>)
with this
(?:^\s+)?(<)((?i:script))\b(?!^>]
/>)(?!.*</script>)

this version also have bugs on commenting but its much better then original

link on modified portable version (win32) https://docs.google.com/file/d/0B1gBa3_5Y2mBa2tDX0g4N25IdkE

0 Likes