From 3ac76977bca5ce204fea856d69d52895f67332e5 Mon Sep 17 00:00:00 2001 From: Kira Kawai <66677201+ras0q@users.noreply.github.com> Date: Fri, 2 Jan 2026 15:49:28 +0900 Subject: [PATCH] fix(diagnostic): check for extmark in get_logical_pos #37127 Problem: The function get_logical_pos did not account for the possibility that a diagnostic might not have an associated extmark, leading to potential errors or incorrect behavior. Solution: Add a check for diagnostic._extmark_id and return the logical positions directly if it does not exist. --- runtime/lua/vim/diagnostic.lua | 4 ++++ test/functional/lua/diagnostic_spec.lua | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 002d9a63cb..37089f972e 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -655,6 +655,10 @@ local sign_highlight_map = make_highlight_map('Sign') --- @return integer end_col --- @return boolean valid local function get_logical_pos(diagnostic) + if not diagnostic._extmark_id then + return diagnostic.lnum, diagnostic.col, diagnostic.end_lnum, diagnostic.end_col, true + end + local ns = M.get_namespace(diagnostic.namespace) local extmark = api.nvim_buf_get_extmark_by_id( diagnostic.bufnr, diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index e37471273f..a73fc3b0fe 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -608,6 +608,21 @@ describe('vim.diagnostic', function() vim.diagnostic.hide(_G.diagnostic_ns) end) end) + + it('handles diagnostics without extmark_id', function() + exec_lua(function() + vim.diagnostic.config({ virtual_text = true }) + + vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, { + _G.make_error('Error message', 0, 0, 0, 5), + }) + + local diags = vim.diagnostic.get(_G.diagnostic_bufnr, { namespace = _G.diagnostic_ns }) + diags[1]._extmark_id = nil + + vim.diagnostic.show(_G.diagnostic_ns, _G.diagnostic_bufnr, diags) + end) + end) end) describe('enable() and disable()', function()