symbols-outline.nvim
symbols-outline.nvim copied to clipboard
Keep focus on current buffer when SymbolsOutline opens
I use vim.cmd [[SymbolsOutlineOpen]] when setting up my lsp_server (on_attach). How to prevent focus on SymbolsOutline's buffer when it opens?
Hey, I was having exactly the same problem as you. You might have some luck saving the current buffer name with
local buffer_current = vim.api.nvim_buf_get_name(0)
And then reverting to that buffer with
vim.cmd('buffer ' .. buffer_current)
⚠️ disclaimer ⚠️
I have tried this myself but it seems like there is a delay between calling SymbolsOuline
and SymbolsOutline actually being drawn, so this technique does not work on its own. You might have some luck though by opening SymbolsOutline asynchroneously using plenary's async library and setting a delay inside the thread. Something like this might work:
-- tries and load async plugin
local async, async_loaded = pcall(require, 'plenary.async')
if not async_loaded then
return
end
-- gets the current buffer name
local bufname = vim.api.nvim_buf_get_name(0)
-- following code runs asynchroneously
async.run(function ()
-- opens SymbolsOutline
vim.cmd [[SymbolsOutlineOpen]]
-- waits 20ms
async.util.sleep(20)
-- returns to previous buffer
vim.cmd('buffer ' .. bufname)
end)
What would be most useful though would be some form of publicly available library callback, kind of like ToggleTerm's on_open
and on_close
callbacks which can be specified in the plugin's setup
.
After some further testing I would recommend using wincmd
to switch back to buffer instead of 'buffer'
Hi, for the time being (until simrat39 comes back to merge some PRs) I'm maintaining a fork where I've implemented this feature and merged some other PRs -- see the focus_on_open
option in the readme.
Also note on the fork status in the readme, feel free to try it if you really want this feature but do proceed with caution.