Skip to content

Commit

Permalink
Add suport for react-native
Browse files Browse the repository at this point in the history
  • Loading branch information
Nhacsam committed May 20, 2017
1 parent 13b3121 commit e105a60
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion autoload/node.vim
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 33 additions & 0 deletions autoload/node/lib.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,13 +91,41 @@ 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
if filereadable(path) | return path | endif
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)
Expand Down
78 changes: 78 additions & 0 deletions test/autoload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e105a60

Please sign in to comment.