Browse Source

inital commit

master
Ruel Tmeizeh 7 years ago
parent
commit
b5c8fa716c
68 changed files with 6139 additions and 5 deletions
  1. +0
    -1
      bundle/node
  2. +1
    -0
      bundle/node/.mailmap
  3. +8
    -0
      bundle/node/.packignore
  4. +7
    -0
      bundle/node/.travis.yml
  5. +57
    -0
      bundle/node/CHANGELOG.md
  6. +11
    -0
      bundle/node/Gemfile
  7. +59
    -0
      bundle/node/Gemfile.lock
  8. +8
    -0
      bundle/node/Guardfile
  9. +20
    -0
      bundle/node/LICENSE
  10. +40
    -0
      bundle/node/Makefile
  11. +105
    -0
      bundle/node/README.md
  12. +110
    -0
      bundle/node/autoload/node.vim
  13. +151
    -0
      bundle/node/autoload/node/lib.vim
  14. +8
    -0
      bundle/node/ftdetect/node.vim
  15. +47
    -0
      bundle/node/plugin/node.vim
  16. +466
    -0
      bundle/node/test/autoload/lib_test.rb
  17. +538
    -0
      bundle/node/test/autoload_test.rb
  18. +45
    -0
      bundle/node/test/ftdetect_test.rb
  19. +54
    -0
      bundle/node/test/helper.rb
  20. +85
    -0
      bundle/node/test/plugin_test.rb
  21. +14
    -0
      bundle/node/test/vimrc
  22. +0
    -1
      bundle/vim-javascript
  23. +0
    -1
      bundle/vim-javascript-lib
  24. +22
    -0
      bundle/vim-javascript-lib/LICENSE
  25. +51
    -0
      bundle/vim-javascript-lib/README.md
  26. +9
    -0
      bundle/vim-javascript-lib/after/syntax/html.vim
  27. +61
    -0
      bundle/vim-javascript-lib/after/syntax/javascript.vim
  28. BIN
      bundle/vim-javascript-lib/screenshots/after.png
  29. BIN
      bundle/vim-javascript-lib/screenshots/before.png
  30. +13
    -0
      bundle/vim-javascript/ISSUE_TEMPLATE.md
  31. +119
    -0
      bundle/vim-javascript/README.md
  32. +12
    -0
      bundle/vim-javascript/after/ftplugin/javascript.vim
  33. +16
    -0
      bundle/vim-javascript/compiler/eslint.vim
  34. +8
    -0
      bundle/vim-javascript/extras/ctags
  35. +105
    -0
      bundle/vim-javascript/extras/flow.vim
  36. +39
    -0
      bundle/vim-javascript/extras/jsdoc.vim
  37. +3
    -0
      bundle/vim-javascript/extras/ngdoc.vim
  38. +17
    -0
      bundle/vim-javascript/ftdetect/javascript.vim
  39. +475
    -0
      bundle/vim-javascript/indent/javascript.vim
  40. +387
    -0
      bundle/vim-javascript/syntax/javascript.vim
  41. +0
    -1
      bundle/vim-node
  42. +1
    -0
      bundle/vim-node/.mailmap
  43. +8
    -0
      bundle/vim-node/.packignore
  44. +7
    -0
      bundle/vim-node/.travis.yml
  45. +57
    -0
      bundle/vim-node/CHANGELOG.md
  46. +11
    -0
      bundle/vim-node/Gemfile
  47. +59
    -0
      bundle/vim-node/Gemfile.lock
  48. +8
    -0
      bundle/vim-node/Guardfile
  49. +20
    -0
      bundle/vim-node/LICENSE
  50. +40
    -0
      bundle/vim-node/Makefile
  51. +105
    -0
      bundle/vim-node/README.md
  52. +110
    -0
      bundle/vim-node/autoload/node.vim
  53. +151
    -0
      bundle/vim-node/autoload/node/lib.vim
  54. +8
    -0
      bundle/vim-node/ftdetect/node.vim
  55. +47
    -0
      bundle/vim-node/plugin/node.vim
  56. +466
    -0
      bundle/vim-node/test/autoload/lib_test.rb
  57. +538
    -0
      bundle/vim-node/test/autoload_test.rb
  58. +45
    -0
      bundle/vim-node/test/ftdetect_test.rb
  59. +54
    -0
      bundle/vim-node/test/helper.rb
  60. +85
    -0
      bundle/vim-node/test/plugin_test.rb
  61. +14
    -0
      bundle/vim-node/test/vimrc
  62. +0
    -1
      bundle/vim-nodejs-complete
  63. +82
    -0
      bundle/vim-nodejs-complete/README.md
  64. +2
    -0
      bundle/vim-nodejs-complete/after/autoload/nodejs-doc.vim
  65. +609
    -0
      bundle/vim-nodejs-complete/after/autoload/nodejscomplete.vim
  66. +357
    -0
      bundle/vim-nodejs-complete/after/autoload/update-nodejs-doc.js
  67. +3
    -0
      bundle/vim-nodejs-complete/after/ftplugin/javascript.vim
  68. +81
    -0
      bundle/vim-nodejs-complete/after/test/a.js

+ 0
- 1
bundle/node

@ -1 +0,0 @@
Subproject commit 13b31218447335e176d46dd5f497b274f7f49595

+ 1
- 0
bundle/node/.mailmap View File

@ -0,0 +1 @@
Andri Möll <andri@dot.ee>

+ 8
- 0
bundle/node/.packignore View File

