forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotChat.lua
More file actions
129 lines (117 loc) · 4.6 KB
/
CopilotChat.lua
File metadata and controls
129 lines (117 loc) · 4.6 KB
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
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
local group = vim.api.nvim_create_augroup('CopilotChat', {})
-- Setup highlights
local function setup_highlights()
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 })
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
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, 'CopilotChatResource', { link = 'Constant', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatTool', { link = 'Function', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatPrompt', { link = 'Statement', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatModel', { link = 'Type', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatUri', { link = 'Underlined', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatAnnotation', { link = 'ColorColumn', default = true })
local fg = vim.api.nvim_get_hl(0, { name = 'CopilotChatStatus', link = false }).fg
local bg = vim.api.nvim_get_hl(0, { name = 'CopilotChatAnnotation', link = false }).bg
vim.api.nvim_set_hl(0, 'CopilotChatAnnotationHeader', { fg = fg, bg = bg })
end
vim.api.nvim_create_autocmd('ColorScheme', {
group = group,
callback = function()
setup_highlights()
end,
})
setup_highlights()
vim.api.nvim_create_autocmd('FileType', {
pattern = 'copilot-chat',
group = group,
callback = vim.schedule_wrap(function()
vim.cmd.syntax('match CopilotChatResource "#\\S\\+"')
vim.cmd.syntax('match CopilotChatTool "@\\S\\+"')
vim.cmd.syntax('match CopilotChatPrompt "/\\S\\+"')
vim.cmd.syntax('match CopilotChatModel "\\$\\S\\+"')
vim.cmd.syntax('match CopilotChatUri "##\\S\\+"')
end),
})
-- 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('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 = group,
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