Skip to content

Commit 5a5e4bd

Browse files
committed
feat: adapt create_term and create_and_name_term actions to work when a user has their toggleterm config's direction key set to 'float'
If the term direction is 'float' and exit_on_action is false, create the terminal in the background (term:spawn) to prevent flashes that are caused by telescope and toggleterm both being floating windows. If exit_on_action is true, treat it the same as 'horizontal' and 'vertical' toggleterm terminals, which is to exit telescope and focus the terminal.
1 parent bcef380 commit 5a5e4bd

2 files changed

Lines changed: 47 additions & 18 deletions

File tree

lua/lib/actions/init.lua

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
local actions = require("telescope.actions")
22
local actions_state = require("telescope.actions.state")
33
local toggleterm_ui = require("toggleterm.ui")
4+
local toggleterm_config = require("toggleterm.config")
45
local util = require("util")
56
local Terminal = require("toggleterm.terminal").Terminal
67

78
local M = {}
89

9-
--- Create a new terminal and open it. If exit_on_action is true, focus it.
10+
--- Create a new terminal and open it. If exit_on_action is true, focus it. If the term's direction is "float" and
11+
--- exit_on_action is false, don't automatically open the terminal upon creation to prevent flashes.
1012
--- @param prompt_bufnr number The buffer number of the telescope prompt.
1113
--- @param exit_on_action boolean Whether to exit the telescope buffer when the action executes.
1214
function M.create_term(prompt_bufnr, exit_on_action)
15+
local float = toggleterm_config.get("direction") == "float"
16+
1317
-- forward declare `term` so it can be used inside `on_open_terminal`.
1418
local term
1519

@@ -27,26 +31,36 @@ function M.create_term(prompt_bufnr, exit_on_action)
2731
end
2832
end
2933

30-
term = Terminal:new({ on_open = on_open_terminal })
31-
3234
if exit_on_action then
3335
actions.close(prompt_bufnr)
3436

3537
-- the autocommand setup in telescope/init.lua that starts insert mode on leaving the telescope buffer
3638
-- won't work here since the cursor may move to a non-toggleterm buftype for a brief moment while the
3739
-- toggleterm buffer is being created
3840
util.start_insert_mode()
41+
end
42+
43+
term = Terminal:new({ hidden = float, on_open = on_open_terminal })
44+
45+
if float and exit_on_action then
46+
term:open()
47+
elseif float then
48+
term:spawn()
49+
util.refresh_picker(prompt_bufnr, term)
50+
-- remove the on_open callback to avoid potential side effects in future actions.
51+
term.on_open = nil
3952
else
4053
util.focus_on_origin_win()
54+
term:open()
4155
end
42-
43-
term:open()
4456
end
4557

4658
--- Create and name a new terminal and open it. If exit_on_action is true, focus it.
4759
--- @param prompt_bufnr number The buffer number of the telescope prompt.
4860
--- @param exit_on_action boolean Whether to exit the telescope buffer when the action executes.
4961
function M.create_and_name_term(prompt_bufnr, exit_on_action)
62+
local float = toggleterm_config.get("direction") == "float"
63+
5064
local prompt = "Name terminal: "
5165

5266
vim.ui.input({ prompt = prompt }, function(name)
@@ -67,18 +81,28 @@ function M.create_and_name_term(prompt_bufnr, exit_on_action)
6781
end
6882
end
6983

70-
term = Terminal:new({ display_name = name, on_open = on_open_terminal })
71-
7284
if exit_on_action then
7385
actions.close(prompt_bufnr)
86+
7487
-- the autocommand setup in telescope/init.lua that starts insert mode on leaving the telescope buffer
7588
-- won't work here since the cursor may move to a non-toggleterm buftype for a brief moment while the
7689
-- toggleterm buffer is being created
7790
util.start_insert_mode()
91+
end
92+
93+
term = Terminal:new({ display_name = name, hidden = float, on_open = on_open_terminal })
94+
95+
if float and exit_on_action then
96+
term:open()
97+
elseif float then
98+
term:spawn()
99+
util.refresh_picker(prompt_bufnr, term)
100+
-- remove the on_open callback to avoid potential side effects in future actions.
101+
term.on_open = nil
78102
else
79103
util.focus_on_origin_win()
104+
term:open()
80105
end
81-
term:open()
82106
end
83107
end)
84108
end
@@ -180,6 +204,7 @@ function M.toggle_term(prompt_bufnr, exit_on_action)
180204
if exit_on_action then
181205
actions.close(prompt_bufnr)
182206
term:toggle()
207+
util.start_insert_mode()
183208
return
184209
end
185210

@@ -196,7 +221,7 @@ function M.toggle_term(prompt_bufnr, exit_on_action)
196221
util.refresh_picker(prompt_bufnr, term)
197222
end
198223

199-
--- Rename a terminal. If exit_on_action is true, focus it.
224+
--- Rename a terminal. If exit_on_action is true and the terminal is open, focus it.
200225
--- @param prompt_bufnr number The buffer number of the telescope prompt.
201226
--- @param exit_on_action boolean Whether to exit the telescope buffer when the action executes.
202227
function M.rename_term(prompt_bufnr, exit_on_action)

lua/util.lua

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,23 @@ end
3636
--- @return table, table A tuple containing a list of toggleterm/terminal objects and a table of options that will be used for
3737
--- creating the telescope entries.
3838
function M.get_terminals()
39-
local bufnrs = vim.tbl_filter(function(b)
40-
return vim.api.nvim_buf_get_option(b, "filetype") == "toggleterm"
41-
end, vim.api.nvim_list_bufs())
39+
-- local bufnrs = vim.tbl_filter(function(b)
40+
-- return vim.api.nvim_buf_get_option(b, "filetype") == "toggleterm"
41+
-- end, vim.api.nvim_list_bufs())
4242

43-
if #bufnrs == 0 then
43+
local terms = toggleterm.get_all(true)
44+
if #terms == 0 then
4445
return {}, {}
4546
end
4647
local terminals = {}
4748
local entry_maker_opts = {}
48-
local term_name_lengths, bufname_lengths = {}, {}
49+
local bufnrs, term_name_lengths, bufname_lengths = {}, {}, {}
4950

5051
local cwd = vim.fn.expand(vim.loop.cwd())
5152

52-
for _, bufnr in ipairs(bufnrs) do
53-
local id = vim.api.nvim_buf_get_var(bufnr, "toggle_number")
54-
local term = toggleterm.get(id)
53+
for _, term in ipairs(terms) do
54+
-- local id = vim.api.nvim_buf_get_var(term, "toggle_number")
55+
-- local term = toggleterm.get(id)
5556

5657
local info = vim.fn.getbufinfo(term.bufnr)[1]
5758

@@ -64,6 +65,7 @@ function M.get_terminals()
6465
local bufname = info.name ~= "" and info.name or "No Name"
6566
bufname = Path:new(bufname):normalize(cwd) -- if bufname is inside the cwd, trim that part of the string
6667

68+
table.insert(bufnrs, term.bufnr)
6769
table.insert(term_name_lengths, #term_name)
6870
table.insert(bufname_lengths, #info.name)
6971

@@ -203,7 +205,9 @@ function M.refresh_picker(prompt_bufnr, selection, deleted)
203205

204206
if not deleted then
205207
-- Update the telescope picker's original window id to the term window id that was just created
206-
current_picker.original_win_id = selection.window
208+
if selection.window then
209+
current_picker.original_win_id = selection.window
210+
end
207211
end
208212
end
209213

0 commit comments

Comments
 (0)