Skip to content

Commit 31318b8

Browse files
committed
refactor: move util to lib directory and rename it to utils/init.lua
Expose utils/init.lua so users can access it if needed when making custom actions (i.e. local utils = require('toggleterm-manager').utils)
1 parent 214f404 commit 31318b8

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

lua/lib/actions/init.lua

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local actions = require("telescope.actions")
22
local actions_state = require("telescope.actions.state")
33
local toggleterm_ui = require("toggleterm.ui")
44
local toggleterm_config = require("toggleterm.config")
5-
local util = require("util")
5+
local utils = require("lib.utils")
66
local Terminal = require("toggleterm.terminal").Terminal
77

88
local M = {}
@@ -23,8 +23,8 @@ function M.create_term(prompt_bufnr, exit_on_action)
2323
-- set origin window to current term before switching back to telescope
2424
-- this ensures the cursor is moved to the correct term window after closing a term
2525
toggleterm_ui.set_origin_window()
26-
util.focus_on_telescope(prompt_bufnr)
27-
util.refresh_picker(prompt_bufnr, term)
26+
utils.focus_on_telescope(prompt_bufnr)
27+
utils.refresh_picker(prompt_bufnr, term)
2828
-- remove the on_open callback to avoid potential side effects in future actions.
2929
term.on_open = nil
3030
end)
@@ -37,7 +37,7 @@ function M.create_term(prompt_bufnr, exit_on_action)
3737
-- the autocommand setup in telescope/init.lua that starts insert mode on leaving the telescope buffer
3838
-- won't work here since the cursor may move to a non-toggleterm buftype for a brief moment while the
3939
-- toggleterm buffer is being created
40-
util.start_insert_mode()
40+
utils.start_insert_mode()
4141
end
4242

4343
term = Terminal:new({ hidden = float, on_open = on_open_terminal })
@@ -46,11 +46,11 @@ function M.create_term(prompt_bufnr, exit_on_action)
4646
term:open()
4747
elseif float then
4848
term:spawn()
49-
util.refresh_picker(prompt_bufnr, term)
49+
utils.refresh_picker(prompt_bufnr, term)
5050
-- remove the on_open callback to avoid potential side effects in future actions.
5151
term.on_open = nil
5252
else
53-
util.focus_on_origin_win()
53+
utils.focus_on_origin_win()
5454
term:open()
5555
end
5656
end
@@ -64,7 +64,7 @@ function M.create_and_name_term(prompt_bufnr, exit_on_action)
6464
local prompt = "Name terminal: "
6565

6666
vim.ui.input({ prompt = prompt }, function(name)
67-
util.clear_command_line()
67+
utils.clear_command_line()
6868
if name and #name > 0 then
6969
local term
7070
local function on_open_terminal()
@@ -73,8 +73,8 @@ function M.create_and_name_term(prompt_bufnr, exit_on_action)
7373
-- set origin window to current term before switching back to telescope
7474
-- this ensures the cursor is moved to the correct term window after closing a term
7575
toggleterm_ui.set_origin_window()
76-
util.focus_on_telescope(prompt_bufnr)
77-
util.refresh_picker(prompt_bufnr, term)
76+
utils.focus_on_telescope(prompt_bufnr)
77+
utils.refresh_picker(prompt_bufnr, term)
7878
-- remove the on_open callback to avoid potential side effects in future actions.
7979
term.on_open = nil
8080
end)
@@ -87,7 +87,7 @@ function M.create_and_name_term(prompt_bufnr, exit_on_action)
8787
-- the autocommand setup in telescope/init.lua that starts insert mode on leaving the telescope buffer
8888
-- won't work here since the cursor may move to a non-toggleterm buftype for a brief moment while the
8989
-- toggleterm buffer is being created
90-
util.start_insert_mode()
90+
utils.start_insert_mode()
9191
end
9292

9393
term = Terminal:new({ display_name = name, hidden = float, on_open = on_open_terminal })
@@ -96,11 +96,11 @@ function M.create_and_name_term(prompt_bufnr, exit_on_action)
9696
term:open()
9797
elseif float then
9898
term:spawn()
99-
util.refresh_picker(prompt_bufnr, term)
99+
utils.refresh_picker(prompt_bufnr, term)
100100
-- remove the on_open callback to avoid potential side effects in future actions.
101101
term.on_open = nil
102102
else
103-
util.focus_on_origin_win()
103+
utils.focus_on_origin_win()
104104
term:open()
105105
end
106106
end
@@ -120,7 +120,7 @@ function M.rename_term(prompt_bufnr, exit_on_action)
120120

