Skip to content

Commit

Permalink
Merge pull request #6 from hotoo/feat/dir
Browse files Browse the repository at this point in the history
feat(dir): If isdirectory, but without index file, goto dir
  • Loading branch information
hotoo authored Jul 21, 2016
2 parents 30ec243 + 5ab170c commit 052fba3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
30 changes: 18 additions & 12 deletions plugin/jsgf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ function! InitJSGF()
setlocal isfname+=@-@
" setlocal includeexpr=v:fname.'/index'
let node_modules = finddir('node_modules', expand('%:p:h') . ';')
execute "setlocal path+=" . node_modules
execute 'setlocal path+=' . node_modules
" setlocal path+=node_modules
" let project_root=findfile('package.json', expand('%:p:h') . ';')
" execute "setlocal path+=". fnamemodify(project_root, ':p:h') . "/node_modules"
" execute 'setlocal path+=' . fnamemodify(project_root, ':p:h') . '/node_modules'
endfunction

function! FindFileOrDir(filename)
Expand All @@ -31,42 +31,48 @@ function! FindFileOrDir(filename)
return fname
endif
endfor
return a:filename
return ''
endfunction

function! JSGF(filepath)
let filename = a:filepath
if isdirectory(filename)
let pkg_file = filename . "/package.json"
let pkg_file = filename . '/package.json'

if filereadable(pkg_file)

" node_modules.
let pkg = readfile(pkg_file)
let main = matchstr(pkg, '"main" *: *"\([^"]\+\)"')
if main == ""
if main == ''
" Not set `main` in package.json
let main = "index"
let main = 'index'
else
let main = substitute(main, '.*"main" *: *"', '', '')
let main = substitute(main, '".*', '', '')
endif
let filename = filename . "/" . main
let filename = filename . '/' . main

else

if (FindFileOrDir(filename) == filename)
if (FindFileOrDir(filename) == '')
" relative file path.
let filename = filename . '/index.js'
endif

endif
endif

let filename = FindFileOrDir(filename)
let fname = FindFileOrDir(filename)
if (fname == '')
let filename = a:filepath
else
let filename = fname
endif

endif

if !filereadable(filename)
echoerr "E447: Can't find file \"" . filename . "\" in path [jsgf.vim]."
if !filereadable(filename) && !isdirectory(filename)
echoerr 'E447: Can not find file "' . filename . '" in path [jsgf.vim].'
return
endif

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/lib/dir-no-index/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// lib/dir-no-index/test.js
//
// <Ctrl-o>
4 changes: 4 additions & 0 deletions test/fixtures/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ require('./index/');
require('./index/index');
require('./index/index.js');

require('./dir-no-index/');
require('./dir-no-index/test');
require('./dir-no-index/test.js');

require('./js');
require('./js.js');
require('./jsx');
Expand Down

0 comments on commit 052fba3

Please sign in to comment.