From 8495d9623809eef5cf40b45ea3a5c2b0bd4077b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Jos=C3=A9=20Solano?= Date: Thu, 24 Apr 2025 10:25:27 -0700 Subject: [PATCH] fix(lsp): ensure `bufstate` when calling `vim.lsp.document_color.is_enabled` --- runtime/lua/vim/lsp/document_color.lua | 8 +++++++- test/functional/plugin/lsp/document_color_spec.lua | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/runtime/lua/vim/lsp/document_color.lua b/runtime/lua/vim/lsp/document_color.lua index b03055c233..c306acf696 100644 --- a/runtime/lua/vim/lsp/document_color.lua +++ b/runtime/lua/vim/lsp/document_color.lua @@ -238,7 +238,13 @@ end function M.is_enabled(bufnr) vim.validate('bufnr', bufnr, 'number', true) - return bufstates[vim._resolve_bufnr(bufnr)].enabled + bufnr = vim._resolve_bufnr(bufnr) + + if not bufstates[bufnr] then + reset_bufstate(bufnr, false) + end + + return bufstates[bufnr].enabled end --- Enables document highlighting from the given language client in the given buffer. diff --git a/test/functional/plugin/lsp/document_color_spec.lua b/test/functional/plugin/lsp/document_color_spec.lua index b08757de9a..a938d367d4 100644 --- a/test/functional/plugin/lsp/document_color_spec.lua +++ b/test/functional/plugin/lsp/document_color_spec.lua @@ -159,6 +159,15 @@ body { end) ) end) + + it('does not error when called on a new unattached buffer', function() + eq( + false, + exec_lua(function() + return vim.lsp.document_color.is_enabled(vim.api.nvim_create_buf(false, true)) + end) + ) + end) end) describe('enable()', function()