Skip to content

Commit 0854efb

Browse files
authored
Toggleterm: improve focusing and scrolling capabilities (#250)
* toggleterm: add scroll_on_error add the option to only scroll when the operation failed. Together with 'auto_scroll = false' this is a nice alternative. update the documentation for auto_scroll as it was slightly incorrect. On long lasting operations you can notice that it scrolls an new input received. * toggleterm: add auto_focus default it to true (this was the behavior all the time) but now it's possible to disable it which would let the cursor stay in the editor. * toggleterm: add focus_on_error An addition similar to scroll_on_error. This lets the user select auto_scroll = false and then the terminal will only get the focus when an error occurred. * dap: add initCommands
1 parent f061713 commit 0854efb

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

lua/cmake-tools/const.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ local const = {
4141
toggleterm = {
4242
direction = "float", -- 'vertical' | 'horizontal' | 'tab' | 'float'
4343
close_on_exit = false, -- whether close the terminal when exit
44-
auto_scroll = true, -- whether auto scroll to the bottom
44+
auto_scroll = true, -- auto scroll on new input
45+
scroll_on_error = false, -- scroll to bottom on error
46+
auto_focus = true, -- auto focus the terminal on activation
47+
focus_on_error = false, -- focus on error
4548
singleton = true, -- single instance, autocloses the opened one, if present
4649
},
4750
overseer = {
@@ -85,7 +88,10 @@ local const = {
8588
toggleterm = {
8689
direction = "float", -- 'vertical' | 'horizontal' | 'tab' | 'float'
8790
close_on_exit = false, -- whether close the terminal when exit
88-
auto_scroll = true, -- whether auto scroll to the bottom
91+
auto_scroll = true, -- auto scroll on new input
92+
scroll_on_error = false, -- scroll to bottom on error
93+
auto_focus = true, -- auto focus the terminal on activation
94+
focus_on_error = false, -- focus on error
8995
singleton = true, -- single instance, autocloses the opened one, if present
9096
},
9197
overseer = {

lua/cmake-tools/init.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,20 @@ function cmake.register_dap_function()
13861386
end)
13871387
end
13881388

1389+
local initCmds = function()
1390+
local commands = {}
1391+
local sources = { vim.env.HOME .. "/.lldbinit", vim.fn.getcwd() .. "/.lldbinit" }
1392+
for idx, source in ipairs(sources) do
1393+
local file = io.open(source, "r")
1394+
if file then
1395+
local command = "command source " .. source
1396+
table.insert(commands, command)
1397+
file:close()
1398+
end
1399+
end
1400+
return commands
1401+
end
1402+
13891403
if opt.target then
13901404
-- explicit target requested. use that instead of the configured one
13911405
return cmake.build({ target = opt.target }, function()
@@ -1397,6 +1411,7 @@ function cmake.register_dap_function()
13971411
cwd = cmake.get_launch_path(opt.target),
13981412
args = opt.args and opt.args or config.target_settings[opt.target].args,
13991413
env = env,
1414+
initCommands = initCmds,
14001415
}
14011416
-- close cmake console
14021417
cmake.close_executor()
@@ -1436,6 +1451,7 @@ function cmake.register_dap_function()
14361451
cwd = cmake.get_launch_path(cmake.get_launch_target()),
14371452
args = cmake:get_launch_args(),
14381453
env = env,
1454+
initCommands = initCmds,
14391455
}
14401456
-- close cmake console
14411457
cmake.close_executor()

lua/cmake-tools/toggleterm.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,32 @@ function _toggleterm.run(cmd, env_script, env, args, cwd, opts, on_exit, on_outp
6767
on_output(data)
6868
end, -- callback for processing output on stdout
6969
on_stderr = function(t, job, data, name)
70+
if opts.scroll_on_error then
71+
_toggleterm.term:scroll_bottom()
72+
end
7073
on_output(nil, data)
7174
end, -- callback for processing output on stderr
7275
on_exit = function(t, job, exit_code, name)
7376
on_exit(exit_code)
77+
if exit_code ~= 0 then -- operation failed
78+
if opts.scroll_on_error then
79+
_toggleterm.term:scroll_bottom()
80+
end
81+
if opts.focus_on_error then
82+
vim.cmd("wincmd p")
83+
end
84+
end
7485
_toggleterm.chan_id = nil
7586
_toggleterm.cmd = nil
7687
end, -- function to run when terminal process exits
7788
})
78-
_toggleterm.term:toggle()
89+
if not _toggleterm.term:is_open() then
90+
_toggleterm.term:open()
91+
if not opts.auto_focus then -- focus back on editor
92+
vim.cmd("wincmd p")
93+
vim.cmd("stopinsert!")
94+
end
95+
end
7996
_toggleterm.chan_id = _toggleterm.term.job_id
8097
end
8198

0 commit comments

Comments
 (0)