aboutsummaryrefslogtreecommitdiff
path: root/.vim/plugin/node.vim
diff options
context:
space:
mode:
authorYaroslav <contact@yaroslavps.com>2019-09-05 19:35:38 +0300
committerYaroslav <contact@yaroslavps.com>2019-09-05 19:35:38 +0300
commit212dcd0bf753f08c0127a26a71b673c734b45c02 (patch)
tree4a43e43c7c9b9ccfcaef4088ba9e5f52154994b5 /.vim/plugin/node.vim
downloadvimrice-212dcd0bf753f08c0127a26a71b673c734b45c02.tar.gz
vimrice-212dcd0bf753f08c0127a26a71b673c734b45c02.zip
init commit, extracted vim config from i3rice
Diffstat (limited to '.vim/plugin/node.vim')
-rw-r--r--.vim/plugin/node.vim47
1 files changed, 47 insertions, 0 deletions
diff --git a/.vim/plugin/node.vim b/.vim/plugin/node.vim
new file mode 100644
index 0000000..a541292
--- /dev/null
+++ b/.vim/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("<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