Skip to content

fix(buffer): ignore stale extmark range errors - #214

Merged
catgoose merged 1 commit into
catgoose:masterfrom
fei6409:master
Jul 11, 2026
Merged

fix(buffer): ignore stale extmark range errors#214
catgoose merged 1 commit into
catgoose:masterfrom
fei6409:master

Conversation

@fei6409

@fei6409 fei6409 commented May 5, 2026

Copy link
Copy Markdown

When asynchronous updates or byte-offset misalignments produce stale ranges, nvim_buf_set_extmark() can raise an out-of-range error.

Ignore only stale col, end_col, and line range errors for both non-virtualtext and virtualtext highlights. Re-raise all other extmark errors.

@catgoose

Copy link
Copy Markdown
Owner

Can we do something like

  local function set_extmark_ignore_stale_range(bufnr, ns_id, line, col, opts)
    local ok, err = pcall(vim.api.nvim_buf_set_extmark, bufnr, ns_id, line, col, opts)
    if ok then
      return true
    end

    if type(err) == "string" and err:find("Invalid 'end_col': out of range", 1, true) then
      return false
    end

    error(err, 2)
  end

  -- replace your pcall with:

  set_extmark_ignore_stale_range(bufnr, ns_id, linenr, hl.range[1], {
    end_col = hl.range[2],
    hl_group = hlname,
    priority = priority,
  })

Perhaps using something like

  local stale_extmark_range_errors = {
    "Invalid 'col': out of range",
    "Invalid 'end_col': out of range",
    "Invalid 'line': out of range",
  }

  local function is_stale_extmark_range_error(err)
    if type(err) ~= "string" then
      return false
    end
    for _, msg in ipairs(stale_extmark_range_errors) do
      if err:find(msg, 1, true) then
        return true
      end
    end
    return false
  end

So we don't swallow all errors, only specific extmark ones you are concerned about.

Ignore stale column and line range errors when applying highlights.
Re-raise other nvim_buf_set_extmark failures for both non-virtualtext and
virtualtext highlights.
@fei6409 fei6409 changed the title fix(buffer): wrap non-virtualtext nvim_buf_set_extmark in pcall fix(buffer): ignore stale extmark range errors Jul 11, 2026
@fei6409

fei6409 commented Jul 11, 2026

Copy link
Copy Markdown
Author

Addressed, both non-virtualtext and virtualtext extmarks now use the helper.

@catgoose
catgoose merged commit 149fbd9 into catgoose:master Jul 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants