gabebw/vim-github-link-opener

Support opening Node.js require('package') using npm repo

jdsutherland opened this issue · 1 comments

I'm ambivalent as to whether this is an appropriate fit for this plugin but it seems similar enough that I thought I'd mention it. If there are similar ways of opening the docs/repo page from the various ecosystem package managers then it might make sense to make a separate plugin around that, but this is the only plugin I'm aware of that extends gx with the idea related to seeing something url-like that would otherwise require manually reaching for a browser.

Support for opening Node.js package github pages might fairly easily be added using npm repo. When I come across an unfamiliar library, it's quicker to simply press gx from vim rather rather than searching google or typing the npm command.

As I understand it, in node, require statements are either:

  • file modules (starts with '.' or '/')
  • core modules (std library, e.g. require('fs'))
  • node_modules (3rd party, e.g. require("jsonwebtoken"))

For example, gx positioned inside the quotes require("jsonwebtoken") would open https://github.com/auth0/node-jsonwebtoken.

It'd be possible to press gx and attempt to open a non 3rd party package which resulting in garbage (npm would error or it would open the squatted core name rather than official docs) but it seems like it might be worth being pragmatic by trusting the user is sane.

My knowledge of vimscript is pretty low but the following seems to work for me:

  elseif &ft ==# 'javascript' && word =~# 'require\(.*\)'
    execute "!npm repo " . expand("<cword>")

It'd probably be better to parse the word inside the quotes from the word variable (rather than using <cword>) to make the cursor position less stringent - I fumbled around with this for a few minutes and gave up (I suspect vim has some quirky regex syntax) but surely this is trivial using a regex group.

Edit: given that the future is import, it's probably better not to limit this to require.

Also, since the README mentions gem "github/linguist" from a Gemfile, it might be sensible to allow this from Package.json as well.

Thoughts?

Hi Jeff, thanks for submitting this issue!

Yes, this would be an excellent addition. Can you open a PR with your suggested solution?