Java Classes in Code Completion

I've been looking at the Code Completion Tutorial and updating it to 7.0. While doing so, I got some tips from Tomas Zezula, the NetBeans Java editor guru, on how to change that tutorial so that Java classes could be shown in the code completion box:

That's pretty cool, right. A code completion box inside an HTML file, showing Java classes, and narrowing as you type. To achieve the above, start with the NetBeans Code Completion Tutorial and then fill the CompletionResultSet (in the CompletionProvider class) like this:

FileObject fo = getFO(doc);
ClassPath bootCp = ClassPath.getClassPath(fo, ClassPath.BOOT);
ClassPath compileCp = ClassPath.getClassPath(fo, ClassPath.COMPILE);
ClassPath sourcePath = ClassPath.getClassPath(fo, ClassPath.SOURCE);
final ClasspathInfo info = ClasspathInfo.create(bootCp, compileCp, sourcePath);
final Set<ElementHandle<TypeElement>> result = info.getClassIndex().getDeclaredTypes("", ClassIndex.NameKind.PREFIX, EnumSet.of(ClassIndex.SearchScope.SOURCE, ClassIndex.SearchScope.DEPENDENCIES));
for (ElementHandle<TypeElement> te : result) {
    String binaryName = te.getBinaryName();
    if (!binaryName.equals("") && binaryName.startsWith(filter)) {
        resultSet.addItem(new CountriesCompletionItem(te.getBinaryName(), startOffset, caretOffset));

The helper method:

private FileObject getFO(Document doc) {
    Object sdp = doc.getProperty(Document.StreamDescriptionProperty);
    if (sdp instanceof FileObject) {
        return (FileObject) sdp;
    if (sdp instanceof DataObject) {
        DataObject dobj = (DataObject) sdp;
        return dobj.getPrimaryFile();
    return null;

For this code to work, you need (some of these are for other parts of the tutorial) dependencies on the Classpath APIs, Datasystems API, Editor Code Completion, File System API, Java Source, Lookup API, MIME Lookup API, Nodes API, Project API, and Utilities API.

Note that the HTML file needs to be in a "src" folder in a Java project. The "src" folder is a Java compilation unit and hence you can get the classpath as shown above from the HTML file simply because it is in the compilation unit.


Your timing is perfect, Geertjan! I'm trying to integrate a language into NB 7.0 that can also intermingle with java objects, so I need the code completion for Java. I trust your snippet works, but the basic tutorial is never giving me a real caret position, just -1 all the time, so DefaultAction breaks at "doc.insertString(caretOffset, text, null);". This is also the case as I tried to orginally implement code completion for the language I'm trying to integrate. I can't find anything about this on the net. What am I missing?

Posted by Ralph Lance on May 23, 2011 at 06:13 AM PDT #

Diligence paid off. Having slept on it, I grabbed your RCP book and combined code from the chapter on CC, the tutorial, and your snippet here to add the CC for Java. In the book you handle the dot and caret offsets slightly different than the tutorial, but at least it doesn't exception out. The tutorial never worked for me "as-is".

Posted by Ralph Lance on May 24, 2011 at 02:21 AM PDT #

I went through the tutorials and i still can't get it to work.
I open up my netbeans module,
load a sample project containing an html file in the src location,
press ctrl+space,
method query(CompletionResultSet completionResultSet, Document document, int caretOffset) is invoked however the document variable is null which results in an exception.
Any ideas of what could be wrong?
Hope for a reply.

Posted by Kicek on January 11, 2012 at 09:51 PM PST #

No idea. Remove the 'build' folder of your project. Then put your project in a ZIP file and send it to me at geertjan dot wielenga at oracle dot com and I will investigate.

Posted by Geertjan on January 11, 2012 at 11:48 PM PST #

I needed that tutorial for different functionality, I alredy found the solution and i won't bother you with that, thanks for reply.

Posted by Kicek on January 12, 2012 at 01:47 AM PST #

Thank you for this tip, Geertjan. I want to extend code completion for javascript files. But if I do it as described by the tutorial, I replace the code completion always. What I want is very simple: I want to register some prototypes in my script engine and want to get the completion help for it. Is there any way to extend it?

Oh - and I want to high light the prototypes too. Is there also a simple way?

Thanks in advance, Axel

Posted by Axel F on March 23, 2012 at 08:23 AM PDT #

Hello Guys,I'm making an editor to support my own language keywords.
I want to implement the Code Completion interface to my editor.
i'm using java on NetBeans IDE.
if someone got a knowledge about this please guide me.
Its urgent and important .
Thanks ...

Posted by guest on June 07, 2013 at 12:35 PM 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