Comment/Uncomment Lines in HTML/JSP/XML Files

I demoed a small plugin I was working on yesterday to Ludo—it lets you toggle the comment/uncomment tags in HTML, JSP and XML files, which is possible by default in Java source files (and in Properties files) but not in HTML files, JSP files, and XML files. At the time, the plugin only worked for single lines and only via a shortcut or a contextual menu item. However, Ludo told me that he uses the editor's toolbar and that he'd also like multiline selections to be possible. Well, Ludo, both items have been added and now the plugin is available in the Plugin Portal:

Make sure you read the open issues below the screenshots there, so that you're aware of them before you run into them by accident. In short, you're able to select multiple lines, as below, but if you don't, the plugin assumes that you're only interested in the current line, even if you haven't selected anything in the current line. Then you invoke the action, via Ctrl-Shift-C (just like in the Java editor and the Properties editor), or choose the contextual menu item, shown below, or Ludo's button in the editor's toolbar.

And then you find yourself with the comment tags around the selection (as illustrated below) which, when you select the selection again and then invoke the action again, are removed so that the selection is returned to its original state.

Note that the plugin installs this functionality for HTML files, XML files, and JSP files. I have tried it out on Ubuntu and a colleague tried it out on Windows XP. It worked as expected in both environments. Comments/uncomments on this plugin are more than welcome...


Cool, but this would not for facelets (.xhtml files). Which makes me wonder, is there a mime-type hierarchy in place such that one could install something in a layer file without having to know each and every mime-type?

It's very verbose to do a plugin for general-purpose XML when you have to list each an every possible xml mime-type up front (x-schema+xml, xml-dtd, x-ant+xml, x-jsf+xml, x-struts+xml,x-wsdl+xml, xml etc.)

Posted by Casper on May 22, 2008 at 12:44 AM PDT #

Strange, for me Toggle Comment is on Ctrl-Slash.

Editor-related APIs generally use the MIME Lookup API, which lets you use Editors/Something to register "something"s applicable to any editor, Editors/text/x-java/Something for the Java source editor, Editors/text/x-ant+xml/Something for the Ant editor... and Editors/text/xml/Something for any XML-based editor (text/xml or any text/\*+xml).

Posted by Jesse Glick on May 22, 2008 at 01:02 AM PDT #

Yes, all XML types (inc. XHTML) are covered. Anything that is XML (such as Spring config files) has this menu item. Jesse, I believe that Toggle Comment is invoked both by Ctrl-Slash and by Ctrl-Shift-C. I have just now uploaded the 0.7 version of this plugin, which now lets you invoke the action both ways, i.e., not just via Ctrl-Shift-C, but also via Ctrl-Slash.

Posted by Geertjan on May 22, 2008 at 01:34 AM PDT #

Ok thanks Geertjan, I can see your plugin indeed works with xhtml files. Which mime-type do you use for this (can't simply be "xml")?

Also, the plugin has issues with "undo". When "rolling back" from a comment toggle, the entire block is removed rather than only the <!-- --> markers.

Posted by Casper on May 22, 2008 at 02:02 AM PDT #

Casper, the "Undo" thing is already mentioned on the plugin's page in the Plugin Portal. Please go there and read the issues -- good to do before you use it so that you're not surprised by them. Secondly, in answer to your question, and as Jesse points out, I simply used text/xml MIME type, which covers all XML files.

Posted by Geertjan on May 22, 2008 at 02:08 AM PDT #

I can't get it to work with text/xml. Oh well, I'll just have to wait until you decide to offer the source so I can take a look. ;)

Posted by Casper on May 22, 2008 at 02:47 AM PDT #

Really? Works fine for me in text/xml. I downloaded the 0.7 version of the plugin, installed it, created an XML file, and saw everything as it should be. It should work in all XML files. Sources will be made available once I've fixed the currently listed issues.

Posted by Geertjan on May 22, 2008 at 02:54 AM PDT #

Uploaded version 0.8, which fixes the Undo problem.

Posted by Geertjan on May 22, 2008 at 03:30 AM PDT #

Hi! ;)

The plugin works quite well, but I think there is problem with the usability at this moment (which won't be so hard to fix).

IMHO when you select some text (multi-line) and when you invoke the comment action, each line of selection should be commented out separately... This would allow more possibilities for commenting/uncommenting text (I want to comment everything except a few lines...).

Furthermore, try to invoke the comment/uncomment action on multi-line selection now. It is not very comfortable to comment/uncomment some large selection...

Otherwise, great work.

Posted by joshis on May 22, 2008 at 06:15 AM PDT #

Hi joshis, thanks for the comment. You're exactly right, in fact, you perfectly describe the first issue that is listed on the plugin's page. The workaround is described there too. That's the first next thing that will be fixed. Version 0.9 of the plugin will solve it, hopefully in the next few days.

Posted by Geertjan on May 22, 2008 at 06:19 AM PDT #

Uploaded version 0.9, which fixes the multiline select problem discussed above.

Posted by Geertjan on May 22, 2008 at 08:56 AM PDT #

I don't think this should be covered by a separate optional module; it should be part of the base product. This action works in other file types and I thought it was a bug (at least a usability bug even if it can be argued that it's a missing feature) that it didn't work in XML files last time I tried. Can you work with the editor team to get this into the editor implementations?

Posted by Tor Norbye on May 23, 2008 at 04:55 AM PDT #

Geertjan: Hmm, you are right - it was there, sorry for the spam... I should read first before spamming known issues under blogs:)... I was so eager to try out the plugin that I forgot to read about its issues...

Tor Norbye: Yes, you are right, this is in progress. For more details see the nabble thread: and maybe this page: BTW: It is funny that comment/uncomment is working (I tried it and I think it works quite well) for XML, but not for (X)HTML and JSP...

Posted by joshis on May 25, 2008 at 01:12 AM PDT #

For Tor again: If you want to use Comment/Uncomment only for XML (not HTML, XHTML, which is quite funny), you can set the shortcut in Tools > Options > Keymaps, under node Other > "XML Comment" || "XML Uncomment"...

Posted by Joshis on May 25, 2008 at 01:15 AM PDT #

I'm new to netbeans. I downloaded your plugin.
How can I install it.
Could you describe in details?
thanks and regards

Posted by vu the cuong on August 28, 2008 at 10:49 PM PDT #

Hey, I want to try out your plugin but am having problems downloading it. Is there another download URL besides the one on the netbean plugins page? Or a way to fix the download from the plugins page?


Posted by Devin Whalen on September 21, 2009 at 03:56 AM PDT #

No need to try it out. That functionality is now standard in NetBeans IDE.

Posted by Geertjan Wielenga on September 21, 2009 at 08:00 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016