@ -0,0 +1,8 @@
/Makefile
/test/*
/Gemfile
/Gemfile.lock
/Guardfile
/*.zip
/node.tar.gz
tags

+ 7
- 0
bundle/node/.travis.yml View File

@ -0,0 +1,7 @@
language: ruby
rvm:
- 1.9.3
before_install: sudo apt-get install vim-gtk
install: bundle install --deployment --without development
script: xvfb-run make test

+ 57
- 0
bundle/node/CHANGELOG.md View File

@ -0,0 +1,57 @@
## Unreleased
- Fixes `gf` on scoped modules (`require("@scope/example")`).
- Initializes Node.vim `gf` and other mappings when the `'filetype'` is set,
rather than when reading a file.
This allows you to manually set the filetype to JavaScript after opening
a file, for example, and still get Node.vim's mappings.
- Initializes Node.vim mappings for JSX (those with the `jsx` filetype) files.
- Adds `.es` to detected suffixes, so you can `gf` over `./foo` to open
`./foo.es`.
- Updates URLs to use <https://github.com/nodejs/node> instead of the previous
Joyent repo.
Thanks, [Jacky Alciné](https://jacky.wtf), for the help!
## 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`.

+ 11
- 0
bundle/node/Gemfile 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

+ 59
- 0
bundle/node/Gemfile.lock 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!

+ 8
- 0
bundle/node/Guardfile 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

+ 20
- 0
bundle/node/LICENSE 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.

+ 40
- 0
bundle/node/Makefile 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/nodejs/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

+ 105
- 0
bundle/node/README.md View File

@ -0,0 +1,105 @@
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).

+ 110
- 0
bundle/node/autoload/node.vim View File

@ -0,0 +1,110 @@
" 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"]
function! node#initialize(root)
let b:node_root = a:root
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>
silent doautocmd User Node
endfunction
function! node#javascript()
" This might be called multiple times if multiple filetypes match.
if exists("b:node_javascript") && b:node_javascript | return | endif
let b:node_javascript = 1
setlocal 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('%'))"
" @ is used for scopes, but isn't a default filename character on
" non-Windows sytems.
setlocal isfname+=@-@
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

+ 151
- 0
bundle/node/autoload/node/lib.vim 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/nodejs/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

+ 8
- 0
bundle/node/ftdetect/node.vim 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

+ 47
- 0
bundle/node/plugin/node.vim View File

@ -0,0 +1,47 @@
if exists("g:loaded_node") || &cp || v:version < 700 | finish | endif
let g:loaded_node = 1
let s:filetypes = ["javascript", "json", "jsx"]
if exists("g:node_filetypes") | let s:filetypes = g:node_filetypes | endif
function! s:detect(dir)
if exists("b:node_root") | return | endif
let dir = a:dir
while 1
let is_node = 0
let is_node = is_node || filereadable(dir . "/package.json")
let is_node = is_node || isdirectory(dir . "/node_modules")
if is_node | return node#initialize(dir) | endif
let parent = fnamemodify(dir, ":h")
if parent == dir | return | endif
let dir = parent
endwhile
endfunction
function! s:permutate(ft)
" Don't know right now how to detect javascript.jsx and other permutations
" without precomputing them in advance. Please let me know if you do.
return [a:ft, a:ft . ".*", "*." . a:ft, "*." . a:ft . ".*"]
endfunction
function! s:flatten(list)
let values = []
for value in a:list
if type(value) == type([]) | call extend(values, value)
else | add(values, value)
endif
endfor
return values
endfunction
augroup Node
au!
au VimEnter * if empty(expand("<amatch>")) | call s:detect(getcwd()) | endif
au BufRead,BufNewFile * call s:detect(expand("<amatch>:p"))
let s:filetype_patterns = s:flatten(map(s:filetypes, "<SID>permutate(v:val)"))
let s:filetype_patterns_joined = join(s:filetype_patterns, ",")
execute "au FileType " s:filetype_patterns_joined " call node#javascript()"
augroup end

+ 466
- 0
bundle/node/test/autoload/lib_test.rb View File

@ -0,0 +1,466 @@
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
target = touch File.join(@dir, "README")
touch File.join(@dir, "README.js")
$vim.edit File.join(@dir, "index.js")
find("./README").must_equal target
end
it "must return ./README.txt relative to file" do
target = touch File.join(@dir, "lib", "README.txt")
$vim.edit File.join(@dir, "lib", "index.js")
find("./README.txt").must_equal target
end
it "must return ../README.txt" do
target = 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 target
end
it "must return /.../README.txt" do
target = 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 target
end
it "must return ./other.js given ./other" do
target = touch File.join(@dir, "other.js")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ./other.jsx given ./other" do
target = touch File.join(@dir, "other.jsx")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ./other.es given ./other" do
target = touch File.join(@dir, "other.es")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ./other.js given ./other relative to file" do
target = touch File.join(@dir, "lib", "other.js")
$vim.edit File.join(@dir, "lib", "index.js")
find("./other").must_equal target
end
it "must return ./other.js before ./other/index.js given ./other" do
target = touch File.join(@dir, "other.js")
touch File.join(@dir, "other", "index.js")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ../other.js given ../other" do
target = touch File.join(@dir, "other.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("../other").must_equal target
end
it "must return /.../other.js given /.../other" do
target = 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 target
end
it "must return ./package.json given ./package" do
target = touch File.join(@dir, "package.json")
$vim.edit File.join(@dir, "index.js")
find("./package").must_equal target
end
it "must return ./index.js given ." do
target = touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
find(".").must_equal target
end
it "must return ./index.js given ./" do
target = touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
find("./").must_equal target
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
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "other.js")
find("..").must_equal target
end
it "must return ../index.js given ../" do
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "other.js")
find("../").must_equal target
end
it "must return /.../index.js given /..." do
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir").must_equal target
end
it "must return /.../index.js given /.../" do
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir/").must_equal target
end
it "must return ./lib/index.js given ./lib" do
target = touch File.join(@dir, "lib", "index.js")
$vim.edit File.join(@dir, "index.js")
find("./lib").must_equal target
end
it "must return ./lib/other.js given ./lib with main" do
target = 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 target
end
it "must return ./lib/index.js given ./lib with empty main" do
target = 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 target
end
it "must return ./lib/index.js given ./lib with non-existent main" do
target = 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 target
end
it "must return ./other.js before ./other/index.js given . with main" do
touch File.join(@dir, "package.json"), JSON.dump(:main => "other")
target = touch File.join(@dir, "other.js")
touch File.join(@dir, "other", "index.js")
$vim.edit File.join(@dir, "index.js")
find(".").must_equal target
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")
target = touch File.join(mod, "other.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal target
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")
target = touch File.join(mod, "lib/index.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal target
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")
target = touch File.join(mod, "lib/index.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal target
end
it "must return node_modules/@scope/foo/index.js given @scope/foo" do
target = File.join(@dir, "node_modules", "@scope", "foo", "index.js")
touch target
$vim.edit File.join(@dir, "index.js")
find("@scope/foo").must_equal target
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/nodejs/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/nodejs/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/nodejs/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/nodejs/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

+ 538
- 0
bundle/node/test/autoload_test.rb View File

@ -0,0 +1,538 @@
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 mapped 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 edit ./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 edit ./node_modules/foo/index.js given foo" do
touch File.join(@dir, "index.js"), %(require("foo"))
target = touch File.join(@dir, "node_modules", "foo", "index.js")
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must edit ./node_modules/@scope/foo/index.js given @scope/foo" do
touch File.join(@dir, "index.js"), %(require("@scope/foo"))
target = File.join(@dir, "node_modules", "@scope", "foo", "index.js")
touch target
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must not show an error when searching for 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 searching for 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 when filetype is JavaScript and file exists" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.js"), %(require("foo"))
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype is JavaScript and file new" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
$vim.edit File.join(@dir, "index.js")
$vim.insert %(require("foo"))
$vim.normal
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype is JSON" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
# NOTE: Use an extensionless file and set ft manually to not have
# filetype.vim set its filetype to JavaScript automatically.
$vim.command("au BufReadPre package set ft=json")
touch File.join(@dir, "package"), %({"dependencies": {"foo": "1.x"}})
$vim.edit File.join(@dir, "package")
$vim.echo("&filetype").must_equal "json"
$vim.feedkeys "/foo\\<CR>gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal target
end
it "must find when filetype set to JavaScript after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype javascript")
$vim.echo("&filetype").must_equal "javascript"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype set to JSX after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype jsx")
$vim.echo("&filetype").must_equal "jsx"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype set to JavaScript and Foo after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype javascript.foo")
$vim.echo("&filetype").must_equal "javascript.foo"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
# Ensure the autocommand detects both orderings.
it "must find when filetype set to Foo and JavaScript after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype foo.javascript")
$vim.echo("&filetype").must_equal "foo.javascript"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
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
target = touch File.join(@dir, "node_modules", "foo", "index.js")
$vim.edit File.join(@dir, "README.txt")
$vim.command("Nedit foo")
$vim.echo(%(bufname("%"))).must_equal target
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 = touch File.join(@dir, "node_modules", "foo", "lib", "utils.js")
$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

+ 45
- 0
bundle/node/test/ftdetect_test.rb 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

+ 54
- 0
bundle/node/test/helper.rb View File

@ -0,0 +1,54 @@
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)
if contents.nil? || contents.empty?
FileUtils.touch(path)
else
File.open(path, "w") {|f| f.write contents }
end
path
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]

+ 85
- 0
bundle/node/test/plugin_test.rb 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

+ 14
- 0
bundle/node/test/vimrc View File

@ -0,0 +1,14 @@
set nocompatible
set noswapfile
set nobackup
set hidden
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

+ 0
- 1
bundle/vim-javascript

@ -1 +0,0 @@
Subproject commit 0922d26d1477421d213f82ad4224c959b4c2357e

+ 0
- 1
bundle/vim-javascript-lib

@ -1 +0,0 @@
Subproject commit f406e8c85fe61fb82921214e49f9b77475258b08

+ 22
- 0
bundle/vim-javascript-lib/LICENSE View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2014 Crusoe.Xia
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

+ 51
- 0
bundle/vim-javascript-lib/README.md View File

@ -0,0 +1,51 @@
vim-javascript-lib
==================
This is a companion plugin of [vim-javascript](https://github.com/pangloss/vim-javascript), which provide the keyword highlight of famous js libraries and start to support native methods. This plugin will not try to highlight every method of the libs, it only highlight most frequent used __global variables__, such as *_*, *$*, *Backbone*, because __sometimes the more you see the less you get__, we just care the most important stuff here.
Install
-------
### Install with [Vundle](https://github.com/gmarik/Vundle.vim)
Plugin 'crusoexia/vim-javascript-lib'
Dependency
----------
This plugin is designed to work with [vim-javascript](https://github.com/pangloss/vim-javascript), so [vim-javascript](https://github.com/pangloss/vim-javascript) is hard dependency, you must install it to make the plugin works.
JS native highlight
-------------------
* Array methods
Libraries
---------
Right now the list is:
* underscore / Lo-Dash
* jQuery / Zepto
* Backbone
* angular
* Handlebars / Mustache
* mocha
* AMD
* Q
HTML
----
This plugin will also fix the html attribute highlight issue of original vim.
Screenshots
-----------
### Before:
![before](./screenshots/before.png)
### After:
![after](./screenshots/after.png)

+ 9
- 0
bundle/vim-javascript-lib/after/syntax/html.vim View File

@ -0,0 +1,9 @@
" Vim syntax file
" Fix the default html attribute highlight.
"
" Language: HTML
" Maintainer: crusoexia
" URL: https://github.com/crusoexia/vim-javascript-lib
setlocal iskeyword+=-
syntax match htmlArg /\v<\w%(\w|\-)*>/ contained

+ 61
- 0
bundle/vim-javascript-lib/after/syntax/javascript.vim View File

@ -0,0 +1,61 @@
" Vim syntax file
" This is a [vim-javascript](https://github.com/pangloss/vim-javascript)'s companion,
" which is used for highlight the javascript library keywords.
"
" Language: JavaScript
" Maintainer: crusoexia
" URL: https://github.com/crusoexia/vim-javascript-lib
syntax match jsPropertyAccessExpr /\.\<[a-zA-Z_$][0-9a-zA-Z_$]*\>/ contains=jsArrayMethods
" Native: {{{
syntax keyword jsArrayMethods length from isArray observe of concat copyWithin entries every fill filter find findIndex forEach includes indexOf join keys lastIndexOf map pop push reduce reduceRight reverse shift slice some sort splice toLocaleString toSource toString unshift values contained
" }}}
" 3rd party libraries: {{{
syntax keyword jsLibrary _ underscore lodash
syntax keyword jsLibrary jQuery Zepto $
syntax keyword jsLibrary Backbone nextgroup=jsBBoneAccessExpr
syntax keyword jsLibrary angular
syntax keyword jsLibrary Handlebars Mustache
syntax keyword jsLibrary jasmine
syntax keyword jsLibrary Q
syntax keyword jsLibrary sinon
syntax match jsBBoneAccessExpr /\./ contained nextgroup=jsBackboneClass
syntax keyword jsBackboneClass Model View Collection Events Router History contained
syntax match jsJQDelimiter /\v<\$/
" }}}
" Module: {{{
syntax keyword jsInclude require
syntax keyword jsDefine define exports module
syntax cluster jsModule contains=jsInclude,jsDefine
" }}}
" Testing: {{{
syntax keyword jsTesting describe it test before after beforeEach afterEach
" }}}
" Add to [vim-javascript](https://gi thub.com/pangloss/vim-javascript) syntax groups.
syntax cluster jsExpression add=jsLibrary,jsJQDelimiter,@jsModule,jsTesting,@jsNativeMethods,jsPropertyAccessExpr
if version >= 508 || !exists("did_javascript_lib_syn_inits")
if version < 508
let did_javascript_lib_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsLibrary Constant
HiLink jsBackboneClass Constant
HiLink jsJQDelimiter Delimiter
HiLink jsInclude Include
HiLink jsDefine Define
HiLink jsTesting Statement
HiLink jsArrayMethods Special
delcommand HiLink
endif

BIN
bundle/vim-javascript-lib/screenshots/after.png View File

Before After
Width: 1834  |  Height: 1260  |  Size: 308 KiB

BIN
bundle/vim-javascript-lib/screenshots/before.png View File

Before After
Width: 1832  |  Height: 1264  |  Size: 304 KiB

+ 13
- 0
bundle/vim-javascript/ISSUE_TEMPLATE.md View File

@ -0,0 +1,13 @@
*Requisite minimal reproducible example, formatted as plain text :*
<hr>
#### Optional: concerning jsx.
PLEASE PLEASE PLEASE make sure you have properly
setup and are sourcing this plugin https://github.com/mxw/vim-jsx
WE DO NOT support JSX automatically, you need another plugin to add get this
functionality.
Make sure the bug still exists if you disable all other javascript plugins
except the one noted above, mxw/vim-jsx

+ 119
- 0
bundle/vim-javascript/README.md View File

@ -0,0 +1,119 @@
# vim-javascript
JavaScript bundle for vim, this bundle provides syntax highlighting and
improved indentation.
## Installation
### Install with [pathogen](https://github.com/tpope/vim-pathogen)
git clone https://github.com/pangloss/vim-javascript.git ~/.vim/bundle/vim-javascript
alternatively, use a package manager like [vim-plug](https://github.com/junegunn/vim-plug)
## Configuration Variables
The following variables control certain syntax highlighting plugins. You can
add them to your `.vimrc` to enable their features.
-----------------
```
let g:javascript_plugin_jsdoc = 1
```
Enables syntax highlighting for [JSDocs](http://usejsdoc.org/).
Default Value: 0
-----------------
```
let g:javascript_plugin_ngdoc = 1
```
Enables some additional syntax highlighting for NGDocs. Requires JSDoc plugin
to be enabled as well.
Default Value: 0
-----------------
```
let g:javascript_plugin_flow = 1
```
Enables syntax highlighting for [Flow](https://flowtype.org/).
Default Value: 0
-----------------
```vim
augroup javascript_folding
au!
au FileType javascript setlocal foldmethod=syntax
augroup END
```
Enables code folding for javascript based on our syntax file.
Please note this can have a dramatic effect on performance.
## Concealing Characters
You can customize concealing characters, if your font provides the glyph you want, by defining one or more of the following
variables:
let g:javascript_conceal_function = "ƒ"
let g:javascript_conceal_null = "ø"
let g:javascript_conceal_this = "@"
let g:javascript_conceal_return = "⇚"
let g:javascript_conceal_undefined = "¿"
let g:javascript_conceal_NaN = "ℕ"
let g:javascript_conceal_prototype = "¶"
let g:javascript_conceal_static = "•"
let g:javascript_conceal_super = "Ω"
let g:javascript_conceal_arrow_function = "⇒"
let g:javascript_conceal_noarg_arrow_function = "🞅"
let g:javascript_conceal_underscore_arrow_function = "🞅"
You can enable concealing within VIM with:
set conceallevel=1
OR if you wish to toggle concealing you may wish to bind a command such as the following which will map `<LEADER>l` (leader is usually the `\` key) to toggling conceal mode:
map <leader>l :exec &conceallevel ? "set conceallevel=0" : "set conceallevel=1"<CR>
## Indentation Specific
* `:h cino-:`
* `:h cino-=`
* `:h cino-star`
* `:h cino-(`
* `:h cino-w`
* `:h cino-W`
* `:h cino-U`
* `:h cino-m`
* `:h cino-M`
* `:h 'indentkeys'`
## Contributing
Please follow the general code style
guides (read the code) and in your pull request explain the reason for the
proposed change and how it is valuable. All p.r.'s will be reviewed by a
maintainer(s) then, hopefully, merged.
Thank you!
## License
Distributed under the same terms as Vim itself. See `:help license`.

+ 12
- 0
bundle/vim-javascript/after/ftplugin/javascript.vim View File

@ -0,0 +1,12 @@
" Vim filetype plugin file
" Language: JavaScript
" Maintainer: vim-javascript community
" URL: https://github.com/pangloss/vim-javascript
setlocal iskeyword+=$ suffixesadd+=.js
if exists('b:undo_ftplugin')
let b:undo_ftplugin .= ' | setlocal iskeyword< suffixesadd<'
else
let b:undo_ftplugin = 'setlocal iskeyword< suffixesadd<'
endif

+ 16
- 0
bundle/vim-javascript/compiler/eslint.vim View File

@ -0,0 +1,16 @@
" Vim compiler plugin
" Language: JavaScript
" Maintainer: vim-javascript community
" URL: https://github.com/pangloss/vim-javascript
if exists("current_compiler")
finish
endif
let current_compiler = "eslint"
if exists(":CompilerSet") != 2
command! -nargs=* CompilerSet setlocal <args>
endif
CompilerSet makeprg=eslint\ -f\ compact\ %
CompilerSet errorformat=%f:\ line\ %l\\,\ col\ %c\\,\ %m

+ 8
- 0
bundle/vim-javascript/extras/ctags View File

@ -0,0 +1,8 @@
--langdef=js
--langmap=js:.js
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/,object/
--regex-js=/([A-Za-z0-9._$()]+)[ \t]*[:=][ \t]*function[ \t]*\(/\1/,function/
--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*([^)])/\1/,function/
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/,array/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^"]'[^']*/\1/,string/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^']"[^"]*/\1/,string/

+ 105
- 0
bundle/vim-javascript/extras/flow.vim View File

