From 5ab170cb23d704f3581b015557d4e42412caadde Mon Sep 17 00:00:00 2001 From: hotoo Date: Thu, 21 Jul 2016 12:15:38 +0800 Subject: [PATCH] feat(dir): If isdirectory, but without index file, goto dir fixed #2 --- plugin/jsgf.vim | 30 +++++++++++++++----------- test/fixtures/lib/dir-no-index/test.js | 3 +++ test/fixtures/lib/index.js | 4 ++++ 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/lib/dir-no-index/test.js diff --git a/plugin/jsgf.vim b/plugin/jsgf.vim index 298d1e0..53ef39d 100644 --- a/plugin/jsgf.vim +++ b/plugin/jsgf.vim @@ -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) @@ -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 diff --git a/test/fixtures/lib/dir-no-index/test.js b/test/fixtures/lib/dir-no-index/test.js new file mode 100644 index 0000000..63c6a62 --- /dev/null +++ b/test/fixtures/lib/dir-no-index/test.js @@ -0,0 +1,3 @@ +// lib/dir-no-index/test.js +// +// diff --git a/test/fixtures/lib/index.js b/test/fixtures/lib/index.js index 349ebb1..43ab04f 100644 --- a/test/fixtures/lib/index.js +++ b/test/fixtures/lib/index.js @@ -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');