From 4fdce02df7046b7f8be8b112377d8c65593f2553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3n=C3=A1n=20Carrigan?= Date: Sun, 31 Jul 2022 11:52:44 +0100 Subject: [PATCH] perf(diagnostic): avoid checking bufnr for each path Potentially helps with #78 --- lua/neotest/consumers/diagnostic.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lua/neotest/consumers/diagnostic.lua b/lua/neotest/consumers/diagnostic.lua index 9017abc8..86502aa0 100644 --- a/lua/neotest/consumers/diagnostic.lua +++ b/lua/neotest/consumers/diagnostic.lua @@ -192,9 +192,21 @@ local function init(client) client.listeners.results = function(adapter_id, results) local files = {} + local tree = client:get_position(nil, { adapter = adapter_id }) + if not tree then + return + end + --- Could be thousands of file paths in the results. To avoid checking if each one is loaded which involves a + --- vimscript call to bufnr, we create the set of buffers that are loaded and check against that. + local valid_bufs = {} + for _, bufnr in async.api.nvim_list_bufs() do + local bufpath = async.fn.fnamemodify(async.api.nvim_buf_get_name(bufnr), ":p") + valid_bufs[bufpath] = true + end + for pos_id, _ in pairs(results) do - local node = client:get_position(pos_id, { adapter = adapter_id }) - if node and node:data().type ~= "dir" then + local node = tree:get_key(pos_id) + if node and node:data().type ~= "dir" and valid_bufs[node:data().path] then files[node:data().path] = true end end