Download NetBeans!

20080808 Friday August 08, 2008

Tutorial for Editor Code Completion API

When you press Ctrl-Space in the IDE, a code completion box pops up with words that you can use to complete the word under the curser. Here's a new tutorial that explains how you can create code completion functionality for your own MIME types or for one of the existing ones:

NetBeans Code Completion Tutorial

The tutorial replaces an older one. It's much better because it focuses specifically on code completion, not on extraneous stuff like web service clients and Options window integrations. In the end, you'll have a simple code completion box in HTML files, listing countries that can be selected and inserted into the editor:

The tutorial applies to NetBeans IDE 6.0 and upwards. It also contains a link to a plugin that provides the sources of the sample, in case you get stuck.

Aug 08 2008, 08:24:21 AM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/tutorial_for_editor_code_completion
Comments:

Very interesting. We have a desire in our team to get code completion for JPA queries. However, JPA is an embedded DSL inside string annotations. Does the approach described in the tutorial also apply if one wanted to "sub-tokenize" and provide completion inside a String token of a x-java MIME type?

Posted by Casper Bang on August 08, 2008 at 10:59 AM PDT #

In the tutorial, there should be "at " in:
public CompletionTask createTask(int i,at JTextComponent jTextComponent) {
}

Posted by Vincent Cantin on August 09, 2008 at 07:33 AM PDT #

After trying the cool tutorial, a few observations newbies like myself might choke on:

In "Implementing the Completion Item Class" item 5 you instruct the user to return the text member instance from the getInsertPrefix, and in the line under, you say to return null from
getInsertPrefix. The former appears to be correct.

Also, you should probably have the user add the "Utilities API" module library such as to later be able to resolve Uilities.loadImage(...).

In "Implementing the Action" item 1, you forgot to wrap doc.insertString with a try...catch (though visible later in "Implementing the Filter" item 3).

The download includes the compiled and packaged .nbm, not the actual source code.

Posted by Casper Bang on August 10, 2008 at 11:40 AM PDT #

"The download includes the compiled and packaged .nbm, not the actual source code."

Rubbish. Install it and you have a sample in the New Project wizard, as shown on the plugin's page. Go back there and look at the screenshots.

Thanks for the other comments, I'll look into them.

Posted by Geertjan on August 10, 2008 at 11:46 AM PDT #

Ohh, the .nbm is a sample extension to NetBeans. In that case, all is well and fine. :)

Posted by Casper Bang on August 10, 2008 at 12:37 PM PDT #

Exactly! Have fun with it (and read the next blog entry too if you haven't yet).

Posted by Geertjan on August 10, 2008 at 12:43 PM PDT #

Thanks for the tutorial information. It is very easy to follow.

Following the example, I've begun adding code completion to my plug-in for the NWScript scripting language. I've managed to extend the example to display one piece of text (full function declaration or constant with datatype) and insert the useable code (the constant name, or function name with datatypes or default values for parameters).

My question is - how does Netbeans display the datatype for Java functions on the right side of the completion window all nice and neat? Is there a class I can look at as an example?

Posted by Karl Nickels on August 12, 2008 at 09:16 AM PDT #

I continued on without worrying about the "neatness" of my completion window and ran into another issue.

The implementation I chose for storing my lists is apparently an unwise choice. Needless to say, I made a static class with two static string arrays, which worked fine for the initial testing with a few entries, but Java apparently does not like a string array with over 8,000 entries, much less two.

Should I be reading the entries from a data file instead? Should I choose something more dynamic like ArrayList? Or, if you could point me to the source of the relevent section of the Netbeans Java editor, I can try to follow that. I have been unsuccessful in locating it so far.

Posted by Karl Nickels on August 12, 2008 at 02:06 PM PDT #

great codes

Posted by msn avatarları on August 26, 2008 at 04:41 PM PDT #

The link to the tutorial is broken, use this one instead: http://platform.netbeans.org/tutorials/nbm-code-completion.html

Posted by Johannes on December 17, 2008 at 08:20 AM PST #

I am newbie using of Java. I tried getInsertPrefix too. But it doesnt work for me...

Posted by Dedicated on February 02, 2009 at 03:29 PM PST #

we say this kind of infos as kolbasti

Posted by kolbastı on February 04, 2009 at 10:01 AM PST #

your labor health

Posted by Tabela on February 23, 2009 at 04:59 AM PST #

Thanks

Posted by Jobs on April 07, 2009 at 08:06 PM PDT #

Thanks for your article..

Posted by Web Hosting on May 06, 2009 at 07:31 PM PDT #

thank you blogs.sun!

Posted by joomla hosting on May 13, 2009 at 01:20 AM PDT #

Thanks for your article..

Posted by sunucu on May 13, 2009 at 01:22 AM PDT #

thank you bro!...

Posted by beylikdüzü halı yıkama on July 03, 2009 at 06:21 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed