weirongxu / coc-explorer

📁 Explorer for coc.nvim
MIT License
1.15k stars 45 forks source link

How to change current directory of explorer when changing to different buffer of different directory? #244

Closed MuhammadMuradG closed 4 years ago

MuhammadMuradG commented 4 years ago

I want to change the current work directory of explorer each time I navigate between different buffers of different path.

Is there an option or command that provides this feature?

PS: This feature is available only when the opened files are in the same directory by highlighted the current opened file.

issue-label-bot[bot] commented 4 years ago

Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.55. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

weirongxu commented 4 years ago

You can use the Vim API to execute cd action to change the directory

MuhammadMuradG commented 4 years ago

Thanks for your guidance, I created the following vim script:

autocmd BufEnter * silent! let dir = getcwd()
autocmd BufEnter * call CocAction('runCommand', 'explorer.doAction', 'closest', {'name': 'cd', 'args': [dir]})

they are working correctly when I call them in command mode (:) but when I put them in my init.vim the following error is produced after open nvim: Screenshot at 2020-07-31 11-08-14

weirongxu commented 4 years ago

Make sure this script call after CocNvimInit

autocmd User CocNvimInit call s:initCoc()

MuhammadMuradG commented 4 years ago

This is my script:

" Sure scripts are called after CocNvimInit
autocmd User CocNvimInit call s:initCoc()

nmap <space>e :CocCommand explorer<CR>
autocmd BufEnter * if (winnr("$") == 1 && &filetype == 'coc-explorer') | q | endif

" Automatically open coc-explorer
autocmd BufEnter * silent! let dir = getcwd()
autocmd VimEnter * if argc() == 1 | exe 'CocCommand explorer --no-focus ' . dir | endif
autocmd VimEnter * if argc() == 0 | Startify | exe 'CocCommand explorer --no-focus ' . dir | endif

autocmd BufEnter * call CocAction('runCommand', 'explorer.doAction', 'closest', {'name': 'cd', 'args': [dir]})
weirongxu commented 4 years ago

autocmd User CocNvimInit call s:coc_inited()

Declare a s:coc_inited() function, and put your scripts into this function.

MuhammadMuradG commented 4 years ago

When I use CocNvimInit event the following error is appear: Screenshot at 2020-07-31 12-50-34 So, I use CocExplorerOpenPost event instead which works fine.

@weirongxu Thanks again for your guidance.

For who interested with this behaviour, put the following in the init.vim (for NeoVim):

" Sure the following script is called after CocExplorerOpenPost
function s:explorer_inited()
    autocmd BufEnter * if (&filetype != 'coc-explorer') | exe 'silent! let dir = getcwd()'
    autocmd BufEnter * call CocAction("runCommand", "explorer.doAction", "closest", {"name": "cd", "args": [dir]})
endfunction

autocmd User CocExplorerOpenPost call s:explorer_inited()
MuhammadMuradG commented 4 years ago

Sorry, but when use the previous script all things seem good except when I am trying open file from coc-explorer which leads to the nvim stuck and does not respond to any command. I tried using BufWinEnter event instead of BufEnterevent but that leaded to open the selected file but still nvim stuck and does not respond to any command.

Could you please provide me with probable event or group of event or the solve of the problem?

weirongxu commented 4 years ago

Use CocActionAsync instead of the CocAction, BTW, you should save the previous dir to avoid calling cd redundantly

MuhammadMuradG commented 4 years ago

Everything seem working good now! Thanks again for your help.