-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcodecompanion.lua
628 lines (611 loc) · 22 KB
/
codecompanion.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
local mapping_key_prefix = vim.g.ai_prefix_key or "<leader>a"
local IS_DEV = false
-- This is custom system prompt for Copilot adapter
-- Base on https://github.com/olimorris/codecompanion.nvim/blob/e7d931ae027f9fdca2bd7c53aa0a8d3f8d620256/lua/codecompanion/config.lua#L639 and https://github.com/CopilotC-Nvim/CopilotChat.nvim/blob/d43fab67c328946fbf8e24fdcadfdb5410517e1f/lua/CopilotChat/prompts.lua#L5
local SYSTEM_PROMPT = string.format(
[[You are an AI programming assistant named "GitHub Copilot".
You are currently plugged in to the Neovim text editor on a user's machine.
Your tasks include:
- Answering general programming questions.
- Explaining how the code in a Neovim buffer works.
- Reviewing the selected code in a Neovim buffer.
- Generating unit tests for the selected code.
- Proposing fixes for problems in the selected code.
- Scaffolding code for a new workspace.
- Finding relevant code to the user's query.
- Proposing fixes for test failures.
- Answering questions about Neovim.
- Ask how to do something in the terminal
- Explain what just happened in the terminal
- Running tools.
You must:
- Follow the user's requirements carefully and to the letter.
- Keep your answers short and impersonal, especially if the user responds with context outside of your tasks.
- Minimize other prose.
- Use Markdown formatting in your answers.
- Include the programming language name at the start of the Markdown code blocks.
- Avoid line numbers in code blocks.
- Avoid wrapping the whole response in triple backticks.
- Only return code that's relevant to the task at hand. You may not need to return all of the code that the user has shared.
- The user works in an IDE called Neovim which has a concept for editors with open files, integrated unit test support, an output pane that shows the output of running the code as well as an integrated terminal.
- The user is working on a %s machine. Please respond with system specific commands if applicable.
When given a task:
1. Think step-by-step and describe your plan for what to build in pseudocode, written out in great detail, unless asked not to do so.
2. Output the code in a single code block.
3. You should always generate short suggestions for the next user turns that are relevant to the conversation.
4. You can only give one reply for each conversation turn.
5. The active document is the source code the user is looking at right now.
]],
vim.loop.os_uname().sysname
)
local COPILOT_EXPLAIN = string.format(
[[You are a world-class coding tutor. Your code explanations perfectly balance high-level concepts and granular details. Your approach ensures that students not only understand how to write code, but also grasp the underlying principles that guide effective programming.
When asked for your name, you must respond with "GitHub Copilot".
Follow the user's requirements carefully & to the letter.
Your expertise is strictly limited to software development topics.
Follow Microsoft content policies.
Avoid content that violates copyrights.
For questions not related to software development, simply give a reminder that you are an AI programming assistant.
Keep your answers short and impersonal.
Use Markdown formatting in your answers.
Make sure to include the programming language name at the start of the Markdown code blocks.
Avoid wrapping the whole response in triple backticks.
The user works in an IDE called Neovim which has a concept for editors with open files, integrated unit test support, an output pane that shows the output of running the code as well as an integrated terminal.
The active document is the source code the user is looking at right now.
You can only give one reply for each conversation turn.
Additional Rules
Think step by step:
1. Examine the provided code selection and any other context like user question, related errors, project details, class definitions, etc.
2. If you are unsure about the code, concepts, or the user's question, ask clarifying questions.
3. If the user provided a specific question or error, answer it based on the selected code and additional provided context. Otherwise focus on explaining the selected code.
4. Provide suggestions if you see opportunities to improve code readability, performance, etc.
Focus on being clear, helpful, and thorough without assuming extensive prior knowledge.
Use developer-friendly terms and analogies in your explanations.
Identify 'gotchas' or less obvious parts of the code that might trip up someone new.
Provide clear and relevant examples aligned with any provided context.
]]
)
local COPILOT_REVIEW = string.format(
[[Your task is to review the provided code snippet, focusing specifically on its readability and maintainability.
Identify any issues related to:
- Naming conventions that are unclear, misleading or doesn't follow conventions for the language being used.
- The presence of unnecessary comments, or the lack of necessary ones.
- Overly complex expressions that could benefit from simplification.
- High nesting levels that make the code difficult to follow.
- The use of excessively long names for variables or functions.
- Any inconsistencies in naming, formatting, or overall coding style.
- Repetitive code patterns that could be more efficiently handled through abstraction or optimization.
Your feedback must be concise, directly addressing each identified issue with:
- A clear description of the problem.
- A concrete suggestion for how to improve or correct the issue.
Format your feedback as follows:
- Explain the high-level issue or problem briefly.
- Provide a specific suggestion for improvement.
If the code snippet has no readability issues, simply confirm that the code is clear and well-written as is.
]]
)
local COPILOT_REFACTOR = string.format(
[[Your task is to refactor the provided code snippet, focusing specifically on its readability and maintainability.
Identify any issues related to:
- Naming conventions that are unclear, misleading or doesn't follow conventions for the language being used.
- The presence of unnecessary comments, or the lack of necessary ones.
- Overly complex expressions that could benefit from simplification.
- High nesting levels that make the code difficult to follow.
- The use of excessively long names for variables or functions.
- Any inconsistencies in naming, formatting, or overall coding style.
- Repetitive code patterns that could be more efficiently handled through abstraction or optimization.
]]
)
return {
{
"folke/which-key.nvim",
optional = true,
opts = {
spec = {
{ mapping_key_prefix, group = "AI Code Companion", mode = { "n", "v" } },
},
},
},
{
"nvim-treesitter/nvim-treesitter",
opts = { ensure_installed = { "yaml", "markdown" } },
},
{
"CopilotC-Nvim/CopilotChat.nvim",
enabled = false,
},
{
dir = IS_DEV and "~/Projects/research/codecompanion.nvim" or nil,
"olimorris/codecompanion.nvim",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter",
"ibhagwan/fzf-lua", -- For fzf provider, file or buffer picker
"jellydn/spinner.nvim", -- Show loading spinner when request is started
},
opts = {
strategies = {
chat = {
adapter = "copilot",
roles = { llm = " Copilot Chat", user = "IT Man" },
slash_commands = {
["buffer"] = {
callback = "strategies.chat.slash_commands.buffer",
description = "Insert open buffers",
opts = {
contains_code = true,
provider = "fzf_lua", -- default|telescope|mini_pick|fzf_lua
},
},
["file"] = {
callback = "strategies.chat.slash_commands.file",
description = "Insert a file",
opts = {
contains_code = true,
max_lines = 1000,
provider = "fzf_lua", -- telescope|mini_pick|fzf_lua
},
},
},
keymaps = {
send = {
modes = {
n = "<CR>",
i = "<C-CR>",
},
index = 1,
callback = "keymaps.send",
description = "Send",
},
close = {
modes = {
n = "q",
},
index = 3,
callback = "keymaps.close",
description = "Close Chat",
},
stop = {
modes = {
n = "<C-c>",
},
index = 4,
callback = "keymaps.stop",
description = "Stop Request",
},
},
},
inline = { adapter = "copilot" },
agent = { adapter = "copilot" },
},
inline = {
layout = "buffer", -- vertical|horizontal|buffer
},
display = {
chat = {
-- Change to true to show the current model
show_settings = false,
window = {
layout = "vertical", -- float|vertical|horizontal|buffer
},
},
},
opts = {
log_level = "DEBUG",
system_prompt = SYSTEM_PROMPT,
},
prompt_library = {
-- Custom the default prompt
["Generate a Commit Message"] = {
prompts = {
{
role = "user",
content = function()
return "Write commit message with commitizen convention. Write clear, informative commit messages that explain the 'what' and 'why' behind changes, not just the 'how'."
.. "\n\n```\n"
.. vim.fn.system("git diff")
.. "\n```"
end,
opts = {
contains_code = true,
},
},
},
},
["Explain"] = {
strategy = "chat",
description = "Explain how code in a buffer works",
opts = {
default_prompt = true,
modes = { "v" },
short_name = "explain",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = COPILOT_EXPLAIN,
opts = {
visible = false,
},
},
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return "Please explain how the following code works:\n\n```"
.. context.filetype
.. "\n"
.. code
.. "\n```\n\n"
end,
opts = {
contains_code = true,
},
},
},
},
["Explain Code"] = {
strategy = "chat",
description = "Explain how code works",
opts = {
short_name = "explain-code",
auto_submit = false,
is_slash_cmd = true,
},
prompts = {
{
role = "system",
content = COPILOT_EXPLAIN,
opts = {
visible = false,
},
},
{
role = "user",
content = [[Please explain how the following code works.]],
},
},
},
-- Add custom prompts
["Generate a Commit Message for Staged"] = {
strategy = "chat",
description = "Generate a commit message for staged change",
opts = {
short_name = "staged-commit",
auto_submit = true,
is_slash_cmd = true,
},
prompts = {
{
role = "user",
content = function()
return "Write commit message for the change with commitizen convention. Write clear, informative commit messages that explain the 'what' and 'why' behind changes, not just the 'how'."
.. "\n\n```\n"
.. vim.fn.system("git diff --staged")
.. "\n```"
end,
opts = {
contains_code = true,
},
},
},
},
["Inline Document"] = {
strategy = "inline",
description = "Add documentation for code.",
opts = {
modes = { "v" },
short_name = "inline-doc",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return "Please provide documentation in comment code for the following code and suggest to have better naming to improve readability.\n\n```"
.. context.filetype
.. "\n"
.. code
.. "\n```\n\n"
end,
opts = {
contains_code = true,
},
},
},
},
["Document"] = {
strategy = "chat",
description = "Write documentation for code.",
opts = {
modes = { "v" },
short_name = "doc",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return "Please brief how it works and provide documentation in comment code for the following code. Also suggest to have better naming to improve readability.\n\n```"
.. context.filetype
.. "\n"
.. code
.. "\n```\n\n"
end,
opts = {
contains_code = true,
},
},
},
},
["Review"] = {
strategy = "chat",
description = "Review the provided code snippet.",
opts = {
modes = { "v" },
short_name = "review",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = COPILOT_REVIEW,
opts = {
visible = false,
},
},
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return "Please review the following code and provide suggestions for improvement then refactor the following code to improve its clarity and readability:\n\n```"
.. context.filetype
.. "\n"
.. code
.. "\n```\n\n"
end,
opts = {
contains_code = true,
},
},
},
},
["Review Code"] = {
strategy = "chat",
description = "Review code and provide suggestions for improvement.",
opts = {
short_name = "review-code",
auto_submit = false,
is_slash_cmd = true,
},
prompts = {
{
role = "system",
content = COPILOT_REVIEW,
opts = {
visible = false,
},
},
{
role = "user",
content = "Please review the following code and provide suggestions for improvement then refactor the following code to improve its clarity and readability.",
},
},
},
["Refactor"] = {
strategy = "inline",
description = "Refactor the provided code snippet.",
opts = {
modes = { "v" },
short_name = "refactor",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "system",
content = COPILOT_REFACTOR,
opts = {
visible = false,
},
},
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return "Please refactor the following code to improve its clarity and readability:\n\n```"
.. context.filetype
.. "\n"
.. code
.. "\n```\n\n"
end,
opts = {
contains_code = true,
},
},
},
},
["Refactor Code"] = {
strategy = "chat",
description = "Refactor the provided code snippet.",
opts = {
short_name = "refactor-code",
auto_submit = false,
is_slash_cmd = true,
},
prompts = {
{
role = "system",
content = COPILOT_REFACTOR,
opts = {
visible = false,
},
},
{
role = "user",
content = "Please refactor the following code to improve its clarity and readability.",
},
},
},
["Naming"] = {
strategy = "inline",
description = "Give betting naming for the provided code snippet.",
opts = {
modes = { "v" },
short_name = "naming",
auto_submit = true,
user_prompt = false,
stop_context_insertion = true,
},
prompts = {
{
role = "user",
content = function(context)
local code = require("codecompanion.helpers.actions").get_code(context.start_line, context.end_line)
return "Please provide better names for the following variables and functions:\n\n```"
.. context.filetype
.. "\n"
.. code
.. "\n```\n\n"
end,
opts = {
contains_code = true,
},
},
},
},
["Better Naming"] = {
strategy = "chat",
description = "Give betting naming for the provided code snippet.",
opts = {
short_name = "better-naming",
auto_submit = false,
is_slash_cmd = true,
},
prompts = {
{
role = "user",
content = "Please provide better names for the following variables and functions.",
},
},
},
},
},
config = function(_, options)
require("codecompanion").setup(options)
-- Show loading spinner when request is started
local spinner = require("spinner")
local group = vim.api.nvim_create_augroup("CodeCompanionHooks", {})
vim.api.nvim_create_autocmd({ "User" }, {
pattern = "CodeCompanionRequest*",
group = group,
callback = function(request)
if request.match == "CodeCompanionRequestStarted" then
spinner.show()
end
if request.match == "CodeCompanionRequestFinished" then
spinner.hide()
end
end,
})
end,
keys = {
-- Recommend setup
{
mapping_key_prefix .. "a",
"<cmd>CodeCompanionActions<cr>",
desc = "Code Companion - Actions",
},
{
mapping_key_prefix .. "v",
"<cmd>CodeCompanionChat Toggle<cr>",
desc = "Code Companion - Toggle",
mode = { "n", "v" },
},
-- Some common usages with visual mode
{
mapping_key_prefix .. "e",
"<cmd>CodeCompanion /explain<cr>",
desc = "Code Companion - Explain code",
mode = "v",
},
{
mapping_key_prefix .. "f",
"<cmd>CodeCompanion /fix<cr>",
desc = "Code Companion - Fix code",
mode = "v",
},
{
mapping_key_prefix .. "l",
"<cmd>CodeCompanion /lsp<cr>",
desc = "Code Companion - Explain LSP diagnostic",
mode = { "n", "v" },
},
{
mapping_key_prefix .. "t",
"<cmd>CodeCompanion /tests<cr>",
desc = "Code Companion - Generate unit test",
mode = "v",
},
{
mapping_key_prefix .. "m",
"<cmd>CodeCompanion /commit<cr>",
desc = "Code Companion - Git commit message",
},
-- Custom prompts
{
mapping_key_prefix .. "M",
"<cmd>CodeCompanion /staged-commit<cr>",
desc = "Code Companion - Git commit message (staged)",
},
{
mapping_key_prefix .. "d",
"<cmd>CodeCompanion /inline-doc<cr>",
desc = "Code Companion - Inline document code",
mode = "v",
},
{ mapping_key_prefix .. "D", "<cmd>CodeCompanion /doc<cr>", desc = "Code Companion - Document code", mode = "v" },
{
mapping_key_prefix .. "r",
"<cmd>CodeCompanion /refactor<cr>",
desc = "Code Companion - Refactor code",
mode = "v",
},
{
mapping_key_prefix .. "R",
"<cmd>CodeCompanion /review<cr>",
desc = "Code Companion - Review code",
mode = "v",
},
{
mapping_key_prefix .. "n",
"<cmd>CodeCompanion /naming<cr>",
desc = "Code Companion - Better naming",
mode = "v",
},
-- Quick chat
{
mapping_key_prefix .. "q",
function()
local input = vim.fn.input("Quick Chat: ")
if input ~= "" then
vim.cmd("CodeCompanion " .. input)
end
end,
desc = "Code Companion - Quick chat",
},
},
},
}