diff --git a/bundle/node b/bundle/node deleted file mode 160000 index 13b3121..0000000 --- a/bundle/node +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 13b31218447335e176d46dd5f497b274f7f49595 diff --git a/bundle/node/.mailmap b/bundle/node/.mailmap new file mode 100644 index 0000000..fcc6121 --- /dev/null +++ b/bundle/node/.mailmap @@ -0,0 +1 @@ +Andri Möll diff --git a/bundle/node/.packignore b/bundle/node/.packignore new file mode 100644 index 0000000..1015f70 --- /dev/null +++ b/bundle/node/.packignore @@ -0,0 +1,8 @@ +/Makefile +/test/* +/Gemfile +/Gemfile.lock +/Guardfile +/*.zip +/node.tar.gz +tags diff --git a/bundle/node/.travis.yml b/bundle/node/.travis.yml new file mode 100644 index 0000000..3eb81d4 --- /dev/null +++ b/bundle/node/.travis.yml @@ -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 diff --git a/bundle/node/CHANGELOG.md b/bundle/node/CHANGELOG.md new file mode 100644 index 0000000..e057097 --- /dev/null +++ b/bundle/node/CHANGELOG.md @@ -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 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 which used to be named . + 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 `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 `NodeVSplitGotoFile` for those who want `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 `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`. diff --git a/bundle/node/Gemfile b/bundle/node/Gemfile new file mode 100644 index 0000000..da3b0f8 --- /dev/null +++ b/bundle/node/Gemfile @@ -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 diff --git a/bundle/node/Gemfile.lock b/bundle/node/Gemfile.lock new file mode 100644 index 0000000..f7fe6c4 --- /dev/null +++ b/bundle/node/Gemfile.lock @@ -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! diff --git a/bundle/node/Guardfile b/bundle/node/Guardfile new file mode 100644 index 0000000..03c4a45 --- /dev/null +++ b/bundle/node/Guardfile @@ -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 diff --git a/bundle/node/LICENSE b/bundle/node/LICENSE new file mode 100644 index 0000000..47c9190 --- /dev/null +++ b/bundle/node/LICENSE @@ -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. diff --git a/bundle/node/Makefile b/bundle/node/Makefile new file mode 100644 index 0000000..fdbb228 --- /dev/null +++ b/bundle/node/Makefile @@ -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 diff --git a/bundle/node/README.md b/bundle/node/README.md new file mode 100644 index 0000000..7d63374 --- /dev/null +++ b/bundle/node/README.md @@ -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 `f` to open the file under the cursor in a new vertical split? +`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 f NodeVSplitGotoFile | + \ nmap 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). diff --git a/bundle/node/autoload/node.vim b/bundle/node/autoload/node.vim new file mode 100644 index 0000000..3954e4c --- /dev/null +++ b/bundle/node/autoload/node.vim @@ -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(, bufname("%"), "edit") + command! -bar -bang -nargs=1 -buffer -complete=customlist,s:complete Nopen + \ exe s:nopen(, bufname("%"), "edit") + + nnoremap NodeGotoFile + \ :call edit(expand(""), bufname("%")) + nnoremap NodeSplitGotoFile + \ :call edit(expand(""), bufname("%"), "split") + nnoremap NodeVSplitGotoFile + \ :call edit(expand(""), bufname("%"), "vsplit") + nnoremap NodeTabGotoFile + \ :call edit(expand(""), bufname("%"), "tab split") + + 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 = '\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 gf NodeGotoFile + nmap f NodeSplitGotoFile + nmap NodeSplitGotoFile + nmap gf 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 diff --git a/bundle/node/autoload/node/lib.vim b/bundle/node/autoload/node/lib.vim new file mode 100644 index 0000000..239b864 --- /dev/null +++ b/bundle/node/autoload/node/lib.vim @@ -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 diff --git a/bundle/node/ftdetect/node.vim b/bundle/node/ftdetect/node.vim new file mode 100644 index 0000000..ed50604 --- /dev/null +++ b/bundle/node/ftdetect/node.vim @@ -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 diff --git a/bundle/node/plugin/node.vim b/bundle/node/plugin/node.vim new file mode 100644 index 0000000..a541292 --- /dev/null +++ b/bundle/node/plugin/node.vim @@ -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("")) | call s:detect(getcwd()) | endif + au BufRead,BufNewFile * call s:detect(expand(":p")) + + let s:filetype_patterns = s:flatten(map(s:filetypes, "permutate(v:val)")) + let s:filetype_patterns_joined = join(s:filetype_patterns, ",") + execute "au FileType " s:filetype_patterns_joined " call node#javascript()" +augroup end diff --git a/bundle/node/test/autoload/lib_test.rb b/bundle/node/test/autoload/lib_test.rb new file mode 100644 index 0000000..74a696d --- /dev/null +++ b/bundle/node/test/autoload/lib_test.rb @@ -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 diff --git a/bundle/node/test/autoload_test.rb b/bundle/node/test/autoload_test.rb new file mode 100644 index 0000000..464d803 --- /dev/null +++ b/bundle/node/test/autoload_test.rb @@ -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("NodeGotoFile", "n"))).wont_equal "" + end + + it "must not be mapped in non-JavaScript files" do + $vim.edit File.join(@dir, "README") + $vim.echo(%(hasmapto("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\\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.\\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.\\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 diff --git a/bundle/node/test/ftdetect_test.rb b/bundle/node/test/ftdetect_test.rb new file mode 100644 index 0000000..b28326e --- /dev/null +++ b/bundle/node/test/ftdetect_test.rb @@ -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 diff --git a/bundle/node/test/helper.rb b/bundle/node/test/helper.rb new file mode 100644 index 0000000..5c50977 --- /dev/null +++ b/bundle/node/test/helper.rb @@ -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] diff --git a/bundle/node/test/plugin_test.rb b/bundle/node/test/plugin_test.rb new file mode 100644 index 0000000..722c287 --- /dev/null +++ b/bundle/node/test/plugin_test.rb @@ -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 diff --git a/bundle/node/test/vimrc b/bundle/node/test/vimrc new file mode 100644 index 0000000..2f05b18 --- /dev/null +++ b/bundle/node/test/vimrc @@ -0,0 +1,14 @@ +set nocompatible +set noswapfile +set nobackup +set hidden + +set runtimepath-=~/.vim +set runtimepath-=~/.vim/after +let &runtimepath = expand(":p:h:h") . "," . &runtimepath + +filetype plugin indent on +syntax on + +set columns=80 lines=24 +winpos 1337 0 diff --git a/bundle/vim-javascript b/bundle/vim-javascript deleted file mode 160000 index 0922d26..0000000 --- a/bundle/vim-javascript +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0922d26d1477421d213f82ad4224c959b4c2357e diff --git a/bundle/vim-javascript-lib b/bundle/vim-javascript-lib deleted file mode 160000 index f406e8c..0000000 --- a/bundle/vim-javascript-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f406e8c85fe61fb82921214e49f9b77475258b08 diff --git a/bundle/vim-javascript-lib/LICENSE b/bundle/vim-javascript-lib/LICENSE new file mode 100644 index 0000000..dd35382 --- /dev/null +++ b/bundle/vim-javascript-lib/LICENSE @@ -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. + diff --git a/bundle/vim-javascript-lib/README.md b/bundle/vim-javascript-lib/README.md new file mode 100644 index 0000000..02e73e0 --- /dev/null +++ b/bundle/vim-javascript-lib/README.md @@ -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) diff --git a/bundle/vim-javascript-lib/after/syntax/html.vim b/bundle/vim-javascript-lib/after/syntax/html.vim new file mode 100644 index 0000000..e82a088 --- /dev/null +++ b/bundle/vim-javascript-lib/after/syntax/html.vim @@ -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 diff --git a/bundle/vim-javascript-lib/after/syntax/javascript.vim b/bundle/vim-javascript-lib/after/syntax/javascript.vim new file mode 100644 index 0000000..aa55ba3 --- /dev/null +++ b/bundle/vim-javascript-lib/after/syntax/javascript.vim @@ -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 + else + command -nargs=+ HiLink hi def link + 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 diff --git a/bundle/vim-javascript-lib/screenshots/after.png b/bundle/vim-javascript-lib/screenshots/after.png new file mode 100644 index 0000000..e1ffacf Binary files /dev/null and b/bundle/vim-javascript-lib/screenshots/after.png differ diff --git a/bundle/vim-javascript-lib/screenshots/before.png b/bundle/vim-javascript-lib/screenshots/before.png new file mode 100644 index 0000000..fece6d8 Binary files /dev/null and b/bundle/vim-javascript-lib/screenshots/before.png differ diff --git a/bundle/vim-javascript/ISSUE_TEMPLATE.md b/bundle/vim-javascript/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..b1ddccb --- /dev/null +++ b/bundle/vim-javascript/ISSUE_TEMPLATE.md @@ -0,0 +1,13 @@ +*Requisite minimal reproducible example, formatted as plain text :* + +
+ +#### 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 diff --git a/bundle/vim-javascript/README.md b/bundle/vim-javascript/README.md new file mode 100644 index 0000000..b9ac49f --- /dev/null +++ b/bundle/vim-javascript/README.md @@ -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 `l` (leader is usually the `\` key) to toggling conceal mode: + + map l :exec &conceallevel ? "set conceallevel=0" : "set conceallevel=1" + + +## 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`. diff --git a/bundle/vim-javascript/after/ftplugin/javascript.vim b/bundle/vim-javascript/after/ftplugin/javascript.vim new file mode 100644 index 0000000..c6c42d6 --- /dev/null +++ b/bundle/vim-javascript/after/ftplugin/javascript.vim @@ -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 diff --git a/bundle/vim-javascript/compiler/eslint.vim b/bundle/vim-javascript/compiler/eslint.vim new file mode 100644 index 0000000..7695a07 --- /dev/null +++ b/bundle/vim-javascript/compiler/eslint.vim @@ -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 +endif + +CompilerSet makeprg=eslint\ -f\ compact\ % +CompilerSet errorformat=%f:\ line\ %l\\,\ col\ %c\\,\ %m diff --git a/bundle/vim-javascript/extras/ctags b/bundle/vim-javascript/extras/ctags new file mode 100644 index 0000000..cdc4edc --- /dev/null +++ b/bundle/vim-javascript/extras/ctags @@ -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/ diff --git a/bundle/vim-javascript/extras/flow.vim b/bundle/vim-javascript/extras/flow.vim new file mode 100644 index 0000000..204fdbe --- /dev/null +++ b/bundle/vim-javascript/extras/flow.vim @@ -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=// 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=// 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=// contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncArgs +syntax region jsFlowClassGroup contained matchgroup=jsFlowNoise start=// 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 + else + command -nargs=+ HiLink hi def link + 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 diff --git a/bundle/vim-javascript/extras/jsdoc.vim b/bundle/vim-javascript/extras/jsdoc.vim new file mode 100644 index 0000000..a19f3a3 --- /dev/null +++ b/bundle/vim-javascript/extras/jsdoc.vim @@ -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 + else + command -nargs=+ HiLink hi def link + 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 diff --git a/bundle/vim-javascript/extras/ngdoc.vim b/bundle/vim-javascript/extras/ngdoc.vim new file mode 100644 index 0000000..c513d87 --- /dev/null +++ b/bundle/vim-javascript/extras/ngdoc.vim @@ -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\|\"\|-\|\.\|:\|{\|}\|\/\|\[\|]\|=\)\+" diff --git a/bundle/vim-javascript/ftdetect/javascript.vim b/bundle/vim-javascript/ftdetect/javascript.vim new file mode 100644 index 0000000..ff07f4d --- /dev/null +++ b/bundle/vim-javascript/ftdetect/javascript.vim @@ -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 call s:SourceFlowSyntax() + +fun! s:SelectJavascript() + if getline(1) =~# '^#!.*/bin/\%(env\s\+\)\?node\>' + set ft=javascript + endif +endfun +au BufNewFile,BufRead * call s:SelectJavascript() diff --git a/bundle/vim-javascript/indent/javascript.vim b/bundle/vim-javascript/indent/javascript.vim new file mode 100644 index 0000000..a675386 --- /dev/null +++ b/bundle/vim-javascript/indent/javascript.vim @@ -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