AutoComplete is a code completion library for Swing JTextComponents, with enhanced functionality available for instances of RSyntaxTextArea.
AutoComplete is available under a modified BSD license.
- A completion choices list that updates as the user types
- A "documentation" companion window for displaying documentation about the currently selected completion choice
- Parameter assistance (e.g. tabbing through function/method parameters, with tool tip assistance for each argument and a possible list of valid variable completions for each)
This library is available in the
Maven Central repository (com.fifesoft:autocomplete:XXX).
SNAPSHOT builds of the in-development, unreleased version are hosted on
Sonatype.
AutoComplete is built using Gradle. It requires Java 17 to build but runs on Java 8 or later. To compile the source, run all tests, and build the distribution jar, simply run the following gradle command:
gradlew clean build --warning-mode allThe example below shows how to add code completion for simple keywords to
RSyntaxTextArea. For more examples, see the AutoCompleteDemo
submodule in this project.
importjava.awt.*;
importjavax.swing.*;
importorg.fife.ui.autocomplete.*;
importorg.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
importorg.fife.ui.rsyntaxtextarea.SyntaxConstants;
importorg.fife.ui.rtextarea.RTextScrollPane;
publicclassAutoCompleteDemoextendsJFrame {
publicAutoCompleteDemo() {
JPanelcontentPane = newJPanel(newBorderLayout());
RSyntaxTextAreatextArea = newRSyntaxTextArea(20, 60);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
textArea.setCodeFoldingEnabled(true);
contentPane.add(newRTextScrollPane(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.CompletionProviderprovider = 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.AutoCompletionac = newAutoCompletion(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. */privateCompletionProvidercreateCompletionProvider() {
// 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.DefaultCompletionProviderprovider = newDefaultCompletionProvider();
// Add completions for all Java keywords. A BasicCompletion is just// a straightforward word completion.provider.addCompletion(newBasicCompletion(provider, "abstract"));
provider.addCompletion(newBasicCompletion(provider, "assert"));
provider.addCompletion(newBasicCompletion(provider, "break"));
provider.addCompletion(newBasicCompletion(provider, "case"));
// ... etc ...provider.addCompletion(newBasicCompletion(provider, "transient"));
provider.addCompletion(newBasicCompletion(provider, "try"));
provider.addCompletion(newBasicCompletion(provider, "void"));
provider.addCompletion(newBasicCompletion(provider, "volatile"));
provider.addCompletion(newBasicCompletion(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(newShorthandCompletion(provider, "sysout",
"System.out.println(", "System.out.println("));
provider.addCompletion(newShorthandCompletion(provider, "syserr",
"System.err.println(", "System.err.println("));
returnprovider;
}
publicstaticvoidmain(String[] args) {
// Instantiate GUI on the EDT.SwingUtilities.invokeLater(() -> {
try {
Stringlaf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
} catch (Exceptione) { /* Never happens */ }
newAutoCompleteDemo().setVisible(true);
});
}
}- RSyntaxTextArea provides syntax highlighting, code folding, and many other features out-of-the-box.
- RSTALanguageSupport - Code completion for RSTA for the following languages: Java, JavaScript, HTML, PHP, JSP, Perl, C, Unix Shell. Built on both RSTA and AutoComplete.
- SpellChecker - Adds squiggle-underline spell checking to RSyntaxTextArea.
- RSTAUI - Common dialogs needed by text editing applications: Find, Replace, Go to Line, File Properties.
- Add an issue on GitHub