@@ -32,6 +32,9 @@ function M.format_results_title(mappings)
3232 return table.concat (mapping_descriptions , " " )
3333end
3434
35+ --- Get the list of terminals and their properties.
36+ --- @return table , table A tuple containing a list of toggleterm /terminal objects and a table of options that will be used for
37+ --- creating the telescope entries.
3538function M .get_terminals ()
3639 local bufnrs = vim .tbl_filter (function (b )
3740 return vim .api .nvim_buf_get_option (b , " filetype" ) == " toggleterm"
@@ -79,6 +82,10 @@ function M.get_terminals()
7982 return terminals , entry_maker_opts
8083end
8184
85+ --- Create a telescope finder with the current terminals. Sort the terminal objects based on the user's
86+ --- sort table provided in their config. This determines the order they appear in the telescope buffer.
87+ --- @param cur_row_term_id number | nil The id of the current terminal to find.
88+ --- @return function , number A new finder function and the row number of the current terminal.
8289function M .create_finder (cur_row_term_id )
8390 local config = require (" config" ).options
8491 local terms , entry_maker_opts = M .get_terminals ()
@@ -154,17 +161,35 @@ function M.create_finder(cur_row_term_id)
154161 new_row_num
155162end
156163
157- -- function M.refresh_picker_with_term(prompt_bufnr, term)
158- -- M.focus_on_telescope(prompt_bufnr)
159- -- local current_picker = actions_state.get_current_picker(prompt_bufnr)
160- -- local finder, new_row_number = M.create_finder(term.id)
161- -- current_picker:refresh(finder, { reset_prompt = false })
162- -- M.set_selection_row(current_picker, new_row_number)
163- -- current_picker.original_win_id = term.window
164- -- end
164+ --- Focuses on toggleterm's current origin window without closing telescope (the use of noautocmd prevents the
165+ --- telescope prompt from automatically closing). Useful for actions where exit_on_action is false.
166+ function M .focus_on_origin_win ()
167+ local window = toggleterm_ui .get_origin_window ()
168+ vim .cmd (string.format (" noautocmd lua vim.api.nvim_set_current_win(%s)" , window ))
169+ end
170+
171+ --- Focus on the telescope prompt buffer window. Useful for actions where exit_on_action is false. This would typically be called
172+ --- after calling focus_on_origin_win and manipulating terminal buffers (i.e. open, close, create).
173+ --- @param prompt_bufnr number The buffer number of the telescope prompt.
174+ function M .focus_on_telescope (prompt_bufnr )
175+ -- Go through all the windows
176+ for _ , win in ipairs (vim .api .nvim_list_wins ()) do
177+ -- Check if the window's buffer is the one we're looking for
178+ if vim .api .nvim_win_get_buf (win ) == prompt_bufnr then
179+ -- Set focus to that window
180+ vim .api .nvim_set_current_win (win )
181+ return
182+ end
183+ end
184+ print (" Telescope buffer not visible in any window" )
185+ end
165186
187+ --- Focus on the open telescope buffer and refresh the picker so the changes to the terminal buffers caused by an action in
188+ --- actions/init.lua are reflected in telescope.
189+ --- @param prompt_bufnr number The buffer number of the prompt.
190+ --- @param selection table The current selection object.
191+ --- @param deleted boolean | nil A boolean indicating if the selection was deleted.
166192function M .refresh_picker (prompt_bufnr , selection , deleted )
167- M .focus_on_telescope (prompt_bufnr )
168193 local current_picker = actions_state .get_current_picker (prompt_bufnr )
169194 local finder , new_row_number = M .create_finder (selection .id )
170195
@@ -182,44 +207,29 @@ function M.refresh_picker(prompt_bufnr, selection, deleted)
182207 end
183208end
184209
185- -- registering a callback is necessary to call set_selection (which is used to keep the selection on the entry
186- -- that was just renamed in this case) after calling the refresh method. Otherwise, because of the async behavior
187- -- of refresh, set_selection will be called before the refresh is complete and the selection will just move
188- -- to the first entry
210+ --- Set the selection row in the telescope picker. Useful for keeping the selection on the current entry after an action in
211+ --- actions/init.lua is run and refresh_picker is run.
212+ --- @param picker table The telescope picker object.
213+ --- @param row_number number The row number to set the selection to.
189214function M .set_selection_row (picker , row_number )
190215 local current_row = picker :get_selection_row ()
191216
217+ -- Registering a callback is necessary to call set_selection after calling the
218+ -- refresh method. Otherwise, because of the async behavior of refresh, set_selection will be called before the refresh is complete
219+ -- and the selection will just move to the first entry
192220 local callbacks = { unpack (picker ._completion_callbacks ) } -- shallow copy
193221 picker :register_completion_callback (function (self )
194222 self :set_selection (row_number or current_row )
195223 self ._completion_callbacks = callbacks
196224 end )
197225end
198226
199- -- focuses on toggleterm's current origin window without closing telescope (the use of noautocmd prevents the
200- -- telescope prompt from automatically closing)
201- function M .focus_on_origin_win ()
202- local window = toggleterm_ui .get_origin_window ()
203- vim .cmd (string.format (" noautocmd lua vim.api.nvim_set_current_win(%s)" , window ))
204- end
205-
206- function M .focus_on_telescope (prompt_bufnr )
207- -- Go through all the windows
208- for _ , win in ipairs (vim .api .nvim_list_wins ()) do
209- -- Check if the window's buffer is the one we're looking for
210- if vim .api .nvim_win_get_buf (win ) == prompt_bufnr then
211- -- Set focus to that window
212- vim .api .nvim_set_current_win (win )
213- return
214- end
215- end
216- print (" Telescope buffer not visible in any window" )
217- end
218-
227+ --- Clear the command line after naming a toggleterm terminal.
219228function M .clear_command_line ()
220229 vim .cmd (" echo ''" )
221230end
222231
232+ --- Start insert mode.
223233function M .start_insert_mode ()
224234 vim .schedule (function ()
225235 vim .cmd (" startinsert!" )
0 commit comments