Skip to content

Commit

Permalink
Merge pull request #226 from mr-mustash/copilot_chat
Browse files Browse the repository at this point in the history
Copilot Chat
  • Loading branch information
mr-mustash authored Mar 2, 2025
2 parents a6b0359 + eea6a08 commit 2170d2e
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 127 deletions.
11 changes: 8 additions & 3 deletions tilde/.config/nvim/ftdetect/terraform.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
if exists('did_load_filetypes')
finish
endif

augroup ftdetect_terraform
autocmd!
autocmd BufRead,BufNewFile *.tf setlocal filetype=terraform
autocmd BufRead,BufNewFile *.tfvars setlocal filetype=terraform
autocmd BufRead,BufNewFile *.tfstate setlocal filetype=json
autocmd BufRead,BufNewFile *.tf,*.tfvars setfiletype terraform
autocmd BufRead,BufNewFile *.tfstate setfiletype json
autocmd BufRead,BufNewFile *.hcl setfiletype hcl
autocmd BufRead,BufNewFile terragrunt.hcl setfiletype terragrunt
augroup END
10 changes: 10 additions & 0 deletions tilde/.config/nvim/ftplugin/terragrunt.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if exists('b:did_ftplugin')
finish
endif
let b:did_ftplugin = 1

setlocal commentstring=#%s
setlocal tabstop=2
setlocal softtabstop=2
setlocal shiftwidth=2
setlocal expandtab
98 changes: 52 additions & 46 deletions tilde/.config/nvim/lua/pking/plugin/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ return {
require("cmp_git").setup()
require("luasnip.loaders.from_vscode").lazy_load()

--[[
require('copilot').setup({
suggestion = { enabled = false },
panel = { enabled = false },
filetypes = {
[""] = false,
commit = false,
["copilot-chat"] = false,
cvs = false,
git = false,
gitcommit = false,
Expand All @@ -47,30 +47,31 @@ return {
markdown = false,
md = false,
mkd = false,
netrw = false,
svn = false,
tex = false,
text = false,
yaml = false,
},
})
]]--

require("copilot_cmp").setup()

-- Setup copilot icon in lspkind
lspkind.init({
preset = 'codicons',
symbol_map = {
Copilot = "",
},
})
vim.api.nvim_set_hl(0, "CmpItemKindCopilot", {fg ="#6CC644"})

cmp.setup({
enabled = function()
-- Disable completion in copilot-chat buffers
local buftype = vim.api.nvim_buf_get_option(0, 'filetype')
if buftype == "copilot-chat" or buftype == 'TelescopePrompt' then
return false
end
return true
end,
completion = {
completeopt = "menu,menuone,preview,noselect"
},
snippet = { -- configure how nvim-cmp interacts with snippet engine
snippet = { -- Configure how nvim-cmp interacts with snippet engine
expand = function(args)
luasnip.lsp_expand(args.body)
end,
Expand All @@ -88,26 +89,34 @@ return {
{ name = "copilot", group_index = 2, max_item_count = 3 },
{ name = "nvim_lsp", group_index = 2 },
{ name = "luasnip" }, -- snippets
{ name = "path" }, -- file system paths
{ name = "path" }, -- filesystem paths
{
name = 'buffer',
option = {
get_bufnrs = function()
local bufs = {}
local bufs = {}
for _, win in ipairs(vim.api.nvim_list_wins()) do
bufs[vim.api.nvim_win_get_buf(win)] = true
end
return vim.tbl_keys(bufs)
end
return vim.tbl_keys(bufs)
end,
keyword_length = 4,
keyword_pattern = [[\k\+]],
max_item_count = 5,
}
}
}),
-- configure lspkind for vs-code like pictograms in completion menu
-- Configure lspkind for vs-code like pictograms in completion menu
formatting = {
format = lspkind.cmp_format({
mode = 'symbol',
preset = 'codicons',
mode = 'symbol_text',
maxwidth = 80,
ellipsis_char = "...",
show_labelDetails = true,
symbol_map = {
Copilot = "",
},
}),
},
sorting = {
Expand All @@ -118,24 +127,28 @@ return {
cmp.config.compare.exact,
cmp.config.compare.sort_text,
cmp.config.compare.score,
cmp.config.compare.recently_used,
cmp.config.compare.locality,
cmp.config.compare.kind,
cmp.config.compare.length,
cmp.config.compare.order,
cmp.config.compare.recently_used,
cmp.config.compare.locality,
},
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
})
-- Set up specific sources for file types
cmp.setup.filetype('gitcommit', {
sources = cmp.config.sources({
{ name = 'git' },
{ name = 'buffer' },
})
filetype = {
['copilot-chat'] = {
enabled = false
},
gitcommit = {
sources = cmp.config.sources({
{ name = 'git' },
{ name = 'buffer' },
})
}
}
})

-- This doesn't work while using the cmd window. See below
Expand All @@ -159,28 +172,21 @@ return {
-- Hack to get cmp to work in command window https://github.com/hrsh7th/cmp-cmdline/pull/61#issuecomment-1243380455
vim.api.nvim_create_augroup('CMP', { clear = true })
vim.api.nvim_create_autocmd('CmdwinEnter', {
group = 'CMP',
pattern = '*',
callback = function()
cmp.setup.buffer({
sources = cmp.config.sources({
{ name = 'cmdline' },
{ name = 'path' },
{
name = 'buffer',
option = {
get_bufnrs = function()
local bufs = {}
for _, win in ipairs(vim.api.nvim_list_wins()) do
bufs[vim.api.nvim_win_get_buf(win)] = true
end
return vim.tbl_keys(bufs)
end
}
},
group = 'CMP',
pattern = '*',
callback = function()
cmp.setup.buffer({
sources = cmp.config.sources({
{ name = 'cmdline' },
{ name = 'path' },
{ name = 'buffer' }
}),
mapping = cmp.mapping.preset.cmdline(), -- Add this line
completion = {
completeopt = 'menu,menuone,noselect'
}
})
})
end
end
})
end
}
189 changes: 189 additions & 0 deletions tilde/.config/nvim/lua/pking/plugin/copilot-chat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
local system_prompt = [[
You are an advanced AI assistant designed to assist users with code-related tasks. Your primary goal is to provide accurate, efficient, and well-structured responses while ensuring clarity and best practices. Follow these guidelines when responding:
1) Be Concise Yet Informative
* Provide clear and direct answers focused on code-related topics
* Include brief explanations before presenting code only when necessary
* For non-coding questions, provide a brief response and suggest relevant documentation or resources
2) Stay Within Scope
* Focus exclusively on coding and development-related topics
* If a question is outside the scope of programming or development, politely redirect to appropriate resources
* Only engage in broader discussions when explicitly requested by the user
3) Code Formatting and Style
* Always enclose code within properly formatted markdown blocks with language specification
* Follow language-specific conventions and best practices
* Include error handling where appropriate
4) Adapt to User Intent
* If a request is unclear or ambiguous, ask for clarification instead of making assumptions
* When multiple solutions exist, present the most relevant approach first
* Adjust the technical depth of responses based on the user's apparent expertise level
5) Response Structure
* Start with the most direct solution to the problem
* If multiple approaches exist, present the recommended solution first
* Include documentation references where relevant
Remember: Keep responses focused on code and development while being responsive to the user's needs and expertise level.]]

local function get_encoded_path(filepath)
return filepath:gsub("^%s*(.-)%s*$", "%1") -- trim
:gsub("^~", "~=+") -- encode home dir
:gsub("/", "=+") -- encode path separators
end

return {
{
"CopilotC-Nvim/CopilotChat.nvim",
dependencies = {
{ "zbirenbaum/copilot.lua" },
{ "nvim-lua/plenary.nvim", branch = "master" },
},
keys = {
-- Automatically load the chat history
{ "<leader>co", function()
-- Get the current buffer's file path
local current_file = vim.fn.expand('%:p')
if current_file and current_file ~= "" then
-- Convert the path to vim-style encoding (replace / with =+)
local encoded_path = get_encoded_path(current_file)

-- Load the chat history for this file
vim.cmd(string.format("CopilotChatLoad %s", vim.fn.fnameescape(encoded_path)))
end

-- Create a new chat buffer
vim.cmd("CopilotChatOpen")
end, desc = "Open Copilot Chat" },
{ "<leader>cot", " <cmd>CopilotChatToggle<cr>", desc = "Toggle Chat" },
{ "<leader>coe", "<cmd>CopilotChatExplain<cr>", desc = "Explain Code" },
{ "<leader>cof", "<cmd>CopilotChatFix<cr>", desc = "Fix Code" },
{ "<leader>cor", "<cmd>CopilotChatReset<cr>", desc = "Reset Chat" },
},
cmd = "CopilotChat",
build = "make tiktoken",
opts = {
system_prompt = system_prompt,
model = "claude-3.5-sonnet",
context = "buffers",
sticky = {
'#buffers',
},
mappings = {
submit_prompt = {
normal = '<C-CR>',
insert = '<C-CR>'
},
show_diff = {
full_diff = true
},
close = {
normal = 'q',
insert = '<C-c>'
},
toggle = {
normal = '<leader>cot',
},
},
code_actions = {
enabled = true,
show_action_hints = true
},
question_header = " User ",
answer_header = " Copilot ",
error_header = "󰬅 Error ",
auto_follow_cursor = true,
clear_chat_on_close = false,
debug = false,
highlight_selection = false,
window = {
border = 'rounded',
width = 0.4,
relative = 'editor',
},
show_help = true,
},
config = function(_, opts)
require("CopilotChat").setup(opts)

-- Start in insert mode
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "copilot-chat",
callback = function()
if vim.bo.filetype == "copilot-chat" then
vim.opt_local.number = false
vim.opt_local.relativenumber = false
vim.opt_local.signcolumn = "no"

-- Make sure this runs after any other FileType autocmds
vim.schedule(function()
vim.cmd("startinsert!")
end)
end
end,
})

vim.api.nvim_set_hl(0, "CopilotChatHeader", { fg = "#268BD2" })
vim.api.nvim_set_hl(0, "CopilotChatSeparator", { fg = "#6C71C4" })


local skip_filetypes = {
[""] = true,
["copilot-chat"] = true,
["TelescopePrompt"] = true,
["help"] = true,
["qf"] = true,
["terminal"] = true,
["prompt"] = true,
["netrw"] = true,
["commit"] = true,
["cvs"] = true,
["git"] = true,
["gitcommit"] = true,
["gitrebase"] = true,
["gitsendmail"] = true,
["hgcommit"] = true,
["man"] = true,
["markdown"] = true,
["md"] = true,
["mkd"] = true,
["svn"] = true,
["tex"] = true,
["text"] = true,
["yaml"] = true,
}

local skip_buftypes = {
["terminal"] = true,
["prompt"] = true,
["quickfix"] = true,
["nofile"] = true,
["help"] = true,
["acwrite"] = true,
["nowrite"] = true,
}

-- Automatically save the chat history for each file
local chat_augroup = vim.api.nvim_create_augroup("CopilotChatSave", { clear = true })
vim.api.nvim_create_autocmd({"BufWritePre", "VimLeavePre"}, {
group = chat_augroup,
callback = function()
local bufnr = vim.api.nvim_get_current_buf()
-- Skip special buffers and copilot-chat itself
if skip_buftypes[vim.bo[bufnr].buftype] or skip_filetypes[vim.bo[bufnr].filetype] then
return
end

local current_file = vim.fn.fnamemodify(vim.fn.bufname(bufnr), ':p')
if current_file and current_file ~= "" then
local encoded_path = get_encoded_path(current_file)
vim.cmd(string.format("CopilotChatSave %s", vim.fn.fnameescape(encoded_path)))
end
end,
})

end,
},
}
Loading

0 comments on commit 2170d2e

Please sign in to comment.