Skip navigation

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 ElementResultItem.java and AttributeResultItem.java. Since I want additional text to be added when the user wants to add a new XML tag (element), I will modify the ElementResultItem.java. 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.

 

Advertisements

One Comment

  1. Thomas: I am not yet certain about commiting this to the NB Source. I am looking into it. Also, your suggestions are valid, I should leave the tag display text just like that, and add the default values. Thanks for pointing that out!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: