neoclide / coc-tsserver

Tsserver extension for coc.nvim that provide rich features like VSCode for javascript & typescript
MIT License
1.05k stars 68 forks source link

yarn2 pnp goto definition support #244

Open svrana opened 3 years ago

svrana commented 3 years ago

When using a yarn v2 project that uses yarn plug-in-play, it would be nice if coc-definition would jump into the archive that holds the source definition. In pnp, yarn stores project dependencies as zip files which appears to complicate things.

Oddly depending on how I attempt to jump, I get two different behaviors. If I attempt to jump to definition with ctrl-] (with set tagfunc=CocTagFunc), I get E429: File "/zip:/home/shaw/Projects/foo/.yarn/cache/@types-react-npm-17.0.0-d4f5adb6f0-dcef2034b8.zip::node_modules/@types/react/global.d.ts does not exist. However, if I run coc-definition, the screen begins to flashes/flickers until I exit neovim.

This was initially opened against here but was told this was a language server issue.

vim version: NVIM v0.5.0-dev (4ed5204bc9d58) node version: v12.18.4 coc.nvim version: 0.0.80-7642d233d6

chemzqm commented 3 years ago

Need some help for open zip file in vim.

svrana commented 3 years ago

Neovim is able to open up that file if instead I change the filename to be:

zipfile:/home/shaw/Projects/foo/.yarn/cache/@types-react-npm-17.0.0-d4f5adb6f0-dcef2034b8.zip::node_modules/@types/react/global.d.ts

i.e., nvim zipfile:/home/shaw/Projects/foo/.yarn/cache/@types-react-npm-17.0.0-d4f5adb6f0-dcef2034b8.zip::node_modules/@types/react/global.d.ts works as you would hope; opens the archive and I can view the contents of global.d.ts.

If I just nvim on the zip file, neovim says that it is using zip.vim to view the archive. Presumably that plugin ships with neovim as I did not install it.

So, the path coc is trying to open is close, but :/zip should be zipfile, and there should be a :: instead of / after the .zip. Course not sure if that method will work within coc.

BenElgar commented 3 years ago

I had success with adding the following to my Neovim config:

function! OpenZippedFile(f)
  " get number of new (empty) buffer
  let l:b = bufnr('%')
  " construct full path
  let l:f = substitute(a:f, '.zip/', '.zip::', '')
  let l:f = substitute(l:f, '/zip:', 'zipfile:', '')

  " swap back to original buffer
  b #
  " delete new one
  exe 'bd! ' . l:b
  " open buffer with correct path
  sil exe 'e ' . l:f
  " read in zip data
  call zip#Read(l:f, 1)
endfunction

au BufReadCmd /zip:*.yarn/cache/*.zip/* call OpenZippedFile(expand('<afile>'))

This is a slightly modified version of this: https://www.reddit.com/r/vim/comments/euxaml/is_it_possible_to_open_files_in_a_zip_directly/fftbf2u/?context=3

tapayne88 commented 3 years ago

@BenElgar thanks for the above, really helpful!

I find though I still have instances of the bad buffer names in my buffer list

:ls                                                                                                            
  1 #a   "src/index.tsx"                line 1                                                                 
  3 %a   "zipfile:/home/tpayne/git/relay-star-wars/.yarn/cache/@types-react-relay-npm-7.0.8-1010fbc610-541a56cc
eb.zip::node_modules/@types/react-relay/lib/relay-experimental/preloadQuery.d.ts" line 4                       
  6  h   "/^zipfile:/home/tpayne/git/relay-star-wars/.yarn/cache/@types-react-relay-npm-7.0.8-1010fbc610-541a56
cceb.zip::node_modules/@types/react-relay/lib/relay-experimental/preloadQuery.d.ts" line 0

Even with a manual :bwipe [bad buffer name] the buffer reappears only with a new buffer index. Is this something you see?

BenElgar commented 3 years ago

Yes, I've experienced that. I'm afraid I'm not quite sure why and I haven't spent the time to figure it out. 🙃

cometkim commented 3 years ago

https://github.com/yarnpkg/berry/pull/2598 is merged into upstream. go to definition will works with @BenElgar's snippet or vim-rzip plugin in next release.

geoyws commented 3 years ago

Currently I can go-to-definition once but when I go-to-definition again in the same file it will lead me to an empty buffer. Anyone else getting this? I'm using nvim and vim-rzip.

clhuang commented 2 years ago

With the change to neovim expecting zipfile://{filename} instead of zipfile:{filename}, go to definition has stopped working for me completely. I wrote this PR in an attempt to make it work again: https://github.com/neoclide/coc-tsserver/pull/360

jackielii commented 1 year ago

Maybe unrelated, I'm exploring grafana dev and did yarn dlx @yarnpkg/sdks vim. But all the external imports are not resolving. And tsserver log shows

Using tsserver from: $HOME/.config/coc/extensions/node_modules/coc-tsserver/node_modules/typescript/lib

However, after adding "tsserver.useLocalTsdk": true, to .vim/coc-settings.json then it all worked