6666--- @class Session
6767local Session = {}
6868
69- local terminal_buf , terminal_width , terminal_height
7069
7170local function json_decode (payload )
7271 return vim .json .decode (payload , { luanil = { object = true }})
@@ -139,6 +138,47 @@ local function create_terminal_buf(terminal_win_cmd)
139138end
140139
141140
141+ local terminals = {}
142+ do
143+ --- @type table<number , boolean>
144+ local pool = {}
145+
146+ --- @return number , number | nil
147+ function terminals .acquire (win_cmd , config )
148+ local buf = next (pool )
149+ if buf then
150+ pool [buf ] = nil
151+ if api .nvim_buf_is_valid (buf ) then
152+ vim .bo [buf ].modified = false
153+ return buf
154+ end
155+ end
156+ local terminal_win
157+ buf , terminal_win = create_terminal_buf (win_cmd )
158+ if terminal_win then
159+ if vim .fn .has (' nvim-0.8' ) == 1 then
160+ -- older versions don't support the `win` key
161+ api .nvim_set_option_value (' number' , false , { scope = ' local' , win = terminal_win })
162+ api .nvim_set_option_value (' relativenumber' , false , { scope = ' local' , win = terminal_win })
163+ api .nvim_set_option_value (' signcolumn' , ' no' , { scope = ' local' , win = terminal_win })
164+ else
165+ -- this is like `:set` so new windows will inherit the values :/
166+ vim .wo [terminal_win ].number = false
167+ vim .wo [terminal_win ].relativenumber = false
168+ vim .wo [terminal_win ].signcolumn = " no"
169+ end
170+ end
171+ vim .b [buf ][' dap-type' ] = config .type
172+ return buf , terminal_win
173+ end
174+
175+ --- @param b number
176+ function terminals .release (b )
177+ pool [b ] = true
178+ end
179+ end
180+
181+
142182local function run_in_terminal (self , request )
143183 local body = request .arguments
144184 log .debug (' run_in_terminal' , body )
@@ -160,28 +200,7 @@ local function run_in_terminal(self, request)
160200 end
161201 end
162202 local cur_buf = api .nvim_get_current_buf ()
163- if terminal_buf and api .nvim_buf_is_valid (terminal_buf ) then
164- api .nvim_buf_set_option (terminal_buf , ' modified' , false )
165- else
166- local terminal_win
167- terminal_buf , terminal_win = create_terminal_buf (settings .terminal_win_cmd )
168- if terminal_win then
169- if vim .fn .has (' nvim-0.8' ) == 1 then
170- -- older versions don't support the `win` key
171- api .nvim_set_option_value (' number' , false , { scope = ' local' , win = terminal_win })
172- api .nvim_set_option_value (' relativenumber' , false , { scope = ' local' , win = terminal_win })
173- api .nvim_set_option_value (' signcolumn' , ' no' , { scope = ' local' , win = terminal_win })
174- else
175- -- this is like `:set` so new windows will inherit the values :/
176- vim .wo [terminal_win ].number = false
177- vim .wo [terminal_win ].relativenumber = false
178- vim .wo [terminal_win ].signcolumn = " no"
179- end
180- end
181- vim .b [terminal_buf ][' dap-type' ] = self .config .type
182- terminal_width = terminal_win and api .nvim_win_get_width (terminal_win ) or 80
183- terminal_height = terminal_win and api .nvim_win_get_height (terminal_win ) or 40
184- end
203+ local terminal_buf , terminal_win = terminals .acquire (settings .terminal_win_cmd , self .config )
185204 local terminal_buf_name = ' [dap-terminal] ' .. (self .config .name or body .args [1 ])
186205 local terminal_name_ok = pcall (api .nvim_buf_set_name , terminal_buf , terminal_buf_name )
187206 if not terminal_name_ok then
@@ -203,8 +222,8 @@ local function run_in_terminal(self, request)
203222 local opts = {
204223 env = next (body .env or {}) and body .env or vim .empty_dict (),
205224 cwd = (body .cwd and body .cwd ~= ' ' ) and body .cwd or nil ,
206- height = terminal_height ,
207- width = terminal_width ,
225+ height = terminal_win and api . nvim_win_get_height ( terminal_win ) or 40 ,
226+ width = terminal_win and api . nvim_win_get_width ( terminal_win ) or 80 ,
208227 pty = true ,
209228 on_stdout = function (_ , data )
210229 local count = # data
@@ -225,6 +244,7 @@ local function run_in_terminal(self, request)
225244 on_exit = function (_ , exit_code )
226245 pcall (api .nvim_chan_send , chan , ' \r\n [Process exited ' .. tostring (exit_code ) .. ' ]' )
227246 pcall (api .nvim_buf_set_keymap , terminal_buf , " t" , " <CR>" , " <cmd>bd!<CR>" , { noremap = true , silent = true })
247+ terminals .release (terminal_buf )
228248 end ,
229249 }
230250 jobid = vim .fn .jobstart (body .args , opts )
0 commit comments