-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathCopilotChat.lua
102 lines (93 loc) · 3.54 KB
/
CopilotChat.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
if vim.g.loaded_copilot_chat then
return
end
local min_version = '0.10.0'
if vim.fn.has('nvim-' .. min_version) ~= 1 then
vim.notify_once(('CopilotChat.nvim requires Neovim >= %s'):format(min_version), vim.log.levels.ERROR)
return
end
-- Setup highlights
vim.api.nvim_set_hl(0, 'CopilotChatStatus', { link = 'DiagnosticHint', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatHelp', { link = 'DiagnosticInfo', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatKeyword', { link = 'Keyword', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatInput', { link = 'Special', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatHeader', { link = '@markup.heading.2.markdown', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatSeparator', { link = '@punctuation.special.markdown', default = true })
-- Setup commands
vim.api.nvim_create_user_command('CopilotChat', function(args)
local chat = require('CopilotChat')
local input = args.args
if input and vim.trim(input) ~= '' then
chat.ask(input)
else
chat.open()
end
end, {
nargs = '*',
force = true,
range = true,
})
vim.api.nvim_create_user_command('CopilotChatPrompts', function()
local chat = require('CopilotChat')
chat.select_prompt()
end, { force = true, range = true })
vim.api.nvim_create_user_command('CopilotChatModels', function()
local chat = require('CopilotChat')
chat.select_model()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatAgents', function()
local chat = require('CopilotChat')
chat.select_agent()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatOpen', function()
local chat = require('CopilotChat')
chat.open()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatClose', function()
local chat = require('CopilotChat')
chat.close()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatToggle', function()
local chat = require('CopilotChat')
chat.toggle()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatStop', function()
local chat = require('CopilotChat')
chat.stop()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatReset', function()
local chat = require('CopilotChat')
chat.reset()
end, { force = true })
local function complete_load()
local chat = require('CopilotChat')
local options = vim.tbl_map(function(file)
return vim.fn.fnamemodify(file, ':t:r')
end, vim.fn.glob(chat.config.history_path .. '/*', true, true))
if not vim.tbl_contains(options, 'default') then
table.insert(options, 1, 'default')
end
return options
end
vim.api.nvim_create_user_command('CopilotChatSave', function(args)
local chat = require('CopilotChat')
chat.save(args.args)
end, { nargs = '*', force = true, complete = complete_load })
vim.api.nvim_create_user_command('CopilotChatLoad', function(args)
local chat = require('CopilotChat')
chat.load(args.args)
end, { nargs = '*', force = true, complete = complete_load })
-- Store the current directory to window when directory changes
-- I dont think there is a better way to do this that functions
-- with "rooter" plugins, LSP and stuff as vim.fn.getcwd() when
-- i pass window number inside doesnt work
vim.api.nvim_create_autocmd({ 'VimEnter', 'WinEnter', 'DirChanged' }, {
group = vim.api.nvim_create_augroup('CopilotChat', {}),
callback = function()
vim.w.cchat_cwd = vim.fn.getcwd()
end,
})
-- Setup treesitter
vim.treesitter.language.register('markdown', 'copilot-chat')
vim.g.loaded_copilot_chat = true