symbols-outline.nvim icon indicating copy to clipboard operation
symbols-outline.nvim copied to clipboard

Keep focus on current buffer when SymbolsOutline opens

Open alloc33 opened this issue 2 years ago • 4 comments

I use vim.cmd [[SymbolsOutlineOpen]] when setting up my lsp_server (on_attach). How to prevent focus on SymbolsOutline's buffer when it opens?

alloc33 avatar Jul 10 '22 06:07 alloc33

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)

Trantorian1 avatar Oct 18 '22 15:10 Trantorian1

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.

Trantorian1 avatar Oct 18 '22 15:10 Trantorian1

After some further testing I would recommend using wincmd to switch back to buffer instead of 'buffer'

Trantorian1 avatar Oct 18 '22 17:10 Trantorian1

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.

hedyhli avatar Nov 01 '23 09:11 hedyhli