1
0
Fork 0
mirror of synced 2024-11-29 20:25:34 -05:00

add vim-node

This commit is contained in:
gooooloo 2014-06-13 21:52:19 +08:00
parent 809f06d938
commit 65f454e78b
17 changed files with 1671 additions and 0 deletions

View file

@ -0,0 +1,44 @@
## 0.8.1 (Apr 15, 2014)
- Updates the URL from which Node.vim downloads Node core module source files.
Uses <http://rawgit.com> which used to be named <http://rawgithub.com>.
Because of Vim Netrw's inability to handle HTTPS, it does so over HTTP. Sorry.
## 0.8.0 (Sep 6, 2013)
- Adds `node` as a core module so you could use `:Nedit node` to open the file Node uses to bootstrap its core.
## 0.7.0 (Aug 28, 2013)
- Adds support for opening core Node.js modules, such as `http`, `util`, etc. with `gf` or `:Nedit`.
They're shown straight from Node's online repository without you having to download everything.
## 0.6.0 (Aug 23, 2013)
- Adds `:Nedit` command for editing modules or files relative to the Node project root.
For example: `:Nedit any-module/lib` or `:Nedit ./package`.
- Adds `:Nopen` command which behaves like `:Nedit`, but also `lcd`s to the module's directory.
- Makes `<Plug>NodeGotoFile` available for your mapping in any Node project file, but maps it to `gf` automatically only on JavaScript files.
- Maps `gf` also for JSON files for easy jumping to modules.
- Makes `:Nedit` and `:Nopen` available immediately when starting Vim in a directory of a Node project.
## 0.5.1 (Aug 8, 2013)
- Adds `Node` autocommand.
Use it with `autocmd User Node` to customize settings for files in Node projects.
- Adds `<Plug>NodeVSplitGotoFile` for those who want `<C-w>f` to split vertically.
## 0.5.0 (Aug 5, 2013)
- Adds `&include` pattern so Vim can recognize included/required files, e.g. for looking up keywords with `[I`.
- Cleans `&path` from `/usr/include` for JavaScript files.
- Adds a new superb `gf` handler to handle all relative and module paths, incl. support for `require(".")` to open `./index.js`. This is spot on how Node.js finds your requires.
- Adds `<Plug>NodeGotoFile` should you want to remap Node.vim's file opener.
- Opens files before directories should both, e.g. `./foo.js` and `./foo`, exist. This matches Node.js's behavior.
- Adds a full automated integration test suite to Node.vim which is freaking amazing!
## 0.2.0 (Jul 28, 2013)
- Adds full support for navigating to module files by using `gf` on `require("any-module")`.
- Adds `.json` to `&suffixesadd` so you could use `gf` on `require("./package")` to open package.json.
## 0.1.1 (Jul 28, 2013)
- Removes an innocent but forgotten debugging line.
## 0.1.0 (Jul 28, 2013)
- First release to get the nodeballs rolling.
- Sets the filetype to JavaScript for files with Node's shebang (`#!`).
- Adds `.js` to `&suffixesadd` so you could use `gf` on `require("./foo")` to open `foo.js`.

View file

@ -0,0 +1,11 @@
source "https://rubygems.org"
group :development do
gem "guard-minitest"
gem "minitest-reporters"
end
group :test do
gem "minitest", "< 5"
gem "vimrunner", :git => "https://github.com/moll/vimrunner.git"
end

View file

