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 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.

 

About

Rohan Ranade's musings on anything and everything.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today