-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
module.lua
185 lines (158 loc) · 5.59 KB
/
module.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
-- module represents a lua module for the plugin
local M = {}
local Layout = require("nui.layout")
local Popup = require("nui.popup")
local ChatInput = require("chatgpt.input")
local Chat = require("chatgpt.chat")
local Api = require("chatgpt.api")
local Config = require("chatgpt.config")
local Prompts = require("chatgpt.prompts")
local Edits = require("chatgpt.code_edits")
local Settings = require("chatgpt.settings")
local InlineEdit = require("chatgpt.flows.inline_edit")
local open_chat = function()
local chat, chat_input, layout, chat_window
local scroll_chat = function(direction)
local speed = vim.api.nvim_win_get_height(chat_window.winid) / 2
local input = direction > 0 and [[]] or [[]]
local count = math.abs(speed)
vim.api.nvim_win_call(chat_window.winid, function()
vim.cmd([[normal! ]] .. count .. input)
end)
end
chat_window = Popup(Config.options.chat_window)
chat_input = ChatInput(Config.options.chat_input, {
prompt = Config.options.chat_input.prompt,
on_close = function()
chat:close()
Api.close()
layout:unmount()
end,
on_submit = vim.schedule_wrap(function(value)
if chat:isBusy() then
vim.notify("I'm busy, please wait a moment...", vim.log.levels.WARN)
return
end
-- clear input
vim.api.nvim_buf_set_lines(chat_input.bufnr, 0, 1, false, { "" })
chat:addQuestion(value)
chat:showProgess()
local params = vim.tbl_extend("keep", { prompt = chat:toString() }, Settings.params)
Api.completions(params, function(answer)
chat:addAnswer(answer)
end)
end),
})
local params = Config.options.openai_params
local settings_panel = Settings.get_settings_panel("completions", params)
layout = Layout(
Config.options.chat_layout,
Layout.Box({
Layout.Box(chat_window, { grow = 1 }),
Layout.Box(chat_input, { size = 3 }),
}, { dir = "col", grow = 1 })
)
--
-- add keymaps
--
-- yank last answer
chat_input:map("i", Config.options.keymaps.yank_last, function()
local msg = chat:getSelected()
vim.fn.setreg(Config.options.yank_register, msg.text)
vim.notify("Successfully copied to yank register!", vim.log.levels.INFO)
end, { noremap = true })
-- scroll down
chat_input:map("i", Config.options.keymaps.scroll_down, function()
scroll_chat(1)
end, { noremap = true, silent = true })
-- scroll up
chat_input:map("i", Config.options.keymaps.scroll_up, function()
scroll_chat(-1)
end, { noremap = true, silent = true })
-- close
local close_keymaps = Config.options.keymaps.close
if type(close_keymaps) ~= "table" then
close_keymaps = { close_keymaps }
end
for _, keymap in ipairs(close_keymaps) do
chat_input:map("i", keymap, function()
chat_input.input_props.on_close()
end, { noremap = true, silent = true })
end
-- toggle settings
local settings_open = false
for _, popup in ipairs({ settings_panel, chat_input }) do
for _, mode in ipairs({ "n", "i" }) do
popup:map(mode, "<C-o>", function()
if settings_open then
layout:update(Layout.Box({
Layout.Box(chat_window, { grow = 1 }),
Layout.Box(chat_input, { size = 3 }),
}, { dir = "col" }))
settings_panel:hide()
vim.api.nvim_set_current_win(chat_input.winid)
else
layout:update(Layout.Box({
Layout.Box({
Layout.Box(chat_window, { grow = 1 }),
Layout.Box(chat_input, { size = 3 }),
}, { dir = "col", grow = 1 }),
Layout.Box(settings_panel, { size = 40 }),
}, { dir = "row" }))
settings_panel:show()
settings_panel:mount()
vim.api.nvim_set_current_win(settings_panel.winid)
vim.api.nvim_buf_set_option(settings_panel.bufnr, "modifiable", false)
vim.api.nvim_win_set_option(settings_panel.winid, "cursorline", true)
end
settings_open = not settings_open
end, {})
end
end
-- toggle panes
local active_panel = chat_input
for _, popup in ipairs({ settings_panel, chat_input }) do
for _, mode in ipairs({ "n", "i" }) do
popup:map(mode, "<Tab>", function()
if active_panel == settings_panel then
vim.api.nvim_set_current_win(chat_input.winid)
active_panel = chat_input
else
vim.api.nvim_set_current_win(settings_panel.winid)
vim.api.nvim_buf_set_option(settings_panel.bufnr, "modifiable", false)
vim.api.nvim_win_set_option(settings_panel.winid, "cursorline", true)
active_panel = settings_panel
end
end, {})
end
end
-- mount chat component
layout:mount()
-- initialize chat
chat = Chat:new(chat_window.bufnr, chat_window.winid)
-- set custom filetype
vim.api.nvim_buf_set_option(chat_window.bufnr, "filetype", Config.options.chat_window.filetype)
return chat, chat_input, chat_window
end
M.openChat = function()
local chat, _, _ = open_chat()
chat:welcome()
end
M.open_chat_with_awesome_prompt = function()
Prompts.selectAwesomePrompt({
cb = vim.schedule_wrap(function(act, prompt)
local chat, _, chat_window = open_chat()
-- TODO: dry
chat_window.border:set_text("top", " ChatGPT - Acts as " .. act .. " ", "center")
chat:addQuestion(prompt)
chat:showProgess()
local params = vim.tbl_extend("keep", { prompt = chat:toString() }, Settings.params)
Api.completions(params, function(answer)
chat:addAnswer(answer)
end)
end),
})
end
M.edit_with_instructions = Edits.edit_with_instructions
M.inline_edit = InlineEdit.run
return M