Skip to content

Commit cde4092

Browse files
committed
feat: add utils.empty() function for checking empty values
This commit introduces a new utility function `empty()` that checks if a value is empty. This function handles nil values, empty tables, and whitespace-only strings. The function is used to improve empty value checking across the codebase, replacing direct nil checks with the more robust empty() function. Changes: - Add utils.empty() function - Update client.lua to use empty() for response validation - Update chat.lua to use empty() for content checks
1 parent 3126d9d commit cde4092

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

lua/CopilotChat/client.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ function Client:ask(prompt, opts)
659659
end
660660

661661
if is_stream then
662-
if not full_response then
662+
if utils.empty(full_response) then
663663
for _, line in ipairs(vim.split(response.body, '\n')) do
664664
parse_stream_line(line)
665665
end
@@ -668,7 +668,7 @@ function Client:ask(prompt, opts)
668668
parse_line(response.body)
669669
end
670670

671-
if not full_response then
671+
if utils.empty(full_response) then
672672
error('Failed to get response: empty response')
673673
return
674674
end

lua/CopilotChat/ui/chat.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ function Chat:parse_history()
345345

346346
local history = {}
347347
for _, section in ipairs(self.sections) do
348-
if section.content then
348+
if not utils.empty(section.content) then
349349
if section.answer then
350350
table.insert(history, {
351351
content = section.content,

lua/CopilotChat/utils.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,4 +459,23 @@ function M.key_to_info(name, key, surround)
459459
return out
460460
end
461461

462+
--- Check if a value is empty
463+
---@param v any The value
464+
---@return boolean
465+
function M.empty(v)
466+
if not v then
467+
return true
468+
end
469+
470+
if type(v) == 'table' then
471+
return vim.tbl_isempty(v)
472+
end
473+
474+
if type(v) == 'string' then
475+
return vim.trim(v) == ''
476+
end
477+
478+
return false
479+
end
480+
462481
return M

0 commit comments

Comments
 (0)