From 399763cca9001d57d2deea349cb137040a8c5894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Fl=C3=BCgge?= Date: Thu, 14 Nov 2024 22:27:59 +0100 Subject: [PATCH] fix(virtual indent): handle invalid buffer gracefully Avoid errors when refiling content with headlines and enabled virtual indentation. Cause: Indentation is also implicitly calculated during refiling. In this case the buffer could already be invalid. --- lua/orgmode/org/indent.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lua/orgmode/org/indent.lua b/lua/orgmode/org/indent.lua index 71ccab8ef..618d41bc1 100644 --- a/lua/orgmode/org/indent.lua +++ b/lua/orgmode/org/indent.lua @@ -246,6 +246,13 @@ local function indentexpr(linenr, bufnr) query = query or vim.treesitter.query.get('org', 'org_indent') bufnr = bufnr or vim.api.nvim_get_current_buf() + + -- The buffer might be invalid, which can happen, if the function is implicitly called through + -- refile operations. In this case we fallback to autoindent. + if bufnr == -1 or not vim.api.nvim_buf_is_valid(bufnr) then + return -1 + end + local indentexpr_cache = buf_indentexpr_cache[bufnr] or { prev_linenr = -1 } if indentexpr_cache.prev_linenr ~= linenr - 1 or not mode:lower():find('n') then indentexpr_cache.matches = get_matches(bufnr)