RSyntaxTextArea Documentation

repository·master·Indexed 22 days ago

https://github.com/bobbylight/rsyntaxtextarea

A customizable, syntax-highlighting text component for Java Swing applications. It supports over 50 programming languages and features code folding, search/replace, and a ParserManager for handling delayed parsing and visual error feedback. The library is available via Maven (com.fifesoft:rsyntaxtextarea) and offers add-on libraries for autocomplete, spell checking, and UI dialogs.

Tokens
2K
Snippets
3
Records
16
Agent score
78%

What's inside RSyntaxTextArea

  1. Explore RSyntaxTextArea demo applications

    master

    The RSyntaxTextAreaDemo repository contains several sample applications to help you understand the capabilities of RSyntaxTextArea:

    • RSyntaxTextAreaDemoApp: Demonstrates syntax highlighting, code folding, and the ability to toggle between built-in editor themes and other basic configuration options.
    • FindAndReplaceDemo: Provides a basic implementation of search and replace functionality within the editor.
    • SyntaxSchemeDemo: Demonstrates how to customize the editor's appearance by modifying fonts and colors.
  2. Explore RSyntaxTextArea add-on libraries

    master

    For advanced editor features, you can use these specialized add-on libraries:

    • AutoComplete: Adds code completion to RSyntaxTextArea or any JTextComponent.
    • RSTALanguageSupport: Provides code completion specifically for Java, JavaScript, HTML, PHP, JSP, Perl, C, and Unix Shell.
    • SpellChecker: Adds squiggle-underline spell checking.
    • RSTAUI: Provides common text editor dialogs like Find, Replace, Go to Line, and File Properties.
  3. Install RSyntaxTextArea via Maven

    master

    RSyntaxTextArea is available in the Maven Central repository. You can include it in your project using the following coordinates:

    com.fifesoft:rsyntaxtextarea:XXX (replace XXX with the desired version).

    SNAPSHOT builds for unreleased versions are available on Sonatype.

  4. Manage syntax parsers with ParserManager

    master

    The ParserManager class is responsible for managing one or more Parser objects for an RSyntaxTextArea. It handles the lifecycle of parsing, including scheduling parsing tasks after a delay following document modifications to avoid excessive CPU usage. It also manages the visual representation of parsing results, such as error underlines (squiggles) and tooltips.

    Key behaviors:

    • Delayed Parsing: By default, parsing is triggered after a delay (defaulting to 1250ms) following the last document edit.
    • Visual Feedback: It automatically adds highlights to the editor for ParserNotice objects returned by parsers.
    • Tooltips: It enables tooltips to display error descriptions or other information when a user hovers over a highlighted error area.
  5. Use StyledTextTransferable for clipboard and DnD operations

    master

    StyledTextTransferable is a class used during copy/paste and Drag-and-Drop (DnD) operations to represent styled text. It extends the functionality of java.awt.datatransfer.StringSelection by allowing the transferred data to be retrieved in multiple formats: HTML, RTF, or plain text.

    When implementing custom clipboard or DnD logic, you can use this object to ensure that styled text is available to other applications in formats they can understand. It supports the following DataFlavor types:

    • DataFlavor.fragmentHtmlFlavor (HTML format)
    • new DataFlavor("text/rtf", "RTF") (RTF format, returned as a ByteArrayInputStream)
    • DataFlavor.stringFlavor (Plain text format)

    Note: This class is package-private within org.fife.ui.rsyntaxtextarea and is intended to be used by the library's internal components for handling text transfers.

  6. Use RSyntaxTextArea in a Swing application

    master

    RSyntaxTextArea is a subclass of JTextComponent, making it easy to integrate into any Java Swing application. To use it, instantiate RSyntaxTextArea, set its syntax style using SyntaxConstants, and wrap it in an RTextScrollPane to provide scrollbars.

    Key steps:

    1. Instantiate RSyntaxTextArea(rows, columns).
    2. Call setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_...) to enable specific language highlighting.
    3. Call setCodeFoldingEnabled(true) if code folding is desired.
    4. Wrap the component in an RTextScrollPane for proper scrolling behavior.
    import javax.swing.*;
    import java.awt.BorderLayout;
    
    import org.fife.ui.rtextarea.*;
    import org.fife.ui.rsyntaxtextarea.*;
    
    public class TextEditorDemo extends JFrame {
    
        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 TextEditorDemo().setVisible(true));
        }
    
    }
  7. Check Java version compatibility for RSyntaxTextArea

    master

    RSyntaxTextArea version requirements vary by release. Ensure your runtime environment (JRE) and build environment (JDK) meet the following requirements:

    RSTA VersionRequired to build (JDK)Required to run (JRE)
    4.x2511
    3.x178
    2.6.x66