Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Highlight enum

We need to define the Highlight enum, which goes in types. We start with few values and will expand it later:

types.zig
///////////////////////////////////////////////////////////////////////////////
//
//                              Highlight
//
///////////////////////////////////////////////////////////////////////////////

/// All available highlight types.
pub const Highlight = enum(u8) {
    /// The normal highlight
    normal = 0,

    /// Incremental search highlight
    incsearch,

    /// Highlight for error messages
    err,
};

An array for highlight

Our Row type must have an additional array, which will have the same length of the render array, and which will contain the Highlight for each element of the render array:

Row.zig
/// Array with the highlight of the rendered row
hl: []t.Highlight,

We'll initialize this array in Row.init():

        .hl = &.{},

deinitialize it in Row.deinit():

    allocator.free(row.hl);

and will fill it in a new function:

Editor.zig
///////////////////////////////////////////////////////////////////////////////
//
//                              Syntax highlighting
//
///////////////////////////////////////////////////////////////////////////////

/// Update highlight for a row.
fn updateHighlight(e: *Editor, ix: usize) !void {
    const row = e.rowAt(ix);

    // reset the row highlight to normal
    row.hl = try e.alc.realloc(row.hl, row.render.len);
    @memset(row.hl, .normal);
}

Later we'll do syntax highlighting here. This function is called at the end of updateRow(), because every time the rendered row is updated, its highlight must be too.

Editor.zig: updateRow()
    try e.updateHighlight(ix);
}