Fast Syntax Highlighting (F-Sy-H)

repository·master·Indexed 23 days ago

https://github.com/zdharma-continuum/fast-syntax-highlighting

A high-performance syntax highlighting plugin for Zsh featuring accurate command-specific highlighting via 'chromas'. It supports various installation methods (Zinit, Antigen, Zgen, Oh-My-Zsh), theme management through the `fast-theme` command, and custom chroma function implementation for detailed program-specific colorization.

Tokens
2.6K
Snippets
8
Records
16
Agent score
83%

What's inside fast-syntax-highlighting

  1. Use overlays to modify existing themes

    master

    If you only want to change a few styles instead of creating a complete new theme, use an overlay. An overlay is an INI file containing only the styles you wish to overwrite.

    If you name your file overlay.ini, it is treated specially by the system. Styles defined in the overlay will overwrite the corresponding styles in the main theme.

    ; overlay.ini
    [base]
    commandseparator = yellow,bold
    comment          = 17
    
    [command-point]
    function       = green
    command        = 180
  2. How chroma functions work in F-Sy-H

    master

    A chroma function (or chroma) is a plugin-function used to create detailed, custom highlighting for specific programs (e.g., git, grep, awk).

    The Lifecycle

    1. The main highlighting engine (the "big loop") processes the command line token by token.
    2. When the engine encounters a command with an associated chroma, it enters a "chroma" state.
    3. The engine calls the associated chroma function for the current token.
    4. The chroma function manages the highlighting for that token and ensures the "chroma" state persists for subsequent tokens.
    5. The chroma function controls whether the engine continues to use the chroma or falls back to standard highlighting by returning specific values:
      • Return 0: Requests that the engine performs no further processing for this token (the chroma handles everything).
      • Return 1: Requests that the current token be passed back to the engine's "big loop" for standard highlighting.
      • Return 2: (Implicitly used in examples) Signals the end of the chroma-enabled command sequence (e.g., when a new command starts).
  3. Implement a custom chroma function

    master

    To create custom highlighting for a specific command, implement a Zsh function that accepts the four arguments described in the Chroma-Function Arguments Reference and uses the reply array to add highlight entries.

    Key Implementation Details:

    • State Management: Use the FAST_HIGHLIGHT hash array to store state (like counters) between calls without polluting global variables. Always reset your state when $1 (the first call) is detected.
    • Highlighting via reply: To apply color, add an entry to the reply array in the format: "start_index end_index style_name". The style name should be derived from ${FAST_THEME_NAME}style_name.
    • Advancing the Buffer: After processing a token, you must advance the internal pointers by setting _start_pos=$_end_pos and updating this_word to the next_word.
    • Passing to Big Loop: If a token (like a quoted string) should be handled by the standard highlighter, return 1.
    # Example chroma function implementation
    # Colorizes first two arguments as 'builtin' and subsequent as 'globbing'
    
    (( next_word = 2 | 8192 ))
    
    local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
    local __style
    integer __idx1 __idx2
    
    (( __first_call )) && {
        FAST_HIGHLIGHT[chroma-example-counter]=0
        __style=${FAST_THEME_NAME}command
    } || {
        [[ "$__arg_type" = 3 ]] && return 2
    
        if [[ "$__wrd" = -* ]]; then
            [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
                                    __style=${FAST_THEME_NAME}single-hyphen-option
        else
            (( FAST_HIGHLIGHT[chroma-example-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-example-counter] ))
    
            if (( FAST_HIGHLIGHT[chroma-example-counter] <= 2 )); then
                if [[ "$__wrd" = \"* ]]; then
                    return 1
                else
                    __style=${FAST_THEME_NAME}builtin
                fi
            else
                __style=${FAST_THEME_NAME}globbing
            fi
        fi
    }
    
    [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
    
    (( this_word = next_word ))
    _start_pos=$_end_pos
    
    return 0
  4. Set a custom working directory via FAST_WORK_DIR

    master

    You can set the FAST_WORK_DIR environment variable before loading the plugin. This directory is used to store processed theme files (converted from INI to Zsh format). This is useful for maintaining per-user theme setups when the plugin is installed system-wide.

    The path can use ~ or the standard short-hands (XDG:, LOCAL:, OPT:, etc.). For example, FAST_WORK_DIR=XDG will resolve to $HOME/.config/fsh by default.

  5. Install Fast Syntax Highlighting with Zinit Turbo Mode

    master

    To speed up Zsh startup, you can use Zinit's Turbo mode to load the plugin in the background after the first prompt appears. This example also includes zsh-completions and zsh-autosuggestions.

    zinit wait lucid for \
     atinit"ZINIT[COMPINIT_OPTS]=-C; zicompinit; zicdreplay" \
        zdharma-continuum/fast-syntax-highlighting \
     blockf \
        zsh-users/zsh-completions \
     atload"!_zsh_autosuggest_start" \
        zsh-users/zsh-autosuggestions
  6. Manage and create themes with fast-theme

    master

    Themes in F-Sy-H are INI files where each key represents a style. You can use the fast-theme tool to list, select, or create themes.

    • List shipped themes: fast-theme -l
    • Select a theme: fast-theme <theme-name> or fast-theme <path-to-ini>
    • Create a theme from a template: Use fast-theme --copy-shipped-theme {theme-name} to get a template based on an existing shipped theme.

    Note: The .ini extension is optional when specifying a path.