Skip to content

Commit

Permalink
refactor: optimize file list processing order
Browse files Browse the repository at this point in the history
Move file list chunking before content processing to ensure consistent
ordering of operations and improve code flow. Also increase TOP_RELATED
constant from 20 to 25 for better related files detection.

The chunking logic now runs first, followed by optional content processing,
which makes the code more maintainable and logical in its execution order.
  • Loading branch information
deathbeam committed Dec 4, 2024
1 parent 55c1991 commit caf387f
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions lua/CopilotChat/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ local OFF_SIDE_RULE_LANGUAGES = {
}

local TOP_SYMBOLS = 100
local TOP_RELATED = 20
local TOP_RELATED = 25
local MULTI_FILE_THRESHOLD = 5

--- Compute the cosine similarity between two vectors
Expand Down Expand Up @@ -324,6 +324,24 @@ function M.files(winnr, with_content)

local out = {}

-- Create file list in chunks
local chunk_size = 100
for i = 1, #files, chunk_size do
local chunk = {}
for j = i, math.min(i + chunk_size - 1, #files) do
table.insert(chunk, files[j])
end

local chunk_number = math.floor(i / chunk_size)
local chunk_name = chunk_number == 0 and 'file_map' or 'file_map' .. tostring(chunk_number)

table.insert(out, {
content = table.concat(chunk, '\n'),
filename = chunk_name,
filetype = 'text',
})
end

-- Read all files if we want content as well
if with_content then
async.util.scheduler()
Expand All @@ -346,26 +364,6 @@ function M.files(winnr, with_content)
table.insert(out, file_data)
end
end

return out
end

-- Create file list in chunks
local chunk_size = 100
for i = 1, #files, chunk_size do
local chunk = {}
for j = i, math.min(i + chunk_size - 1, #files) do
table.insert(chunk, files[j])
end

local chunk_number = math.floor(i / chunk_size)
local chunk_name = chunk_number == 0 and 'file_map' or 'file_map' .. tostring(chunk_number)

table.insert(out, {
content = table.concat(chunk, '\n'),
filename = chunk_name,
filetype = 'text',
})
end

return out
Expand Down

0 comments on commit caf387f

Please sign in to comment.