Fork me on GitHub
RSyntaxTextArea
A Syntax Highlighting Text Component

Overview

RSyntaxTextArea is a syntax highlighting, code folding text component for Java Swing. It extends JTextComponent so it integrates completely with the standard javax.swing.text package. It is fast and efficient, and can be used in any application that needs to edit or view source code.

RSTA supports syntax highlighting for 40+ programming languages out of the box, as well as code folding for many of them. But you're not limited to well-known languages. Highlighting and folding can be added for custom languages and plugged in with ease.

It can also be extended to provide an IDE-like experience. Parsers can be plugged in to listen for code modifications and denote errors or warnings with squiggle underlines. Sister libraries can be used to provide language-aware code completion and spell checking. Focusable tool tips can display relevant documentation for methods or objects while editing.

This library only requires Java 8 or greater, so it's ready to use in any application.

Jar downloads can be found on SourceForge, or you can clone the source from GitHub.

It is available under a modified BSD license.

Features

Syntax Highlighting
40+ languages supported out-of-the-box, with the possibility of dynamically adding support for more.
Code Folding
Language-aware code folding for many of the built-in languages Like syntax highlighting, custom code folding can be plugged in for other languages as well.
Code Completion
Easily added with the AutoComplete add-on library. Display context-aware completion choices via Ctrl+Space, or even automatically.
Parsing Support
Parsers can be written and installed to identify and squiggle-underline language-specific errors and warnings. These notices are automatically updated as the user types.
Find/Replace
Match case, whole word, regular expressions, mark all occurrences, and searching forward or backward.
File I/O
RSTA knows how to load and save both local and remote files (via FTP). Local files can have their dirty state checked.
Macros
User-defined macros allow you to record and play back complex sets of actions in an editor, to simplify common redundant tasks.
Code Templates
Quickly insert code snippets by typing a short identifier and typing Ctrl+Shift+Space. A more sophisticated version of this feature is available in the AutoComplete library.
Customizable Themes
Fonts and colors can be imported and exported via XML, to allow for easy customization. Themes for Eclipse, IDEA, light-on-dark and more are included out of the box.
Multiple Font Support
Many code editors only support editing with a single font, just different styles of that font. RSTA lets you use a different font for every token type. Even proportional fonts if you're into that.
Mark Occurrences
All occurrences of the identifier, variable, or function at the caret position will be highlighted to easily eyeball its scope/usage.
Much More
Everything you'd expect: line numbers, bracket matching, line highlighting, per-line decorator icons, unlimited undo/redo, drag and drop, clickable hyperlinks, etc.

Example Usage

import java.awt.*;
import javax.swing.*;

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

/**
 * A simple example showing how to use RSyntaxTextArea to add Java syntax
 * highlighting to a Swing application.
 */
public class TextEditorDemo extends JFrame {

   private static final long serialVersionUID = 1L;

   public TextEditorDemo() {

      JPanel cp = new JPanel(new BorderLayout());

      RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
      textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      textArea.setCodeFoldingEnabled(true);
      RTextScrollPane sp = new RTextScrollPane(textArea);
      cp.add(sp);

      setContentPane(cp);
      setTitle("Text Editor Demo");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);

   }

   public static void main(String[] args) {
      // Start all Swing applications on the EDT.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new TextEditorDemo().setVisible(true);
         }
      });
   }

}

Screenshots

Quick Links