Download as Eclipse project
Back to RSyntaxTextArea Home
Description
AutoComplete
is an add-on library for RSyntaxTextArea that allows you to add
code completion to your project. The library is designed to allow for
complex, semantic-aware code completion, but this example will show a simple
usage of AutoComplete to provide completion of keywords.
While this tutorial focuses on using AutoComplete to add code completion to
RSyntaxTextArea, the library can actually be used to add auto-completion to
any Swing JTextComponent. For an example of using AutoComplete in
a JTextField, check out the
RegexAwareComboBox class in the
RText project. It adds auto-completion of
regular expression constructs to the JTextField editor of a JComboBox.
AutoComplete Library Overview
At the heart of the AutoComplete
library is the
CompletionProvider
interface. A CompletionProvider is what examines the text at the text
component's caret position, and returns a list of possible completions. Generally
speaking, CompletionProviders can be shared amongst multiple text components.
An
AutoCompletion
is the middle-man between a CompletionProvider and a text component.
It actually listens for events in the text component's Document and
manages any options associated with the auto-completion (the popup trigger key,
whether to display a documentation window along with completion choices, etc.).
Unlike CompletionProviders, instances of AutoCompletion cannot be shared
amongst multiple text components.
A possible completion is represented by an implementation of the Completion interface. It not only knows what text it wants to insert into the document, but also an (optional) summary/documentation of what's being inserted (which can be used in a tool tip-style window alongside the completion popup), and an (again optional) tool tip string to display when the user mouses over the completion in the text component.
One special type of Completion is the
ParameterizedCompletion.
A ParameterizedCompletion represents a completion choice that can take
parameters (such as a function or method). When you use this completion type, an
AutoCompletion instance can be configured to provide parameter
assistance when that completion is inserted. An example of a
ParameterizedCompletion is the
FunctionCompletion
class. Parameter assistance will be demo'd in a future example.
The Example Sourceimport java.awt.*; import javax.swing.*; import org.fife.ui.autocomplete.*; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rsyntaxtextarea.SyntaxConstants; import org.fife.ui.rtextarea.RTextScrollPane; /** * An example showing a simple usage of the AutoComplete package to add support * for auto-completion of keywords and other things.<p> * * This example uses RSyntaxTextArea 2.0.1.<p> * * Project Home: http://fifesoft.com/rsyntaxtextarea<br> * Downloads: https://sourceforge.net/projects/rsyntaxtextarea */ public class AutoCompleteDemo extends JFrame { private static final long serialVersionUID = 1L; public AutoCompleteDemo() { JPanel contentPane = new JPanel(new BorderLayout()); RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60); textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); textArea.setCodeFoldingEnabled(true); textArea.setAntiAliasingEnabled(true); contentPane.add(new RTextScrollPane(textArea)); // A CompletionProvider is what knows of all possible completions, and // analyzes the contents of the text area at the caret position to // determine what completion choices should be presented. Most // instances of CompletionProvider (such as DefaultCompletionProvider) // are designed so that they can be shared among multiple text // components. CompletionProvider provider = createCompletionProvider(); // An AutoCompletion acts as a "middle-man" between a text component // and a CompletionProvider. It manages any options associated with // the auto-completion (the popup trigger key, whether to display a // documentation window along with completion choices, etc.). Unlike // CompletionProviders, instances of AutoCompletion cannot be shared // among multiple text components. AutoCompletion ac = new AutoCompletion(provider); ac.install(textArea); setContentPane(contentPane); setTitle("AutoComplete Demo"); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); } /** * Create a simple provider that adds some Java-related completions. * * @return The completion provider. */ private CompletionProvider createCompletionProvider() { // A DefaultCompletionProvider is the simplest concrete implementation // of CompletionProvider. This provider has no understanding of // language semantics. It simply checks the text entered up to the // caret position for a match against known completions. This is all // that is needed in the majority of cases. DefaultCompletionProvider provider = new DefaultCompletionProvider(); // Add completions for all Java keywords. A BasicCompletion is just // a straightforward word completion. provider.addCompletion(new BasicCompletion(provider, "abstract")); provider.addCompletion(new BasicCompletion(provider, "assert")); provider.addCompletion(new BasicCompletion(provider, "break")); provider.addCompletion(new BasicCompletion(provider, "case")); provider.addCompletion(new BasicCompletion(provider, "catch")); provider.addCompletion(new BasicCompletion(provider, "class")); provider.addCompletion(new BasicCompletion(provider, "const")); provider.addCompletion(new BasicCompletion(provider, "continue")); provider.addCompletion(new BasicCompletion(provider, "default")); provider.addCompletion(new BasicCompletion(provider, "do")); provider.addCompletion(new BasicCompletion(provider, "else")); provider.addCompletion(new BasicCompletion(provider, "enum")); provider.addCompletion(new BasicCompletion(provider, "extends")); provider.addCompletion(new BasicCompletion(provider, "final")); provider.addCompletion(new BasicCompletion(provider, "finally")); provider.addCompletion(new BasicCompletion(provider, "for")); provider.addCompletion(new BasicCompletion(provider, "goto")); provider.addCompletion(new BasicCompletion(provider, "if")); provider.addCompletion(new BasicCompletion(provider, "implements")); provider.addCompletion(new BasicCompletion(provider, "import")); provider.addCompletion(new BasicCompletion(provider, "instanceof")); provider.addCompletion(new BasicCompletion(provider, "interface")); provider.addCompletion(new BasicCompletion(provider, "native")); provider.addCompletion(new BasicCompletion(provider, "new")); provider.addCompletion(new BasicCompletion(provider, "package")); provider.addCompletion(new BasicCompletion(provider, "private")); provider.addCompletion(new BasicCompletion(provider, "protected")); provider.addCompletion(new BasicCompletion(provider, "public")); provider.addCompletion(new BasicCompletion(provider, "return")); provider.addCompletion(new BasicCompletion(provider, "static")); provider.addCompletion(new BasicCompletion(provider, "strictfp")); provider.addCompletion(new BasicCompletion(provider, "super")); provider.addCompletion(new BasicCompletion(provider, "switch")); provider.addCompletion(new BasicCompletion(provider, "synchronized")); provider.addCompletion(new BasicCompletion(provider, "this")); provider.addCompletion(new BasicCompletion(provider, "throw")); provider.addCompletion(new BasicCompletion(provider, "throws")); provider.addCompletion(new BasicCompletion(provider, "transient")); provider.addCompletion(new BasicCompletion(provider, "try")); provider.addCompletion(new BasicCompletion(provider, "void")); provider.addCompletion(new BasicCompletion(provider, "volatile")); provider.addCompletion(new BasicCompletion(provider, "while")); // Add a couple of "shorthand" completions. These completions don't // require the input text to be the same thing as the replacement text. provider.addCompletion(new ShorthandCompletion(provider, "sysout", "System.out.println(", "System.out.println(")); provider.addCompletion(new ShorthandCompletion(provider, "syserr", "System.err.println(", "System.err.println(")); return provider; } public static void main(String[] args) { // Instantiate GUI on the EDT. SwingUtilities.invokeLater(new Runnable() { public void run() { try { String laf = UIManager.getSystemLookAndFeelClassName(); UIManager.setLookAndFeel(laf); } catch (Exception e) { } new AutoCompleteDemo().setVisible(true); } }); } }Save this file as AutoCompleteDemo.java.
Compiling the ExampleWindows: javac -cp <path-to-jar>\rsyntaxtextarea.jar;<path-to-jar>\autocomplete.jar AutoCompleteDemo.java Uinx: javac -cp <path-to-jar>/rsyntaxtextarea.jar:<path-to-jar>/autocomplete.jar AutoCompleteDemo.javawhere <path-to-jar> is the path to the rsyntaxtextarea.jar and autocomplete.jar files. This should yield no errors or warnings, and on completion there should see a file named AutoCompleteDemo.class in your current directory.
Running the ExampleWindows: java -cp <path-to-jar>\rsyntaxtextarea.jar;<path-to-jar>\autocomplete.jar;. AutoCompleteDemo Unix: java -cp <path-to-jar>/rsyntaxtextarea.jar:<path-to-jar>/autocomplete.jar:. AutoCompleteDemo
A window should pop up, containing a text editor. As you type, the text should be syntax highlighted as Java code. Typing Ctrl+Space will cause a list of code completion choices to popup up. Select one with the arrow keys or mouse, then press Enter or Tab to insert it.