Monday Apr 07, 2008

Some nifty XML editing add-ons

It was a long weekend here in India, and that provided me some time to add some shortcuts which will help users who author XML documents using NetBeans. I wrote a plugin which will add some context sensitive actions to the XML editor. I have just managed to add one action (Delete current attribute), but more are on the way. Actions planned are:

  • Jump to parent tag
  • Jump to next sibling tag
  • Jump to previous sibling tag
  • Jump to end tag (if at start tag)
  • Jump to start tag (if at end tag)
  • Jump to next attribute in a tag
  • Jump to previous attribute in a tag
  • Delete current tag (alongwith children)
  • Delete current attribute (alongwith it's value)
  • Delete value
  • Expand a short tag <a/> into the longer form <a></a>
  • Condense an empty tag <a></a> into it's shorter form <a/>

I am planning to upload the module on the Plugin portal soon. For now, here's a screen shot:

Before the action was selected:

After the action was selected (notice the name attribute is gone):

Monday Feb 25, 2008

XML and NetBeans

NetBeans never boasted of a good editor as compared to IntelliJ IDEA and Eclipse, but things took a complete turn when NetBeans 6.0 came out. And now that NetBeans has conquered Java developers with the shining new editor, NetBeans is going gung-ho for Groovy, Scala, Erlang and what not. Though I totally agree to Groovy and Scala becoming first class citizens in the NetBeans world, I personally feel that NetBeans needs to do something about nicely editing a 10 year old thing - XML.

Why, you might ask. Well, whatever web frameworks I have dealt with, Struts, Struts2, Spring MVC, JSF, require reasonable amount of configuration in XML. Hell, even many non web applications seem to use Spring and it's XML configuration files. Though the deal is now seeming in favor of POJOs with annotations, I don't think developer authored XML is dead, by any means.

In a series of upcoming posts, I plan to compare the XML editing landscape in Eclipse, IntelliJ IDEA and NetBeans. IMO, NetBeans has the weakest XML editor story, but I plan to make a wishlist out of this, hopefully someone will come along and take XML editing in NetBeans to new heights.

Tuesday Dec 11, 2007

NetBeans Module: XML Breadcrumbs

I was trying out IntelliJ IDEA 7.0 and came across the XML breadcrumbs feature. The breadcrumbs bar tracks the current position of the cursor in the XML tag hierarchy.


It would be so nice to have this in NetBeans. So here it is (sources to be uploaded soon):

Friday Nov 30, 2007

A smarter XML completion in NetBeans - Part II

After my last adventure with XML code completion, I am ready for one more nifty add-on. This one is straight from Eclipse XML editor. When you request completion for an attribute in an XML tag, you might want to add the default value (by default). Such a thing is not provided by NetBeans. However, as with yesterday's hack, this one is mighty easy to do.

If you have followed yesterday's post, you know where the sources of the schema aware code completion module reside. When we type CTRL+SPACE for invoking code completion for attributes of a tag, we get a list of org.netbeans.modules.xml.schema.completion.AttributeResultItem objects, each one representing a possible attribute that can belong at the position where completion was invoked. I make the following modifications to the code.

First, I need to find if there is a default value for the particular attribute. So, I add the following code to the constructor of AttributeResultItem:


The getDefaultValue() method is as given below: 


 One final thing. The getReplacementText() method needs to be modified.


That's it. Now, when you invoke code completion for attributes (after you have compiled your changes and re-run the development IDE), you can see the attributes with their default values.



More XML completion fun to come in the next post! 

Wednesday Nov 28, 2007

A smarter XML completion in NetBeans - Part I

If you are used to heavily editing XML files in NetBeans, you would have wished for the code completion assistance to be a bit smarter. As an example, let us take the case of editing a Spring Framework Beans Configuration file. I want to add an <alias> tag to the XML configuration file. This tag has two required attributes: name and alias. It all starts by invoking the code completion assistance by typing in '<'. The following dialog shows up:


When I select the alias tag and hit Enter, the completion assistance just puts in the tag name:

To insert the attributes I have to again press CTRL + SPACE:


If the code completion were a bit smarter, it would insert the required attributes as well. I mean, why will anyone use a tag without using the required attributes? Anyway, NetBeans doesn't offer this feature, so I decided to do some hacking.

I downloaded the NetBeans sources and opened up the module project for the Schema based code completion in NetBeans (NB_SRC_ROOT/xml/schema/completion)



The items displayed in the completion assistance popup are implemented in and Since I want additional text to be added when the user wants to add a new XML tag (element), I will modify the I modified the constructors of the class to add the additional text:


I needed to override a method to return the new text to the completion infrastructure.


That's it. Now I built NetBeans, and run the same scenario for code completion, and here's what I see:


Notice the required attributes appearing alongwith the end tag (another nifty thing which the current XML code completion misses). Now pressing Enter gives me the tag complete with it's required attributes and end tag.


The hack still needs some work, but you can see the point. If there is something you want in NetBeans, just dig through the sources and modify it and contribute it back to the community. This in my mind is the single most advantage of using an Open Source tool.



Rohan Ranade's musings on anything and everything.


« June 2016