1
1
local Popup = require (" nui.popup" )
2
2
local Split = require (" nui.split" )
3
- local config = require (" codegpt.config" )
3
+ local Config = require (" codegpt.config" )
4
4
local event = require (" nui.utils.autocmd" ).event
5
5
6
6
local M = {}
@@ -14,7 +14,7 @@ local function create_horizontal()
14
14
split = Split ({
15
15
relative = " editor" ,
16
16
position = " bottom" ,
17
- size = config .opts .ui .horizontal_popup_size ,
17
+ size = Config .opts .ui .horizontal_popup_size ,
18
18
})
19
19
end
20
20
@@ -26,7 +26,7 @@ local function create_vertical()
26
26
split = Split ({
27
27
relative = " editor" ,
28
28
position = " right" ,
29
- size = config .opts .ui .vertical_popup_size ,
29
+ size = Config .opts .ui .vertical_popup_size ,
30
30
})
31
31
end
32
32
35
35
36
36
local function create_floating ()
37
37
if not popup then
38
- local window_options = config .opts .ui .popup_window_options
38
+ local window_options = Config .opts .ui .popup_window_options
39
39
if window_options == nil then
40
40
window_options = {}
41
41
end
42
42
43
43
local popupOpts = {
44
44
enter = true ,
45
45
focusable = true ,
46
- border = config .opts .ui .popup_border ,
46
+ border = Config .opts .ui .popup_border ,
47
47
position = " 50%" ,
48
48
size = {
49
49
width = " 80%" ,
@@ -58,13 +58,13 @@ local function create_floating()
58
58
popup = Popup (popupOpts )
59
59
end
60
60
61
- popup :update_layout (config .opts .ui .popup_options )
61
+ popup :update_layout (Config .opts .ui .popup_options )
62
62
63
63
return popup
64
64
end
65
65
66
66
local function create_window ()
67
- local popup_type = config .popup_override or config .opts .ui .popup_type
67
+ local popup_type = Config .popup_override or Config .opts .ui .popup_type
68
68
local ui_elem = nil
69
69
if popup_type == " horizontal" then
70
70
ui_elem = create_horizontal ()
@@ -78,24 +78,27 @@ local function create_window()
78
78
end
79
79
80
80
function M .popup (job , lines , filetype , bufnr , start_row , start_col , end_row , end_col )
81
+ if job ~= nil and job .is_shutdown then
82
+ return
83
+ end
81
84
local ui_elem = create_window ()
82
85
-- mount/open the component
83
86
ui_elem :mount ()
84
87
85
- if not (config .persistent_override or config .opts .ui .persistent ) then
88
+ if not (Config .persistent_override or Config .opts .ui .persistent ) then
86
89
-- unmount component when cursor leaves buffer
87
90
ui_elem :on (event .BufLeave , function ()
88
91
ui_elem :unmount ()
89
92
end )
90
93
end
91
94
92
95
-- unmount component when key 'q'
93
- ui_elem :map (" n" , config .opts .ui .actions .quit , function ()
96
+ ui_elem :map (" n" , Config .opts .ui .actions .quit , function ()
94
97
ui_elem :unmount ()
95
98
end , { noremap = true , silent = true })
96
99
--
97
100
-- cancel job if actions.cancel is called
98
- ui_elem :map (" n" , config .opts .ui .actions .cancel , function ()
101
+ ui_elem :map (" n" , Config .opts .ui .actions .cancel , function ()
99
102
job :shutdown ()
100
103
end , { noremap = true , silent = true })
101
104
@@ -104,19 +107,19 @@ function M.popup(job, lines, filetype, bufnr, start_row, start_col, end_row, end
104
107
vim .api .nvim_buf_set_lines (ui_elem .bufnr , 0 , 1 , false , lines )
105
108
106
109
-- replace lines when ctrl-o pressed
107
- ui_elem :map (" n" , config .opts .ui .actions .use_as_output , function ()
110
+ ui_elem :map (" n" , Config .opts .ui .actions .use_as_output , function ()
108
111
vim .api .nvim_buf_set_text (bufnr , start_row , start_col , end_row , end_col , lines )
109
112
ui_elem :unmount ()
110
113
end )
111
114
112
115
-- selecting all the content when ctrl-i is pressed
113
116
-- so the user can proceed with another API request
114
- ui_elem :map (" n" , config .opts .ui .actions .use_as_input , function ()
117
+ ui_elem :map (" n" , Config .opts .ui .actions .use_as_input , function ()
115
118
vim .api .nvim_feedkeys (" ggVG:Chat " , " n" , false )
116
119
end , { noremap = false })
117
120
118
121
-- mapping custom commands
119
- for _ , command in ipairs (config .opts .ui .actions .custom ) do
122
+ for _ , command in ipairs (Config .opts .ui .actions .custom ) do
120
123
ui_elem :map (command [1 ], command [2 ], command [3 ], command [4 ])
121
124
end
122
125
end
@@ -125,27 +128,30 @@ local streaming = false
125
128
local stream_ui_elem = nil
126
129
127
130
function M .popup_stream (job , stream , filetype , bufnr , start_row , start_col , end_row , end_col )
131
+ if job ~= nil and job .is_shutdown then
132
+ return
133
+ end
128
134
if not streaming then
129
135
streaming = true
130
136
stream_ui_elem = create_window ()
131
137
132
138
-- mount/open the component
133
139
stream_ui_elem :mount ()
134
140
135
- if not (config .persistent_override or config .opts .ui .persistent ) then
141
+ if not (Config .persistent_override or Config .opts .ui .persistent ) then
136
142
-- unmount component when cursor leaves buffer
137
143
stream_ui_elem :on (event .BufLeave , function ()
138
144
stream_ui_elem :unmount ()
139
145
end )
140
146
end
141
147
142
148
-- unmount component when key 'q'
143
- stream_ui_elem :map (" n" , config .opts .ui .actions .quit , function ()
149
+ stream_ui_elem :map (" n" , Config .opts .ui .actions .quit , function ()
144
150
stream_ui_elem :unmount ()
145
151
end , { noremap = true , silent = true })
146
152
147
153
-- cancel job if actions.cancel is called
148
- stream_ui_elem :map (" n" , config .opts .ui .actions .cancel , function ()
154
+ stream_ui_elem :map (" n" , Config .opts .ui .actions .cancel , function ()
149
155
job :shutdown ()
150
156
end , { noremap = true , silent = true })
151
157
0 commit comments