@ -0,0 +1,105 @@
syntax region jsFlowDefinition contained start=/:/ end=/\%(\s*[,=;)\n]\)\@=/ contains=@jsFlowCluster containedin=jsParen
syntax region jsFlowArgumentDef contained start=/:/ end=/\%(\s*[,)]\|=>\@!\)\@=/ contains=@jsFlowCluster
syntax region jsFlowArray contained matchgroup=jsFlowNoise start=/\[/ end=/\]/ contains=@jsFlowCluster,jsComment fold
syntax region jsFlowObject contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=@jsFlowCluster,jsComment fold
syntax region jsFlowExactObject contained matchgroup=jsFlowNoise start=/{|/ end=/|}/ contains=@jsFlowCluster,jsComment fold
syntax region jsFlowParens contained matchgroup=jsFlowNoise start=/(/ end=/)/ contains=@jsFlowCluster keepend fold
syntax match jsFlowNoise contained /[:;,<>]/
syntax keyword jsFlowType contained boolean number string null void any mixed JSON array Function object array bool class
syntax keyword jsFlowTypeof contained typeof skipempty skipempty nextgroup=jsFlowTypeCustom,jsFlowType
syntax match jsFlowTypeCustom contained /[0-9a-zA-Z_.]*/ skipwhite skipempty nextgroup=jsFlowGroup
syntax region jsFlowGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster
syntax region jsFlowArrowArguments contained matchgroup=jsFlowNoise start=/(/ end=/)\%(\s*=>\)\@=/ oneline skipwhite skipempty nextgroup=jsFlowArrow contains=@jsFlowCluster
syntax match jsFlowArrow contained /=>/ skipwhite skipempty nextgroup=jsFlowType,jsFlowTypeCustom,jsFlowParens
syntax match jsFlowObjectKey contained /[0-9a-zA-Z_$?]*\(\s*:\)\@=/ contains=jsFunctionKey,jsFlowMaybe skipwhite skipempty nextgroup=jsObjectValue containedin=jsObject
syntax match jsFlowOrOperator contained /|/ skipwhite skipempty nextgroup=@jsFlowCluster
syntax keyword jsFlowImportType contained type skipwhite skipempty nextgroup=jsModuleAsterisk,jsModuleKeyword,jsModuleGroup
syntax match jsFlowWildcard contained /*/
syntax match jsFlowReturn contained /:\s*/ contains=jsFlowNoise skipwhite skipempty nextgroup=@jsFlowReturnCluster,jsFlowArrow,jsFlowReturnParens
syntax region jsFlowReturnObject contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp fold
syntax region jsFlowReturnArray contained matchgroup=jsFlowNoise start=/\[/ end=/\]/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp fold
syntax region jsFlowReturnParens contained matchgroup=jsFlowNoise start=/(/ end=/)/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp,jsFlowReturnArrow fold
syntax match jsFlowReturnArrow contained /=>/ skipwhite skipempty nextgroup=@jsFlowReturnCluster
syntax match jsFlowReturnKeyword contained /\k\+/ contains=jsFlowType,jsFlowTypeCustom skipwhite skipempty nextgroup=jsFlowReturnGroup,jsFuncBlock,jsFlowReturnOrOp
syntax match jsFlowReturnMaybe contained /?/ skipwhite skipempty nextgroup=jsFlowReturnKeyword,jsFlowReturnObject
syntax region jsFlowReturnGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp
syntax match jsFlowReturnOrOp contained /\s*|\s*/ skipwhite skipempty nextgroup=@jsFlowReturnCluster
syntax match jsFlowWildcardReturn contained /*/ skipwhite skipempty nextgroup=jsFuncBlock
syntax region jsFlowFunctionGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncArgs
syntax region jsFlowClassGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsClassBlock
syntax region jsFlowTypeStatement start=/type\%(\s\+\k\)\@=/ end=/=\@=/ contains=jsFlowTypeOperator oneline skipwhite skipempty nextgroup=jsFlowTypeValue keepend
syntax region jsFlowTypeValue contained matchgroup=jsFlowNoise start=/=/ end=/[\n;]/ contains=@jsFlowCluster,jsFlowGroup,jsFlowMaybe
syntax match jsFlowTypeOperator contained /=/ containedin=jsFlowTypeValue
syntax match jsFlowTypeOperator contained /=/
syntax keyword jsFlowTypeKeyword contained type
syntax keyword jsFlowDeclare declare skipwhite skipempty nextgroup=jsFlowTypeStatement,jsClassDefinition,jsStorageClass,jsFlowModule,jsFlowInterface
syntax match jsFlowClassProperty contained /\<[0-9a-zA-Z_$]*\>:\@=/ skipwhite skipempty nextgroup=jsFlowClassDef containedin=jsClassBlock
syntax region jsFlowClassDef contained start=/:/ end=/\%(\s*[,=;)\n]\)\@=/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsClassValue
syntax region jsFlowModule contained start=/module/ end=/{\@=/ skipempty skipempty nextgroup=jsFlowDeclareBlock contains=jsString
syntax region jsFlowInterface contained start=/interface/ end=/{\@=/ skipempty skipempty nextgroup=jsFlowInterfaceBlock contains=@jsFlowCluster
syntax region jsFlowDeclareBlock contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=jsFlowDeclare,jsFlowNoise fold
" NOTE: It appears the nextgroup was causing a ton of breakages... testing it
" witout a nextgroup, but keeping this arround for reference incase something breaks
" syntax match jsFlowMaybe contained /?/ nextgroup=jsFlowType,jsFlowTypeCustom,jsFlowParens,jsFlowArrowArguments,jsFlowObject,jsFlowReturnObject extend keepend
syntax match jsFlowMaybe contained /?/
syntax region jsFlowInterfaceBlock contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectMethodType,jsGenerator,jsComment,jsObjectStringKey,jsSpreadExpression,jsFlowNoise keepend fold
syntax region jsFlowParenAnnotation contained start=/:/ end=/[,=)]\@=/ containedin=jsParen contains=@jsFlowCluster
syntax cluster jsFlowReturnCluster contains=jsFlowNoise,jsFlowReturnObject,jsFlowReturnArray,jsFlowReturnKeyword,jsFlowReturnGroup,jsFlowReturnMaybe,jsFlowReturnOrOp,jsFlowWildcardReturn,jsFlowReturnArrow
syntax cluster jsFlowCluster contains=jsFlowArray,jsFlowObject,jsFlowExactObject,jsFlowNoise,jsFlowTypeof,jsFlowType,jsFlowGroup,jsFlowArrowArguments,jsFlowMaybe,jsFlowParens,jsFlowOrOperator,jsFlowWildcard
if version >= 508 || !exists("did_javascript_syn_inits")
if version < 508
let did_javascript_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsFlowDefinition PreProc
HiLink jsFlowClassDef jsFlowDefinition
HiLink jsFlowArgumentDef jsFlowDefinition
HiLink jsFlowType Type
HiLink jsFlowTypeCustom PreProc
HiLink jsFlowTypeof PreProc
HiLink jsFlowArray PreProc
HiLink jsFlowObject PreProc
HiLink jsFlowExactObject PreProc
HiLink jsFlowParens PreProc
HiLink jsFlowGroup PreProc
HiLink jsFlowReturn PreProc
HiLink jsFlowParenAnnotation PreProc
HiLink jsFlowReturnObject jsFlowReturn
HiLink jsFlowReturnArray jsFlowArray
HiLink jsFlowReturnParens jsFlowParens
HiLink jsFlowReturnGroup jsFlowGroup
HiLink jsFlowFunctionGroup PreProc
HiLink jsFlowClassGroup PreProc
HiLink jsFlowArrowArguments PreProc
HiLink jsFlowArrow PreProc
HiLink jsFlowReturnArrow PreProc
HiLink jsFlowTypeStatement PreProc
HiLink jsFlowTypeKeyword PreProc
HiLink jsFlowTypeOperator Operator
HiLink jsFlowMaybe PreProc
HiLink jsFlowReturnMaybe PreProc
HiLink jsFlowClassProperty jsClassProperty
HiLink jsFlowDeclare PreProc
HiLink jsFlowModule PreProc
HiLink jsFlowInterface PreProc
HiLink jsFlowNoise Noise
HiLink jsFlowObjectKey jsObjectKey
HiLink jsFlowOrOperator jsOperator
HiLink jsFlowReturnOrOp jsFlowOrOperator
HiLink jsFlowWildcard PreProc
HiLink jsFlowWildcardReturn PreProc
HiLink jsFlowImportType PreProc
HiLink jsFlowTypeValue PreProc
delcommand HiLink
endif

+ 39
- 0
bundle/vim-javascript/extras/jsdoc.vim View File

@ -0,0 +1,39 @@
"" syntax coloring for javadoc comments (HTML)
syntax region jsComment matchgroup=jsComment start="/\*\s*" end="\*/" contains=jsDocTags,jsCommentTodo,jsCvsTag,@jsHtml,@Spell fold
" tags containing a param
syntax match jsDocTags contained "@\(alias\|api\|augments\|borrows\|class\|constructs\|default\|defaultvalue\|emits\|exception\|exports\|extends\|fires\|kind\|link\|listens\|member\|member[oO]f\|mixes\|module\|name\|namespace\|requires\|template\|throws\|var\|variation\|version\)\>" skipwhite nextgroup=jsDocParam
" tags containing type and param
syntax match jsDocTags contained "@\(arg\|argument\|cfg\|param\|property\|prop\|typedef\)\>" skipwhite nextgroup=jsDocType
" tags containing type but no param
syntax match jsDocTags contained "@\(callback\|define\|enum\|external\|implements\|this\|type\|return\|returns\)\>" skipwhite nextgroup=jsDocTypeNoParam
" tags containing references
syntax match jsDocTags contained "@\(lends\|see\|tutorial\)\>" skipwhite nextgroup=jsDocSeeTag
" other tags (no extra syntax)
syntax match jsDocTags contained "@\(abstract\|access\|accessor\|async\|author\|classdesc\|constant\|const\|constructor\|copyright\|deprecated\|desc\|description\|dict\|event\|example\|file\|file[oO]verview\|final\|function\|global\|ignore\|inheritDoc\|inner\|instance\|interface\|license\|localdoc\|method\|mixin\|nosideeffects\|override\|overview\|preserve\|private\|protected\|public\|readonly\|since\|static\|struct\|todo\|summary\|undocumented\|virtual\)\>"
syntax region jsDocType contained matchgroup=jsDocTypeBrackets start="{" end="}" contains=jsDocTypeRecord oneline skipwhite nextgroup=jsDocParam
syntax match jsDocType contained "\%(#\|\"\|\w\|\.\|:\|\/\)\+" skipwhite nextgroup=jsDocParam
syntax region jsDocTypeRecord contained start=/{/ end=/}/ contains=jsDocTypeRecord extend
syntax region jsDocTypeRecord contained start=/\[/ end=/\]/ contains=jsDocTypeRecord extend
syntax region jsDocTypeNoParam contained start="{" end="}" oneline
syntax match jsDocTypeNoParam contained "\%(#\|\"\|\w\|\.\|:\|\/\)\+"
syntax match jsDocParam contained "\%(#\|\$\|-\|'\|\"\|{.\{-}}\|\w\|\.\|:\|\/\|\[.\{-}]\|=\)\+"
syntax region jsDocSeeTag contained matchgroup=jsDocSeeTag start="{" end="}" contains=jsDocTags
if version >= 508 || !exists("did_javascript_syn_inits")
if version < 508
let did_javascript_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsDocTags Special
HiLink jsDocSeeTag Function
HiLink jsDocType Type
HiLink jsDocTypeBrackets jsDocType
HiLink jsDocTypeRecord jsDocType
HiLink jsDocTypeNoParam Type
HiLink jsDocParam Label
delcommand HiLink
endif

+ 3
- 0
bundle/vim-javascript/extras/ngdoc.vim View File

@ -0,0 +1,3 @@
syntax match jsDocTags contained /@\(link\|method[oO]f\|ngdoc\|ng[iI]nject\|restrict\)/ nextgroup=jsDocParam skipwhite
syntax match jsDocType contained "\%(#\|\$\|\w\|\"\|-\|\.\|:\|\/\)\+" nextgroup=jsDocParam skipwhite
syntax match jsDocParam contained "\%(#\|\$\|\w\|\"\|-\|\.\|:\|{\|}\|\/\|\[\|]\|=\)\+"

+ 17
- 0
bundle/vim-javascript/ftdetect/javascript.vim View File

@ -0,0 +1,17 @@
au BufNewFile,BufRead *.{js,mjs,jsm,es,es6},Jakefile setf javascript
fun! s:SourceFlowSyntax()
if !exists('javascript_plugin_flow') && !exists('b:flow_active') &&
\ search('\v\C%^\_s*%(//\s*|/\*[ \t\n*]*)\@flow>','nw')
runtime extras/flow.vim
let b:flow_active = 1
endif
endfun
au FileType javascript au BufRead,BufWritePost <buffer> call s:SourceFlowSyntax()
fun! s:SelectJavascript()
if getline(1) =~# '^#!.*/bin/\%(env\s\+\)\?node\>'
set ft=javascript
endif
endfun
au BufNewFile,BufRead * call s:SelectJavascript()

+ 475
- 0
bundle/vim-javascript/indent/javascript.vim View File

