From 8a228f414ca8d78b9c028177095531ca28c3586d Mon Sep 17 00:00:00 2001 From: dylan madisetti Date: Thu, 14 Mar 2024 21:44:16 -0400 Subject: [PATCH] feat(context.lua): handle nil outline and fix typos This commit includes a fix for handling nil outlines when building the outline for a buffer. It also includes a typo correction in a comment. The changes ensure that the outline building process is more robust and the code is more readable. --- lua/CopilotChat/context.lua | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lua/CopilotChat/context.lua b/lua/CopilotChat/context.lua index eaf9d95e..95314a94 100644 --- a/lua/CopilotChat/context.lua +++ b/lua/CopilotChat/context.lua @@ -80,7 +80,10 @@ function M.build_outline(bufnr) local name = vim.api.nvim_buf_get_name(bufnr) local ft = vim.bo[bufnr].filetype local lang = vim.treesitter.language.get_lang(ft) - local ok, parser = lang and pcall(vim.treesitter.get_parser, bufnr, lang) or false, nil + local ok, parser = false, nil + if lang then + ok, parser = pcall(vim.treesitter.get_parser, bufnr, lang) + end if not ok or not parser then ft = string.gsub(ft, 'react', '') ok, parser = pcall(vim.treesitter.get_parser, bufnr, ft) @@ -189,7 +192,7 @@ function M.find_for_query(copilot, opts) local outline = {} if context == 'buffers' then - -- For multiiple buffers, only make outlines + -- For multiple buffers, only make outlines outline = vim.tbl_map( function(b) return M.build_outline(b) @@ -208,10 +211,17 @@ function M.find_for_query(copilot, opts) filetype = filetype, }) else - table.insert(outline, M.build_outline(bufnr)) + local result = M.build_outline(bufnr) + if result ~= nil then + table.insert(outline, result) + end end end + outline = vim.tbl_filter(function(item) + return item ~= nil + end, outline) + if #outline == 0 then on_done({}) return