forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.lua
More file actions
203 lines (176 loc) · 5.17 KB
/
Copy pathchat.lua
File metadata and controls
203 lines (176 loc) · 5.17 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
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
---@class CopilotChat.Chat
---@field bufnr number
---@field winnr number
---@field spinner CopilotChat.Spinner
---@field valid fun(self: CopilotChat.Chat)
---@field visible fun(self: CopilotChat.Chat)
---@field append fun(self: CopilotChat.Chat, str: string)
---@field last fun(self: CopilotChat.Chat)
---@field clear fun(self: CopilotChat.Chat)
---@field open fun(self: CopilotChat.Chat, config: CopilotChat.config)
---@field close fun(self: CopilotChat.Chat)
---@field focus fun(self: CopilotChat.Chat)
---@field follow fun(self: CopilotChat.Chat)
local Spinner = require('CopilotChat.spinner')
local utils = require('CopilotChat.utils')
local is_stable = utils.is_stable
local class = utils.class
function CopilotChatFoldExpr(lnum, separator)
local line = vim.fn.getline(lnum)
if string.match(line, separator .. '$') then
return '>1'
end
return '='
end
local function create_buf()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, 'copilot-chat')
vim.bo[bufnr].filetype = 'markdown'
vim.bo[bufnr].syntax = 'markdown'
local ok, parser = pcall(vim.treesitter.get_parser, bufnr, 'markdown')
if ok and parser then
vim.treesitter.start(bufnr, 'markdown')
end
return bufnr
end
local Chat = class(function(self, on_buf_create)
self.on_buf_create = on_buf_create
self.bufnr = nil
self.spinner = nil
self.winnr = nil
end)
function Chat:valid()
return self.bufnr
and vim.api.nvim_buf_is_valid(self.bufnr)
and vim.api.nvim_buf_is_loaded(self.bufnr)
end
function Chat:visible()
return self.winnr and vim.api.nvim_win_is_valid(self.winnr)
end
function Chat:validate()
if self:valid() then
return
end
self.bufnr = create_buf()
if not self.spinner then
self.spinner = Spinner(self.bufnr, 'copilot-chat')
else
self.spinner.bufnr = self.bufnr
end
self.on_buf_create(self.bufnr)
end
function Chat:last()
self:validate()
local line_count = vim.api.nvim_buf_line_count(self.bufnr)
local last_line = line_count - 1
if last_line < 0 then
return 0, 0, line_count
end
local last_line_content = vim.api.nvim_buf_get_lines(self.bufnr, -2, -1, false)
if not last_line_content or #last_line_content == 0 then
return last_line, 0, line_count
end
local last_column = #last_line_content[1]
return last_line, last_column, line_count
end
function Chat:append(str)
self:validate()
local last_line, last_column, _ = self:last()
vim.api.nvim_buf_set_text(
self.bufnr,
last_line,
last_column,
last_line,
last_column,
vim.split(str, '\n')
)
end
function Chat:clear()
self:validate()
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
end
function Chat:open(config)
self:validate()
if self:visible() then
return
end
local window = config.window
local win_opts = {
style = 'minimal',
}
local layout = window.layout
if layout == 'float' then
win_opts.zindex = window.zindex
win_opts.relative = window.relative
win_opts.border = window.border
win_opts.title = window.title
win_opts.row = window.row or math.floor(vim.o.lines * ((1 - config.window.height) / 2))
win_opts.col = window.col or math.floor(vim.o.columns * ((1 - window.width) / 2))
win_opts.width = math.floor(vim.o.columns * window.width)
win_opts.height = math.floor(vim.o.lines * window.height)
if not is_stable() then
win_opts.footer = window.footer
end
elseif layout == 'vertical' then
if is_stable() then
local orig = vim.api.nvim_get_current_win()
vim.cmd('vsplit')
self.winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
vim.api.nvim_set_current_win(orig)
else
win_opts.vertical = true
end
elseif layout == 'horizontal' then
if is_stable() then
local orig = vim.api.nvim_get_current_win()
vim.cmd('split')
self.winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
vim.api.nvim_set_current_win(orig)
else
win_opts.vertical = false
end
end
if not self.winnr or not vim.api.nvim_win_is_valid(self.winnr) then
self.winnr = vim.api.nvim_open_win(self.bufnr, false, win_opts)
end
vim.wo[self.winnr].wrap = true
vim.wo[self.winnr].linebreak = true
vim.wo[self.winnr].cursorline = true
vim.wo[self.winnr].conceallevel = 2
vim.wo[self.winnr].concealcursor = 'niv'
vim.wo[self.winnr].foldlevel = 99
if config.show_folds then
vim.wo[self.winnr].foldcolumn = '1'
vim.wo[self.winnr].foldmethod = 'expr'
vim.wo[self.winnr].foldexpr = "v:lua.CopilotChatFoldExpr(v:lnum, '" .. config.separator .. "')"
else
vim.wo[self.winnr].foldcolumn = '0'
end
end
function Chat:close()
if self.spinner then
self.spinner:finish()
end
if self:visible() then
vim.api.nvim_win_close(self.winnr, true)
self.winnr = nil
end
end
function Chat:focus()
if self:visible() then
vim.api.nvim_set_current_win(self.winnr)
end
end
function Chat:follow()
if not self:visible() then
return
end
local last_line, last_column, line_count = self:last()
if line_count == 0 then
return
end
vim.api.nvim_win_set_cursor(self.winnr, { last_line + 1, last_column })
end
return Chat