@ -0,0 +1,475 @@
" Vim indent file
" Language: Javascript
" Maintainer: Chris Paul ( https://github.com/bounceme )
" URL: https://github.com/pangloss/vim-javascript
" Last Change: September 18, 2017
" Only load this indent file when no other was loaded.
if exists('b:did_indent')
finish
endif
let b:did_indent = 1
" Now, set up our indentation expression and keys that trigger it.
setlocal indentexpr=GetJavascriptIndent()
setlocal autoindent nolisp nosmartindent
setlocal indentkeys+=0],0)
" Testable with something like:
" vim -eNs "+filetype plugin indent on" "+syntax on" "+set ft=javascript" \
" "+norm! gg=G" '+%print' '+:q!' testfile.js \
" | diff -uBZ testfile.js -
let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<'
" Only define the function once.
if exists('*GetJavascriptIndent')
finish
endif
let s:cpo_save = &cpo
set cpo&vim
" indent correctly if inside <script>
" vim/vim@690afe1 for the switch from cindent
" overridden with b:html_indent_script1
call extend(g:,{'html_indent_script1': 'inc'},'keep')
" Regex of syntax group names that are or delimit string or are comments.
let s:bvars = {
\ 'syng_strcom': 'string\|comment\|regex\|special\|doc\|template\%(braces\)\@!',
\ 'syng_str': 'string\|template\|special' }
" template strings may want to be excluded when editing graphql:
" au! Filetype javascript let b:syng_str = '^\%(.*template\)\@!.*string\|special'
" au! Filetype javascript let b:syng_strcom = '^\%(.*template\)\@!.*string\|comment\|regex\|special\|doc'
function s:GetVars()
call extend(b:,extend(s:bvars,{'js_cache': [0,0,0]}),'keep')
endfunction
" Get shiftwidth value
if exists('*shiftwidth')
function s:sw()
return shiftwidth()
endfunction
else
function s:sw()
return &l:shiftwidth ? &l:shiftwidth : &l:tabstop
endfunction
endif
" Performance for forwards search(): start search at pos rather than masking
" matches before pos.
let s:z = has('patch-7.4.984') ? 'z' : ''
" Expression used to check whether we should skip a match with searchpair().
let s:skip_expr = "s:SynAt(line('.'),col('.')) =~? b:syng_strcom"
let s:in_comm = s:skip_expr[:-14] . "'comment\\|doc'"
let s:rel = has('reltime')
" searchpair() wrapper
if s:rel
function s:GetPair(start,end,flags,skip)
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1,a:skip ==# 's:SkipFunc()' ? 2000 : 200)
endfunction
else
function s:GetPair(start,end,flags,skip)
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1)
endfunction
endif
function s:SynAt(l,c)
let byte = line2byte(a:l) + a:c - 1
let pos = index(s:synid_cache[0], byte)
if pos == -1
let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]]
endif
return s:synid_cache[1][pos]
endfunction
function s:ParseCino(f)
let [divider, n, cstr] = [0] + matchlist(&cino,
\ '\%(.*,\)\=\%(\%d'.char2nr(a:f).'\(-\)\=\([.s0-9]*\)\)\=')[1:2]
for c in split(cstr,'\zs')
if c == '.' && !divider
let divider = 1
elseif c ==# 's'
if n !~ '\d'
return n . s:sw() + 0
endif
let n = str2nr(n) * s:sw()
break
else
let [n, divider] .= [c, 0]
endif
endfor
return str2nr(n) / max([str2nr(divider),1])
endfunction
" Optimized {skip} expr, only callable from the search loop which
" GetJavascriptIndent does to find the containing [[{(] (side-effects)
function s:SkipFunc()
if s:top_col == 1
throw 'out of bounds'
elseif s:check_in
if eval(s:skip_expr)
return 1
endif
let s:check_in = 0
elseif getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$'
if eval(s:skip_expr)
return 1
endif
elseif search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn)
if eval(s:skip_expr)
let s:check_in = 1
return 1
endif
else
let s:synid_cache[:] += [[line2byte('.') + col('.') - 1], ['']]
endif
let [s:looksyn, s:top_col] = getpos('.')[1:2]
endfunction
function s:AlternatePair()
let [pat, l:for] = ['[][(){};]', 2]
while s:SearchLoop(pat,'bW','s:SkipFunc()')
if s:LookingAt() == ';'
if !l:for
if s:GetPair('{','}','bW','s:SkipFunc()')
return
endif
break
else
let [pat, l:for] = ['[{}();]', l:for - 1]
endif
else
let idx = stridx('])}',s:LookingAt())
if idx == -1
return
elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
break
endif
endif
endwhile
throw 'out of bounds'
endfunction
function s:Nat(int)
return a:int * (a:int > 0)
endfunction
function s:LookingAt()
return getline('.')[col('.')-1]
endfunction
function s:Token()
return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt()
endfunction
function s:PreviousToken(...)
let [l:pos, tok] = [getpos('.'), '']
if search('\m\k\{1,}\|\S','ebW')
if getline('.')[col('.')-2:col('.')-1] == '*/'
if eval(s:in_comm) && !s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm)
call setpos('.',l:pos)
else
let tok = s:Token()
endif
else
let two = a:0 || line('.') != l:pos[1] ? strridx(getline('.')[:col('.')],'//') + 1 : 0
if two && eval(s:in_comm)
call cursor(0,two)
let tok = s:PreviousToken(1)
if tok is ''
call setpos('.',l:pos)
endif
else
let tok = s:Token()
endif
endif
endif
return tok
endfunction
function s:Pure(f,...)
return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]")
endfunction
function s:SearchLoop(pat,flags,expr)
return s:GetPair(a:pat,'\_$.',a:flags,a:expr)
endfunction
function s:ExprCol()
if getline('.')[col('.')-2] == ':'
return 1
endif
let bal = 0
while s:SearchLoop('[{}?:]','bW',s:skip_expr)
if s:LookingAt() == ':'
if getline('.')[col('.')-2] == ':'
call cursor(0,col('.')-1)
continue
endif
let bal -= 1
elseif s:LookingAt() == '?'
if getline('.')[col('.'):col('.')+1] =~ '^\.\d\@!'
continue
elseif !bal
return 1
endif
let bal += 1
elseif s:LookingAt() == '{'
return !s:IsBlock()
elseif !s:GetPair('{','}','bW',s:skip_expr)
break
endif
endwhile
endfunction
" configurable regexes that define continuation lines, not including (, {, or [.
let s:opfirst = '^' . get(g:,'javascript_opfirst',
\ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)')
let s:continuation = get(g:,'javascript_continuation',
\ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$'
function s:Continues()
let tok = matchstr(strpart(getline('.'),col('.')-15,15),s:continuation)
if tok =~ '[a-z:]'
return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.'
elseif tok !~ '[/>]'
return tok isnot ''
endif
return s:SynAt(line('.'),col('.')) !~? (tok == '>' ? 'jsflow\|^html' : 'regex')
endfunction
" Check if line 'lnum' has a balanced amount of parentheses.
function s:Balanced(lnum,line)
let l:open = 0
let pos = match(a:line, '[][(){}]')
while pos != -1
if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom
let l:open += match(' ' . a:line[pos],'[[({]')
if l:open < 0
return
endif
endif
let pos = match(a:line, !l:open ? '[][(){}]' : '()' =~ a:line[pos] ?
\ '[()]' : '{}' =~ a:line[pos] ? '[{}]' : '[][]', pos + 1)
endwhile
return !l:open
endfunction
function s:OneScope()
if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr)
let tok = s:PreviousToken()
return (count(split('for if let while with'),tok) ||
\ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') &&
\ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile())
elseif s:Token() =~# '^else$\|^do$'
return s:Pure('s:PreviousToken') != '.'
endif
return strpart(getline('.'),col('.')-2,2) == '=>'
endfunction
function s:DoWhile()
let cpos = searchpos('\m\<','cbW')
while s:SearchLoop('\C[{}]\|\<\%(do\|while\)\>','bW',s:skip_expr)
if s:LookingAt() =~ '\a'
if s:Pure('s:IsBlock')
if s:LookingAt() ==# 'd'
return 1
endif
break
endif
elseif s:LookingAt() != '}' || !s:GetPair('{','}','bW',s:skip_expr)
break
endif
endwhile
call call('cursor',cpos)
endfunction
" returns total offset from braceless contexts. 'num' is the lineNr which
" encloses the entire context, 'cont' if whether a:firstline is a continued
" expression, which could have started in a braceless context
function s:IsContOne(cont)
let [l:num, b_l] = [b:js_cache[1] + !b:js_cache[1], 0]
let pind = b:js_cache[1] ? indent(b:js_cache[1]) + s:sw() : 0
let ind = indent('.') + !a:cont
while line('.') > l:num && ind > pind || line('.') == l:num
if indent('.') < ind && s:OneScope()
let b_l += 1
elseif !a:cont || b_l || ind < indent(a:firstline)
break
else
call cursor(0,1)
endif
let ind = min([ind, indent('.')])
if s:PreviousToken() is ''
break
endif
endwhile
return b_l
endfunction
function s:IsSwitch()
call call('cursor',b:js_cache[1:])
return search('\m\C\%#.\_s*\%(\%(\/\/.*\_$\|\/\*\_.\{-}\*\/\)\@>\_s*\)*\%(case\|default\)\>','nWc'.s:z)
endfunction
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
function s:IsBlock()
let tok = s:PreviousToken()
if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx'
let s:in_jsx = 1
return tok != '{'
elseif tok =~ '\k'
if tok ==# 'type'
return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'")
elseif tok ==# 'of'
return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||"
\ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'")
endif
return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof')
\ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.'
elseif tok == '>'
return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html'
elseif tok == '*'
return s:Pure('s:PreviousToken') == ':'
elseif tok == ':'
return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()")
elseif tok == '/'
return s:SynAt(line('.'),col('.')) =~? 'regex'
elseif tok !~ '[=~!<,.?^%|&([]'
return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok
endif
endfunction
function GetJavascriptIndent()
call s:GetVars()
let s:synid_cache = [[],[]]
let l:line = getline(v:lnum)
" use synstack as it validates syn state and works in an empty line
let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')")
" start with strings,comments,etc.
if s:stack[-1] =~? 'comment\|doc'
if l:line =~ '^\s*\*'
return cindent(v:lnum)
elseif l:line !~ '^\s*\/[/*]'
return -1
endif
elseif s:stack[-1] =~? b:syng_str
if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1,getline(v:lnum-1))
let b:js_cache[0] = v:lnum
endif
return -1
endif
let s:l1 = max([0,prevnonblank(v:lnum) - (s:rel ? 2000 : 1000),
\ get(get(b:,'hi_indent',{}),'blocklnr')])
call cursor(v:lnum,1)
if s:PreviousToken() is ''
return
endif
let [l:lnum, pline] = [line('.'), getline('.')[:col('.')-1]]
let l:line = substitute(l:line,'^\s*','','')
let l:line_raw = l:line
if l:line[:1] == '/*'
let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','')
endif
if l:line =~ '^\/[/*]'
let l:line = ''
endif
" the containing paren, bracket, or curly. Many hacks for performance
call cursor(v:lnum,1)
let idx = index([']',')','}'],l:line[0])
if b:js_cache[0] > l:lnum && b:js_cache[0] < v:lnum ||
\ b:js_cache[0] == l:lnum && s:Balanced(l:lnum,pline)
call call('cursor',b:js_cache[1:])
else
let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0,
\ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])]
try
if idx != -1
call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$'
call s:GetPair('{','}','bW','s:SkipFunc()')
else
call s:AlternatePair()
endif
catch /^\Cout of bounds$/
call cursor(v:lnum,1)
endtry
let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2]
endif
let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]]
let [num_ind, is_op, b_l, l:switch_offset, s:in_jsx] = [s:Nat(indent(num)),0,0,0,0]
if !num || s:LookingAt() == '{' && s:IsBlock()
let ilnum = line('.')
if num && !s:in_jsx && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr)
if ilnum == num
let [num, num_ind] = [line('.'), indent('.')]
endif
if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch()
let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':')
if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>'
return s:Nat(num_ind + l:switch_offset)
elseif &cino =~ '='
let l:case_offset = s:ParseCino('=')
endif
endif
endif
if idx == -1 && pline[-1:] !~ '[{;]'
call cursor(l:lnum, len(pline))
let sol = matchstr(l:line,s:opfirst)
if sol is '' || sol == '/' && s:SynAt(v:lnum,
\ 1 + len(getline(v:lnum)) - len(l:line)) =~? 'regex'
if s:Continues()
let is_op = s:sw()
endif
elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$' &&
\ s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) &&
\ s:PreviousToken() == ')' && s:GetPair('(',')','bW',s:skip_expr) &&
\ (s:PreviousToken() == ']' || s:LookingAt() =~ '\k' &&
\ s:{s:PreviousToken() == '*' ? 'Previous' : ''}Token() !=# 'function')
return num_ind + s:sw()
else
let is_op = s:sw()
endif
call cursor(l:lnum, len(pline))
let b_l = s:Nat(s:IsContOne(is_op) - (!is_op && l:line =~ '^{')) * s:sw()
endif
elseif idx.s:LookingAt().&cino =~ '^-1(.*(' && (search('\m\S','nbW',num) || s:ParseCino('U'))
let pval = s:ParseCino('(')
if !pval
let [Wval, vcol] = [s:ParseCino('W'), virtcol('.')]
if search('\m\S','W',num)
return s:ParseCino('w') ? vcol : virtcol('.')-1
endif
return Wval ? s:Nat(num_ind + Wval) : vcol
endif
return s:Nat(num_ind + pval + searchpair('\m(','','\m)','nbrmW',s:skip_expr,num) * s:sw())
endif
" main return
if l:line =~ '^[])}]\|^|}'
if l:line_raw[0] == ')'
if s:ParseCino('M')
return indent(l:lnum)
elseif num && &cino =~# 'm' && !s:ParseCino('m')
return virtcol('.') - 1
endif
endif
return num_ind
elseif num
return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op)
endif
return b_l + is_op
endfunction
let &cpo = s:cpo_save
unlet s:cpo_save

+ 387
- 0
bundle/vim-javascript/syntax/javascript.vim View File

