diff --git a/autoload/node.vim b/autoload/node.vim index 3954e4c..df870a3 100644 --- a/autoload/node.vim +++ b/autoload/node.vim @@ -1,6 +1,6 @@ " 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"] +let node#suffixesadd = [".js", ".json", ".es", ".jsx", ".ios.js", ".android.js", ".ios.jsx", ".android.jsx"] function! node#initialize(root) let b:node_root = a:root diff --git a/autoload/node/lib.vim b/autoload/node/lib.vim index 239b864..d4fab3e 100644 --- a/autoload/node/lib.vim +++ b/autoload/node/lib.vim @@ -33,11 +33,16 @@ function! node#lib#version() endfunction function! s:absolutize(name, from) + let PACKAGENAME = s:nameFromPackage(b:node_root . "/package.json") if a:name =~# s:ABSPATH return a:name elseif a:name =~# s:RELPATH let dir = isdirectory(a:from) ? a:from : fnamemodify(a:from, ":h") return dir . "/" . a:name + elseif a:name =~# "^" . PACKAGENAME && !empty(PACKAGENAME) + let l:slashPos = match(a:name, "/") + let finalPath = a:name[l:slashPos :] + return b:node_root . finalPath else return b:node_root . "/node_modules/" . a:name endif @@ -86,6 +91,17 @@ function! s:mainFromPackage(path) endfor endfunction +function! s:nameFromPackage(path) + if !filereadable(a:path) + return + endif + for line in readfile(a:path) + if line !~# '"name"\s*:' | continue | endif + return matchstr(line, '"name"\s*:\s*"\zs[^"]\+\ze"') + endfor + return "" +endfunction + function! s:resolveSuffix(path) for suffix in s:uniq([""] + g:node#suffixesadd + split(&l:suffixesadd, ",")) let path = a:path . suffix @@ -93,6 +109,23 @@ function! s:resolveSuffix(path) endfor endfunction +function! s:resolveReactNativeGlobal(path) + " React-Native allow to use absolute path using the name of a package.json + " https://medium.com/@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1 + " Here is implemented only with the main one + if filereadable(b:node_root . "/package.json") + let name = s:nameFromPackage(b:node_root . "/package.json") + + let slashPos = match(a:path, "/") + let prefix = a:path[0: slashPos] + let finalPath = a:path[slashPos:] + + if (prefix == name) + return s:resolve(b:node_root . finalPath) + endif + endif +endfunction + let s:GLOB_WILDIGNORE = 1 function! node#lib#glob(name) diff --git a/test/autoload_test.rb b/test/autoload_test.rb index 464d803..4561b97 100644 --- a/test/autoload_test.rb +++ b/test/autoload_test.rb @@ -253,6 +253,84 @@ $vim.feedkeys "$hhgf" $vim.echo(%(bufname("%"))).must_equal target end + + it "must edit ./other.android.js relative to file" do + touch File.join(@dir, "foo", "index.js"), %(require("./other")) + touch File.join(@dir, "foo", "other.android.js") + + $vim.edit File.join(@dir, "foo", "index.js") + $vim.feedkeys "f.gf" + + bufname = File.realpath($vim.echo(%(bufname("%")))) + bufname.must_equal File.join(@dir, "foo", "other.android.js") + end + + it "must edit ./other.ios.js relative to file" do + touch File.join(@dir, "foo", "index.js"), %(require("./other")) + touch File.join(@dir, "foo", "other.ios.js") + + $vim.edit File.join(@dir, "foo", "index.js") + $vim.feedkeys "f.gf" + + bufname = File.realpath($vim.echo(%(bufname("%")))) + bufname.must_equal File.join(@dir, "foo", "other.ios.js") + end + + it "must edit ./other.ios.js relative to file if both android and ios exist" do + touch File.join(@dir, "foo", "index.js"), %(require("./other")) + touch File.join(@dir, "foo", "other.ios.js") + touch File.join(@dir, "foo", "other.android.js") + + $vim.edit File.join(@dir, "foo", "index.js") + $vim.feedkeys "f.gf" + + bufname = File.realpath($vim.echo(%(bufname("%")))) + bufname.must_equal File.join(@dir, "foo", "other.ios.js") + end + + it "must edit ./node_modules/foo/index.android.js given foo" do + touch File.join(@dir, "index.js"), %(require("foo")) + target = touch File.join(@dir, "node_modules", "foo", "index.android.js") + + $vim.edit File.join(@dir, "index.js") + $vim.feedkeys "$hhgf" + $vim.echo(%(bufname("%"))).must_equal target + end + + it "must edit ./bar.js given packagejsonname/bar" do + touch File.join(@dir, "foo", "index.js"), %(require("packagejsonname/bar")) + touch File.join(@dir, "package.json"), %({ "name": "packagejsonname" }) + touch File.join(@dir, "bar.js") + + $vim.edit File.join(@dir, "foo", "index.js") + $vim.feedkeys "$hhgf" + + bufname = File.realpath($vim.echo(%(bufname("%")))) + bufname.must_equal File.join(@dir, "bar.js") + end + + it "must edit ./node_modules/foo/bar.js given foo/bar" do + touch File.join(@dir, "index.js"), %(require("foo/bar")) + touch File.join(@dir, "package.json"), %({ "name": "packagejsonname" }) + target = touch File.join(@dir, "node_modules", "foo", "bar.js") + + $vim.edit File.join(@dir, "index.js") + $vim.feedkeys "$hhgf" + $vim.echo(%(bufname("%"))).must_equal target + end + + it "must edit ./bar.js given packagejsonname/bar event if module exists" do + touch File.join(@dir, "foo", "index.js"), %(require("packagejsonname/bar")) + touch File.join(@dir, "package.json"), %({ "name": "packagejsonname" }) + target = touch File.join(@dir, "node_modules", "packagejsonname", "bar.js") + touch File.join(@dir, "bar.js") + + $vim.edit File.join(@dir, "foo", "index.js") + $vim.feedkeys "$hhgf" + + bufname = File.realpath($vim.echo(%(bufname("%")))) + bufname.must_equal File.join(@dir, "bar.js") + end end describe "Goto file with split" do