Skip to content

Commit

Permalink
feat(chat): set filetype to markdown for better integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekaboo committed Dec 23, 2024
1 parent d20ca20 commit 3ab3f9c
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 34 deletions.
65 changes: 40 additions & 25 deletions lua/codecompanion/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local buf_utils = require("codecompanion.utils.buffers")
local config = require("codecompanion.config")
local context_utils = require("codecompanion.utils.context")
local log = require("codecompanion.utils.log")
Expand Down Expand Up @@ -265,26 +266,31 @@ M.setup = function(opts)
local group = "codecompanion.syntax"
api.nvim_create_augroup(group, { clear = true })
api.nvim_create_autocmd("FileType", {
pattern = "codecompanion",
pattern = "markdown",
group = group,
callback = vim.schedule_wrap(function()
vim.iter(config.strategies.chat.variables):each(function(name, var)
vim.cmd.syntax('match CodeCompanionChatVariable "#' .. name .. '"')
if var.opts and var.opts.has_params then
vim.cmd.syntax('match CodeCompanionChatVariable "#' .. name .. ':\\d\\+-\\?\\d\\+"')
end
end)
vim.iter(config.strategies.agent.tools):each(function(name, _)
vim.cmd.syntax('match CodeCompanionChatTool "@' .. name .. '"')
end)
vim
.iter(config.strategies.agent)
:filter(function(name)
return name ~= "tools"
callback = vim.schedule_wrap(function(info)
if not api.nvim_buf_is_valid(info.buf) or not buf_utils.is_codecompanion_buffer(info.buf) then
return
end
api.nvim_buf_call(info.buf, function()
vim.iter(config.strategies.chat.variables):each(function(name, var)
vim.cmd.syntax('match CodeCompanionChatVariable "#' .. name .. '"')
if var.opts and var.opts.has_params then
vim.cmd.syntax('match CodeCompanionChatVariable "#' .. name .. ':\\d\\+-\\?\\d\\+"')
end
end)
:each(function(name, _)
vim.cmd.syntax('match CodeCompanionChatAgent "@' .. name .. '"')
vim.iter(config.strategies.agent.tools):each(function(name, _)
vim.cmd.syntax('match CodeCompanionChatTool "@' .. name .. '"')
end)
vim
.iter(config.strategies.agent)
:filter(function(name)
return name ~= "tools"
end)
:each(function(name, _)
vim.cmd.syntax('match CodeCompanionChatAgent "@' .. name .. '"')
end)
end)
end),
})

Expand Down Expand Up @@ -334,26 +340,35 @@ M.setup = function(opts)
cmp.register_source("codecompanion_slash_commands", require(completion .. ".slash_commands").new(config))
cmp.register_source("codecompanion_tools", require(completion .. ".tools").new(config))
cmp.register_source("codecompanion_variables", require(completion .. ".variables").new())
cmp.setup.filetype("codecompanion", {
cmp.setup.filetype("markdown", {
enabled = true,
sources = {
sources = vim.list_extend({
{ name = "codecompanion_models" },
{ name = "codecompanion_slash_commands" },
{ name = "codecompanion_tools" },
{ name = "codecompanion_variables" },
},
}, cmp.get_config().sources),
})
end

-- Capture the last terminal buffer
_G.codecompanion_last_terminal = nil

---Check if given buffer is a terminal buffer
---and set `_G.codecompanion_last_terminal` if it is
---@param buf number? when nil, the current buffer is used
local function check_terminal(buf)
local buf = buf ~= 0 and buf or api.nvim_get_current_buf()
if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buftype == "terminal" then
_G.codecompanion_last_terminal = buf
end
end

check_terminal()
api.nvim_create_autocmd("TermEnter", {
desc = "Capture the last terminal buffer",
callback = function()
local bufnr = api.nvim_get_current_buf()
if vim.bo[bufnr].buftype == "terminal" then
_G.codecompanion_last_terminal = bufnr
end
callback = function(info)
check_terminal(info.buf)
end,
})

Expand Down
3 changes: 2 additions & 1 deletion lua/codecompanion/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local config = require("codecompanion.config")
local async = require("plenary.async")
local ts = require("codecompanion.utils.treesitter")
local ui = require("codecompanion.utils.ui")
local buf = require("codecompanion.utils.buffers")
local util = require("codecompanion.utils")

local api = vim.api
Expand Down Expand Up @@ -33,7 +34,7 @@ local function open_float(lines, opts)
local height = window.height > 1 and window.height or opts.height or 17

local bufnr = api.nvim_create_buf(false, true)
util.set_option(bufnr, "filetype", opts.filetype or "codecompanion")
buf.set_codecompanion_buffer(bufnr)
local winnr = api.nvim_open_win(bufnr, true, {
relative = opts.relative or "cursor",
border = "single",
Expand Down
3 changes: 2 additions & 1 deletion lua/codecompanion/providers/completion/blink/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local completion = require("codecompanion.completion")
local buf = require("codecompanion.utils.buffers")

local slash_commands = completion.slash_commands()
local tools = completion.tools()
Expand All @@ -15,7 +16,7 @@ function M:get_trigger_characters()
end

function M:enabled()
return vim.bo.filetype == "codecompanion"
return buf.is_codecompanion_buffer()
end

function M:get_completions(ctx, callback)
Expand Down
4 changes: 3 additions & 1 deletion lua/codecompanion/providers/completion/cmp/models.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local buf = require("codecompanion.utils.buffers")

local source = {}

---@param config table
Expand All @@ -8,7 +10,7 @@ function source.new(config)
end

function source:is_available()
return vim.bo.filetype == "codecompanion" and self.config.display.chat.show_settings
return buf.is_codecompanion_buffer() and self.config.display.chat.show_settings
end

source.get_position_encoding_kind = function()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local SlashCommands = require("codecompanion.strategies.chat.slash_commands")
local buf = require("codecompanion.utils.buffers")
local completion = require("codecompanion.completion")
local strategy = require("codecompanion.strategies")

Expand All @@ -9,7 +10,7 @@ function source.new(config)
end

function source:is_available()
return vim.bo.filetype == "codecompanion"
return buf.is_codecompanion_buffer()
end

function source:get_trigger_characters()
Expand Down
4 changes: 3 additions & 1 deletion lua/codecompanion/providers/completion/cmp/tools.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
local buf = require("codecompanion.utils.buffers")

local source = {}

function source.new(config)
return setmetatable({ config = config }, { __index = source })
end

function source:is_available()
return vim.bo.filetype == "codecompanion"
return buf.is_codecompanion_buffer()
end

source.get_position_encoding_kind = function()
Expand Down
4 changes: 3 additions & 1 deletion lua/codecompanion/providers/completion/cmp/variables.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
local buf = require("codecompanion.utils.buffers")

local source = {}

function source.new()
return setmetatable({}, { __index = source })
end

function source:is_available()
return vim.bo.filetype == "codecompanion"
return buf.is_codecompanion_buffer()
end

source.get_position_encoding_kind = function()
Expand Down
3 changes: 2 additions & 1 deletion lua/codecompanion/strategies/chat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local keymaps = require("codecompanion.utils.keymaps")
local log = require("codecompanion.utils.log")
local util = require("codecompanion.utils")
local yaml = require("codecompanion.utils.yaml")
local buf = require("codecompanion.utils.buffers")

local api = vim.api

Expand Down Expand Up @@ -227,7 +228,7 @@ function Chat.new(args)
create_buf = function()
local bufnr = api.nvim_create_buf(false, true)
api.nvim_buf_set_name(bufnr, string.format("[CodeCompanion] %d", id))
vim.bo[bufnr].filetype = "codecompanion"
buf.set_codecompanion_buffer(bufnr)

return bufnr
end,
Expand Down
21 changes: 20 additions & 1 deletion lua/codecompanion/utils/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function M.get_visible_lines()
for _, win in ipairs(wins) do
local bufnr = vim.api.nvim_win_get_buf(win)

if vim.api.nvim_get_option_value("filetype", { buf = bufnr }) ~= "codecompanion" then
if not buf.is_codecompanion_buffer(bufnr) then
local start_line = vim.api.nvim_win_call(win, function()
return vim.fn.line("w0")
end)
Expand Down Expand Up @@ -145,4 +145,23 @@ function M.format(bufnr, range)
)
end

---Check if a buffer is a codecompanion buffer
---@param bufnr integer? default to current buffer
---@return boolean?
function M.is_codecompanion_buffer(bufnr)
return vim.b[bufnr or 0].codecompanion
end

---Set filetype and buffer-local variable to mark a buffer as a codecompanion
---buffer
---
---Notice that we first set the buffer-local variable and then the filetype so
---that user can set autocmds on `FileType` event and check if the buffer is a
---codecompanion buffer by checking `vim.b.codecompanion` variable
---@param bufnr integer? default to current buffer
function M.set_codecompanion_buffer(bufnr)
vim.b[bufnr or 0].codecompanion = true
vim.bo[bufnr or 0].filetype = "markdown"
end

return M
8 changes: 7 additions & 1 deletion lua/legendary/extensions/codecompanion.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
local buf = require("codecompanion.utils.buffers")

local function to_legendary_keymap(key, keymap)
return {
key,
-- prefix makes it easier to search in legendary.nvim window
desc = string.format("CodeCompanion: %s", require("legendary.util").get_desc(keymap)),
-- keymaps are all for the chat buffer
filters = { filetype = "codecompanion" },
filters = {
function(_, context)
return buf.is_codecompanion_buffer(context.buf)
end,
},
}
end

Expand Down

0 comments on commit 3ab3f9c

Please sign in to comment.