Skip to content

Commit

Permalink
refactor: clean up helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Oct 8, 2024
1 parent 59c23e5 commit c5c741d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 57 deletions.
3 changes: 1 addition & 2 deletions lua/codecompanion/actions/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local config = require("codecompanion").config
local Strategy = require("codecompanion.strategies")
local default = require("codecompanion.actions.providers.default")
local prompt_library = require("codecompanion.actions.prompt_library")
local static_actions = require("codecompanion.actions.static")

Expand Down Expand Up @@ -48,7 +47,7 @@ function Actions.items(context)
end
end

if config.prompt_library and util.count(config.prompt_library) > 0 then
if config.prompt_library and not vim.tbl_isempty(config.prompt_library) then
local prompts = prompt_library.resolve(context, config)
for _, prompt in ipairs(prompts) do
table.insert(_cached_actions, prompt)
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/adapters/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ return {

-- _github_token = { token = "ABC123", expires_at = os.time() + 3600 }
_github_token = authorize_token()
if not _github_token or util.count(_github_token) == 0 then
if not _github_token or vim.tbl_isempty(_github_token) then
log:error("Copilot Adapter: Could not authorize your GitHub Copilot token")
return false
end
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ M.chat = function(args)
end
end

local has_messages = util.count(messages) > 0
local has_messages = not vim.tbl_isempty(messages)

return require("codecompanion.strategies.chat").new({
context = context,
Expand Down
42 changes: 17 additions & 25 deletions lua/codecompanion/strategies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,25 @@ end
---@param context table
---@return table
function Strategies.evaluate_prompts(prompts, context)
local messages = {}

for _, prompt in ipairs(prompts) do
if prompt.opts and prompt.opts.contains_code and not config.opts.send_code then
goto continue
end
if prompt.condition and not prompt.condition(context) then
goto continue
end

local content
if type(prompt.content) == "function" then
content = prompt.content(context)
else
content = prompt.content
end

table.insert(messages, {
role = prompt.role,
content = content,
opts = prompt.opts or {},
})

::continue::
if type(prompts) ~= "table" or vim.tbl_isempty(prompts) then
return {}
end

return messages
return vim
.iter(prompts)
:filter(function(prompt)
return not (prompt.opts and prompt.opts.contains_code and not config.opts.send_code)
and not (prompt.condition and not prompt.condition(context))
end)
:map(function(prompt)
local content = type(prompt.content) == "function" and prompt.content(context) or prompt.content
return {
role = prompt.role or "",
content = content,
opts = prompt.opts or {},
}
end)
:totable()
end

return Strategies
12 changes: 6 additions & 6 deletions lua/codecompanion/strategies/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function Chat:render()
spacer()
end

if util.is_empty(self.messages) then
if vim.tbl_isempty(self.messages) then
log:trace("Setting the header for the chat buffer")
set_header(user_role)
spacer()
Expand Down Expand Up @@ -758,7 +758,7 @@ function Chat:submit(opts)
local bufnr = self.bufnr

local message = buf_parse_message(bufnr)
if util.count(message) == 0 then
if vim.tbl_isempty(message) then
return log:warn("No messages to submit")
end

Expand Down Expand Up @@ -989,7 +989,7 @@ end
---Determine if the chat buffer has any tools in use
---@return boolean
function Chat:has_tools()
return util.count(self.tools_in_use) > 0
return not vim.tbl_isempty(self.tools_in_use)
end

---Follow the cursor in the chat buffer
Expand Down Expand Up @@ -1208,7 +1208,7 @@ end

---Display the chat buffer's settings and messages
function Chat:debug()
if util.count(self.messages) == 0 then
if vim.tbl_isempty(self.messages) then
return
end

Expand All @@ -1232,7 +1232,7 @@ end
---Returns the last chat that was visible
---@return CodeCompanion.Chat|nil
function Chat.last_chat()
if util.is_empty(last_chat) then
if not last_chat or vim.tbl_isempty(last_chat) then
return nil
end
return last_chat
Expand All @@ -1241,7 +1241,7 @@ end
---Close the last chat buffer
---@return nil
function Chat.close_last_chat()
if last_chat and not util.is_empty(last_chat) and last_chat:is_visible() then
if last_chat and not vim.tbl_isempty(last_chat) and last_chat:is_visible() then
last_chat:hide()
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/strategies/inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ function Inline:submit()
})

-- Add the context from the chat buffer
if util.count(self.chat_context) > 0 then
if not vim.tbl_isempty(self.chat_context) then
local messages = msg_utils.pluck_messages(self.chat_context, CONSTANTS.LLM_ROLE)

if #messages > 0 then
Expand Down
21 changes: 0 additions & 21 deletions lua/codecompanion/utils/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,6 @@ M.capitalize = function(str)
return (str:gsub("^%l", string.upper))
end

---@param t table
---@return integer
M.count = function(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end

---Check if a table is empty
---@param t? table
---@return boolean
M.is_empty = function(t)
if t == nil then
return true
end

return next(t) == nil
end

---Check if a table is an array
---@param t table
---@return boolean
Expand Down

0 comments on commit c5c741d

Please sign in to comment.