@ -0,0 +1,387 @@
" Vim syntax file
" Language: JavaScript
" Maintainer: vim-javascript community
" URL: https://github.com/pangloss/vim-javascript
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = 'javascript'
endif
" Dollar sign is permitted anywhere in an identifier
if v:version > 704 || v:version == 704 && has('patch1142')
syntax iskeyword @,48-57,_,192-255,$
else
setlocal iskeyword+=$
endif
syntax sync fromstart
" TODO: Figure out what type of casing I need
" syntax case ignore
syntax case match
syntax match jsNoise /[:,;]/
syntax match jsNoise /\./ skipwhite skipempty nextgroup=jsObjectProp,jsFuncCall,jsPrototype,jsTaggedTemplate
syntax match jsObjectProp contained /\<\K\k*/
syntax match jsFuncCall /\<\K\k*\ze\s*(/
syntax match jsParensError /[)}\]]/
" Program Keywords
syntax keyword jsStorageClass const var let skipwhite skipempty nextgroup=jsDestructuringBlock,jsDestructuringArray,jsVariableDef
syntax match jsVariableDef contained /\<\K\k*/ skipwhite skipempty nextgroup=jsFlowDefinition
syntax keyword jsOperator delete instanceof typeof void new in of skipwhite skipempty nextgroup=@jsExpression
syntax match jsOperator "[-!|&+<>=%/*~^]" skipwhite skipempty nextgroup=@jsExpression
syntax match jsOperator /::/ skipwhite skipempty nextgroup=@jsExpression
syntax keyword jsBooleanTrue true
syntax keyword jsBooleanFalse false
" Modules
syntax keyword jsImport import skipwhite skipempty nextgroup=jsModuleAsterisk,jsModuleKeyword,jsModuleGroup,jsFlowImportType
syntax keyword jsExport export skipwhite skipempty nextgroup=@jsAll,jsModuleGroup,jsExportDefault,jsModuleAsterisk,jsModuleKeyword,jsFlowTypeStatement
syntax match jsModuleKeyword contained /\<\K\k*/ skipwhite skipempty nextgroup=jsModuleAs,jsFrom,jsModuleComma
syntax keyword jsExportDefault contained default skipwhite skipempty nextgroup=@jsExpression
syntax keyword jsExportDefaultGroup contained default skipwhite skipempty nextgroup=jsModuleAs,jsFrom,jsModuleComma
syntax match jsModuleAsterisk contained /\*/ skipwhite skipempty nextgroup=jsModuleKeyword,jsModuleAs,jsFrom
syntax keyword jsModuleAs contained as skipwhite skipempty nextgroup=jsModuleKeyword,jsExportDefaultGroup
syntax keyword jsFrom contained from skipwhite skipempty nextgroup=jsString
syntax match jsModuleComma contained /,/ skipwhite skipempty nextgroup=jsModuleKeyword,jsModuleAsterisk,jsModuleGroup,jsFlowTypeKeyword
" Strings, Templates, Numbers
syntax region jsString start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial,@Spell extend
syntax region jsTemplateString start=+`+ skip=+\\`+ end=+`+ contains=jsTemplateExpression,jsSpecial,@Spell extend
syntax match jsTaggedTemplate /\<\K\k*\ze`/ nextgroup=jsTemplateString
syntax match jsNumber /\c\<\%(\d\+\%(e[+-]\=\d\+\)\=\|0b[01]\+\|0o\o\+\|0x\x\+\)\>/
syntax keyword jsNumber Infinity
syntax match jsFloat /\c\<\%(\d\+\.\d\+\|\d\+\.\|\.\d\+\)\%(e[+-]\=\d\+\)\=\>/
" Regular Expressions
syntax match jsSpecial contained "\v\\%(x\x\x|u%(\x{4}|\{\x{4,5}})|c\u|.)"
syntax region jsTemplateExpression contained matchgroup=jsTemplateBraces start=+${+ end=+}+ contains=@jsExpression keepend
syntax region jsRegexpCharClass contained start=+\[+ skip=+\\.+ end=+\]+ contains=jsSpecial
syntax match jsRegexpBoundary contained "\v\c[$^]|\\b"
syntax match jsRegexpBackRef contained "\v\\[1-9]\d*"
syntax match jsRegexpQuantifier contained "\v[^\\]%([?*+]|\{\d+%(,\d*)?})\??"lc=1
syntax match jsRegexpOr contained "|"
syntax match jsRegexpMod contained "\v\(\?[:=!>]"lc=1
syntax region jsRegexpGroup contained start="[^\\]("lc=1 skip="\\.\|\[\(\\.\|[^]]\+\)\]" end=")" contains=jsRegexpCharClass,@jsRegexpSpecial keepend
syntax region jsRegexpString start=+\%(\%(\<return\|\<typeof\|\_[^)\]'"[:blank:][:alnum:]_$]\)\s*\)\@<=/\ze[^*/]+ skip=+\\.\|\[[^]]\{1,}\]+ end=+/[gimyus]\{,6}+ contains=jsRegexpCharClass,jsRegexpGroup,@jsRegexpSpecial oneline keepend extend
syntax cluster jsRegexpSpecial contains=jsSpecial,jsRegexpBoundary,jsRegexpBackRef,jsRegexpQuantifier,jsRegexpOr,jsRegexpMod
" Objects
syntax match jsObjectKey contained /\<\k*\ze\s*:/ contains=jsFunctionKey skipwhite skipempty nextgroup=jsObjectValue
syntax match jsObjectColon contained /:/ skipwhite skipempty
syntax region jsObjectKeyString contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial,@Spell skipwhite skipempty nextgroup=jsObjectValue
syntax region jsObjectKeyComputed contained matchgroup=jsBrackets start=/\[/ end=/]/ contains=@jsExpression skipwhite skipempty nextgroup=jsObjectValue,jsFuncArgs extend
syntax match jsObjectSeparator contained /,/
syntax region jsObjectValue contained matchgroup=jsNoise start=/:/ end=/[,}]\@=/ contains=@jsExpression extend
syntax match jsObjectFuncName contained /\<\K\k*\ze\_s*(/ skipwhite skipempty nextgroup=jsFuncArgs
syntax match jsFunctionKey contained /\<\K\k*\ze\s*:\s*function\>/
syntax match jsObjectMethodType contained /\<[gs]et\ze\s\+\K\k*/ skipwhite skipempty nextgroup=jsObjectFuncName
syntax region jsObjectStringKey contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs,jsObjectValue
exe 'syntax keyword jsNull null '.(exists('g:javascript_conceal_null') ? 'conceal cchar='.g:javascript_conceal_null : '')
exe 'syntax keyword jsReturn return contained '.(exists('g:javascript_conceal_return') ? 'conceal cchar='.g:javascript_conceal_return : '').' skipwhite skipempty nextgroup=@jsExpression'
exe 'syntax keyword jsUndefined undefined '.(exists('g:javascript_conceal_undefined') ? 'conceal cchar='.g:javascript_conceal_undefined : '')
exe 'syntax keyword jsNan NaN '.(exists('g:javascript_conceal_NaN') ? 'conceal cchar='.g:javascript_conceal_NaN : '')
exe 'syntax keyword jsPrototype prototype '.(exists('g:javascript_conceal_prototype') ? 'conceal cchar='.g:javascript_conceal_prototype : '')
exe 'syntax keyword jsThis this '.(exists('g:javascript_conceal_this') ? 'conceal cchar='.g:javascript_conceal_this : '')
exe 'syntax keyword jsSuper super contained '.(exists('g:javascript_conceal_super') ? 'conceal cchar='.g:javascript_conceal_super : '')
" Statement Keywords
syntax match jsBlockLabel /\<\K\k*\s*::\@!/ contains=jsNoise skipwhite skipempty nextgroup=jsBlock
syntax match jsBlockLabelKey contained /\<\K\k*\ze\s*\_[;]/
syntax keyword jsStatement contained with yield debugger
syntax keyword jsStatement contained break continue skipwhite skipempty nextgroup=jsBlockLabelKey
syntax keyword jsConditional if skipwhite skipempty nextgroup=jsParenIfElse
syntax keyword jsConditional else skipwhite skipempty nextgroup=jsCommentIfElse,jsIfElseBlock
syntax keyword jsConditional switch skipwhite skipempty nextgroup=jsParenSwitch
syntax keyword jsRepeat while for skipwhite skipempty nextgroup=jsParenRepeat,jsForAwait
syntax keyword jsDo do skipwhite skipempty nextgroup=jsRepeatBlock
syntax region jsSwitchCase contained matchgroup=jsLabel start=/\<\%(case\|default\)\>/ end=/:\@=/ contains=@jsExpression,jsLabel skipwhite skipempty nextgroup=jsSwitchColon keepend
syntax keyword jsTry try skipwhite skipempty nextgroup=jsTryCatchBlock
syntax keyword jsFinally contained finally skipwhite skipempty nextgroup=jsFinallyBlock
syntax keyword jsCatch contained catch skipwhite skipempty nextgroup=jsParenCatch
syntax keyword jsException throw
syntax keyword jsAsyncKeyword async await
syntax match jsSwitchColon contained /::\@!/ skipwhite skipempty nextgroup=jsSwitchBlock
" Keywords
syntax keyword jsGlobalObjects Array Boolean Date Function Iterator Number Object Symbol Map WeakMap Set WeakSet RegExp String Proxy Promise Buffer ParallelArray ArrayBuffer DataView Float32Array Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array Uint8Array Uint8ClampedArray JSON Math console document window Intl Collator DateTimeFormat NumberFormat fetch
syntax keyword jsGlobalNodeObjects module exports global process __dirname __filename
syntax match jsGlobalNodeObjects /\<require\>/ containedin=jsFuncCall
syntax keyword jsExceptions Error EvalError InternalError RangeError ReferenceError StopIteration SyntaxError TypeError URIError
syntax keyword jsBuiltins decodeURI decodeURIComponent encodeURI encodeURIComponent eval isFinite isNaN parseFloat parseInt uneval
" DISCUSS: How imporant is this, really? Perhaps it should be linked to an error because I assume the keywords are reserved?
syntax keyword jsFutureKeys abstract enum int short boolean interface byte long char final native synchronized float package throws goto private transient implements protected volatile double public
" DISCUSS: Should we really be matching stuff like this?
" DOM2 Objects
syntax keyword jsGlobalObjects DOMImplementation DocumentFragment Document Node NodeList NamedNodeMap CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction
syntax keyword jsExceptions DOMException
" DISCUSS: Should we really be matching stuff like this?
" DOM2 CONSTANT
syntax keyword jsDomErrNo INDEX_SIZE_ERR DOMSTRING_SIZE_ERR HIERARCHY_REQUEST_ERR WRONG_DOCUMENT_ERR INVALID_CHARACTER_ERR NO_DATA_ALLOWED_ERR NO_MODIFICATION_ALLOWED_ERR NOT_FOUND_ERR NOT_SUPPORTED_ERR INUSE_ATTRIBUTE_ERR INVALID_STATE_ERR SYNTAX_ERR INVALID_MODIFICATION_ERR NAMESPACE_ERR INVALID_ACCESS_ERR
syntax keyword jsDomNodeConsts ELEMENT_NODE ATTRIBUTE_NODE TEXT_NODE CDATA_SECTION_NODE ENTITY_REFERENCE_NODE ENTITY_NODE PROCESSING_INSTRUCTION_NODE COMMENT_NODE DOCUMENT_NODE DOCUMENT_TYPE_NODE DOCUMENT_FRAGMENT_NODE NOTATION_NODE
" DISCUSS: Should we really be special matching on these props?
" HTML events and internal variables
syntax keyword jsHtmlEvents onblur onclick oncontextmenu ondblclick onfocus onkeydown onkeypress onkeyup onmousedown onmousemove onmouseout onmouseover onmouseup onresize
" Code blocks
syntax region jsBracket matchgroup=jsBrackets start=/\[/ end=/\]/ contains=@jsExpression,jsSpreadExpression extend fold
syntax region jsParen matchgroup=jsParens start=/(/ end=/)/ contains=@jsExpression extend fold nextgroup=jsFlowDefinition
syntax region jsParenDecorator contained matchgroup=jsParensDecorator start=/(/ end=/)/ contains=@jsAll extend fold
syntax region jsParenIfElse contained matchgroup=jsParensIfElse start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsCommentIfElse,jsIfElseBlock extend fold
syntax region jsParenRepeat contained matchgroup=jsParensRepeat start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsCommentRepeat,jsRepeatBlock extend fold
syntax region jsParenSwitch contained matchgroup=jsParensSwitch start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsSwitchBlock extend fold
syntax region jsParenCatch contained matchgroup=jsParensCatch start=/(/ end=/)/ skipwhite skipempty nextgroup=jsTryCatchBlock extend fold
syntax region jsFuncArgs contained matchgroup=jsFuncParens start=/(/ end=/)/ contains=jsFuncArgCommas,jsComment,jsFuncArgExpression,jsDestructuringBlock,jsDestructuringArray,jsRestExpression,jsFlowArgumentDef skipwhite skipempty nextgroup=jsCommentFunction,jsFuncBlock,jsFlowReturn extend fold
syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodType,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsClassStringKey,jsAsyncKeyword,jsNoise extend fold
syntax region jsFuncBlock contained matchgroup=jsFuncBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsIfElseBlock contained matchgroup=jsIfElseBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsTryCatchBlock contained matchgroup=jsTryCatchBraces start=/{/ end=/}/ contains=@jsAll,jsBlock skipwhite skipempty nextgroup=jsCatch,jsFinally extend fold
syntax region jsFinallyBlock contained matchgroup=jsFinallyBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsSwitchBlock contained matchgroup=jsSwitchBraces start=/{/ end=/}/ contains=@jsAll,jsBlock,jsSwitchCase extend fold
syntax region jsRepeatBlock contained matchgroup=jsRepeatBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsDestructuringBlock contained matchgroup=jsDestructuringBraces start=/{/ end=/}/ contains=jsDestructuringProperty,jsDestructuringAssignment,jsDestructuringNoise,jsDestructuringPropertyComputed,jsSpreadExpression,jsComment extend fold
syntax region jsDestructuringArray contained matchgroup=jsDestructuringBraces start=/\[/ end=/\]/ contains=jsDestructuringPropertyValue,jsNoise,jsDestructuringProperty,jsSpreadExpression,jsComment extend fold
syntax region jsObject contained matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectMethodType,jsGenerator,jsComment,jsObjectStringKey,jsSpreadExpression,jsDecorator,jsAsyncKeyword extend fold
syntax region jsBlock matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll,jsSpreadExpression extend fold
syntax region jsModuleGroup contained matchgroup=jsModuleBraces start=/{/ end=/}/ contains=jsModuleKeyword,jsModuleComma,jsModuleAs,jsComment,jsFlowTypeKeyword skipwhite skipempty nextgroup=jsFrom fold
syntax region jsSpreadExpression contained matchgroup=jsSpreadOperator start=/\.\.\./ end=/[,}\]]\@=/ contains=@jsExpression
syntax region jsRestExpression contained matchgroup=jsRestOperator start=/\.\.\./ end=/[,)]\@=/
syntax region jsTernaryIf matchgroup=jsTernaryIfOperator start=/?/ end=/\%(:\|}\@=\)/ contains=@jsExpression extend skipwhite skipempty nextgroup=@jsExpression
syntax match jsOperator /?\.\ze\_D/
syntax match jsGenerator contained /\*/ skipwhite skipempty nextgroup=jsFuncName,jsFuncArgs,jsFlowFunctionGroup
syntax match jsFuncName contained /\<\K\k*/ skipwhite skipempty nextgroup=jsFuncArgs,jsFlowFunctionGroup
syntax region jsFuncArgExpression contained matchgroup=jsFuncArgOperator start=/=/ end=/[,)]\@=/ contains=@jsExpression extend
syntax match jsFuncArgCommas contained ','
syntax keyword jsArguments contained arguments
syntax keyword jsForAwait contained await skipwhite skipempty nextgroup=jsParenRepeat
" Matches a single keyword argument with no parens
syntax match jsArrowFuncArgs /\<\K\k*\ze\s*=>/ skipwhite contains=jsFuncArgs skipwhite skipempty nextgroup=jsArrowFunction extend
" Matches a series of arguments surrounded in parens
syntax match jsArrowFuncArgs /([^()]*)\ze\s*=>/ contains=jsFuncArgs skipempty skipwhite nextgroup=jsArrowFunction extend
exe 'syntax match jsFunction /\<function\>/ skipwhite skipempty nextgroup=jsGenerator,jsFuncName,jsFuncArgs,jsFlowFunctionGroup skipwhite '.(exists('g:javascript_conceal_function') ? 'conceal cchar='.g:javascript_conceal_function : '')
exe 'syntax match jsArrowFunction /=>/ skipwhite skipempty nextgroup=jsFuncBlock,jsCommentFunction '.(exists('g:javascript_conceal_arrow_function') ? 'conceal cchar='.g:javascript_conceal_arrow_function : '')
exe 'syntax match jsArrowFunction /()\ze\s*=>/ skipwhite skipempty nextgroup=jsArrowFunction '.(exists('g:javascript_conceal_noarg_arrow_function') ? 'conceal cchar='.g:javascript_conceal_noarg_arrow_function : '')
exe 'syntax match jsArrowFunction /_\ze\s*=>/ skipwhite skipempty nextgroup=jsArrowFunction '.(exists('g:javascript_conceal_underscore_arrow_function') ? 'conceal cchar='.g:javascript_conceal_underscore_arrow_function : '')
" Classes
syntax keyword jsClassKeyword contained class
syntax keyword jsExtendsKeyword contained extends skipwhite skipempty nextgroup=@jsExpression
syntax match jsClassNoise contained /\./
syntax match jsClassMethodType contained /\<\%([gs]et\|static\)\ze\s\+\K\k*/ skipwhite skipempty nextgroup=jsAsyncKeyword,jsFuncName,jsClassProperty
syntax region jsClassDefinition start=/\<class\>/ end=/\(\<extends\>\s\+\)\@<!{\@=/ contains=jsClassKeyword,jsExtendsKeyword,jsClassNoise,@jsExpression,jsFlowClassGroup skipwhite skipempty nextgroup=jsCommentClass,jsClassBlock,jsFlowClassGroup
syntax match jsClassProperty contained /\<\K\k*\ze\s*=/ skipwhite skipempty nextgroup=jsClassValue,jsFlowClassDef
syntax region jsClassValue contained start=/=/ end=/\_[;}]\@=/ contains=@jsExpression
syntax region jsClassPropertyComputed contained matchgroup=jsBrackets start=/\[/ end=/]/ contains=@jsExpression skipwhite skipempty nextgroup=jsFuncArgs,jsClassValue extend
syntax match jsClassFuncName contained /\<\K\k*\ze\s*(/ skipwhite skipempty nextgroup=jsFuncArgs
syntax region jsClassStringKey contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs
" Destructuring
syntax match jsDestructuringPropertyValue contained /\k\+/
syntax match jsDestructuringProperty contained /\k\+\ze\s*=/ skipwhite skipempty nextgroup=jsDestructuringValue
syntax match jsDestructuringAssignment contained /\k\+\ze\s*:/ skipwhite skipempty nextgroup=jsDestructuringValueAssignment
syntax region jsDestructuringValue contained start=/=/ end=/[,}\]]\@=/ contains=@jsExpression extend
syntax region jsDestructuringValueAssignment contained start=/:/ end=/[,}=]\@=/ contains=jsDestructuringPropertyValue,jsDestructuringBlock,jsNoise,jsDestructuringNoise skipwhite skipempty nextgroup=jsDestructuringValue extend
syntax match jsDestructuringNoise contained /[,[\]]/
syntax region jsDestructuringPropertyComputed contained matchgroup=jsBrackets start=/\[/ end=/]/ contains=@jsExpression skipwhite skipempty nextgroup=jsDestructuringValue,jsDestructuringNoise extend fold
" Comments
syntax keyword jsCommentTodo contained TODO FIXME XXX TBD
syntax region jsComment start=+//+ end=/$/ contains=jsCommentTodo,@Spell extend keepend
syntax region jsComment start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell fold extend keepend
syntax region jsEnvComment start=/\%^#!/ end=/$/ display
" Specialized Comments - These are special comment regexes that are used in
" odd places that maintain the proper nextgroup functionality. It sucks we
" can't make jsComment a skippable type of group for nextgroup
syntax region jsCommentFunction contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturn extend keepend
syntax region jsCommentFunction contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturn fold extend keepend
syntax region jsCommentClass contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsClassBlock,jsFlowClassGroup extend keepend
syntax region jsCommentClass contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsClassBlock,jsFlowClassGroup fold extend keepend
syntax region jsCommentIfElse contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsIfElseBlock extend keepend
syntax region jsCommentIfElse contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsIfElseBlock fold extend keepend
syntax region jsCommentRepeat contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsRepeatBlock extend keepend
syntax region jsCommentRepeat contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsRepeatBlock fold extend keepend
" Decorators
syntax match jsDecorator /^\s*@/ nextgroup=jsDecoratorFunction
syntax match jsDecoratorFunction contained /\h[a-zA-Z0-9_.]*/ nextgroup=jsParenDecorator
if exists("javascript_plugin_jsdoc")
runtime extras/jsdoc.vim
" NGDoc requires JSDoc
if exists("javascript_plugin_ngdoc")
runtime extras/ngdoc.vim
endif
endif
if exists("javascript_plugin_flow")
runtime extras/flow.vim
endif
syntax cluster jsExpression contains=jsBracket,jsParen,jsObject,jsTernaryIf,jsTaggedTemplate,jsTemplateString,jsString,jsRegexpString,jsNumber,jsFloat,jsOperator,jsBooleanTrue,jsBooleanFalse,jsNull,jsFunction,jsArrowFunction,jsGlobalObjects,jsExceptions,jsFutureKeys,jsDomErrNo,jsDomNodeConsts,jsHtmlEvents,jsFuncCall,jsUndefined,jsNan,jsPrototype,jsBuiltins,jsNoise,jsClassDefinition,jsArrowFunction,jsArrowFuncArgs,jsParensError,jsComment,jsArguments,jsThis,jsSuper,jsDo,jsForAwait,jsAsyncKeyword,jsStatement
syntax cluster jsAll contains=@jsExpression,jsStorageClass,jsConditional,jsRepeat,jsReturn,jsException,jsTry,jsNoise,jsBlockLabel
" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_javascript_syn_inits")
if version < 508
let did_javascript_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsComment Comment
HiLink jsEnvComment PreProc
HiLink jsParensIfElse jsParens
HiLink jsParensRepeat jsParens
HiLink jsParensSwitch jsParens
HiLink jsParensCatch jsParens
HiLink jsCommentTodo Todo
HiLink jsString String
HiLink jsObjectKeyString String
HiLink jsTemplateString String
HiLink jsObjectStringKey String
HiLink jsClassStringKey String
HiLink jsTaggedTemplate StorageClass
HiLink jsTernaryIfOperator Operator
HiLink jsRegexpString String
HiLink jsRegexpBoundary SpecialChar
HiLink jsRegexpQuantifier SpecialChar
HiLink jsRegexpOr Conditional
HiLink jsRegexpMod SpecialChar
HiLink jsRegexpBackRef SpecialChar
HiLink jsRegexpGroup jsRegexpString
HiLink jsRegexpCharClass Character
HiLink jsCharacter Character
HiLink jsPrototype Special
HiLink jsConditional Conditional
HiLink jsBranch Conditional
HiLink jsLabel Label
HiLink jsReturn Statement
HiLink jsRepeat Repeat
HiLink jsDo Repeat
HiLink jsStatement Statement
HiLink jsException Exception
HiLink jsTry Exception
HiLink jsFinally Exception
HiLink jsCatch Exception
HiLink jsAsyncKeyword Keyword
HiLink jsForAwait Keyword
HiLink jsArrowFunction Type
HiLink jsFunction Type
HiLink jsGenerator jsFunction
HiLink jsArrowFuncArgs jsFuncArgs
HiLink jsFuncName Function
HiLink jsClassFuncName jsFuncName
HiLink jsObjectFuncName Function
HiLink jsArguments Special
HiLink jsError Error
HiLink jsParensError Error
HiLink jsOperator Operator
HiLink jsOf Operator
HiLink jsStorageClass StorageClass
HiLink jsClassKeyword Keyword
HiLink jsExtendsKeyword Keyword
HiLink jsThis Special
HiLink jsSuper Constant
HiLink jsNan Number
HiLink jsNull Type
HiLink jsUndefined Type
HiLink jsNumber Number
HiLink jsFloat Float
HiLink jsBooleanTrue Boolean
HiLink jsBooleanFalse Boolean
HiLink jsObjectColon jsNoise
HiLink jsNoise Noise
HiLink jsBrackets Noise
HiLink jsParens Noise
HiLink jsBraces Noise
HiLink jsFuncBraces Noise
HiLink jsFuncParens Noise
HiLink jsClassBraces Noise
HiLink jsClassNoise Noise
HiLink jsIfElseBraces Noise
HiLink jsTryCatchBraces Noise
HiLink jsModuleBraces Noise
HiLink jsObjectBraces Noise
HiLink jsObjectSeparator Noise
HiLink jsFinallyBraces Noise
HiLink jsRepeatBraces Noise
HiLink jsSwitchBraces Noise
HiLink jsSpecial Special
HiLink jsTemplateBraces Noise
HiLink jsGlobalObjects Constant
HiLink jsGlobalNodeObjects Constant
HiLink jsExceptions Constant
HiLink jsBuiltins Constant
HiLink jsImport Include
HiLink jsExport Include
HiLink jsExportDefault StorageClass
HiLink jsExportDefaultGroup jsExportDefault
HiLink jsModuleAs Include
HiLink jsModuleComma jsNoise
HiLink jsModuleAsterisk Noise
HiLink jsFrom Include
HiLink jsDecorator Special
HiLink jsDecoratorFunction Function
HiLink jsParensDecorator jsParens
HiLink jsFuncArgOperator jsFuncArgs
HiLink jsClassProperty jsObjectKey
HiLink jsSpreadOperator Operator
HiLink jsRestOperator Operator
HiLink jsRestExpression jsFuncArgs
HiLink jsSwitchColon Noise
HiLink jsClassMethodType Type
HiLink jsObjectMethodType Type
HiLink jsClassDefinition jsFuncName
HiLink jsBlockLabel Identifier
HiLink jsBlockLabelKey jsBlockLabel
HiLink jsDestructuringBraces Noise
HiLink jsDestructuringProperty jsFuncArgs
HiLink jsDestructuringAssignment jsObjectKey
HiLink jsDestructuringNoise Noise
HiLink jsCommentFunction jsComment
HiLink jsCommentClass jsComment
HiLink jsCommentIfElse jsComment
HiLink jsCommentRepeat jsComment
HiLink jsDomErrNo Constant
HiLink jsDomNodeConsts Constant
HiLink jsDomElemAttrs Label
HiLink jsDomElemFuncs PreProc
HiLink jsHtmlEvents Special
HiLink jsHtmlElemAttrs Label
HiLink jsHtmlElemFuncs PreProc
HiLink jsCssStyles Label
delcommand HiLink
endif
" Define the htmlJavaScript for HTML syntax html.vim
syntax cluster htmlJavaScript contains=@jsAll,jsImport,jsExport
syntax cluster javaScriptExpression contains=@jsAll
" Vim's default html.vim highlights all javascript as 'Special'
hi! def link javaScript NONE
let b:current_syntax = "javascript"
if main_syntax == 'javascript'
unlet main_syntax
endif

