simrat39 / symbols-outline.nvim

A tree like view for symbols in Neovim using the Language Server Protocol. Supports all your favourite languages.
MIT License
1.85k stars 100 forks source link

Keep focus on current buffer when SymbolsOutline opens #143

Open alloc33 opened 2 years ago

alloc33 commented 2 years ago

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

Trantorian1 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

hedyhli commented 11 months ago

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.