preservim / nerdtree

A tree explorer plugin for vim.
Do What The F*ck You Want To Public License
19.56k stars 1.44k forks source link

Command to Find current file in tree with VCS root #1369

Closed Hritik14 closed 10 months ago

Hritik14 commented 1 year ago

Hello, I'm looking for a command like :NERDTreeFindVCSToggle it is essentially like :NERDTreeFind but also ensures VCS root is used and it toggles nicely when assigned to a hotkey.

I looked at the code and I think this can be implemented by:

  1. Decouple NERDTreeFocus from findAndRevealPath. This will ensure we do not always want to NERDTreeFocus but at times want NERDTreeToggle instead. Here: https://github.com/preservim/nerdtree/blob/32168889bdbc1e7d1d313e3e41c1cc794b38eac5/autoload/nerdtree/ui_glue.vim#L332
  2. Implement a NERDTreeFocusToggle now that we have the above decoupled
  3. Implement a NERDTreeFocusToggleVCS using next steps:
    1. Find VCS root using https://github.com/preservim/nerdtree/blob/32168889bdbc1e7d1d313e3e41c1cc794b38eac5/nerdtree_plugin/vcs.vim#L34-L46
    2. Change root using https://github.com/preservim/nerdtree/blob/32168889bdbc1e7d1d313e3e41c1cc794b38eac5/plugin/NERD_tree.vim#L220
    3. Call NERDTreeFocusToggle

I don't have any experience in writing vim plugins so this might not be the best way to do it. If so, can someone guide me please?

rzvxa commented 12 months ago

This can be achieved by chaining these commands, Maybe it's not the prettiest approach, But it will do exactly what you want(If I'm not mistaken about your situation)

map <C-f> :NERDTreeVCS \| NERDTreeClose \| NERDTreeFind<CR>

WindowsTerminal_7dJNyXPAq6

Let me know if it didn't fix your problem, In that case, we can work on something else together.

Hritik14 commented 12 months ago

@rzvxa

This appears weird on my machine. :NERDTreeVCS \| NERDTreeClose \| NERDTreeFind<CR> does not work but when commands separately, it works.

:NERDTreeVCS | NERDTreeClose | NERDTreeFind asciicast

Issued separately: asciicast

rzvxa commented 12 months ago

@Hritik14 You should use | when you are entering the command directly, and escape it like this \| if you are using it in your config file(for example using as a custom binding), In addition to \| you may also use <bar>. So either do :NERDTreeVCS | NERDTreeClose | NERDTreeFind or use something like this map <C-f> :NERDTreeVCS \| NERDTreeClose \| NERDTreeFind<CR> in your .vimrc/init.vim to bind these commands to <C-f> (or your preferred short cut)

rzvxa commented 11 months ago

@Hritik14 Have you found this answer to solve your problem? If that's the case I would appreciate it if you go ahead and close your issue.

Hritik14 commented 11 months ago

@rzvxa thanks, this seems to work partially with two problems

  1. Cursor does not switch to NERDTree (https://asciinema.org/a/FHm3cAqoulmYVbZwenZJVAW7J)
  2. I cannot toggle nerdtree, need to use q to quit

I tried solving (1) by adding NERDTreeFocus but to no avail. Although, the original command, when issued from vim without hotkey focuses NERDTree (https://asciinema.org/a/ueYAkoBDOiCioTPGTI9tmv2vU)

rzvxa commented 11 months ago

@rzvxa thanks, this seems to work partially with two problems

1. Cursor does not switch to NERDTree (https://asciinema.org/a/FHm3cAqoulmYVbZwenZJVAW7J)

2. I cannot toggle nerdtree, need to use `q` to quit

I tried solving (1) by adding NERDTreeFocus but to no avail. Although, the original command, when issued from vim without hotkey focuses NERDTree (https://asciinema.org/a/ueYAkoBDOiCioTPGTI9tmv2vU)

that's weird with my setup it works correctly(both get focus and toggle with NERDTreeToggle) would you kindly provide some minimal vim config to reproduce this? try to remove as many lines as possible in your vimrc / init.vim What is your operating system and what version of Vim / Neovim do you use? Currently, I'm using Neovim as my daily driver so it may be a vim specific issue. Anything that can help me with narrowing the problem and get it to be reproducible on my machine can help(I can test on both Linux and Windows but I don't have a Mac machine so I can't really help with Macvim)

Hritik14 commented 10 months ago

@rzvxa (1) is working now. I don't know what went wrong earlier. Thanks.

Although, cannot toggle with the same key mapping (ie <C-f> above). I can use a different hotkey for that or q. thanks

rzvxa commented 10 months ago

for toggling you can use this:

map <C-f> :if g:NERDTree.IsOpen() \| NERDTreeClose \| else \| NERDTreeVCS \| NERDTreeClose \| NERDTreeFind \| endif<cr>

or you can go the extra mile and do it the "right" way:

function! s:NERDTreeFindVCSToggle() abort
  if g:NERDTree.IsOpen()
    execute("NERDTreeClose")
  else
    execute("NERDTreeVCS | NERDTreeClose | NERDTreeFind")
  endif
endfunction
command NERDTreeFindVCSToggle call s:NERDTreeFindVCSToggle()
map <C-f> :NERDTreeFindVCSToggle<cr>