+ 0
- 1
bundle/vim-node

@ -1 +0,0 @@
Subproject commit 13b31218447335e176d46dd5f497b274f7f49595

+ 1
- 0
bundle/vim-node/.mailmap View File

@ -0,0 +1 @@
Andri Möll <andri@dot.ee>

+ 8
- 0
bundle/vim-node/.packignore View File

@ -0,0 +1,8 @@
/Makefile
/test/*
/Gemfile
/Gemfile.lock
/Guardfile
/*.zip
/node.tar.gz
tags

+ 7
- 0
bundle/vim-node/.travis.yml View File

@ -0,0 +1,7 @@
language: ruby
rvm:
- 1.9.3
before_install: sudo apt-get install vim-gtk
install: bundle install --deployment --without development
script: xvfb-run make test

+ 57
- 0
bundle/vim-node/CHANGELOG.md View File

@ -0,0 +1,57 @@
## Unreleased
- Fixes `gf` on scoped modules (`require("@scope/example")`).
- Initializes Node.vim `gf` and other mappings when the `'filetype'` is set,
rather than when reading a file.
This allows you to manually set the filetype to JavaScript after opening
a file, for example, and still get Node.vim's mappings.
- Initializes Node.vim mappings for JSX (those with the `jsx` filetype) files.
- Adds `.es` to detected suffixes, so you can `gf` over `./foo` to open
`./foo.es`.
- Updates URLs to use <https://github.com/nodejs/node> instead of the previous
Joyent repo.
Thanks, [Jacky Alciné](https://jacky.wtf), for the help!
## 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`.

+ 11
- 0
bundle/vim-node/Gemfile 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

+ 59
- 0
bundle/vim-node/Gemfile.lock 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!

+ 8
- 0
bundle/vim-node/Guardfile 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

+ 20
- 0
bundle/vim-node/LICENSE 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.

+ 40
- 0
bundle/vim-node/Makefile 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/nodejs/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

+ 105
- 0
bundle/vim-node/README.md View File

@ -0,0 +1,105 @@
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).

+ 110
- 0
bundle/vim-node/autoload/node.vim View File

@ -0,0 +1,110 @@
" 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"]
function! node#initialize(root)
let b:node_root = a:root
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>
silent doautocmd User Node
endfunction
function! node#javascript()
" This might be called multiple times if multiple filetypes match.
if exists("b:node_javascript") && b:node_javascript | return | endif
let b:node_javascript = 1
setlocal 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('%'))"
" @ is used for scopes, but isn't a default filename character on
" non-Windows sytems.
setlocal isfname+=@-@
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

+ 151
- 0
bundle/vim-node/autoload/node/lib.vim 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/nodejs/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

+ 8
- 0
bundle/vim-node/ftdetect/node.vim 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

+ 47
- 0
bundle/vim-node/plugin/node.vim View File

@ -0,0 +1,47 @@
if exists("g:loaded_node") || &cp || v:version < 700 | finish | endif
let g:loaded_node = 1
let s:filetypes = ["javascript", "json", "jsx"]
if exists("g:node_filetypes") | let s:filetypes = g:node_filetypes | endif
function! s:detect(dir)
if exists("b:node_root") | return | endif
let dir = a:dir
while 1
let is_node = 0
let is_node = is_node || filereadable(dir . "/package.json")
let is_node = is_node || isdirectory(dir . "/node_modules")
if is_node | return node#initialize(dir) | endif
let parent = fnamemodify(dir, ":h")
if parent == dir | return | endif
let dir = parent
endwhile
endfunction
function! s:permutate(ft)
" Don't know right now how to detect javascript.jsx and other permutations
" without precomputing them in advance. Please let me know if you do.
return [a:ft, a:ft . ".*", "*." . a:ft, "*." . a:ft . ".*"]
endfunction
function! s:flatten(list)
let values = []
for value in a:list
if type(value) == type([]) | call extend(values, value)
else | add(values, value)
endif
endfor
return values
endfunction
augroup Node
au!
au VimEnter * if empty(expand("<amatch>")) | call s:detect(getcwd()) | endif
au BufRead,BufNewFile * call s:detect(expand("<amatch>:p"))
let s:filetype_patterns = s:flatten(map(s:filetypes, "<SID>permutate(v:val)"))
let s:filetype_patterns_joined = join(s:filetype_patterns, ",")
execute "au FileType " s:filetype_patterns_joined " call node#javascript()"
augroup end

+ 466
- 0
bundle/vim-node/test/autoload/lib_test.rb View File

@ -0,0 +1,466 @@
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
target = touch File.join(@dir, "README")
touch File.join(@dir, "README.js")
$vim.edit File.join(@dir, "index.js")
find("./README").must_equal target
end
it "must return ./README.txt relative to file" do
target = touch File.join(@dir, "lib", "README.txt")
$vim.edit File.join(@dir, "lib", "index.js")
find("./README.txt").must_equal target
end
it "must return ../README.txt" do
target = 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 target
end
it "must return /.../README.txt" do
target = 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 target
end
it "must return ./other.js given ./other" do
target = touch File.join(@dir, "other.js")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ./other.jsx given ./other" do
target = touch File.join(@dir, "other.jsx")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ./other.es given ./other" do
target = touch File.join(@dir, "other.es")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ./other.js given ./other relative to file" do
target = touch File.join(@dir, "lib", "other.js")
$vim.edit File.join(@dir, "lib", "index.js")
find("./other").must_equal target
end
it "must return ./other.js before ./other/index.js given ./other" do
target = touch File.join(@dir, "other.js")
touch File.join(@dir, "other", "index.js")
$vim.edit File.join(@dir, "index.js")
find("./other").must_equal target
end
it "must return ../other.js given ../other" do
target = touch File.join(@dir, "other.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("../other").must_equal target
end
it "must return /.../other.js given /.../other" do
target = 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 target
end
it "must return ./package.json given ./package" do
target = touch File.join(@dir, "package.json")
$vim.edit File.join(@dir, "index.js")
find("./package").must_equal target
end
it "must return ./index.js given ." do
target = touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
find(".").must_equal target
end
it "must return ./index.js given ./" do
target = touch File.join(@dir, "index.js")
$vim.edit File.join(@dir, "other.js")
find("./").must_equal target
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
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "other.js")
find("..").must_equal target
end
it "must return ../index.js given ../" do
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "other.js")
find("../").must_equal target
end
it "must return /.../index.js given /..." do
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir").must_equal target
end
it "must return /.../index.js given /.../" do
target = touch File.join(@dir, "index.js")
Dir.mkdir File.join(@dir, "lib")
$vim.edit File.join(@dir, "lib", "index.js")
find("#@dir/").must_equal target
end
it "must return ./lib/index.js given ./lib" do
target = touch File.join(@dir, "lib", "index.js")
$vim.edit File.join(@dir, "index.js")
find("./lib").must_equal target
end
it "must return ./lib/other.js given ./lib with main" do
target = 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 target
end
it "must return ./lib/index.js given ./lib with empty main" do
target = 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 target
end
it "must return ./lib/index.js given ./lib with non-existent main" do
target = 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 target
end
it "must return ./other.js before ./other/index.js given . with main" do
touch File.join(@dir, "package.json"), JSON.dump(:main => "other")
target = touch File.join(@dir, "other.js")
touch File.join(@dir, "other", "index.js")
$vim.edit File.join(@dir, "index.js")
find(".").must_equal target
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")
target = touch File.join(mod, "other.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal target
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")
target = touch File.join(mod, "lib/index.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal target
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")
target = touch File.join(mod, "lib/index.js")
$vim.edit File.join(@dir, "index.js")
find("foo").must_equal target
end
it "must return node_modules/@scope/foo/index.js given @scope/foo" do
target = File.join(@dir, "node_modules", "@scope", "foo", "index.js")
touch target
$vim.edit File.join(@dir, "index.js")
find("@scope/foo").must_equal target
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/nodejs/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/nodejs/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/nodejs/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/nodejs/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

+ 538
- 0
bundle/vim-node/test/autoload_test.rb View File

@ -0,0 +1,538 @@
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 mapped 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 edit ./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 edit ./node_modules/foo/index.js given foo" do
touch File.join(@dir, "index.js"), %(require("foo"))
target = touch File.join(@dir, "node_modules", "foo", "index.js")
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must edit ./node_modules/@scope/foo/index.js given @scope/foo" do
touch File.join(@dir, "index.js"), %(require("@scope/foo"))
target = File.join(@dir, "node_modules", "@scope", "foo", "index.js")
touch target
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must not show an error when searching for 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 searching for 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 when filetype is JavaScript and file exists" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.js"), %(require("foo"))
$vim.edit File.join(@dir, "index.js")
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype is JavaScript and file new" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
$vim.edit File.join(@dir, "index.js")
$vim.insert %(require("foo"))
$vim.normal
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype is JSON" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
# NOTE: Use an extensionless file and set ft manually to not have
# filetype.vim set its filetype to JavaScript automatically.
$vim.command("au BufReadPre package set ft=json")
touch File.join(@dir, "package"), %({"dependencies": {"foo": "1.x"}})
$vim.edit File.join(@dir, "package")
$vim.echo("&filetype").must_equal "json"
$vim.feedkeys "/foo\\<CR>gf"
bufname = File.realpath($vim.echo(%(bufname("%"))))
bufname.must_equal target
end
it "must find when filetype set to JavaScript after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype javascript")
$vim.echo("&filetype").must_equal "javascript"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype set to JSX after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype jsx")
$vim.echo("&filetype").must_equal "jsx"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
it "must find when filetype set to JavaScript and Foo after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype javascript.foo")
$vim.echo("&filetype").must_equal "javascript.foo"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
end
# Ensure the autocommand detects both orderings.
it "must find when filetype set to Foo and JavaScript after open" do
target = touch File.join(@dir, "node_modules", "foo", "index.js")
touch File.join(@dir, "index.react"), %(require("foo"))
$vim.edit File.join(@dir, "index.react")
$vim.echo("&filetype").must_equal ""
$vim.command("setfiletype foo.javascript")
$vim.echo("&filetype").must_equal "foo.javascript"
$vim.feedkeys "$hhgf"
$vim.echo(%(bufname("%"))).must_equal target
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
target = touch File.join(@dir, "node_modules", "foo", "index.js")
$vim.edit File.join(@dir, "README.txt")
$vim.command("Nedit foo")
$vim.echo(%(bufname("%"))).must_equal target
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 = touch File.join(@dir, "node_modules", "foo", "lib", "utils.js")
$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

+ 45
- 0
bundle/vim-node/test/ftdetect_test.rb 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

+ 54
- 0
bundle/vim-node/test/helper.rb View File

@ -0,0 +1,54 @@
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)
if contents.nil? || contents.empty?
FileUtils.touch(path)
else
File.open(path, "w") {|f| f.write contents }
end
path
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]

+ 85
- 0
bundle/vim-node/test/plugin_test.rb 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

+ 14
- 0
bundle/vim-node/test/vimrc View File

@ -0,0 +1,14 @@
set nocompatible
set noswapfile
set nobackup
set hidden
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

+ 0
- 1
bundle/vim-nodejs-complete

@ -1 +0,0 @@
Subproject commit 1e2f98f4366c6dec27fa8df72971fa9fa33e9f07

+ 82
- 0
bundle/vim-nodejs-complete/README.md View File

@ -0,0 +1,82 @@
vim-nodejs-complete
===================
Nodejs `'omnifunc'` function of vim
Support node's builtin module's method&property completion(`ctrl-x ctrl-o`) in `js` file with preview.
## Install
Download the [tarball](https://github.com/myhere/vim-nodejs-complete/zipball/master) and extract to your vimfiles(`~/.vim` or `~/vimfiles`) folder.
Completion require `:filetype plugin on`, please make sure it's on.
## Settings
All the settings is optional.
```viml
let g:nodejs_complete_config = {
\ 'js_compl_fn': 'jscomplete#CompleteJS',
\ 'max_node_compl_len': 15
\}
```
### js_compl_fn
* `String` function name, called to complete javascript
* default to `javascriptcomplete#CompleteJS`
* if you want to integrate with [jscomplete](https://github.com/teramako/jscomplete-vim), set it to `jscomplete#CompleteJS`, make sure it's installed.
### max_node_compl_len
* `Number`, length of nodejs complete items. set to 0 if unlimited
## Example
```js
var fs = req
// then hit ctrl-x_ctrl-o you'll get:
var fs = require(
// and then hit ctrl-x_ctrl-o again you'll get module name completion
var fs = require('f
// then hit ctrl-x_ctrl-o
fs.
// then hit ctrl-x_ctrl-o
proc
// then hit ctrl-x_ctrl-o
process.ex
// then hit ctrl-x_ctrl-o
```
## Tip
1. Close the method preview window
`ctrl-w_ctrl-z` or `:pc`.
If you want close it automatically, put the code(from [spf13-vim](https://github.com/spf13/spf13-vim/blob/3.0/.vimrc)) below in your `.vimrc` file.
```vim
" automatically open and close the popup menu / preview window
au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
```
2. About vim's complete
Vim supports several kinds of completion, `:h ins-completion` for help.
3. Completion of module in `node_modules` will cache result of every `js` file
If you modified module in `node_modules` directory, use code below to clear the cache.
```vim
:unlet b:npm_module_names
```
## Feedback
[feedback or advice or feature-request](https://github.com/myhere/vim-nodejs-complete/issues)

+ 2
- 0
bundle/vim-nodejs-complete/after/autoload/nodejs-doc.vim
File diff suppressed because it is too large
View File


+ 609
- 0
bundle/vim-nodejs-complete/after/autoload/nodejscomplete.vim View File

@ -0,0 +1,609 @@
" Vim completion script
" Language: Javascript(node)
" Maintainer: Lin Zhang ( myhere.2009 AT gmail DOT com )
" Last Change: 2012-8-18 1:32:00
" save current dir
let s:nodejs_doc_file = expand('<sfile>:p:h') . '/nodejs-doc.vim'
let s:js_varname_reg = '[$a-zA-Z_][$a-zA-Z0-9_]*'
let s:js_obj_declare_type = {
\ 'global': 0,
\ 'require': 1,
\ 'constructor': 2
\ }
" settings
" default setting
let s:nodejs_complete_config = {
\ 'js_compl_fn': 'javascriptcomplete#CompleteJS',
\ 'max_node_compl_len': 15
\}
if exists('g:nodejs_complete_config') && type(g:nodejs_complete_config) == type({})
let g:nodejs_complete_config = extend(s:nodejs_complete_config, g:nodejs_complete_config)
else
let g:nodejs_complete_config = s:nodejs_complete_config
endif
unlet s:nodejs_complete_config
function! nodejscomplete#CompleteJS(findstart, base)"{{{
if a:findstart
try
let JS_compl_fn = function(g:nodejs_complete_config.js_compl_fn)
let start = call(JS_compl_fn, [a:findstart, a:base])
catch /.*/
echo '!!!!!!!!!!function [' . g:nodejs_complete_config.js_compl_fn . '] is not exists!'
endtry
"Decho 'start: ' . start
" str[start: end] end 为负数时从末尾截取
if start - 1 < 0
let b:nodecompl_context = ''
else
let line = getline('.')
let b:nodecompl_context = line[:start-1]
endif
return start
else
let posi = getpos('.')
let result = s:getNodeComplete(a:base, b:nodecompl_context)
" the function above will move the cursor
" so we restore the cursor position
" JS_compl_fn below may rely on the cursor position
call setpos('.', posi)
"Decho 'nodecomplete: ' . string(result)
unlet b:nodecompl_context
let nodejs_compl = result.complete
" limit nodejs complete count
if g:nodejs_complete_config.max_node_compl_len != 0
let nodejs_compl = nodejs_compl[0 : g:nodejs_complete_config.max_node_compl_len - 1]
endif
if result.continue
try
let JS_compl_fn = function(g:nodejs_complete_config.js_compl_fn)
let js_compl = call(JS_compl_fn, [a:findstart, a:base])
catch /.*/
echo '!!!!!!!!!!function [' . g:nodejs_complete_config.js_compl_fn . '] is not exists!'
endtry
"Decho 'js_compl: ' . string(js_compl)
return nodejs_compl + js_compl
else
return nodejs_compl
endif
endif
endfunction"}}}
" get complete
function! s:getNodeComplete(base, context)"{{{
"Decho 'base: ' . a:base
"Decho 'context: ' . a:context
" TODO: 排除 module.property.h 情况
let mod_reg = '\(' . s:js_varname_reg . '\)\s*\(\.\|\[\s*["'']\?\)\s*$'
let matched = matchlist(a:context, mod_reg)
"Decho 'mod_reg: ' . mod_reg
" 模块属性补全
if len(matched) > 0
let var_name = matched[1]
let operator = matched[2]
let position = [line('.'), len(a:context) - len(matched[0])]
"Decho 'var_name: ' . var_name . ' ; operator: ' . operator
let declare_info = s:getObjDeclareInfo(var_name, position)
"Decho 'mod_info: ' . string(declare_info) . '; compl_prefix: ' . a:base
let compl_list = s:getObjectComplete(declare_info.type, declare_info.value,
\ a:base, operator)
let ret = {
\ 'complete': compl_list
\ }
if len(compl_list) == 0
let ret.continue = 1
else
let ret.continue = 0
endif
" 全局补全
else
"Decho 'var complete'
let ret = {
\ 'continue': 1,
\ 'complete': s:getVariableComplete(a:context, a:base)
\ }
endif
return ret
endfunction"}}}
function! s:getObjDeclareInfo(var_name, position)"{{{
let position = s:fixPosition(a:position)
"Decho 'position: ' . string(position)
if position[0] <= 0
return {
\ 'type': s:js_obj_declare_type.global,
\ 'value': a:var_name
\}
endif
let decl_stmt_prefix_reg = '\<' . a:var_name . '\_s*=\_s*'
" search backward, don't move the cursor, don't wrap
call cursor(position[0], position[1])
let begin_position = searchpos(decl_stmt_prefix_reg, 'bnW')
if begin_position[0] == 0
return {
\ 'type': s:js_obj_declare_type.global,
\ 'value': a:var_name
\}
endif
" make sure it's not in comments...
if !s:isDeclaration(begin_position)
return s:getObjDeclareInfo(a:var_name, begin_position)
endif
let lines = s:getLinesInRange(begin_position, position)
"Decho 'lines: ' . string(lines)
let code = join(lines, "\n")
" require
let require_stmt_reg = decl_stmt_prefix_reg .
\ 'require\_s*(\_s*\([''"]\)\zs[^)''"]\+\ze\1\_s*)'
let matched = matchstr(code, require_stmt_reg)
if len(matched)
return {
\ 'type': s:js_obj_declare_type.require,
\ 'value': matched
\}
endif
" new
let new_stmt_reg = decl_stmt_prefix_reg .
\ 'new\_s\+\zs' . s:js_varname_reg . '\%(\_s*\.\_s*' .
\ s:js_varname_reg . '\)*\ze'
let matched = matchstr(code, new_stmt_reg)
if len(matched)
let parts = split(matched, '\.')
return {
\ 'type': s:js_obj_declare_type.constructor,
\ 'value': [s:getObjDeclareInfo(parts[0], begin_position), join(parts[1:], '.')]
\}
endif
" new
" var emitter = new (require('events')).EventEmitter;
let new_stmt_reg = decl_stmt_prefix_reg .
\ 'new\_s\+' .
\ '(\_s*require\_s*(\([''"]\)\(' . s:js_varname_reg . '\)\1\_s*)\_s*)' .
\ '\_s*' .
\ '\(\%(\.' . s:js_varname_reg . '\)\+\)'
let matchedList = matchlist(code, new_stmt_reg)
if (len(matchedList))
"Decho 'new stmt: ' . string(matchedList)
let props = [matchedList[3][1:]] + matchedList[4:]
"Decho 'props: ' . string(props)
return {
\ 'type': s:js_obj_declare_type.constructor,
\ 'value': [
\ {
\ 'type': s:js_obj_declare_type.require,
\ 'value': matchedList[2]
\ },
\ join(props, '')
\ ]
\}
endif
" assign
let assign_stmt_reg = decl_stmt_prefix_reg . '\zs' . s:js_varname_reg . '\ze'
let matched = matchstr(code, assign_stmt_reg)
if len(matched)
return s:getObjDeclareInfo(a:var_name, begin_position)
endif
" continure to search backward
return s:getObjDeclareInfo(a:var_name, begin_position)
endfunction"}}}
function! s:isDeclaration(position)"{{{
let [line_num, col_num] = a:position
" syntaxName @see: $VIMRUNTIME/syntax/javascript.vim
let syntaxName = synIDattr(synID(line_num, col_num, 0), 'name')
if syntaxName =~ '^javaScript\%(Comment\|LineComment\|String\|RegexpString\)'
return 0
else
return 1
endif
endfunction"}}}
" only complete nodejs's module info
function! s:getObjectComplete(type, mod_name, prop_name, operator)"{{{
" new
if a:type == s:js_obj_declare_type.constructor
let list = s:getConstructedObjectComplete(a:mod_name)
" require and global
else
let list = s:getNodeDocList(a:type, a:mod_name, 'props')
endif
if !len(list)
return list
else
" no prop_name suplied
if (len(a:prop_name) == 0)
let ret = list
else
let ret = s:smartFilter(list, 'v:val["word"]', a:prop_name)
endif
let [prefix, suffix] = ['', '']
let matched = matchlist(a:operator, '\[\s*\(["'']\)\?')
"Decho 'operator_matched: ' . string(matched)
if len(matched)
if len(matched[1])
let [prefix, suffix] = ['', matched[1] . ']']
else
let [prefix, suffix] = ['''', ''']']
endif
endif
for item in ret
let item.word = prefix . item.word . suffix
endfor
call s:addFunctionParen(ret)
return ret
endif
endfunction"}}}
function! s:getVariableComplete(context, var_name)"{{{
"Decho 'var_name: ' . a:var_name
" complete require's arguments
let matched = matchlist(a:context, 'require\s*(\s*\%(\([''"]\)\(\.\{1,2}.*\)\=\)\=$')
if (len(matched) > 0)
"Decho 'require complete: ' . string(matched)
if (len(matched[2]) > 0) " complete -> require('./
let mod_names = s:getModuleInCurrentDir(a:context, a:var_name, matched)
else
let mod_names = s:getModuleNames()
if (len(matched[1]) == 0) " complete -> require(
call map(mod_names, '"''" . v:val . "'')"')
elseif (len(a:var_name) == 0) " complete -> require('
call map(mod_names, 'v:val . "' . escape(matched[1], '"') . ')"')
else " complete -> require('ti
let mod_names = filter(mod_names, 'v:val =~# "^' . a:var_name . '"')
call map(mod_names, 'v:val . "' . escape(matched[1], '"') . ')"')
endif
endif
return mod_names
endif
" complete global variables
let vars = []
if (len(a:var_name) == 0)
return vars
endif
call s:loadNodeDocData()
if (has_key(g:nodejs_complete_data, 'vars'))
let vars = deepcopy(g:nodejs_complete_data.vars)
endif
let ret = s:smartFilter(vars, 'v:val["word"]', a:var_name)
call s:addFunctionParen(ret)
return ret
endfunction"}}}
function! s:getModuleInCurrentDir(context, var_name, matched)"{{{
let mod_names = []
let path = a:matched[2] . a:var_name
" typed as require('..
" complete as require('../
" cause the latter one is more common
let compl_prefix = ''
if (path =~# '\.\.$')
let compl_prefix = '/'
let path = path . compl_prefix
endif
"Decho 'path: ' . path
let current_dir = expand('%:p:h')
let glob_path = current_dir . '/' . path . '*'
let files = s:fuzglob(glob_path)
"Decho 'glob: ' . glob_path
"Decho 'current dir files: ' . string(files)
for file in files
" not '.' and '..'
if ((isdirectory(file) ) || file =~? '\.json$\|\.js$')
let mod_file = file
" directory
if (file !~? '\.json$\|\.js$')
let mod_file = mod_file . '/'
endif
" get complete word
let mod_file = substitute(mod_file, '\', '/', 'g')
let start = len(glob_path) - 1 " substract character '*'
let compl_infix = strpart(mod_file, start)
"Decho 'idx: ' . start
"Decho 'compl_infix: ' . compl_infix
"Decho 'relative file: ' . mod_file
let mod_name = compl_prefix . a:var_name . compl_infix
" file module, not a directory
if (compl_infix !~# '/$')
let mod_name = mod_name . a:matched[1] . ')'
endif
"Decho 'mod_name: ' . mod_name
call add(mod_names, mod_name)
endif
endfor
"Decho 'relative path: ' . path
return mod_names
endfunction"}}}
function! s:getModuleNames()"{{{
call s:loadNodeDocData()
let mod_names = []
" build-in module name
if (has_key(g:nodejs_complete_data, 'modules'))
let mod_names = keys(g:nodejs_complete_data.modules)
endif
" find module in 'module_dir' folder
if (!exists('b:npm_module_names'))
let current_dir = expand('%:p:h')
let b:npm_module_names = s:getModuleNamesInNode_modulesFolder(current_dir)
endif
let mod_names = mod_names + b:npm_module_names
return sort(mod_names)
endfunction"}}}
function! s:getModuleNamesInNode_modulesFolder(current_dir)"{{{
" ensure platform coincidence
let base_dir = substitute(a:current_dir, '\', '/', 'g')
"Decho 'base_dir: ' . base_dir
let ret = []
let parts = split(base_dir, '/', 1)
"Decho 'parts: ' . string(parts)
let idx = 0
let len = len(parts)
let sub_parts = []
while idx < len
let sub_parts = add(sub_parts, parts[idx])
let module_dir = join(sub_parts, '/') . '/node_modules'
"Decho 'directory: ' . module_dir
if (isdirectory(module_dir))
let files = s:fuzglob(module_dir . '/*')
"Decho 'node_module files: ' . string(files)
for file in files
if (isdirectory(file) || file =~? '\.json$\|\.js$')
let mod_name = matchstr(file, '[^/\\]\+$')
let ret = add(ret, mod_name)
endif
endfor
endif
let idx = idx + 1
endwhile
"Decho 'npm modules: ' . string(ret)
return ret
endfunction"}}}
function! s:getConstructedObjectComplete(constructor_info)"{{{
"Decho 'getConstructedObjectComplete, constructor_info: ' . string(a:constructor_info)
let ret = []
let [declare_info, class_name] = a:constructor_info
let mod_name = declare_info.value
" global
if declare_info.type == s:js_obj_declare_type.global
" Buffer
if class_name == ''
let class_name = '.self'
endif
" global.Buffer
if mod_name == 'global'
let mod_name = class_name
let class_name = '.self'
endif
endif
" global or require
if declare_info.type == s:js_obj_declare_type.global ||
\ declare_info.type == s:js_obj_declare_type.require
let ret = s:getNodeDocList(declare_info.type, mod_name, 'classes', class_name)
endif
return ret
endfunction"}}}
function! s:addFunctionParen(compl_list)"{{{
for item in a:compl_list
if type(item) == 4
if item.kind == 'f'
let item.word = item.word . '('
endif
endif
endfor
return a:compl_list
endfunction"}}}
function! s:loadNodeDocData()"{{{
" load node module data
if (!exists('g:nodejs_complete_data'))
" load data from external file
let filename = s:nodejs_doc_file
"Decho 'filename: ' . filename
if (filereadable(filename))
execute 'so ' . filename
"Decho string(g:nodejs_complete_data)
else
"Decho 'not readable: ' . filename
endif
endif
endfunction"}}}
" get infomation from g:nodejs_complete_data
" @param mod_type {Enum}
" @param mod_name {String}
" @param type {Enum} 'props' | 'classes'
" @param {String} if type == 'classes', then it exists and is class_name
" else do not exist
function! s:getNodeDocList(mod_type, mod_name, type, ...)"{{{
call s:loadNodeDocData()
if a:mod_type == s:js_obj_declare_type.require
let type = 'modules'
else
let type = 'globals'
endif
if (has_key(g:nodejs_complete_data[type], a:mod_name))
let mod = g:nodejs_complete_data[type][a:mod_name]
else
let mod = {}
endif
" class
if a:0 != 0
let class_name = a:1
if (has_key(mod, a:type))
let classes = mod[a:type]
else
let classes = {}
endif
if (has_key(classes, a:1))
let ret = classes[class_name]
else
let ret = []
endif
" property
else
if (has_key(mod, a:type))
let ret = mod[a:type]
else
let ret = []
endif
endif
return deepcopy(ret)
endfunction"}}}
" copied from FuzzyFinder/autoload/fuf.vim
" returns list of paths.
" An argument for glob() is normalized in order to avoid a bug on Windows.
function! s:fuzglob(expr)"{{{
" Substitutes "\", because on Windows, "**\" doesn't include ".\",
" but "**/" include "./". I don't know why.
return split(glob(substitute(a:expr, '\', '/', 'g')), "\n")
endfunction"}}}
" when x <= 0, return [0, 0]
" when y <= 0, move to previous line end
function! s:fixPosition(position)"{{{
let [x, y] = a:position
if x <= 0
return [0, 0]
endif
if y <= 0
let x -= 1
let y = len(getline(x))
return s:fixPosition([x, y])
endif
return [x, y]
endfunction"}}}
" return a List contains every line
function! s:getLinesInRange(begin_position, end_position)"{{{
let [begin_x, begin_y] = a:begin_position
let [end_x, end_y] = a:end_position
let lines = []
if begin_x == end_x
let line = getline(begin_x)
call add(lines, line[begin_y - 1 : end_y - 1])
else
let line = getline(begin_x)
call add(lines, line[begin_y - 1 :])
let x = begin_x + 1
while x < end_x
let line = getline(x)
call add(lines, line)
let x += 1
endwhile
let line = getline(end_x)
call add(lines, line[: end_y - 1])
endif
return lines
endfunction"}}}
" filter items with exact match at first
function! s:smartFilter(items, str, keyword)"{{{
let items = filter(a:items, a:str . ' =~ "' . a:keyword . '"')
let [exact_ret, fuzzy_ret] = [[], []]
for item in items
if item.word =~ '^' . a:keyword
call add(exact_ret, item)
else
call add(fuzzy_ret, item)
endif
endfor
return exact_ret + fuzzy_ret
endfunction"}}}
"
" use plugin Decho(https://github.com/vim-scripts/Decho) for debug
"
" turn off debug mode
" :%s;^\(\s*\)\(Decho\);\1"\2;g | :w | so %
"
" turn on debug mode
" :%s;^\(\s*\)"\(Decho\);\1\2;g | :w | so %
"
" vim:set foldmethod=marker:

+ 357
- 0
bundle/vim-nodejs-complete/after/autoload/update-nodejs-doc.js View File

@ -0,0 +1,357 @@
#!/usr/bin/env node
/**
* @author: Lin Zhang ( myhere.2009 AT gmail DOT com )
* @fileoverview: This script for auto-generate nodejs-doc.vim
*/
var util = require('util'),
fs = require('fs'),
path = require('path'),
os = require('os'),
emitter = new (require('events')).EventEmitter();
init();
function init() {
initEvents();
initLoading();
getNodejsDoc();
}
function initEvents() {
// uncatched exception
process.on('uncaughtException', function(err) {
clearLoading();
console.error('Error: ' + err.stack);
});
emitter.on('vimscript/done', function(message) {
clearLoading();
console.log(message);
console.log('Done!');
});
}
function initLoading() {
var chars = [
'-',
'\\',
'|',
'/'
];
var index = 0,
total = chars.length;
initLoading.timer = setInterval(function() {
index = ++index % total;
var c = chars[index];
// clear console
// @see: https://groups.google.com/forum/?fromgroups#!topic/nodejs/i-oqYFVty5I
process.stdout.write('\033[2J\033[0;0H');
console.log('please wait:');
console.log(c);
}, 200);
}
function clearLoading() {
clearInterval(initLoading.timer);
}
function getNodejsDoc() {
var http = require('http');
var req = http.get('http://nodejs.org/api/all.json', function(res){
var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
var buf = Buffer.concat(chunks),
body = buf.toString('utf-8');
extract2VimScript(body);
});
}).on('error', function(e) {
console.error('problem with request: ' + e.message);
});
}
function extract2VimScript(body) {
// for debug
fs.writeFile('./nodejs-doc-all.json', body);
var json = JSON.parse(body),
vimObject;
var _globals = sortModuleByName(mergeObject(getModsInfo(json.globals),
getModsInfo(json.vars))),
_modules = sortModuleByName(getModsInfo(json.modules)),
_vars = (getVarInfo(json.vars))
.concat(getVarInfo(json.globals))
.sort(sortCompleteWord);
_globals = copyGlobals(_globals, _modules);
vimObject = {
'globals': _globals,
'modules': _modules,
'vars': _vars
};
var filename = path.join(__dirname, 'nodejs-doc.vim'),
comment = '" this file is auto created by "' + __filename + '", please do not edit it yourself!',
content = 'let g:nodejs_complete_data = ' + JSON.stringify(vimObject),
content = comment + os.EOL + content;
fs.writeFile(filename, content, function(err) {
emitter.emit('vimscript/done', 'write file to "' + filename + '" complete.');
});
// for debug
fs.writeFileSync(filename + '.js', JSON.stringify(vimObject, null, 2));
}
function getModsInfo(mods) {
var ret = {};
if (!util.isArray(mods)) {
return ret;
}
mods.forEach(function(mod) {
var mod_name = getModName(mod),
mod_props = getModProps(mod);
// class
var mod_classes = {};
var classes = mod.classes || [];
classes.forEach(function(cls) {
var names = getClassName(cls, mod_name);
var cls_name = names.cls_name,
_mod_name = names.mod_name;
if (_mod_name && mod_name != _mod_name) {
mod_name = names.mod_name;
}
mod_classes[cls_name] = getModProps(cls);
});
if (mod_props.length == 0 && classes.length == 0) {
} else {
ret[mod_name] = {
props: mod_props,
classes: mod_classes
}
}
});
return ret;
}
function getModProps(mod) {
var is_legal_property_name = function(name) {
name += '';
name = name.trim();
return (/^[$a-zA-Z_][$a-zA-Z0-9_]*$/i).test(name);
};
var props = [];
// properties
var properties = mod.properties || [];
properties.forEach(function(property) {
var name = property.name;
if (is_legal_property_name(name)) {
var item = {};
item.word = name;
item.kind = 'm';
item.info = ' ';
props.push(item);
} else {
console.log('illegal name: ' + name);
}
});
// methods
var methods = mod.methods || [];
methods.forEach(function(method) {
var name = method.name;
if (is_legal_property_name(name)) {
var item = {};
if (method.type == 'method') {
item.word = name;
item.info = method.textRaw;
item.kind = 'f';
props.push(item);
}
} else {
console.log('illegal name: ' + name);
}
});
// classes
var classes = mod.classes || [];
classes.forEach(function(cls) {
var mod_name = getModName(mod);
var names = getClassName(cls, mod_name);
var name = names.cls_name;
if (is_legal_property_name(name)) {
var item = {};
item.word = names.cls_name;
item.kind = 'f';
item.info = ' ';
props.push(item);
} else {
console.log('illegal name: ' + name);
}
});
props = props.sort(sortCompleteWord);
return props;
}
function getModName(mod) {
// module name
var mod_name = mod.name;
// invalid module name like 'tls_(ssl)'
// then guess the module name from textRaw 'TLS (SSL)'
if ((/[^_a-z\d\$]/i).test(mod_name)) {
var textRaw = mod.textRaw;
var matched = textRaw.match(/^[_a-z\d\$]+/i);
if (matched) {
var mod_name_len = matched[0].length;
mod_name = mod_name.substr(0, mod_name_len);
}
}
return mod_name;
}
function getClassName(cls, mod_name) {
var str = cls.name;
var names = str.split('.');
var _mod_name = names[0],
cls_name;
if (names.length == 1) {
cls_name = _mod_name;
}
else {
// 修正 mod_name; events.EventEmitter
if (_mod_name.toLowerCase() == mod_name.toLowerCase()) {
mod_name = _mod_name;
}
cls_name = names.slice(1).join('.');
}
return {
mod_name: mod_name,
cls_name: cls_name
};
}
function getVarInfo(vars) {
var ret = [];
if (!util.isArray(vars)) {
return ret;
}
vars.forEach(function(_var) {
// if var is a function
if ((/\([^\(\)]*\)\s*$/).test(_var.textRaw)) {
ret.push({
word: _var.name,
info: _var.textRaw,
kind: 'f'
});
} else {
ret.push({
word: _var.name,
kind: 'v',
info: ' '
});
}
});
// sort
ret = ret.sort(sortCompleteWord);
return ret;
}
function copyGlobals(globals, modules) {
var _Buffer = modules.buffer.classes.Buffer;
globals.Buffer = {
props: [],
classes: {
'.self': _Buffer
}
};
return globals;
}
// helpers
/**
* @param {Object}
*/
function sortModuleByName(mods) {
var keys = Object.keys(mods);
// sort
keys.sort();
var ret = {};
keys.forEach(function(k) {
ret[k] = mods[k];
});
return ret;
}
/**
* @param {Object}
* @param {Object}
*/
function sortCompleteWord(a, b) {
var a_w = a.word.toLowerCase(),
b_w = b.word.toLowerCase();
return a_w < b_w ? -1 : (a_w > b_w ? 1 : 0);
}
/**
* @desc merge Object
* @arguemnts: {Object}
*
* @return: return the new merged Object
*/
function mergeObject() {
var ret = {},
args = Array.prototype.slice.call(arguments);
args.forEach(function(obj) {
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
ret[p] = obj[p];
}
}
});
return ret;
}

+ 3
- 0
bundle/vim-nodejs-complete/after/ftplugin/javascript.vim View File

@ -0,0 +1,3 @@
if exists('&ofu')
setlocal omnifunc=nodejscomplete#CompleteJS
endif

+ 81
- 0
bundle/vim-nodejs-complete/after/test/a.js View File

@ -0,0 +1,81 @@
var fs = require('fs');
fs
fs.
fs.read
fs[
fs["
fs['chmodSync'](
var a = {hello: 'world'},
b = require('path'),
// b = new A,
// b = a;
b.
var emitter = new (require('events')).EventEmitter;
emitter.
var events = require('events');
var emitter = new events.EventEmitter();
var buffer = new Buffer;
// var buffer = new global.Buffer;
var tester = new A.B.C();
emitter.
buffer.
tester.
events.
cons
{
"globals": {
"Buffer": {
"classes": [
{
".self": [
{
"word": "write",
"info": "buf.write(string, [offset], [length], [encoding])",
"kind": "f"
}
]
}
],
"protos": [
]
}
},
"modules": {
"events" {
"classes": [
],
"protos": [
]
}
},
"vars": [
{
"word": "__dirname",
"kind": "v"
}
]
}

Loading…
Cancel
Save