Skip to content

Instantly share code, notes, and snippets.

@hotoo
Last active August 9, 2019 12:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotoo/0d15b5e32b1948c7739f to your computer and use it in GitHub Desktop.
Save hotoo/0d15b5e32b1948c7739f to your computer and use it in GitHub Desktop.
gf to open commonjs files
" http://usevim.com/2013/01/04/vim101-jumping/
function! InitJavaScript()
setl suffixesadd+=.js
setl isfname+=@-@
let node_modules = finddir('node_modules', expand('%:p:h') . ';')
exec "setl path+=". node_modules
"let project_root=findfile('package.json', expand('%:p:h') . ';')
"exec "setl path+=". fnamemodify(project_root, ':p:h') . "/node_modules"
endfunction
autocmd FileType javascript,json call InitJavaScript()
function! CommonJSGFOpen(filepath)
let filename = a:filepath
if isdirectory(filename)
let pkg_file = filename . "/package.json"
if filereadable(pkg_file)
" node_modules.
let pkg = readfile(pkg_file)
let main = matchstr(pkg, '"main" *: *"\([^"]\+\)"')
if main == ""
let main = "index.js"
else
let main = substitute(main, '.*"main" *: *"', '', '')
let main = substitute(main, '".*', '', '')
endif
let filename = filename . "/" . main
" package.json:main = xxx, without `.js`
if !filereadable(filename)
let filename = filename . '.js'
endif
if !filereadable(filename)
echoerr "E447: Can't find file \"" . filename . "\" in path"
return
endif
else
" relative file path.
let filename = filename . '/index.js'
endif
endif
exe 'e' filename
endfunction
autocmd FileType javascript,json nmap gf :call CommonJSGFOpen("<C-R><C-P>")<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment