polarmutex/git-worktree.nvim

Switch worktree doesn't work when using plugins like oil.

Closed this issue · 3 comments

I'f i have neotree or oil open, switch doesn't actually work.
Works perfectly from netrw and all other files.

gKits commented

I would recommend reading about the Hooks feature. You can basically combine all the git-worktree events with different plugins and add custom behaviour.
I also use oil and I wrote a simple on switch hook which checks weither my current buffers path starts with oil:/// (which is the prefix of oil buffers) and opens the new working directory in oil.
It's a quite crappy solution but it works for 99% of my use cases but you could probably improve it for you.

local Hooks = require("git-worktree.hooks")

-- register a new hook on SWITCH event
Hooks.register(Hooks.type.SWITCH, function(path, prev_path)
    -- log a nice switch message
    print(prev_path .. "  ~>  " .. path)
    -- check if current buffer is an oil buffer
    if vim.fn.expand("%"):find("^oil:///") then
        -- switch to new cwd in oil
        require("oil").open(vim.fn.getcwd())
    else
        -- use built in hook for non oil buffers
        Hooks.builtins.update_current_buffer_on_switch(path, prev_path)
    end
end)

@gKits That is a nice solution, will look into it. I will close the issue if it works for me.

I would recommend reading about the Hooks feature. You can basically combine all the git-worktree events with different plugins and add custom behaviour. I also use oil and I wrote a simple on switch hook which checks weither my current buffers path starts with oil:/// (which is the prefix of oil buffers) and opens the new working directory in oil. It's a quite crappy solution but it works for 99% of my use cases but you could probably improve it for you.

local Hooks = require("git-worktree.hooks")

-- register a new hook on SWITCH event
Hooks.register(Hooks.type.SWITCH, function(path, prev_path)
    -- log a nice switch message
    print(prev_path .. "  ~>  " .. path)
    -- check if current buffer is an oil buffer
    if vim.fn.expand("%"):find("^oil:///") then
        -- switch to new cwd in oil
        require("oil").open(vim.fn.getcwd())
    else
        -- use built in hook for non oil buffers
        Hooks.builtins.update_current_buffer_on_switch(path, prev_path)
    end
end)

Thanks, i didn't realize that i should of used a hook for this, which makes total sense if you think of it.
Tested it yesterday and today and it has worked with no issue!