@ -0,0 +1,59 @@
GIT
remote: https://github.com/moll/vimrunner.git
revision: 4b1ee072e52dea871a0c3370f215f54fe596a9dc
specs:
vimrunner (0.3.0)
GEM
remote: https://rubygems.org/
specs:
ansi (1.4.3)
builder (3.2.2)
coderay (1.0.9)
ffi (1.9.0)
formatador (0.2.4)
guard (1.8.2)
formatador (>= 0.2.4)
listen (>= 1.0.0)
lumberjack (>= 1.0.2)
pry (>= 0.9.10)
thor (>= 0.14.6)
guard-minitest (1.0.1)
guard (>= 1.8)
minitest (>= 2.1)
hashie (2.0.5)
listen (1.2.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
rb-kqueue (>= 0.2)
lumberjack (1.0.4)
method_source (0.8.2)
minitest (4.7.5)
minitest-reporters (0.14.20)
ansi
builder
minitest (>= 2.12, < 5.0)
powerbar
powerbar (1.0.11)
ansi (~> 1.4.0)
hashie (>= 1.1.0)
pry (0.9.12.2)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
rb-fsevent (0.9.3)
rb-inotify (0.9.0)
ffi (>= 0.5.0)
rb-kqueue (0.2.0)
ffi (>= 0.5.0)
slop (3.4.6)
thor (0.18.1)
PLATFORMS
ruby
DEPENDENCIES
guard-minitest
minitest (< 5)
minitest-reporters
vimrunner!

View file

@ -0,0 +1,8 @@
guard :minitest, :all_on_start => false, :cli => ENV["TEST_OPTS"] do
watch(%r(^(.*)\.vim$)) {|m| "test/#{m[1]}_test.rb" }
watch(%r(^([^/]+)/[^/]+\.vim$)) {|m| "test/#{m[1]}_test.rb" }
watch(%r(^([^/]+)/[^/]+/(.*)\.vim$)) {|m| "test/#{m[1]}/#{m[2]}_test.rb" }
watch(%r(^test/(.*)\/?_test\.rb))
watch(%r(^test/helper\.rb)) { "test" }
end

View file

@ -0,0 +1,20 @@
Node.vim
Copyright (C) 2013 Andri Möll
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or any later version.
Additional permission under the GNU Affero GPL version 3 section 7:
If you modify this Program, or any covered work, by linking or
combining it with other code, such other code is not for that reason
alone subject to any of the requirements of the GNU Affero GPL version 3.
In summary:
- You can use this program for no cost.
- You can use this program for both personal and commercial reasons.
- You do not have to share your own program's code which uses this program.
- You have to share modifications (e.g bug-fixes) you've made to this program.
For the full copy of the GNU Affero General Public License see:
http://www.gnu.org/licenses.

View file

@ -0,0 +1,40 @@
NAME = node
TITLE = Node.vim
VERSION = 0.8.1
ID = 4674
TEST_OPTS =
love:
@echo "Feel like makin' love."
test: spec
autotest: autospec
spec: $(shell find . -name "*_test.rb")
@ruby -rbundler/setup $(addprefix -r./,$^) -e "" -- $(TEST_OPTS)
autospec:
@bundle exec guard start --no-interactions
pack:
rm -rf "$(NAME)-$(VERSION).zip"
zip -r "$(NAME)-$(VERSION).zip" * -x @.packignore
publish:
open "http://www.vim.org/scripts/add_script_version.php?script_id=$(ID)"
tag:
git tag "v$(VERSION)"
node.tar.gz:
wget -c "https://github.com/joyent/node/archive/master.tar.gz" -O node.tar.gz
list-core-modules: node.tar.gz
tar tf node.tar.gz |\
egrep "^node[^/]*/lib/.+" |\
xargs -n1 basename -s .js |\
{ cat; echo node; } | sort
.PHONY: love
.PHONY: spec autospec
.PHONY: pack publish tag

View file

@ -0,0 +1,104 @@
Node.vim
========
[![Build status](https://travis-ci.org/moll/vim-node.png?branch=master)](https://travis-ci.org/moll/vim-node)
Tools to make Vim superb for developing with Node.js.
It's the Node equivalent of [Rails.vim (vimscript #1567)](https://github.com/tpope/vim-rails) and [Rake.vim (vimscript #3669)](https://github.com/tpope/vim-rake).
This is just the first release to get the nodes rolling. If you've collected great helpers and shortcuts that help you work with Node, please share them via [email](mailto:andri@dot.ee), [Twitter](https://twitter.com/theml) or [GitHub issues](https://github.com/moll/vim-node/issues) so we could incorporate them here, too! Thanks!
### Tour
- Use `gf` on paths or requires to open the same file Node.js would.
- Use `gf` on `require(".")` to open `./index.js`
- Use `gf` on `require("./dir")` to open `./dir/index.js`
- Use `gf` on `require("./foo")` to open `foo.js`.
- Use `gf` on `require("./package")` and have it open package.json.
- Use `gf` on `require("module")` to open the module's main file (parsed for you from `package.json`).
- Use `gf` on `require("module/lib/utils")` and open files inside the module.
- Automatically sets the filetype to JavaScript for files with Node's shebang (`#!`).
- Use `[I` etc. to look for a keyword in required files (Sets Vim's `&include`).
- Use `:Nedit` to quickly edit any module, file in a module or your project file.
- Use `:Nopen` to quickly edit any module and `lcd` to its directory.
- Lets you even open Node's core modules. They're shown straight from Node's online repository without you having to download everything.
- Node.vim itself is tested with a thorough automated integration test suite! No cowboy coding here!
Expect more to come soon and feel free to let me know what you're after!
PS. Node.vim is absolutely intended to work on Windows, but not yet tested there at all. If you could help, try it out and report issues, I'd be grateful!
Installing
----------
The easiest and most modular way is to download this to `~/.vim/bundle`:
```
mkdir -p ~/.vim/bundle/node
```
Using Git:
```
git clone https://github.com/moll/vim-node.git ~/.vim/bundle/node
```
Using Wget:
```
wget https://github.com/moll/vim-node/archive/master.tar.gz -O- | tar -xf- --strip-components 1 -C ~/.vim/bundle/node
```
Then prepend that directory to Vim's `&runtimepath` (or use [Pathogen](https://github.com/tpope/vim-pathogen)):
```
:set runtimepath^=~/.vim/bundle/node
```
### Vundle
Or use [Vundle](https://github.com/gmarik/Vundle.vim):
```
:BundleInstall moll/vim-node
```
Using
-----
Open any JavaScript file inside a Node project and you're all set.
- Use `gf` inside `require("...")` to jump to source and module files.
- Use `[I` on any keyword to look for it in the current and required files.
- Use `:Nedit module_name` to edit the main file of a module.
- Use `:Nedit module_name/lib/foo` to edit its `lib/foo.js` file.
- Use `:Nedit .` to edit your Node projects main (usually `index.js`) file.
#### Want to customize settings for files inside a Node projects?
Use the `Node` autocommand. For example:
```vim
autocmd User Node if &filetype == "javascript" | setlocal expandtab | endif
```
#### Want `<C-w>f` to open the file under the cursor in a new vertical split?
`<C-w>f` by default opens it in a horizontal split. To have it open vertically, drop this in your `vimrc`:
```vim
autocmd User Node
\ if &filetype == "javascript" |
\ nmap <buffer> <C-w>f <Plug>NodeVSplitGotoFile |
\ nmap <buffer> <C-w><C-f> <Plug>NodeVSplitGotoFile |
\ endif
```
License
-------
Node.vim is released under a *Lesser GNU Affero General Public License*, which in summary means:
- You **can** use this program for **no cost**.
- You **can** use this program for **both personal and commercial reasons**.
- You **do not have to share your own program's code** which uses this program.
- You **have to share modifications** (e.g bug-fixes) you've made to this program.
For more convoluted language, see the `LICENSE` file.
About
-----
**[Andri Möll](http://themoll.com)** authored this in SublemacslipseMate++.
[Monday Calendar](https://mondayapp.com) supported the engineering work.
If you find Node.vim needs improving or you've got a question, please don't hesitate to email me anytime at [andri@dot.ee](mailto:andri@dot.ee), tweet at [@theml](https://twitter.com/theml) or [create an issue online](https://github.com/moll/vim-node/issues).

View file

@ -0,0 +1,104 @@
let node#suffixesadd = [".js", ".json"]
let node#filetypes = ["javascript", "json"]
function! node#initialize(root)
let b:node_root = a:root
call s:initializeCommands()
if index(g:node#filetypes, &ft) > -1 | call s:initializeJavaScript() | en
silent doautocmd User Node
endfunction
function! s:initializeCommands()
command! -bar -bang -nargs=1 -buffer -complete=customlist,s:complete Nedit
\ exe s:nedit(<q-args>, bufname("%"), "edit<bang>")
command! -bar -bang -nargs=1 -buffer -complete=customlist,s:complete Nopen
\ exe s:nopen(<q-args>, bufname("%"), "edit<bang>")
nnoremap <buffer><silent> <Plug>NodeGotoFile
\ :call <SID>edit(expand("<cfile>"), bufname("%"))<CR>
nnoremap <buffer><silent> <Plug>NodeSplitGotoFile
\ :call <SID>edit(expand("<cfile>"), bufname("%"), "split")<CR>
nnoremap <buffer><silent> <Plug>NodeVSplitGotoFile
\ :call <SID>edit(expand("<cfile>"), bufname("%"), "vsplit")<CR>
nnoremap <buffer><silent> <Plug>NodeTabGotoFile
\ :call <SID>edit(expand("<cfile>"), bufname("%"), "tab split")<CR>
endfunction
function! s:initializeJavaScript()
setl path-=/usr/include
let &l:suffixesadd .= "," . join(g:node#suffixesadd, ",")
let &l:include = '\<require(\(["'']\)\zs[^\1]\+\ze\1'
let &l:includeexpr = "node#lib#find(v:fname, bufname('%'))"
if !hasmapto("<Plug>NodeGotoFile")
" Split gotofiles don't take a count for the new window's width, but for
" opening the nth file. Though Node.vim doesn't support counts atm.
nmap <buffer> gf <Plug>NodeGotoFile
nmap <buffer> <C-w>f <Plug>NodeSplitGotoFile
nmap <buffer> <C-w><C-f> <Plug>NodeSplitGotoFile
nmap <buffer> <C-w>gf <Plug>NodeTabGotoFile
endif
endfunction
function! s:edit(name, from, ...)
if empty(a:name) | return | endif
let dir = isdirectory(a:from) ? a:from : fnamemodify(a:from, ":h")
let command = a:0 == 1 ? a:1 : "edit"
" If just a plain filename with no directory part, check if it exists:
if a:name !~# '^\v(/|\./|\.\./)' && filereadable(dir . "/" . a:name)
let path = dir . "/" . a:name
else
let path = node#lib#find(a:name, dir)
end
if empty(path)
return s:error("E447: Can't find file \"" . a:name . "\" in path")
endif
exe command . " " . fnameescape(path)
endfunction
function! s:nedit(name, from, ...)
let command = a:0 == 1 ? a:1 : "edit"
call s:edit(a:name, b:node_root, command)
endfunction
function! s:nopen(name, from, ...)
let command = a:0 == 1 ? a:1 : "edit"
call s:nedit(a:name, a:from, command)
if exists("b:node_root") | exe "lcd " . fnameescape(b:node_root) | endif
endfunction
function! s:complete(arg, cmd, cursor)
let matches = node#lib#glob(s:dirname(a:arg))
" Show private modules (_*) only if explicitly asked:
if a:arg[0] != "_" | call filter(matches, "v:val[0] != '_'") | endif
let filter = "stridx(v:val, a:arg) == 0"
let ignorecase = 0
let ignorecase = ignorecase || exists("&fileignorecase") && &fileignorecase
let ignorecase = ignorecase || exists("&wildignorecase") && &wildignorecase
if ignorecase | let filter = "stridx(tolower(v:val),tolower(a:arg)) == 0" | en
return filter(matches, filter)
endfunction
function! s:dirname(path)
let dirname = fnamemodify(a:path, ":h")
if dirname == "." | return "" | endif
" To not change the amount of final consecutive slashes, using this
" dirname/basename trick:
let basename = fnamemodify(a:path, ":t")
return a:path[0 : 0 - len(basename) - 1]
endfunction
" Using the built-in :echoerr prints a stacktrace, which isn't that nice.
function! s:error(msg)
echohl ErrorMsg
echomsg a:msg
echohl NONE
let v:errmsg = a:msg
endfunction

View file

@ -0,0 +1,151 @@
let s:ABSPATH = '^/'
let s:RELPATH = '\v^\.\.?(/|$)'
let s:MODULE = '\v^(/|\.\.?(/|$))@!'
" Damn Netrw can't handle HTTPS at all. It's 2013! Insecure bastard!
let s:CORE_URL_PREFIX = "http://rawgit.com/joyent/node"
let s:CORE_MODULES = ["_debugger", "_http_agent", "_http_client",
\ "_http_common", "_http_incoming", "_http_outgoing", "_http_server",
\ "_linklist", "_stream_duplex", "_stream_passthrough", "_stream_readable",
\ "_stream_transform", "_stream_writable", "_tls_legacy", "_tls_wrap",
\ "assert", "buffer", "child_process", "cluster", "console", "constants",
\ "crypto", "dgram", "dns", "domain", "events", "freelist", "fs", "http",
\ "https", "module", "net", "node", "os", "path", "punycode", "querystring",
\ "readline", "repl", "smalloc", "stream", "string_decoder", "sys",
\ "timers", "tls", "tty", "url", "util", "vm", "zlib"]
function! node#lib#find(name, from)
if index(s:CORE_MODULES, a:name) != -1
let l:version = node#lib#version()
let l:version = empty(l:version) ? "master" : "v" . l:version
let l:dir = a:name == "node" ? "src" : "lib"
return s:CORE_URL_PREFIX ."/". l:version ."/". l:dir ."/". a:name .".js"
endif
return s:resolve(s:absolutize(a:name, a:from))
endfunction
function! node#lib#version()
if exists("b:node_version") | return b:node_version | endif
if !executable("node") | let b:node_version = "" | return | endif
let b:node_version = matchstr(system("node --version"), '^v\?\zs[0-9.]\+')
return b:node_version
endfunction
function! s:absolutize(name, from)
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
else
return b:node_root . "/node_modules/" . a:name
endif
endfunction
function! s:resolve(path)
" Node checks for files *before* directories, so see if the path does not
" end with a slash or dots and try to match it as a file.
if a:path !~# '\v/(\.\.?/?)?$'
let path_with_suffix = s:resolveSuffix(a:path)
if !empty(path_with_suffix) | return path_with_suffix | endif
endif
if isdirectory(a:path) | return s:resolveFromDirectory(a:path) | endif
endfunction
function! s:resolveFromDirectory(path)
" Node.js checks for package.json in every directory, not just the
" module's parent. According to:
" http://nodejs.org/api/modules.html#modules_all_together
if filereadable(a:path . "/package.json")
" Turns out, even though Node says it does not support directories in
" main, it does.
" NOTE: If package.json's main is empty or refers to a non-existent file,
" ./index.js is still tried.
let main = s:mainFromPackage(a:path . "/package.json")
if !empty(main) && main != ""
let path = s:resolve(a:path . "/" . main)
if !empty(path) | return path | endif
endif
endif
" We need to check for ./index.js's existence here rather than leave it to
" the caller, because otherwise we can't distinguish if this ./index was
" from the directory defaulting to ./index.js or it was the package.json
" which referred to ./index, which in itself could mean both ./index.js and
" ./index/index.js.
return s:resolveSuffix(a:path . "/index")
endfunction
function! s:mainFromPackage(path)
for line in readfile(a:path)
if line !~# '"main"\s*:' | continue | endif
return matchstr(line, '"main"\s*:\s*"\zs[^"]\+\ze"')
endfor
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
let s:GLOB_WILDIGNORE = 1
function! node#lib#glob(name)
let matches = []
if empty(a:name)
let matches += s:CORE_MODULES
endif
if empty(a:name) || a:name =~# s:MODULE
let root = b:node_root . "/node_modules"
let matches += s:glob(empty(a:name) ? root : root . "/" . a:name, root)
endif
if a:name =~# s:ABSPATH
let matches += s:glob(a:name, 0)
endif
if empty(a:name) || a:name =~# s:RELPATH
let root = b:node_root
let relatives = s:glob(empty(a:name) ? root : root . "/" . a:name, root)
"call map(relatives, "substitute(v:val, '^\./\./', './', '')")
if empty(a:name) | call map(relatives, "'./' . v:val") | endif
call filter(relatives, "v:val !~# '^\\.//*node_modules/$'")
let matches += relatives
endif
return matches
endfunction
function! s:glob(path, stripPrefix)
" Remove a single trailing slash because we're adding one with the glob.
let path = substitute(a:path, '/$', "", "")
" Glob() got the ability to return a list only in Vim 7.3.465. Using split
" for compatibility.
let list = split(glob(fnameescape(path)."/*", s:GLOB_WILDIGNORE), "\n")
" Add slashes to directories, like /bin/ls.
call map(list, "v:val . (isdirectory(v:val) ? '/' : '')")
if !empty(a:stripPrefix)
" Counting and removing bytes intentionally as there's no substr function
" that takes character count, only bytes.
let prefix_length = len(a:stripPrefix) + 1
return map(list, "strpart(v:val, prefix_length)")
endif
return list
endfunction
function! s:uniq(list)
let list = reverse(copy(a:list))
return reverse(filter(list, "index(list, v:val, v:key + 1) == -1"))
endfunction

View file

@ -0,0 +1,8 @@
function! s:isNode()
let shebang = getline(1)
if shebang =~# '^#!.*/bin/env\s\+node\>' | return 1 | en
if shebang =~# '^#!.*/bin/node\>' | return 1 | en
return 0
endfunction
au BufRead,BufNewFile * if !did_filetype() && s:isNode() | setf javascript | en

View file

@ -0,0 +1,24 @@
if exists("g:loaded_node") || &cp || v:version < 700 | finish | endif
let g:loaded_node = 1
function! s:detect(path)
if exists("b:node_root") | return | endif
let path = a:path
while 1
let is_node = 0
let is_node = is_node || filereadable(path . "/package.json")
let is_node = is_node || isdirectory(path . "/node_modules")
if is_node | return node#initialize(path) | endif
let parent = fnamemodify(path, ":h")
if parent == path | return | endif
let path = parent
endwhile
endfunction
augroup Node
au!
au VimEnter * if empty(expand("<amatch>")) | call s:detect(getcwd()) | endif
au BufRead,BufNewFile * call s:detect(expand("<amatch>:p"))
augroup end

View file

@ -0,0 +1,447 @@
require_relative "../helper"
require "json"
describe "Lib" do
include WithTemporaryDirectory
before do
Dir.mkdir File.join(@dir, "node_modules")
end
def set_node_version(version)
executable = File.join(@dir, "node")
touch executable, <<-end.strip
#!/bin/sh
[ $# != 1 -o "$1" != --version ] && exit 1
echo "v#{version}"
end
File.chmod 0755, executable
$vim.command(%(let $PATH = "#@dir:" . $PATH))
end
describe "node#lib#find" do
def find(name)
path = $vim.echo(%(node#lib#find("#{name}", expand("%"))))
File.exists?(path) ? File.realpath(path) : path
end
it "must return ./README before ./README.js" do
touch File.join(@dir, "README")
touch File.join(@dir, "README.js")
$vim.edit File.join(@dir, "index.js")
find("./README").must_equal File.join(@dir, "README")
end
it "must return ./README.txt relative to file" do
touch File.join(@dir, "lib", "README.txt")
$vim.edit File.join(@dir, "lib", "index.js")
find("./README.txt").must_equal File.join(@dir, "lib", "README.txt")
end
it "must return ../README.txt" do
touch File.join(@dir, "README.txt")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("../README.txt").must_equal File.join(@dir, "README.txt")
end
it "must return /.../README.txt" do
touch File.join(@dir, "README.txt")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir/README.txt").must_equal File.join(@dir, "README.txt")
end
it "must return ./other.js given ./other" do
touch File.join(@dir, "other.js")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal File.join(@dir, "other.js")
end
it "must return ./other.js given ./other relative to file" do
touch File.join(@dir, "lib", "other.js")
$vim.edit File.join(@dir, "lib", "index.js")
find("./other").must_equal File.join(@dir, "lib", "other.js")
end
it "must return ./other.js before ./other/index.js given ./other" do
touch File.join(@dir, "other.js")
touch File.join(@dir, "other", "index.js")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal File.join(@dir, "other.js")
end
it "must return ../other.js given ../other" do
touch File.join(@dir, "other.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("../other").must_equal File.join(@dir, "other.js")
end
it "must return /.../other.js given /.../other" do
touch File.join(@dir, "other.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir/other").must_equal File.join(@dir, "other.js")
end
it "must return ./package.json given ./package" do
touch File.join(@dir, "package.json")
$vim.edit File.join(@dir, "index.js")
find("./package").must_equal File.join(@dir, "package.json")
end
it "must return ./index.js given ." do
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
find(".").must_equal File.join(@dir, "index.js")
end
it "must return ./index.js given ./" do
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
find("./").must_equal File.join(@dir, "index.js")
end
it "must not find ./index/index.js given ./" do
touch File.join(@dir, "index", "index.js")
$vim.edit File.join(@dir, "other.js")
$vim.echo(%(empty(node#lib#find("./", expand("%"))))).must_equal "1"
end
it "must not find ./.js given ./" do
touch File.join(@dir, ".js")
$vim.edit File.join(@dir, "other.js")
$vim.echo(%(empty(node#lib#find("./", expand("%"))))).must_equal "1"
end
it "must return ../index.js given .." do
touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "other.js")
find("..").must_equal File.join(@dir, "index.js")
end
it "must return ../index.js given ../" do
touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "other.js")
find("../").must_equal File.join(@dir, "index.js")
end
it "must return /.../index.js given /..." do
touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir").must_equal File.join(@dir, "index.js")
end
it "must return /.../index.js given /.../" do
touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir/").must_equal File.join(@dir, "index.js")
end
it "must return ./lib/index.js given ./lib" do
touch File.join(@dir, "lib", "index.js")
$vim.edit File.join(@dir, "index.js")
find("./lib").must_equal File.join(@dir, "lib", "index.js")
end
it "must return ./lib/other.js given ./lib with main" do
touch File.join(@dir, "lib", "other.js")
touch File.join(@dir, "lib", "package.json"), JSON.dump(:main => "other")
$vim.edit File.join(@dir, "index.js")
find("./lib").must_equal File.join(@dir, "lib", "other.js")
end
it "must return ./lib/index.js given ./lib with empty main" do
touch File.join(@dir, "lib", "index.js")
touch File.join(@dir, "lib", "package.json"), JSON.dump(:main => "")
$vim.edit File.join(@dir, "index.js")
find("./lib").must_equal File.join(@dir, "lib", "index.js")
end
it "must return ./lib/index.js given ./lib with non-existent main" do
touch File.join(@dir, "lib", "index.js")
touch File.join(@dir, "lib", "package.json"), JSON.dump(:main => "new")
$vim.edit File.join(@dir, "index.js")
find("./lib").must_equal File.join(@dir, "lib", "index.js")
end
it "must return ./other.js before ./other/index.js given . with main" do
touch File.join(@dir, "package.json"), JSON.dump(:main => "other")
touch File.join(@dir, "other.js")
touch File.join(@dir, "other", "index.js")
$vim.edit File.join(@dir, "index.js")
find(".").must_equal File.join(@dir, "other.js")
end
it "must return node_modules/foo/index.js given foo" do
index = File.join(@dir, "node_modules", "foo", "index.js")
touch index
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal index
end
it "must return node_modules/foo/other.js given foo/other" do
other = File.join(@dir, "node_modules", "foo", "other.js")
touch other
$vim.edit File.join(@dir, "index.js")
find("foo/other").must_equal other
end
it "must return node_modules/foo/other.js given foo/other.js" do
other = File.join(@dir, "node_modules", "foo", "other.js")
touch other
$vim.edit File.join(@dir, "index.js")
find("foo/other.js").must_equal other
end
# When package.json refers to a regular file.
it "must return node_modules/foo/other.js given foo with main" do
mod = File.join(@dir, "node_modules", "foo")
touch File.join(mod, "package.json"), JSON.dump(:main => "./other.js")
touch File.join(mod, "other.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal File.join(mod, "other.js")
end
# When package.json refers to a directory.
it "must return node_modules/foo/lib/index.js given foo with main as ./lib" do
mod = File.join(@dir, "node_modules", "foo")
touch File.join(mod, "package.json"), JSON.dump(:main => "./lib")
touch File.join(mod, "lib/index.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal File.join(mod, "lib/index.js")
end
it "must return node_modules/foo/lib/index.js given foo with main as lib" do
mod = File.join(@dir, "node_modules", "foo")
touch File.join(mod, "package.json"), JSON.dump(:main => "lib")
touch File.join(mod, "lib/index.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal File.join(mod, "lib/index.js")
end
it "must return empty when looking for nothing" do
$vim.edit File.join(@dir, "index.js")
$vim.echo(%(empty(node#lib#find("", expand("%"))))).must_equal "1"
end
it "must return empty when nothing found" do
$vim.edit File.join(@dir, "index.js")
$vim.echo(%(empty(node#lib#find("new", expand("%"))))).must_equal "1"
end
it "must return URL for core module for current Node version" do
set_node_version "0.13.37"
$vim.edit File.join(@dir, "index.js")
url = "http://rawgit.com/joyent/node/v0.13.37/lib/assert.js"
find("assert").must_equal url
end
it "must return URL for core module on master if no Node version" do
touch File.join(@dir, "node"), "#!/bin/sh\nexit 1"
File.chmod 0755, File.join(@dir, "node")
$vim.edit File.join(@dir, "index.js")
$vim.command(%(let $PATH = "#@dir:" . $PATH))
url = "http://rawgit.com/joyent/node/master/lib/assert.js"
find("assert").must_equal url
end
it "must return URL for node.js for current Node version" do
set_node_version "0.13.37"
$vim.edit File.join(@dir, "index.js")
url = "http://rawgit.com/joyent/node/v0.13.37/src/node.js"
find("node").must_equal url
end
it "must return URL for node.js on master if no Node version" do
touch File.join(@dir, "node"), "#!/bin/sh\nexit 1"
File.chmod 0755, File.join(@dir, "node")
$vim.edit File.join(@dir, "index.js")
$vim.command(%(let $PATH = "#@dir:" . $PATH))
url = "http://rawgit.com/joyent/node/master/src/node.js"
find("node").must_equal url
end
end
describe "node#lib#glob" do
require "json"
before do
$vim.edit File.join(@dir, "index.js")
end
def glob(arg = "")
# Because of possible locale and filesystem case-sensitiveness
# differences, sort the output explicitly to be resistant.
JSON.parse($vim.echo(%(node#lib#glob("#{arg}"))).gsub("'", '"')).sort
end
describe "given nothing" do
it "must return files and directories" do
touch File.join(@dir, "index.js")
touch File.join(@dir, "README.txt")
Dir.mkdir File.join(@dir, "test")
files = %w[./README.txt ./index.js ./test/]
glob.must_equal (CORE_MODULES + files).sort
end
it "must return modules and core modules" do
FileUtils.mkpath File.join(@dir, "node_modules", "require-guard")
FileUtils.mkpath File.join(@dir, "node_modules", "export")
FileUtils.mkpath File.join(@dir, "node_modules", "soul")
glob.must_equal (CORE_MODULES + %w[export/ require-guard/ soul/]).sort
end
it "must return core modules without slashes" do
glob.must_equal CORE_MODULES
glob.wont_equal /\//
end
# Even though node.js is the bootstrapper file in src/.
it "must return \"node\" as one of those core modules" do
glob.must_include "node"
end
it "must return files, directories and modules" do
FileUtils.mkpath File.join(@dir, "node_modules", "export")
FileUtils.mkpath File.join(@dir, "node_modules", "soul")
touch File.join(@dir, "index.js")
touch File.join(@dir, "README.txt")
Dir.mkdir File.join(@dir, "test")
files = %w[./README.txt ./index.js ./test/ export/ soul/]
glob.must_equal (CORE_MODULES + files).sort
end
it "must not return the node_modules directory" do
FileUtils.mkpath File.join(@dir, "node_modules")
glob.must_equal CORE_MODULES
end
end
describe "given absolute path" do
it "must return files and directories given /" do
files = Dir.entries("/")
files.reject! {|f| f =~ /^\./ }
files.sort!
files.map! {|f| "/" + f }
files.map! {|f| File.directory?(f) ? f + "/" : f }
glob("/").must_equal files
end
it "must return files and directories given /.../" do
touch File.join(@dir, "index.js")
touch File.join(@dir, "README")
Dir.mkdir File.join(@dir, "test")
files = %W[#@dir/README #@dir/index.js #@dir/node_modules/ #@dir/test/]
glob(@dir).must_equal files
end
it "must return files and directories given /.../test" do
touch File.join(@dir, "test", "index_test.js")
touch File.join(@dir, "test", "helpers.js")
files = %W[#@dir/test/helpers.js #@dir/test/index_test.js]
glob(File.join(@dir, "test")).must_equal files
end
it "must not return modules along with files" do
touch File.join(@dir, "index.js")
touch File.join(@dir, "README")
Dir.mkdir File.join(@dir, "test")
FileUtils.mkpath File.join(@dir, "node_modules", "soul")
files = %W[#@dir/README #@dir/index.js #@dir/node_modules/ #@dir/test/]
glob(@dir).must_equal files
end
end
describe "given relative path" do
it "must return files and directories given ." do
touch File.join(@dir, "index.js")
touch File.join(@dir, "README")
Dir.mkdir File.join(@dir, "test")
glob(".").must_equal %W[./README ./index.js ./test/]
end
it "must return files and directories given ./" do
touch File.join(@dir, "index.js")
touch File.join(@dir, "README")
Dir.mkdir File.join(@dir, "test")
glob("./").must_equal %W[./README ./index.js ./test/]
end
it "must return files and directories given .//" do
touch File.join(@dir, "index.js")
touch File.join(@dir, "README.txt")
Dir.mkdir File.join(@dir, "test")
glob(".//").must_equal %W[.//README.txt .//index.js .//test/]
end
it "must return files and directories given .///" do
touch File.join(@dir, "index.js")
touch File.join(@dir, "README.txt")
Dir.mkdir File.join(@dir, "test")
glob(".///").must_equal %W[.///README.txt .///index.js .///test/]
end
it "must return files and directories given ./test" do
touch File.join(@dir, "test", "test.js")
touch File.join(@dir, "test", "helpers.js")
glob("./test/").must_equal %W[./test/helpers.js ./test/test.js]
end
end
describe "given module name" do
it "must return files and directories given soul" do
touch File.join(@dir, "node_modules", "soul", "index.js")
touch File.join(@dir, "node_modules", "soul", "README")
FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
glob("soul").must_equal %w[soul/README soul/index.js soul/test/]
end
it "must return files and directories given soul/" do
touch File.join(@dir, "node_modules", "soul", "index.js")
FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
glob("soul/").must_equal %w[soul/index.js soul/test/]
end
it "must return files and directories given soul//" do
touch File.join(@dir, "node_modules", "soul", "index.js")
FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
glob("soul//").must_equal %w[soul//index.js soul//test/]
end
it "must return files and directories given soul///" do
touch File.join(@dir, "node_modules", "soul", "index.js")
FileUtils.mkpath File.join(@dir, "node_modules", "soul", "test")
glob("soul///").must_equal %w[soul///index.js soul///test/]
end
it "must return files and directories given soul/test" do
touch File.join(@dir, "node_modules", "soul", "test", "test.js")
touch File.join(@dir, "node_modules", "soul", "test", "helpers.js")
glob("soul/test").must_equal %w[soul/test/helpers.js soul/test/test.js]
end
end
end
describe "node#lib#version" do
it "should return current Node's version" do
set_node_version "0.13.37"
$vim.echo("node#lib#version()").must_equal "0.13.37"
end
end
end

View file

@ -0,0 +1,460 @@
require_relative "./helper"
require "json"
describe "Autoloaded" do
include WithTemporaryDirectory
before do
FileUtils.touch File.join(@dir, "package.json")
end
after do
$vim.command("windo wincmd c")
end
describe "Autocommand" do
it "must fire user autcommand \"Node\"" do
$vim.command "au User Node let node_autocommand = 1337"
$vim.edit File.join(@dir, "other.js")
$vim.echo(%(g:node_autocommand)).must_equal "1337"
end
end
describe "Goto file" do
it "must define plug mapping in non-JavaScript files" do
$vim.edit File.join(@dir, "README")
$vim.echo(%(maparg("<Plug>NodeGotoFile", "n"))).wont_equal ""
end
it "must not be available in non-JavaScript files" do
$vim.edit File.join(@dir, "README")
$vim.echo(%(hasmapto("<Plug>NodeGotoFile"))).must_equal "0"
end
it "must edit README.txt" do
touch File.join(@dir, "index.js"), %(// Please read README.txt)
touch File.join(@dir, "README.txt")
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$gf"
$vim.echo(%(bufname("%"))).must_equal File.join(@dir, "README.txt")
end
it "must edit README before README.js" do
touch File.join(@dir, "index.js"), "// Please read README"
touch File.join(@dir, "README")
touch File.join(@dir, "README.js")
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "README")
end
it "must edit ./README.txt relative to file" do
touch File.join(@dir, "foo", "index.js"), %(// Please read ./README.txt)
touch File.join(@dir, "foo", "README.txt")
$vim.edit File.join(@dir, "foo", "index.js")
$vim.feedkeys "$gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "foo", "README.txt")
end
it "must edit /.../README.txt" do
touch File.join(@dir, "index.js"), %(// Read #@dir/lib/README.txt)
touch File.join(@dir, "lib", "README.txt")
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "lib", "README.txt")
end
it "must open ./other.js relative to file" do
touch File.join(@dir, "foo", "index.js"), %(require("./other"))
touch File.join(@dir, "foo", "other.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.js")
end
it "must edit ./index.js given ." do
touch File.join(@dir, "other.js"), %(require("."))
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
$vim.feedkeys "f.gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "index.js")
end
it "must edit ./index.js given ./" do
touch File.join(@dir, "other.js"), %(require("./"))
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
$vim.feedkeys "f.gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "index.js")
end
it "must edit ../index.js given .." do
touch File.join(@dir, "foo", "other.js"), %(require(".."))
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "foo", "other.js")
$vim.feedkeys "f.gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "index.js")
end
it "must edit ../index.js given ../" do
touch File.join(@dir, "foo", "other.js"), %(require("../"))
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "foo", "other.js")
$vim.feedkeys "f.gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "index.js")
end
it "must open ./node_modules/foo/index.js given foo" do
touch File.join(@dir, "index.js"), %(require("foo"))
index = File.join(@dir, "node_modules", "foo", "index.js")
touch index
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal index
end
it "must not show an error when opening nothing" do
touch File.join(@dir, "index.js"), %("")
$vim.edit File.join(@dir, "index.js")
$vim.command(%(let v:errmsg = ""))
$vim.feedkeys "gf"
error = $vim.command("let v:errmsg").sub(/^\S+\s*/, "")
error.must_equal ""
end
it "must show error when opening a non-existent file" do
touch File.join(@dir, "index.js"), %(require("new"))
$vim.edit File.join(@dir, "index.js")
$vim.command(%(let v:errmsg = ""))
$vim.feedkeys "$hhgf"
error = $vim.command("let v:errmsg").sub(/^\S+\s*/, "")
error.must_equal %(E447: Can't find file "new" in path)
end
it "must find also when filetype is JSON" do
$vim.command("au BufReadPre package set ft=json")
touch File.join(@dir, "package"), %({"dependencies": {"foo": "1.x"}})
index = File.join(@dir, "node_modules", "foo", "index.js")
touch index
$vim.edit File.join(@dir, "package")
$vim.echo("&filetype").must_equal "json"
$vim.command("au! BufReadPre package set ft=json")
$vim.feedkeys "/foo\\<CR>gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal index
end
end
describe "Goto file with split" do
it "must edit file in a new split" do
touch File.join(@dir, "index.js"), %(require("./other"))
touch File.join(@dir, "other.js")
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "f.\\<C-w>f"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "other.js")
$vim.echo(%(winnr("$"))).must_equal "2"
end
end
describe "Goto file with tab" do
it "must edit file in a new tab" do
touch File.join(@dir, "index.js"), %(require("./other"))
touch File.join(@dir, "other.js")
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "f.\\<C-w>gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "other.js")
$vim.echo(%(tabpagenr("$"))).must_equal "2"
end
end
describe "Include file search pattern" do
it "must find matches given a require" do
touch File.join(@dir, "index.js"), <<-end.gsub(/^\s+/, "")
var awesome = require("foo")
awesome()
end
definition = %(module.exports = function awesome() { return 1337 })
touch File.join(@dir, "node_modules", "foo", "index.js"), definition
$vim.edit File.join(@dir, "index.js")
$vim.command("normal G[i").must_equal definition
end
it "must find matches given a relative require" do
touch File.join(@dir, "index.js"), <<-end.gsub(/^\s+/, "")
var awesome = require("./other")
awesome()
end
definition = %(module.exports = function awesome() { return 1337 })
touch File.join(@dir, "other.js"), definition
$vim.edit File.join(@dir, "index.js")
$vim.command("normal G[i").must_equal definition
end
it "must find matches given a relative require in another directory" do
touch File.join(@dir, "foo", "index.js"), <<-end.gsub(/^\s+/, "")
var awesome = require("./other")
awesome()
end
definition = %(module.exports = function awesome() { return 1337 })
touch File.join(@dir, "foo", "other.js"), definition
$vim.edit File.join(@dir, "foo", "index.js")
$vim.command("normal G[i").must_equal definition
end
end
describe ":Nedit" do
# NOTE: Test from a non-JavaScript file everywhere to make sure there are
# no dependencies on JavaScript specific settings.
FULL_COMMAND_MATCH = "2"
it "must be available in non-JavaScript files" do
$vim.edit File.join(@dir, "README.txt")
$vim.echo("exists(':Nedit')").must_equal FULL_COMMAND_MATCH
end
it "must be available in JavaScript files" do
$vim.edit File.join(@dir, "index.js")
$vim.echo("exists(':Nedit')").must_equal FULL_COMMAND_MATCH
end
it "must edit ./README.txt" do
touch File.join(@dir, "README.txt")
$vim.edit File.join(@dir, "CHANGELOG.txt")
$vim.command "Nedit ./README.txt"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "README.txt")
end
it "must edit ./README.txt relative to node_root" do
touch File.join(@dir, "README.txt")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "CHANGELOG.txt")
$vim.command "Nedit ./README.txt"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "README.txt")
end
it "must edit /.../README.txt" do
touch File.join(@dir, "lib", "README.txt")
$vim.edit File.join(@dir, "CHANGELOG.txt")
$vim.command "Nedit #@dir/lib/README.txt"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "lib", "README.txt")
end
it "must edit ./other.js relative to node_root" do
touch File.join(@dir, "other.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "CHANGELOG.txt")
$vim.command "Nedit ./other"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "other.js")
end
it "must edit ./index.js given ." do
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "CHANGELOG.txt")
$vim.command "Nedit ."
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "index.js")
end
it "must edit ./index.js given . relative to node_root" do
touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "CHANGELOG.txt")
$vim.command "Nedit ."
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "index.js")
end
it "must edit ./index.js given ./" do
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "CHANGELOG.txt")
$vim.command "Nedit ./"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal File.join(@dir, "index.js")
end
it "must edit /node_modules/foo/index.js given foo" do
index = File.join(@dir, "node_modules", "foo", "index.js")
touch index
$vim.edit File.join(@dir, "README.txt")
$vim.command("Nedit foo")
$vim.echo(%(bufname("%"))).must_equal index
end
describe "completion" do
after do
$vim.command("set wildignorecase&")
$vim.command("set fileignorecase&")
end
def complete(cmd)
cmdline = $vim.command(%(silent! normal! :#{cmd}e))
cmdline.sub(/^:\w+\s+/, "")
end
public_core_modules = CORE_MODULES.select {|m| m[0] != "_" }
private_core_modules = CORE_MODULES.select {|m| m[0] == "_" }
it "must return files, directories, modules" do
Dir.mkdir File.join(@dir, "node_modules")
Dir.mkdir File.join(@dir, "node_modules", "require-guard")
Dir.mkdir File.join(@dir, "node_modules", "export")
Dir.mkdir File.join(@dir, "node_modules", "soul")
touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "README.txt")
files = %w[export/ require-guard/ soul/ ./index.js ./package.json]
all = public_core_modules + files
complete("Nedit ").split.sort.must_equal all.sort
end
it "must return only public core modules" do
$vim.edit File.join(@dir, "README.txt")
modules = public_core_modules + ["./package.json"]
complete("Nedit ").must_equal modules.join(" ")
end
it "must return private core modules if explicitly asked" do
$vim.edit File.join(@dir, "README.txt")
complete("Nedit _").must_equal private_core_modules.join(" ")
end
it "must return only matching modules" do
Dir.mkdir File.join(@dir, "node_modules")
Dir.mkdir File.join(@dir, "node_modules", "export")
Dir.mkdir File.join(@dir, "node_modules", "soul")
Dir.mkdir File.join(@dir, "node_modules", "soulstash")
$vim.edit File.join(@dir, "README.txt")
modules = "smalloc stream string_decoder sys soul/ soulstash/"
complete("Nedit s").must_equal modules
end
it "must not return modules with matching bit in the middle" do
Dir.mkdir File.join(@dir, "node_modules")
Dir.mkdir File.join(@dir, "node_modules", "soul")
Dir.mkdir File.join(@dir, "node_modules", "soulstash")
Dir.mkdir File.join(@dir, "node_modules", "asoul")
$vim.edit File.join(@dir, "README.txt")
complete("Nedit sou").must_equal "soul/ soulstash/"
end
it "must return files and directories in module's directory" do
touch File.join(@dir, "node_modules", "soul", "index.js")
touch File.join(@dir, "node_modules", "soul", "test", "test.js")
$vim.edit File.join(@dir, "README.txt")
complete("Nedit soul/").must_equal "soul/index.js soul/test/"
end
it "must return files and directories given a double slash" do
touch File.join(@dir, "node_modules", "soul", "index.js")
touch File.join(@dir, "node_modules", "soul", "test", "test.js")
$vim.edit File.join(@dir, "README.txt")
complete("Nedit soul//").must_equal "soul//index.js soul//test/"
end
it "must return files case-insensitively given &fileignorecase" do
skip if $vim.echo(%(exists("&fileignorecase"))) != "1"
$vim.command("set fileignorecase")
$vim.command("set nowildignorecase")
touch File.join(@dir, "node_modules", "soul", "index.js")
touch File.join(@dir, "node_modules", "soul", "CHANGELOG")
$vim.edit File.join(@dir, "README.txt")
complete("Nedit soul/chan").must_equal "soul/CHANGELOG"
end
it "must return files case-insensitively given only &wildignorecase" do
skip if $vim.echo(%(exists("&wildignorecase"))) != "1"
$vim.command("set nofileignorecase")
$vim.command("set wildignorecase")
touch File.join(@dir, "node_modules", "soul", "index.js")
touch File.join(@dir, "node_modules", "soul", "CHANGELOG")
$vim.edit File.join(@dir, "README.txt")
complete("Nedit soul/chan").must_equal "soul/CHANGELOG"
end
end
end
describe ":Nopen" do
it "must edit and lcd to module's directory" do
touch File.join(@dir, "node_modules", "foo", "package.json")
touch File.join(@dir, "node_modules", "foo", "index.js")
$vim.edit File.join(@dir, "README.txt")
$vim.command("vsplit")
$vim.command("Nopen foo")
$vim.echo(%(bufname("%"))).must_equal "index.js"
$vim.command("pwd").must_equal File.join(@dir, "node_modules", "foo")
$vim.command("wincmd p")
$vim.command("pwd").must_equal Dir.pwd
end
it "must edit and lcd to module's root directory" do
touch File.join(@dir, "node_modules", "foo", "package.json")
utils = File.join(@dir, "node_modules", "foo", "lib", "utils.js")
touch utils
$vim.edit File.join(@dir, "README.txt")
$vim.command("vsplit")
$vim.command("Nopen foo/lib/utils")
$vim.echo(%(bufname("%"))).must_equal "lib/utils.js"
$vim.command("pwd").must_equal File.join(@dir, "node_modules", "foo")
$vim.command("wincmd p")
$vim.command("pwd").must_equal Dir.pwd
end
end
end

View file

@ -0,0 +1,45 @@
require_relative "./helper"
describe "Ftdetect" do
[
"#!/usr/bin/env node",
"#!/usr/bin/env node --harmony-generators",
"#!/usr/local/bin/env node",
"#!/usr/local/bin/env node --harmony-generators",
"#!/usr/bin/node",
"#!/usr/bin/node --harmony-generators",
"#!/usr/local/bin/node",
"#!/usr/local/bin/node --harmony-generators",
].each do |shebang|
it %(must detect a file with "#{shebang}" shebang as JavaScript) do
file = Tempfile.new("bang")
file.write shebang + $/
file.close
$vim.edit file.path
$vim.echo("&ft").must_equal "javascript"
end
end
[
"#!/usr/bin/env noder",
"#!/usr/bin/noder",
].each do |shebang|
it %(must not detect a file with "#{shebang}" shebang as JavaScript) do
file = Tempfile.new("bang")
file.write shebang + $/
file.close
$vim.edit file.path
$vim.echo("&ft").wont_equal "javascript"
end
end
it "must not detect a .c file as JavaScript even with Node's shebang" do
file = Tempfile.new(%w[tea .c])
file.write "#!/usr/bin/node" + $/
file.close
$vim.edit file.path
$vim.echo("&ft").wont_equal "javascript"
end
end

View file

@ -0,0 +1,48 @@
require "minitest/autorun"
require "vimrunner"
require "fileutils"
require "tempfile"
MiniTest::Unit::TestCase.define_singleton_method(:test_order) do :alpha end
begin
require "minitest/reporters"
MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
rescue LoadError
end
$vimrc = File.expand_path("../vimrc", __FILE__)
$vim = Vimrunner::Server.new(:vimrc => $vimrc).start
Minitest::Unit.after_tests { $vim.kill }
module WithTemporaryDirectory
def self.included(base)
require "tmpdir"
end
def setup
super
# Mac has the temporary directory symlinked, so need File.realpath to
# match the paths that Vim returns.
@dir = File.realpath(Dir.mktmpdir)
end
def teardown
FileUtils.remove_entry_secure @dir
super
end
end
def touch(path, contents = nil)
FileUtils.mkpath File.dirname(path)
return FileUtils.touch(path) if contents.nil? || contents.empty?
File.open(path, "w") {|f| f.write contents }
end
CORE_MODULES = %w[_debugger _http_agent _http_client _http_common
_http_incoming _http_outgoing _http_server _linklist _stream_duplex
_stream_passthrough _stream_readable _stream_transform _stream_writable
_tls_legacy _tls_wrap assert buffer child_process cluster console
constants crypto dgram dns domain events freelist fs http https module
net node os path punycode querystring readline repl smalloc stream
string_decoder sys timers tls tty url util vm zlib]

View file

@ -0,0 +1,85 @@
require_relative "./helper"
describe "Plugin" do
include WithTemporaryDirectory
describe "b:node_root" do
it "must be set when in same directory with package.json" do
FileUtils.touch File.join(@dir, "package.json")
$vim.edit File.join(@dir, "index.js")
$vim.echo("b:node_root").must_equal @dir
end
it "must be set when in same directory with node_modules" do
Dir.mkdir File.join(@dir, "node_modules")
$vim.edit File.join(@dir, "index.js")
$vim.echo("b:node_root").must_equal @dir
end
it "must be set when ancestor directory has package.json" do
FileUtils.touch File.join(@dir, "package.json")
nested = File.join(@dir, "lib", "awesomeness")
FileUtils.mkdir_p nested
$vim.edit File.join(nested, "index.js")
$vim.echo("b:node_root").must_equal @dir
end
it "must be set when ancestor directory has node_modules" do
Dir.mkdir File.join(@dir, "node_modules")
nested = File.join(@dir, "lib", "awesomeness")
FileUtils.mkdir_p nested
$vim.edit File.join(nested, "index.js")
$vim.echo("b:node_root").must_equal @dir
end
it "must be set also for other filetypes" do
FileUtils.touch File.join(@dir, "package.json")
$vim.edit File.join(@dir, "README.txt")
$vim.echo("b:node_root").must_equal @dir
end
it "must be set in nested Node projects" do
nested = File.join(@dir, "node_modules", "require-guard")
FileUtils.mkdir_p nested
FileUtils.touch File.join(nested, "package.json")
test = File.join(nested, "test")
FileUtils.mkdir_p test
$vim.edit File.join(test, "index_test.js")
$vim.echo("b:node_root").must_equal nested
end
it "must not be set when no ancestor has one" do
$vim.edit File.join(@dir, "index_test.js")
$vim.echo(%(exists("b:node_root"))).must_equal "0"
end
it "must be set from file, not working directory" do
$vim.command "cd #{@dir}"
FileUtils.touch File.join(@dir, "package.json")
nested = File.join(@dir, "node_modules", "require-guard")
FileUtils.mkdir_p nested
FileUtils.touch File.join(nested, "package.json")
$vim.edit File.join(nested, "index_test.js")
$vim.echo("b:node_root").must_equal nested
end
it "must detect directory as Node's when opening Vim" do
begin
Dir.chdir @dir
FileUtils.touch File.join(@dir, "package.json")
vim = Vimrunner::Server.new(:vimrc => $vimrc).start
vim.command("pwd").must_equal @dir
vim.echo("b:node_root").must_equal @dir
ensure
vim.kill if vim
end
end
end
end

View file

@ -0,0 +1,13 @@
set nocompatible
set noswapfile
set nobackup
set runtimepath-=~/.vim
set runtimepath-=~/.vim/after
let &runtimepath = expand("<sfile>:p:h:h") . "," . &runtimepath
filetype plugin indent on
syntax on
set columns=80 lines=24
winpos 1337 0