From 0bfc63dc07c81344b0c30514e71f3142bfb47ae4 Mon Sep 17 00:00:00 2001
From: James Nylen <jnylen@gmail.com>
Date: Wed, 23 Oct 2013 11:02:11 -0400
Subject: [PATCH] Set up for require() helper plugin

Expose list of core modules, add new node#lib#deps function, and add a
note to the readme.
---
 README.md             | 10 ++++++++++
 autoload/node.vim     | 10 ++++++++++
 autoload/node/lib.vim | 28 +++++++++++++++++-----------
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index e3a3bd0..0189e6e 100644
--- a/README.md
+++ b/README.md
@@ -77,6 +77,16 @@ autocmd User Node
   \ endif
 ```
 
+
+Plugins
+-------
+Install Node.vim first, then install one or more of the following plugins in separate folders:
+
+- [require() helper](https://github.com/nylen/vim-node-require-helper)
+
+Please write more plugins!
+
+
 License
 -------
 Node.vim is released under a *Lesser GNU Affero General Public License*, which in summary means:
diff --git a/autoload/node.vim b/autoload/node.vim
index 287344c..f3e0cf4 100644
--- a/autoload/node.vim
+++ b/autoload/node.vim
@@ -1,6 +1,16 @@
 let node#suffixesadd = [".js", ".json"]
 let node#filetypes = ["javascript", "json"]
 
+let node#coreModules = ["_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#initialize(root)
 	let b:node_root = a:root
 	call s:initializeCommands()
diff --git a/autoload/node/lib.vim b/autoload/node/lib.vim
index 702bee3..b7a02a7 100644
--- a/autoload/node/lib.vim
+++ b/autoload/node/lib.vim
@@ -4,18 +4,9 @@ let s:MODULE = '\v^(/|\.\.?(/|$))@!'
 
 " Damn Netrw can't handle HTTPS at all. It's 2013! Insecure bastard!
 let s:CORE_URL_PREFIX = "http://rawgithub.com/joyent/node"
-let s:CORE_MODULES = ["_debugger", "_http_agent", "_http_client",
-	\ "_http_common", "_http_incoming", "_http_outgoing", "_http_server",
-	\ "_linklist", "_stream_duplex", "_stream_passthrough", "_stream_readable",
-	\ "_stream_transform", "_stream_writable", "_tls_legacy", "_tls_wrap",
-	\ "assert", "buffer", "child_process", "cluster", "console", "constants",
-	\ "crypto", "dgram", "dns", "domain", "events", "freelist", "fs", "http",
-	\ "https", "module", "net", "node", "os", "path", "punycode", "querystring",
-	\ "readline", "repl", "smalloc", "stream", "string_decoder", "sys",
-	\ "timers", "tls", "tty", "url", "util", "vm", "zlib"]
 
 function! node#lib#find(name, from)
-	if index(s:CORE_MODULES, a:name) != -1
+	if index(g:node#coreModules, 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"
@@ -86,6 +77,21 @@ function! s:mainFromPackage(path)
 	endfor
 endfunction
 
+function! node#lib#deps(path)
+	let inDeps = 0
+	let deps = []
+	for line in readfile(a:path)
+		if line =~# '\v^\s*"(devDependencies|dependencies)"\s*:\s*\{\s*$'
+			let inDeps = 1
+		elseif line =~# '^\s*}\s*,?\s*$'
+			let inDeps = 0
+		elseif inDeps
+			call add(deps, matchstr(line, '^\s*"\zs[^"]\+\ze"'))
+		endif
+	endfor
+	return deps
+endfunction
+
 function! s:resolveSuffix(path)
 	for suffix in s:uniq([""] + g:node#suffixesadd + split(&l:suffixesadd, ","))
 		let path = a:path . suffix
@@ -99,7 +105,7 @@ function! node#lib#glob(name)
 	let matches = []
 
 	if empty(a:name)
-		let matches += s:CORE_MODULES
+		let matches += g:node#coreModules
 	endif
 
 	if empty(a:name) || a:name =~# s:MODULE