Skip to content

Commit 20ef31e

Browse files
authored
Fix: broken context extraction for large files. (#168)
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.
1 parent dc163c8 commit 20ef31e

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

lua/CopilotChat/context.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ function M.build_outline(bufnr)
8080
local name = vim.api.nvim_buf_get_name(bufnr)
8181
local ft = vim.bo[bufnr].filetype
8282
local lang = vim.treesitter.language.get_lang(ft)
83-
local ok, parser = lang and pcall(vim.treesitter.get_parser, bufnr, lang) or false, nil
83+
local ok, parser = false, nil
84+
if lang then
85+
ok, parser = pcall(vim.treesitter.get_parser, bufnr, lang)
86+
end
8487
if not ok or not parser then
8588
ft = string.gsub(ft, 'react', '')
8689
ok, parser = pcall(vim.treesitter.get_parser, bufnr, ft)
@@ -189,7 +192,7 @@ function M.find_for_query(copilot, opts)
189192

190193
local outline = {}
191194
if context == 'buffers' then
192-
-- For multiiple buffers, only make outlines
195+
-- For multiple buffers, only make outlines
193196
outline = vim.tbl_map(
194197
function(b)
195198
return M.build_outline(b)
@@ -208,10 +211,17 @@ function M.find_for_query(copilot, opts)
208211
filetype = filetype,
209212
})
210213
else
211-
table.insert(outline, M.build_outline(bufnr))
214+
local result = M.build_outline(bufnr)
215+
if result ~= nil then
216+
table.insert(outline, result)
217+
end
212218
end
213219
end
214220

221+
outline = vim.tbl_filter(function(item)
222+
return item ~= nil
223+
end, outline)
224+
215225
if #outline == 0 then
216226
on_done({})
217227
return

0 commit comments

Comments
 (0)