aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/share/nvim/site/plugin/node.vim
blob: a541292522eae13cf9a77cd2419b366a72910471 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
if exists("g:loaded_node") || &cp || v:version < 700 | finish | endif
let g:loaded_node = 1

let s:filetypes = ["javascript", "json", "jsx"]
if exists("g:node_filetypes") | let s:filetypes = g:node_filetypes | endif

function! s:detect(dir)
	if exists("b:node_root") | return | endif
	let dir = a:dir

	while 1
		let is_node = 0
		let is_node = is_node || filereadable(dir . "/package.json")
		let is_node = is_node || isdirectory(dir . "/node_modules")
		if is_node | return node#initialize(dir) | endif

		let parent = fnamemodify(dir, ":h")
		if parent == dir | return | endif
		let dir = parent
	endwhile
endfunction

function! s:permutate(ft)
	" Don't know right now how to detect javascript.jsx and other permutations
	" without precomputing them in advance. Please let me know if you do.
	return [a:ft, a:ft . ".*", "*." . a:ft, "*." . a:ft . ".*"]
endfunction

function! s:flatten(list)
	let values = []
	for value in a:list
		if type(value) == type([]) | call extend(values, value)
		else | add(values, value)
		endif
	endfor
	return values
endfunction

augroup Node
	au!
	au VimEnter * if empty(expand("<amatch>")) | call s:detect(getcwd()) | endif
	au BufRead,BufNewFile * call s:detect(expand("<amatch>:p"))

	let s:filetype_patterns = s:flatten(map(s:filetypes, "<SID>permutate(v:val)"))
	let s:filetype_patterns_joined = join(s:filetype_patterns, ",")
	execute "au FileType " s:filetype_patterns_joined " call node#javascript()"
augroup end