11local actions = require (" telescope.actions" )
22local actions_state = require (" telescope.actions.state" )
33local toggleterm_ui = require (" toggleterm.ui" )
4+ local toggleterm_config = require (" toggleterm.config" )
45local util = require (" util" )
56local Terminal = require (" toggleterm.terminal" ).Terminal
67
78local 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.
1214function 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 ()
4456end
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.
4961function 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 )
84108end
@@ -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 )
197222end
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.
202227function M .rename_term (prompt_bufnr , exit_on_action )
0 commit comments