Skip to content

Commit 4978fb5

Browse files
committed
docs: add type docs to util functions
1 parent b8c7b32 commit 4978fb5

2 files changed

Lines changed: 49 additions & 33 deletions

File tree

lua/lib/actions/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function M.create_term(prompt_bufnr, exit_on_action)
2424
-- set origin window to current term before switching back to telescope
2525
-- this ensures the cursor is moved to the correct term window after closing a term
2626
toggleterm_ui.set_origin_window()
27+
util.focus_on_telescope(prompt_bufnr)
2728
util.refresh_picker(prompt_bufnr, term)
2829
-- remove the on_open callback to avoid potential side effects in future actions.
2930
term.on_open = nil
@@ -63,6 +64,7 @@ function M.create_and_name_term(prompt_bufnr, exit_on_action)
6364
-- set origin window to current term before switching back to telescope
6465
-- this ensures the cursor is moved to the correct term window after closing a term
6566
toggleterm_ui.set_origin_window()
67+
util.focus_on_telescope(prompt_bufnr)
6668
util.refresh_picker(prompt_bufnr, term)
6769
-- remove the on_open callback to avoid potential side effects in future actions.
6870
term.on_open = nil
@@ -111,6 +113,7 @@ function M.open_term(prompt_bufnr, exit_on_action)
111113
term:open()
112114
end
113115

116+
util.focus_on_telescope(prompt_bufnr)
114117
util.refresh_picker(prompt_bufnr, term)
115118
end
116119

@@ -164,6 +167,8 @@ function M.delete_term(prompt_bufnr, exit_on_action)
164167
-- Reset the origin window in the toggleterm UI after deletion.
165168
toggleterm_ui.set_origin_window()
166169

170+
util.focus_on_telescope(prompt_bufnr)
171+
167172
-- Refresh the picker to reflect the changes in the list of terminals.
168173
-- Pass a boolean flag indicating that an item has been deleted
169174
util.refresh_picker(prompt_bufnr, selection, true)
@@ -197,6 +202,7 @@ function M.toggle_term(prompt_bufnr, exit_on_action)
197202
current_picker.original_win_id = term.window
198203
end
199204

205+
util.focus_on_telescope(prompt_bufnr)
200206
util.refresh_picker(prompt_bufnr, term)
201207
end
202208

lua/util.lua

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ function M.format_results_title(mappings)
3232
return table.concat(mapping_descriptions, " ")
3333
end
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.
3538
function 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
8083
end
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.
8289
function 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
155162
end
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.
166192
function 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
183208
end
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.
189214
function 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)
197225
end
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.
219228
function M.clear_command_line()
220229
vim.cmd("echo ''")
221230
end
222231

232+
--- Start insert mode.
223233
function M.start_insert_mode()
224234
vim.schedule(function()
225235
vim.cmd("startinsert!")

0 commit comments

Comments
 (0)