X

Geertjan's Blog

  • May 15, 2011

Java Classes in Code Completion

Geertjan Wielenga
Product Manager

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.


Join the discussion

Comments ( 7 )
  • Ralph Lance Monday, May 23, 2011
    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?
  • Ralph Lance Tuesday, May 24, 2011
    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".
  • Kicek Thursday, January 12, 2012

    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.


  • Geertjan Thursday, January 12, 2012

    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.


  • Kicek Thursday, January 12, 2012

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


  • Axel F Friday, March 23, 2012

    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


  • guest Friday, June 7, 2013

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


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.