Skip to content

Commit dec30fe

Browse files
committed
Fixed relative path resolution + added better error messages
1 parent b20be68 commit dec30fe

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

plugin/vim-npr.vim

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1+
" Vim-NPR
12
" Max number of directory levels gf will traverse upwards
23
" to find a package.json file.
34
let g:vim_npr_max_levels = 5
45

56
" Default file names to try if gf is run on a directory rather than a specific file.
67
" Checked in order of appearance. Empty string to check for exact file match first.
7-
let g:vim_npr_file_names = ["", ".js", "/index.js"]
8+
" The final two are specifically for matching libraries which define their UMD
9+
" module resolution in their package.json, and these are the most common.
10+
let g:vim_npr_file_names = ["", ".js", "/index.js", "/index.jsx", "/src/index.js", "/lib/index.js"]
811

912
" A list of file extensions that the plugin will actively work on.
1013
let g:vim_npr_file_types = ["js", "jsx", "css", "coffee"]
1114

1215
" Default resolution directories if 'resolve' key is not found in package.json.
1316
let g:vim_npr_default_dirs = ["src", "lib", "test", "public", "node_modules"]
1417

15-
function! VimNPRFindFile(fname) abort
18+
function! VimNPRFindFile(cmd) abort
19+
" Get file path pattern under cursor
20+
let l:cfile = expand("<cfile>")
21+
22+
" Iterate over potential directories and search for the file
23+
for filename in g:vim_npr_file_names
24+
let l:possiblePath = expand("%:p:h") . '/' . l:cfile . filename
25+
26+
if filereadable(l:possiblePath)
27+
return s:edit_file(l:possiblePath, a:cmd)
28+
endif
29+
endfor
30+
1631
if index(g:vim_npr_file_types, expand("%:e")) == -1
17-
return '' " Don't run on filetypes that we don't support
32+
return s:print_error("(Error) VimNPR: incorrect file type for to perform resolution within.") " Don't run on filetypes that we don't support
1833
endif
1934

2035
let l:foundPackage = 0
@@ -27,8 +42,7 @@ function! VimNPRFindFile(fname) abort
2742
endwhile
2843

2944
if l:foundPackage == 0
30-
echo "Failed to find package.json, try increasing the levels by increasing g:vim_npr_max_levels variable."
31-
return a:fname
45+
return s:print_error("(Error) VimNPR: Failed to find package.json, try increasing the levels by increasing g:vim_npr_max_levels variable.")
3246
endif
3347

3448
" Handy paths to package.json and parent dir
@@ -44,19 +58,28 @@ function! VimNPRFindFile(fname) abort
4458

4559
" Iterate over potential directories and search for the file
4660
for dir in l:resolveDirs
47-
let l:possiblePath = l:packageDir . "/" . dir . "/" . a:fname
48-
49-
echo l:possiblePath
61+
let l:possiblePath = l:packageDir . "/" . dir . "/" . l:cfile
5062

5163
for filename in g:vim_npr_file_names
5264
if filereadable(possiblePath . filename)
53-
return possiblePath . filename
65+
return s:edit_file(possiblePath . filename, a:cmd)
5466
endif
5567
endfor
5668
endfor
5769

58-
echo "VimNPR failed to find file specified."
59-
return a:fname
70+
" Nothing found, print resolution error
71+
return s:print_error("(Error) VimNPR: Failed to sensibly resolve file in path. If you believe this to be an error, please log an error at github.com/tomarrell/vim-npr.")
72+
endfunction
73+
74+
function! s:edit_file(path, cmd)
75+
exe "edit" . a:cmd . " " . a:path
76+
endfunction
77+
78+
function! s:print_error(error)
79+
echohl ErrorMsg
80+
echomsg a:error
81+
echohl NONE
82+
let v:errmsg = a:msg
6083
endfunction
6184

6285
" Unmap any user mapped gf functionalities. This is to restore gf
@@ -65,10 +88,5 @@ autocmd FileType javascript silent! unmap <buffer> gf
6588
autocmd FileType javascript silent! unmap <buffer> <C-w>f
6689
autocmd FileType javascript silent! unmap <buffer> <C-w><C-f>
6790

68-
" Override includeexpr for Javascript buffer.
69-
" By default vim-node will try to take control.
70-
"
71-
" au[tocmd] [group] {event} {pat} [nested] {cmd}
72-
autocmd BufEnter *.js,*.jsx,*.css,*.coffee set includeexpr=VimNPRFindFile(v:fname)
73-
" Clean up after leaving buffer, allows other plugins to take control of include
74-
autocmd BufLeave *.js,*.jsx,*.css,*.coffee set includeexpr=
91+
" Automap gf when entering JS/css file types
92+
autocmd BufEnter *.js,*.jsx,*.css,*.coffee nmap <buffer> gf :call VimNPRFindFile("")<CR>

0 commit comments

Comments
 (0)