generated from LazyVim/starter
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcopilot-chat-v2.lua
213 lines (206 loc) · 7.02 KB
/
copilot-chat-v2.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
local IS_DEV = false
local prompts = {
-- Code related prompts
Explain = "Please explain how the following code works.",
Review = "Please review the following code and provide suggestions for improvement.",
Tests = "Please explain how the selected code works, then generate unit tests for it.",
Refactor = "Please refactor the following code to improve its clarity and readability.",
FixCode = "Please fix the following code to make it work as intended.",
FixError = "Please explain the error in the following text and provide a solution.",
BetterNamings = "Please provide better names for the following variables and functions.",
Documentation = "Please provide documentation for the following code.",
SwaggerApiDocs = "Please provide documentation for the following API using Swagger.",
SwaggerJsDocs = "Please write JSDoc for the following API using Swagger.",
-- Text related prompts
Summarize = "Please summarize the following text.",
Spelling = "Please correct any grammar and spelling errors in the following text.",
Wording = "Please improve the grammar and wording of the following text.",
Concise = "Please rewrite the following text to make it more concise.",
}
return {
{ import = "plugins.extras.copilot-vim" }, -- Or use { import = "lazyvim.plugins.extras.coding.copilot" },
{
"folke/which-key.nvim",
optional = true,
opts = {
spec = {
{ "<leader>a", group = "ai" },
{ "<leader>gm", group = "Copilot Chat" },
},
},
},
{
"MeanderingProgrammer/render-markdown.nvim",
optional = true,
opts = {
file_types = { "markdown", "copilot-chat" },
},
ft = { "markdown", "copilot-chat" },
},
{
dir = IS_DEV and "~/Projects/research/CopilotChat.nvim" or nil,
"CopilotC-Nvim/CopilotChat.nvim",
branch = "main",
-- version = "v3.3.0", -- Use a specific version to prevent breaking changes
dependencies = {
{ "nvim-telescope/telescope.nvim" }, -- Use telescope for help actions
{ "nvim-lua/plenary.nvim" },
},
opts = {
question_header = "## User ",
answer_header = "## Copilot ",
error_header = "## Error ",
prompts = prompts,
-- model = "claude-3.7-sonnet",
mappings = {
-- Use tab for completion
complete = {
detail = "Use @<Tab> or /<Tab> for options.",
insert = "<Tab>",
},
-- Close the chat
close = {
normal = "q",
insert = "<C-c>",
},
-- Reset the chat buffer
reset = {
normal = "<C-x>",
insert = "<C-x>",
},
-- Submit the prompt to Copilot
submit_prompt = {
normal = "<CR>",
insert = "<C-CR>",
},
-- Accept the diff
accept_diff = {
normal = "<C-y>",
insert = "<C-y>",
},
-- Show help
show_help = {
normal = "g?",
},
},
},
config = function(_, opts)
local chat = require("CopilotChat")
chat.setup(opts)
local select = require("CopilotChat.select")
vim.api.nvim_create_user_command("CopilotChatVisual", function(args)
chat.ask(args.args, { selection = select.visual })
end, { nargs = "*", range = true })
-- Inline chat with Copilot
vim.api.nvim_create_user_command("CopilotChatInline", function(args)
chat.ask(args.args, {
selection = select.visual,
window = {
layout = "float",
relative = "cursor",
width = 1,
height = 0.4,
row = 1,
},
})
end, { nargs = "*", range = true })
-- Restore CopilotChatBuffer
vim.api.nvim_create_user_command("CopilotChatBuffer", function(args)
chat.ask(args.args, { selection = select.buffer })
end, { nargs = "*", range = true })
-- Custom buffer for CopilotChat
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "copilot-*",
callback = function()
vim.opt_local.relativenumber = true
vim.opt_local.number = true
-- Get current filetype and set it to markdown if the current filetype is copilot-chat
local ft = vim.bo.filetype
if ft == "copilot-chat" then
vim.bo.filetype = "markdown"
end
end,
})
end,
event = "VeryLazy",
keys = {
-- Show prompts actions with telescope
{
"<leader>ap",
function()
require("CopilotChat").select_prompt({
context = {
"buffers",
},
})
end,
desc = "CopilotChat - Prompt actions",
},
{
"<leader>ap",
function()
require("CopilotChat").select_prompt()
end,
mode = "x",
desc = "CopilotChat - Prompt actions",
},
-- Code related commands
{ "<leader>ae", "<cmd>CopilotChatExplain<cr>", desc = "CopilotChat - Explain code" },
{ "<leader>at", "<cmd>CopilotChatTests<cr>", desc = "CopilotChat - Generate tests" },
{ "<leader>ar", "<cmd>CopilotChatReview<cr>", desc = "CopilotChat - Review code" },
{ "<leader>aR", "<cmd>CopilotChatRefactor<cr>", desc = "CopilotChat - Refactor code" },
{ "<leader>an", "<cmd>CopilotChatBetterNamings<cr>", desc = "CopilotChat - Better Naming" },
-- Chat with Copilot in visual mode
{
"<leader>av",
":CopilotChatVisual",
mode = "x",
desc = "CopilotChat - Open in vertical split",
},
{
"<leader>ax",
":CopilotChatInline",
mode = "x",
desc = "CopilotChat - Inline chat",
},
-- Custom input for CopilotChat
{
"<leader>ai",
function()
local input = vim.fn.input("Ask Copilot: ")
if input ~= "" then
vim.cmd("CopilotChat " .. input)
end
end,
desc = "CopilotChat - Ask input",
},
-- Generate commit message based on the git diff
{
"<leader>am",
"<cmd>CopilotChatCommit<cr>",
desc = "CopilotChat - Generate commit message for all changes",
},
-- Quick chat with Copilot
{
"<leader>aq",
function()
local input = vim.fn.input("Quick Chat: ")
if input ~= "" then
vim.cmd("CopilotChatBuffer " .. input)
end
end,
desc = "CopilotChat - Quick chat",
},
-- Fix the issue with diagnostic
{ "<leader>af", "<cmd>CopilotChatFixError<cr>", desc = "CopilotChat - Fix Diagnostic" },
-- Clear buffer and chat history
{ "<leader>al", "<cmd>CopilotChatReset<cr>", desc = "CopilotChat - Clear buffer and chat history" },
-- Toggle Copilot Chat Vsplit
{ "<leader>av", "<cmd>CopilotChatToggle<cr>", desc = "CopilotChat - Toggle" },
-- Copilot Chat Models
{ "<leader>a?", "<cmd>CopilotChatModels<cr>", desc = "CopilotChat - Select Models" },
-- Copilot Chat Agents
{ "<leader>aa", "<cmd>CopilotChatAgents<cr>", desc = "CopilotChat - Select Agents" },
},
},
}