Skip to content

Commit

Permalink
refactor: better config options
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Feb 5, 2024
1 parent cfa266b commit 1b4a990
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 48 deletions.
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,27 @@ require("codecompanion").setup({
chat = { -- Options for the chat strategy
type = "float", -- float|buffer
show_settings = false, -- Show the model settings in the chat window
float = { -- Options for the floating window
show_token_count = true, -- Show the token count for the current chat?
buf_options = { -- Buffer options for the chat buffer
buflisted = false,
},
float_options = { -- Float window options if the type is "float"
border = "single",
buflisted = false,
max_height = 0,
max_width = 0,
padding = 1,
},
},
win_options = { -- Options for the chat buffer
cursorcolumn = false,
cursorline = false,
foldcolumn = "0",
linebreak = true,
list = false,
signcolumn = "no",
spell = false,
wrap = true,
win_options = { -- Window options for the chat buffer
cursorcolumn = false,
cursorline = false,
foldcolumn = "0",
linebreak = true,
list = false,
signcolumn = "no",
spell = false,
wrap = true,
},
},
},
keymaps = {
Expand All @@ -158,7 +162,6 @@ require("codecompanion").setup({
},
log_level = "ERROR", -- TRACE|DEBUG|ERROR
send_code = true, -- Send code context to the API?
show_token_count = true, -- Show the token count for the current chat?
silence_notifications = false, -- Silence notifications for actions like saving conversations?
use_default_actions = true, -- Use the default actions in the action palette?
})
Expand Down
27 changes: 15 additions & 12 deletions doc/codecompanion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,27 @@ Click to see the default configuration ~
chat = { -- Options for the chat strategy
type = "float", -- float|buffer
show_settings = false, -- Show the model settings in the chat window
float = { -- Options for the floating window
show_token_count = true, -- Show the token count for the current chat?
buf_options = { -- Buffer options for the chat buffer
buflisted = false,
},
float_options = { -- Float window options if the type is "float"
border = "single",
buflisted = false,
max_height = 0,
max_width = 0,
padding = 1,
},
},
win_options = { -- Options for the chat buffer
cursorcolumn = false,
cursorline = false,
foldcolumn = "0",
linebreak = true,
list = false,
signcolumn = "no",
spell = false,
wrap = true,
win_options = { -- Window options for the chat buffer
cursorcolumn = false,
cursorline = false,
foldcolumn = "0",
linebreak = true,
list = false,
signcolumn = "no",
spell = false,
wrap = true,
},
},
},
keymaps = {
Expand All @@ -133,7 +137,6 @@ Click to see the default configuration ~
},
log_level = "ERROR", -- TRACE|DEBUG|ERROR
send_code = true, -- Send code context to the API?
show_token_count = true, -- Show the token count for the current chat?
silence_notifications = false, -- Silence notifications for actions like saving conversations?
use_default_actions = true, -- Use the default actions in the action palette?
})
Expand Down
6 changes: 2 additions & 4 deletions lua/codecompanion/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ M.static.actions = {
callback = function()
_G.codecompanion_chats[bufnr] = nil

local winid = 0
if config.options.display.chat.type == "float" then
winid = ui.open_float(bufnr, {
display = config.options.display.chat.float,
ui.open_float(bufnr, {
display = config.options.display.chat.float_options,
})
else
vim.api.nvim_set_current_buf(bufnr)
end

ui.set_options(config.options.display.win_options, winid)
ui.buf_scroll_to_end(bufnr)
end,
})
Expand Down
28 changes: 15 additions & 13 deletions lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,26 @@ local defaults = {
chat = {
type = "float",
show_settings = false,
float = {
border = "single",
show_token_count = true,
buf_options = {
buflisted = false,
},
float_options = {
border = "single",
max_height = 0,
max_width = 0,
padding = 1,
},
},
win_options = {
cursorcolumn = false,
cursorline = false,
foldcolumn = "0",
linebreak = true,
list = false,
signcolumn = "no",
spell = false,
wrap = true,
win_options = {
cursorcolumn = false,
cursorline = false,
foldcolumn = "0",
linebreak = true,
list = false,
signcolumn = "no",
spell = false,
wrap = true,
},
},
},
keymaps = {
Expand All @@ -61,7 +64,6 @@ local defaults = {
},
log_level = "ERROR",
send_code = true,
show_token_count = true,
silence_notifications = false,
use_default_actions = true,
}
Expand Down
4 changes: 2 additions & 2 deletions lua/codecompanion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ M.toggle = function()
if action == "show" then
if config.options.display.chat.type == "float" then
ui.open_float(buf, {
display = config.options.display.chat.float,
display = config.options.display.chat.float_options,
})
else
vim.cmd("buffer " .. buf)
Expand All @@ -67,7 +67,7 @@ M.toggle = function()
vim.cmd("hide")
else
-- Show the previous buffer
vim.cmd("buffer " .. vim.fn.buf("#"))
vim.cmd("buffer " .. vim.fn.bufnr("#"))
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions lua/codecompanion/strategy/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ local function render_messages(bufnr, settings, messages, context)
end

local display_tokens = function(bufnr)
if config.options.show_token_count then
if config.options.display.chat.show_token_count then
require("codecompanion.utils.tokens").display_tokens(bufnr)
end
end
Expand Down Expand Up @@ -354,6 +354,8 @@ function Chat.new(args)
api.nvim_buf_set_option(bufnr, "syntax", "markdown")
vim.b[bufnr].codecompanion_type = "chat"

ui.set_buf_options(bufnr, config.options.display.chat.buf_options)

watch_cursor()
chat_autocmds(bufnr, args)

Expand All @@ -378,15 +380,15 @@ function Chat.new(args)

if config.options.display.chat.type == "float" then
winid = ui.open_float(bufnr, {
display = config.options.display.chat.float,
display = config.options.display.chat.float_options,
})
end

if config.options.display.chat.type == "buffer" and args.show_buffer then
api.nvim_set_current_buf(bufnr)
end

ui.set_options(config.options.display.win_options, winid)
ui.set_win_options(winid, config.options.display.chat.win_options)
vim.cmd("setlocal formatoptions-=t")
ui.buf_scroll_to_end(bufnr)

Expand Down
12 changes: 10 additions & 2 deletions lua/codecompanion/utils/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,22 @@ function M.selector(items, opts)
end)
end

---@param opts table
---@param winid number
function M.set_options(opts, winid)
---@param opts table
function M.set_win_options(winid, opts)
for k, v in pairs(opts) do
api.nvim_set_option_value(k, v, { scope = "local", win = winid })
end
end

---@param bufnr number
---@param opts table
function M.set_buf_options(bufnr, opts)
for k, v in pairs(opts) do
api.nvim_buf_set_option(bufnr, k, v)
end
end

---@param bufnr number
---@param opts table
function M.open_float(bufnr, opts)
Expand Down

0 comments on commit 1b4a990

Please sign in to comment.