Skip to content

Commit 5613541

Browse files
committed
refactor: small optimizations
1 parent 4a46735 commit 5613541

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

lua/gp/init.lua

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,10 @@ M.setup = function(opts)
179179
table.sort(M._chat_agents)
180180
table.sort(M._command_agents)
181181

182-
M.refresh_state()
183-
184-
if M.config.default_command_agent then
185-
M.refresh_state({ command_agent = M.config.default_command_agent })
186-
end
187-
188-
if M.config.default_chat_agent then
189-
M.refresh_state({ chat_agent = M.config.default_chat_agent })
190-
end
182+
M.refresh_state({
183+
command_agent = M.config.default_command_agent,
184+
chat_agent = M.config.default_chat_agent,
185+
})
191186

192187
-- register user commands
193188
for hook, _ in pairs(M.hooks) do
@@ -238,14 +233,18 @@ M.setup = function(opts)
238233
Tabnew = ft_completion,
239234
}
240235

236+
local updates = {
237+
ChatHelp = function()
238+
return { show_chat_help = not M._state.show_chat_help }
239+
end,
240+
}
241+
241242
-- register default commands
242243
for cmd, _ in pairs(M.cmd) do
243244
if M.hooks[cmd] == nil then
244245
M.helpers.create_user_command(M.config.cmd_prefix .. cmd, function(params)
245246
M.logger.debug("running command: " .. cmd)
246-
if cmd ~= "ChatHelp" then
247-
M.refresh_state()
248-
end
247+
M.refresh_state((updates[cmd] or function() end)())
249248
M.cmd[cmd](params)
250249
end, completions[cmd])
251250
end
@@ -255,15 +254,20 @@ M.setup = function(opts)
255254
pattern = "*.md",
256255
callback = function(ev)
257256
M.helpers.schedule(function()
258-
local path = ev.file
259257
local buf = ev.buf
260258
local current_ft = vim.bo[buf].filetype
261-
if not M.not_chat(buf, path) and current_ft ~= "markdown.gpchat" then
262-
vim.bo[buf].filetype = "markdown.gpchat"
263-
elseif M.helpers.ends_with(path, ".gp.md") and current_ft ~= "markdown.gpmd" then
264-
vim.bo[buf].filetype = "markdown.gpmd"
259+
260+
if current_ft == "markdown.gpchat" then
261+
vim.cmd("doautocmd User GpRefresh")
262+
elseif current_ft ~= "markdown.gpmd" then
263+
local path = ev.file
264+
if M.helpers.ends_with(path, ".gp.md") then
265+
vim.bo[buf].filetype = "markdown.gpmd"
266+
elseif M.not_chat(buf, path) == nil then
267+
vim.bo[buf].filetype = "markdown.gpchat"
268+
vim.cmd("doautocmd User GpRefresh")
269+
end
265270
end
266-
vim.cmd("doautocmd User GpRefresh")
267271
end, 1)
268272
end,
269273
})
@@ -1159,9 +1163,7 @@ M.chat_header = function(buf)
11591163
vim.api.nvim_buf_set_lines(buf, 0, old_header_end + 1, false, vim.list_slice(lines, 0, header_end + 1))
11601164
end
11611165

1162-
M.cmd.ChatHelp = function()
1163-
M.refresh_state({ show_chat_help = not M._state.show_chat_help })
1164-
end
1166+
M.cmd.ChatHelp = function() end
11651167

11661168
M.cmd.ChatRespond = function(params)
11671169
if params.args == "" and vim.v.count == 0 then

0 commit comments

Comments
 (0)