diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..92a11b9 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +#Config helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/README.md b/README.md index 7d63374..3903add 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,13 @@ autocmd User Node \ endif ``` +#### Want to resolve modules with a custom NODE_PATH? + +```vim +let g:vim_node#node_path = [$HOME.'/project/src', '/absolute/path'] +``` + +Or you can also start vim with the `NODE_PATH` environment variable set. License ------- diff --git a/autoload/node.vim b/autoload/node.vim index 3954e4c..8b226f3 100644 --- a/autoload/node.vim +++ b/autoload/node.vim @@ -1,23 +1,25 @@ " Vim by default sets the filetype to JavaScript for the following suffices. " And, yes, it has .jsx there. -let node#suffixesadd = [".js", ".json", ".es", ".jsx"] +if !exists("node#suffixesadd") + let node#suffixesadd = [".js", ".json", ".es", ".jsx"] +endif function! node#initialize(root) let b:node_root = a:root command! -bar -bang -nargs=1 -buffer -complete=customlist,s:complete Nedit - \ exe s:nedit(, bufname("%"), "edit") + \ exe s:nedit(, expand('%:p'), "edit") command! -bar -bang -nargs=1 -buffer -complete=customlist,s:complete Nopen - \ exe s:nopen(, bufname("%"), "edit") + \ exe s:nopen(, expand('%:p'), "edit") nnoremap NodeGotoFile - \ :call edit(expand(""), bufname("%")) + \ :call edit(expand(""), expand('%:p')) nnoremap NodeSplitGotoFile - \ :call edit(expand(""), bufname("%"), "split") + \ :call edit(expand(""), expand('%:p'), "split") nnoremap NodeVSplitGotoFile - \ :call edit(expand(""), bufname("%"), "vsplit") + \ :call edit(expand(""), expand('%:p'), "vsplit") nnoremap NodeTabGotoFile - \ :call edit(expand(""), bufname("%"), "tab split") + \ :call edit(expand(""), expand('%:p'), "tab split") silent doautocmd User Node endfunction @@ -30,7 +32,7 @@ function! node#javascript() setlocal path-=/usr/include let &l:suffixesadd .= "," . join(g:node#suffixesadd, ",") let &l:include = '\= 0, +" a. if PARTS[I] = "node_modules" CONTINUE +" b. DIR = path join(PARTS[0 .. I] + "node_modules") +" c. DIRS = DIRS + DIR +" d. let I = I - 1 +" 5. return DIRS +function! s:nodeModulePaths(start) + let parts = split(a:start, '/') + + " We want to keep the leading slash of an absolute path + if a:start =~# s:ABSPATH + let parts[0] = '/' . parts[0] + endif + + let i = len(parts) - 1 + let dirs = [] + while i >= 0 + if parts[i] == 'node_modules' | continue | endif + let dir = join(parts[0:i] + ['node_modules'], '/') + let dirs += [dir] + let i = i - 1 + endwhile + + " Add support for NODE_PATH + let NODE_PATH = $NODE_PATH + if !empty(NODE_PATH) + let dirs += [NODE_PATH] + endif + + " Add support for configured NODE_PATH + if !empty(g:vim_node#node_path) + let dirs += g:vim_node#node_path + endif + + return dirs +endfunction + +function! s:getModulePath(name, from) + if a:name =~# s:ABSPATH + return a:name + elseif a:name =~# s:RELPATH + let dir = isdirectory(a:from) ? a:from : s:dirname(a:from) + return dir . "/" . a:name + endif +endfunction + +function! s:dirname(path) + return fnamemodify(a:path, ':h') +endfunction + +function! node#lib#version() + if exists("b:node_version") | return b:node_version | endif + if !executable("node") | let b:node_version = "" | return | endif + let b:node_version = matchstr(system("node --version"), '^v\?\zs[0-9.]\+') + return b:node_version endfunction function! s:mainFromPackage(path)