Implementing Code Completion for Java

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Implementing Code Completion for Java

Postby aallard » Thu May 03, 2012 8:59 pm

I downloaded the latest rsyntaxtextarea from Source Forge, and also downloaded autocomplete package at the same time. I followed the example you provided (http://fifesoft.com/rsyntaxtextarea/exa ... ample5.php). The editor works fine. I can load a java file and the syntax highlighting works as expected. However, code completion does not seem to be working.

For example, when I am in the editor, and type something like BorderLayout. I never get the code insight window to appear when I press the period key. Is there something else I need to do to make this work?

Thanks!
aallard
 
Posts: 2
Joined: Thu May 03, 2012 8:50 pm

Re: Implementing Code Completion for Java

Postby robert » Fri May 04, 2012 2:37 am

Hi aallard,

The AutoComplete library doesn't ship with code completion for Java out of the box. The closest you can get for free is to grab RSTALanguageSupport from Subversion (there is no jar'd version just yet). It has WIP semantic code completion for Java, among several other languages.
User avatar
robert
 
Posts: 786
Joined: Sat May 10, 2008 5:16 pm

Re: Implementing Code Completion for Java

Postby aallard » Fri May 04, 2012 2:49 am

I must have been on the right track... I did download the RSTALanguageSupport files. I couldn't find an example of how to implement the RSTALanguageSupport files, so after digging around in the code, I assume this is how to do it?:

Code: Select all
        
        RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
   textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
   textArea.setCodeFoldingEnabled(true);
   textArea.setAntiAliasingEnabled(true);
   contentPane.add(new RTextScrollPane(textArea));

   CompletionProvider provider = createCompletionProvider();

   AutoCompletion ac = new AutoCompletion(new JavaCompletionProvider());
   ac.install(textArea);


Other Info:

I'm running this on OS X (10.7) and have Java 1.6 installed, and I also use Eclipse. I saw some earlier posts in this forum about issues with the code insight not finding the src.jar files on OS X. I don't see those on my machine under the standard /System/Library/Java... path. Eclipse looks like it is using JRE_LIB, but code insight on Eclipse works just fine. Thanks for your help, I really like this editor and want to use it in a project.
aallard
 
Posts: 2
Joined: Thu May 03, 2012 8:50 pm

Re: Implementing Code Completion for Java

Postby robert » Fri May 04, 2012 3:34 am

Oh, okay, you're on the right track. See the readme file for RSTALanguageSupport. Installing should be as easy as:

Code: Select all
LanguageSupportFactory lsf = LanguageSupportFactory.get();
LanguageSupport support = lsf.getSupportFor(SyntaxConstants.SYNTAX_STYLE_JAVA);
JavaLanguageSupport jls = (JavaLanguageSupport)support;
try {
   jls.getJarManager().addCurrentJreClassFileSource();
} catch (IOException ioe) {
   ioe.printStackTrace();
}

RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
lsf.register(textArea);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);


See if that works for you. Keep in mind that this is a hand-made parser for Java, and it has lots of limitations, but it should be able to suggest completions for class fields and methods, static members, etc.

If it can't find src.jar or src.zip, it'll still give code completion choices for class members, but method parameter names and Javadoc will not be available. I'm not sure how src.jar on OS X getes delivered - do you have to install the developer tools package, or does it just come by default? It was just there on my Macbook when I checked. As for Eclipse, I'd imagine that it too needs either the source jar/zip file, or possibly a Javadoc location. Where is JRE_LIB specified? Is it pointing to a specific folder?

If any of the JDK's installed on your system include a source jar file, you can point to it instead of the "default" JDK (which is just whichever one the application is running with) by changing this line:

Code: Select all
   jls.getJarManager().addCurrentJreClassFileSource();


to this one:

Code: Select all
   File jdkHome = new File("/path/to/jdk/root/to/use");
   jls.getJarManager().addClassFileSource(LibraryInfo.getJreJarInfo(jdkHome));


Typing this out, you're probably right that this should be documented better :D The readme file is pretty clear about installing being a simple call to LanguageSupportFactory.get().register(), but leaves it up to the reader to explore the *LanguageSupport implementations for each language to see what options are available. The trouble is that the Java support requires a little extra setup - specifying the jars to be on the "build path." Also, that method being named "getJreJarInfo()" is a mistake; classes were renamed but evidently this method wasn't renamed to go along with it.
User avatar
robert
 
Posts: 786
Joined: Sat May 10, 2008 5:16 pm

Re: Implementing Code Completion for Java

Postby Guest » Fri May 04, 2012 1:29 pm

Thanks... that works great!

Is there a built-in feature to include all "local" code as well? Such as the classes that I write in a project, or included in an import statement?
Guest
 

Re: Implementing Code Completion for Java

Postby robert » Fri May 04, 2012 1:54 pm

I believe you'd want to use a DirLibraryInfo to point to your "bin" directory and "src" directory. Something like this:

Code: Select all
import org.fife.rsta.ac.java.buildpath.*;

// ...

DirLibraryInfo dirInfo = new DirLibraryInfo("bin/", new DirSourceLocation("src/"));
try {
   jls.getJarManager().addClassFileSource(dirInfo);
} catch (IOException ioe) {
   ioe.printStackTrace();
}


The API hasn't always been this verbose, but recently the need for more robustness was found, so the API has been evolving very recently (just the last week or so). Above is the latest greatest way to do it. Basically, you add DirLibraryInfos for directories containing loose class files, and JarLibraryInfos for jar files containing classes. Then there are the analogous DirSourceLocations and ZipSourceLocations to point to source files in directories vs. archives.
User avatar
robert
 
Posts: 786
Joined: Sat May 10, 2008 5:16 pm

Re: Implementing Code Completion for Java

Postby Guest » Fri May 04, 2012 3:55 pm

Thanks Again! That got it working. I really appreciate your help. :)
Guest
 


Return to Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron