Class DefaultHighlighter

  • All Implemented Interfaces:
    Highlighter

    public class DefaultHighlighter
    extends java.lang.Object
    implements Highlighter
    Default implementation of the Highlighter interface.

    This highlighter provides basic syntax highlighting capabilities for the LineReader, including:

    • Highlighting of search matches
    • Highlighting of errors based on patterns or indices
    • Highlighting of selected regions

    The highlighting is applied using AttributedStyle to change the appearance of text in the terminal, such as colors, bold, underline, etc.

    Applications can customize the highlighting behavior by extending this class and overriding the highlight(LineReader, String) method.

    See Also:
    Highlighter, AttributedStyle, LineReader
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int errorIndex  
      protected java.util.regex.Pattern errorPattern  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void commandStyle​(LineReader reader, org.jline.utils.AttributedStringBuilder sb, boolean enable)  
      org.jline.utils.AttributedString highlight​(LineReader reader, java.lang.String buffer)
      Highlights the provided text buffer with appropriate styling.
      void setErrorIndex​(int errorIndex)
      Sets a specific character position in the buffer to be highlighted as an error.
      void setErrorPattern​(java.util.regex.Pattern errorPattern)
      Sets a regular expression pattern that identifies errors to be highlighted.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • errorPattern

        protected java.util.regex.Pattern errorPattern
      • errorIndex

        protected int errorIndex
    • Constructor Detail

      • DefaultHighlighter

        public DefaultHighlighter()
    • Method Detail

      • setErrorPattern

        public void setErrorPattern​(java.util.regex.Pattern errorPattern)
        Description copied from interface: Highlighter
        Sets a regular expression pattern that identifies errors to be highlighted.

        Text matching this pattern will typically be highlighted with error styling (often red or with a distinctive background color) to indicate problematic input.

        Specified by:
        setErrorPattern in interface Highlighter
        Parameters:
        errorPattern - A regular expression pattern that matches text to be highlighted as errors
      • setErrorIndex

        public void setErrorIndex​(int errorIndex)
        Description copied from interface: Highlighter
        Sets a specific character position in the buffer to be highlighted as an error.

        This is typically used to indicate the exact position of a syntax error or other issue in the input line. The highlighter will apply error styling at this position.

        Specified by:
        setErrorIndex in interface Highlighter
        Parameters:
        errorIndex - The character index in the buffer to be highlighted as an error
      • highlight

        public org.jline.utils.AttributedString highlight​(LineReader reader,
                                                          java.lang.String buffer)
        Description copied from interface: Highlighter
        Highlights the provided text buffer with appropriate styling.

        This method is called by the LineReader to apply syntax highlighting to the current input line. It should analyze the buffer content and return an AttributedString with appropriate styling applied based on the content's syntax, structure, or other relevant characteristics.

        Specified by:
        highlight in interface Highlighter
        Parameters:
        reader - The LineReader instance requesting highlighting
        buffer - The text buffer to be highlighted
        Returns:
        An AttributedString containing the highlighted buffer with styling applied
      • commandStyle

        protected void commandStyle​(LineReader reader,
                                    org.jline.utils.AttributedStringBuilder sb,
                                    boolean enable)