121121
local prompt = string.format("Rename terminal %s: ", selection.term_name)
122122
vim.ui.input({ prompt = prompt }, function(name)
123-
util.clear_command_line()
123+
utils.clear_command_line()
124124
if name and #name > 0 then
125125
-- rename terminal within toggleterm
126126
term.display_name = name
@@ -130,10 +130,10 @@ function M.rename_term(prompt_bufnr, exit_on_action)
130130
term:focus()
131131
else
132132
local current_picker = actions_state.get_current_picker(prompt_bufnr)
133-
local finder, new_row_number = util.create_finder(term.id)
133+
local finder, new_row_number = utils.create_finder(term.id)
134134
current_picker:refresh(finder, { reset_prompt = false })
135135

136-
util.set_selection_row(current_picker, new_row_number)
136+
utils.set_selection_row(current_picker, new_row_number)
137137
end
138138
end
139139
end)
@@ -156,17 +156,17 @@ function M.open_term(prompt_bufnr, exit_on_action)
156156
term:open()
157157
end
158158
term:focus()
159-
util.start_insert_mode()
159+
utils.start_insert_mode()
160160
return
161161
end
162162

163-
util.focus_on_origin_win()
163+
utils.focus_on_origin_win()
164164
if not term:is_open() then
165165
term:open()
166166
end
167167

168-
util.focus_on_telescope(prompt_bufnr)
169-
util.refresh_picker(prompt_bufnr, term)
168+
utils.focus_on_telescope(prompt_bufnr)
169+
utils.refresh_picker(prompt_bufnr, term)
170170
end
171171

172172
--- Toggle a terminal open or closed. If toggling open and exit_on_action is true, focus it.
@@ -185,11 +185,11 @@ function M.toggle_term(prompt_bufnr, exit_on_action)
185185
if exit_on_action then
186186
actions.close(prompt_bufnr)
187187
term:toggle()
188-
util.start_insert_mode()
188+
utils.start_insert_mode()
189189
return
190190
end
191191

192-
util.focus_on_origin_win()
192+
utils.focus_on_origin_win()
193193
if term:is_open() then
194194
term:close()
195195
current_picker.original_win_id = toggleterm_ui.get_origin_window()
@@ -198,8 +198,8 @@ function M.toggle_term(prompt_bufnr, exit_on_action)
198198
current_picker.original_win_id = term.window
199199
end
200200

201-
util.focus_on_telescope(prompt_bufnr)
202-
util.refresh_picker(prompt_bufnr, term)
201+
utils.focus_on_telescope(prompt_bufnr)
202+
utils.refresh_picker(prompt_bufnr, term)
203203
end
204204

205205
--- Delete a terminal.
@@ -226,17 +226,17 @@ function M.delete_term(prompt_bufnr, exit_on_action)
226226
-- and causes telescope to exit. See toggleterm's terminal.lua:__handle_exit and ui.lua:close_split.
227227
term.close_on_exit = false
228228

229-
util.focus_on_origin_win()
229+
utils.focus_on_origin_win()
230230

231231
local force = vim.api.nvim_buf_get_option(selection.bufnr, "buftype") == "terminal"
232232

233-
util.delete_buffer(selection.bufnr, force)
233+
utils.delete_buffer(selection.bufnr, force)
234234

235235
toggleterm_ui.set_origin_window()
236236

237-
util.focus_on_telescope(prompt_bufnr)
237+
utils.focus_on_telescope(prompt_bufnr)
238238

239-
util.refresh_picker(prompt_bufnr, selection, true)
239+
utils.refresh_picker(prompt_bufnr, selection, true)
240240
end
241241

242242
return M

lua/lib/telescope/init.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local pickers = require("telescope.pickers")
22
local telescope_actions = require("telescope.actions")
33
local conf = require("telescope.config").values
4-
local util = require("util")
4+
local utils = require("lib.utils")
55

66
--- Create autocommand to enter insert mode when the cursor leaves the telescope buffer.
77
--- Useful for actions that are called with exit_on_action set to false b/c it allows the user
@@ -20,7 +20,7 @@ local function telescope_leave_autocmd(picker)
2020
local picker_orig_win_bufnr = vim.fn.winbufnr(picker.original_win_id)
2121
local buftype = vim.api.nvim_buf_get_option(picker_orig_win_bufnr, "filetype")
2222
if buftype == "toggleterm" then
23-
util.start_insert_mode()
23+
utils.start_insert_mode()
2424
end
2525
end
2626
end,
@@ -38,10 +38,11 @@ M.open = function(opts)
3838

3939
local picker = pickers.new(opts, {
4040
prompt_title = config.titles.prompt,
41-
results_title = config.display_mappings and util.format_results_title(config.mappings) or config.titles.results,
41+
results_title = config.display_mappings and utils.format_results_title(config.mappings)
42+
or config.titles.results,
4243
preview_title = config.titles.preview,
4344
previewer = conf.grep_previewer(opts),
44-
finder = util.create_finder(),
45+
finder = utils.create_finder(),
4546
sorter = conf.generic_sorter(opts),
4647
attach_mappings = function(prompt_bufnr, map)
4748
local mappings = config.mappings
File renamed without changes.

lua/toggleterm-manager.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ M.open = require("lib.telescope").open
1212
M.setup = function(opts)
1313
require("config").setup(opts)
1414
end
15+
M.utils = require("lib.utils")
16+
1517
return M

0 commit comments

Comments
 (0)