Skip to content

Commit 64f18d0

Browse files
Add nodejs tool layer
1 parent 953d9ba commit 64f18d0

File tree

7 files changed

+204
-13
lines changed

7 files changed

+204
-13
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
" Will do the following checks in order of precedence:
2+
" -Use $NODE or $NODE/node if they exist and are executable
3+
" -check for node as vim-plug plugin (user-specific)
4+
" -check for system-wide install of node
5+
" -If all above fail will get node as vim-plug plugin and build it
6+
" Changes $PATH to include node path
7+
function! spacevim#plug#nodejs#IsBuildRequired(maj, min)
8+
let pathsep = g:spacevim.os.windows ? ';' : ':'
9+
" Prefer $NODE override
10+
if exists('$NODE') && spacevim#util#IsVersionSufficient('$NODE', a:maj, a:min)
11+
let $PATH = fnamemodify('$NODE', ':h').pathsep.$PATH
12+
return 0
13+
endif
14+
" Prefer user plugin Node.js if it exists
15+
if spacevim#util#IsVersionSufficient(g:plug_home.'/node/node', a:maj, a:min)
16+
let $PATH = g:plug_home.'/node'.pathsep.$PATH
17+
return 0
18+
endif
19+
" Last check for system-wide Node.js
20+
if spacevim#util#IsVersionSufficient('node', a:maj, a:min)
21+
return 0
22+
endif
23+
return 1
24+
endfunction
25+
26+
" If no valid $NODE or systemwide node.js: build node.js dependency
27+
function! spacevim#plug#nodejs#Build(info)
28+
if !spacevim#load('programming')
29+
echom "nodejs layer requires the 'programming' layer to build. Add 'programming' layer to init.spacevim"
30+
return
31+
endif
32+
if g:spacevim.os.windows
33+
echom 'Building Node.js automatically on Windows is untested and likely to fail'
34+
echom 'Run msys2_shell.cmd or vcvars64.bat per desired ABI'
35+
return
36+
elseif (!exists('$CC') && executable('gcc') != 1) ||
37+
\ executable('make') != 1 || executable('python3') != 1
38+
echom 'Unable to build Node.js, requires a C++ compiler, make, and python3'
39+
return
40+
endif
41+
42+
echom 'Building Node.js, please wait 60+ seconds for build to start...'
43+
let s:cwd = getcwd()
44+
45+
if get(g:, 'spacevim.build_deps_in_tmp', 1) && getftype('out') !~# 'dir'
46+
" Build intermediate files to temp, vim will delete folder on exit
47+
let temp_dir = fnamemodify(tempname(), ':p:h')
48+
call mkdir(temp_dir.'/out', 'p')
49+
if getftype('out') =~# 'link'
50+
call system('rm out')
51+
endif
52+
call system('ln -s '.temp_dir.'/out out')
53+
endif
54+
55+
let s:jobs_flag = ''
56+
if g:spacevim.os.windows
57+
autocmd User asyncrun.vim ++once execute('AsyncRun -mode=term -pos=tab @ .\\vcbuild openssl-no-asm')
58+
else
59+
let ninja_flag = ''
60+
if executable('ninja')
61+
let ninja_flag=' --ninja'
62+
else
63+
" Determine number of cores/threads for make -j, ninja autodetects
64+
let num_threads = 4
65+
if executable('lscpu')
66+
let result = system("lscpu | grep -E '?^(CPU\\(s\\):|Thread\\(s\\) per core:)' | tr -s ' ' | cut -f 2 -d:")
67+
if !v:shell_error
68+
let num_threads = join(split(result), '*')
69+
endif
70+
endif
71+
let s:jobs_flag = ' -j '.num_threads
72+
endif
73+
call system('./configure'.ninja_flag.' > '.temp_dir.'/nodejs_configure.log')
74+
autocmd User asyncrun.vim ++once execute(
75+
\ 'AsyncRun -cwd='.s:cwd.' -mode=term -pos=tab -program=make -post=call\ spacevim\#plug\#nodejs\#PostBuild(code) @'.s:jobs_flag)
76+
endif
77+
call timer_start(60000, { -> plug#load('asyncrun.vim') })
78+
endfunction
79+
80+
function! spacevim#plug#nodejs#PostBuild(exit_code)
81+
execute 'tcd' fnameescape(s:cwd)
82+
unlet s:cwd s:jobs_flag
83+
if a:exit_code == 0
84+
call system('rm -r deps/icu-tmp node')
85+
call rename('out/Release/node', 'node')
86+
let pathsep = g:spacevim.os.windows ? ';' : ':'
87+
let $PATH = g:plug_home.'/node'.pathsep.$PATH
88+
89+
" This sets up corepack/npm links the same as `make install` does
90+
call system('ln -s deps/corepack/dist/corepack.js corepack')
91+
call system('ln -s deps/npm/bin/npm-cli.js npm')
92+
call system('ln -s deps/npm/bin/npx-cli.js npx')
93+
call system('node corepack enable')
94+
endif
95+
if get(g:, 'spacevim.build_deps_in_tmp', 1)
96+
call system('rm out')
97+
endif
98+
tcd -
99+
endfunction
100+

core/autoload/spacevim/util.vim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,16 @@ function! spacevim#util#CycleQuickfix(action, fallback) abort
213213
normal! zz
214214
endtry
215215
endfunction
216+
217+
function! spacevim#util#IsVersionSufficient(file, maj, min) abort
218+
if executable(a:file) == 1
219+
let fversion = system(a:file.' --version')
220+
if !v:shell_error
221+
let version_parts = map(filter(split(fversion, '\v\ze\.|v'), 'v:val =~# "\\v\\d+"'), 'matchstr(v:val, "\\v\\d+")')
222+
if len(version_parts) >= 2 && (version_parts[0] >= (a:maj+1) || (version_parts[0] == a:maj && version_parts[1] >= a:min))
223+
return 1
224+
endif
225+
endif
226+
endif
227+
return 0
228+
endfunction

layers/+tools/lsp/README.md

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<!-- vim-markdown-toc GFM -->
66

77
* [Description](#description)
8-
* [Requrement](#requrement)
8+
* [Install](#install)
9+
* [Manual Language Server Installation](#manual-language-server-installation)
910
* [Rust](#rust)
1011
* [Python](#python)
1112
* [Go](#go)
@@ -14,7 +15,6 @@
1415
* [Haskell](#haskell)
1516
* [Lua](#lua)
1617
* [vue](#vue)
17-
* [Install](#install)
1818
* [Key Bindings](#key-bindings)
1919
* [Related Projects](#related-projects)
2020

@@ -24,7 +24,48 @@
2424

2525
This layer adds supports [Language Server Protocol](https://langserver.org/).
2626

27-
## Requrement
27+
## Install
28+
29+
To use this configuration layer, add it to your `~/.spacevim`, set it up like so:
30+
31+
```vim
32+
let g:spacevim_layers = [
33+
\ 'lsp',
34+
\ ]
35+
```
36+
37+
Currently [LanguageClient-neovim](https://github.com/autozimu/LanguageClient-neovim) is the default *language server client* when using the lsp layer.
38+
39+
Currently the *language server client* builtin to Neovim is not supported by space-vim [#483](https://github.com/liuchengxu/space-vim/issues/483).
40+
41+
To use [coc.nvim](https://github.com/neoclide/coc.nvim) or [vim-lsp](https://github.com/prabirshrestha/vim-lsp) instead of the default set `g:spacevim_lsp_engine` as desired:
42+
43+
```vim
44+
let g:spacevim_layers = [
45+
\ 'lsp',
46+
\ ]
47+
48+
let g:spacevim_lsp_engine = 'coc'
49+
let g:spacevim_lsp_engine = 'vim_lsp'
50+
```
51+
52+
Note that coc.nvim is dependent on Node.js, the nodejs layer will satisfy that dependency.
53+
54+
If using vim-lsp as the *language server client*, this layer will handle the configuration and installation of many *language servers* for you (this layer includes the [vim-lsp-settings](https://github.com/mattn/vim-lsp-settings) plugin). On opening a file this layer will detect if there is a *language server* for that filetype and prompt you to run the command `:LspInstallServer`.
55+
56+
Unlike other Vim *language server clients*, LanguageClient-neovim does not include configuration and installation of *language servers*. space-vim includes some configuration for *language servers* and this client, but installation must be done manually.
57+
58+
Often the Yarn or npm tools are needed to retrieve a *language server* and its dependencies. For convenience space-vim includes these tools within the nodejs layer, which builds them or locates them using the `$PATH` and `$NODE` environmental variables. You may include the nodejs layer as follows (which may require the programming layer):
59+
60+
```vim
61+
let g:spacevim_layers = [
62+
\ 'programming', 'nodejs', 'lsp',
63+
\ ]
64+
```
65+
66+
## Manual Language Server Installation
67+
68+
If using LanguageClient-neovim, directions for the manual installation of some LSP servers are as follows. Other configuration and installation directions can be found in locations like its github wiki pages.
2869

2970
### Rust
3071

@@ -120,16 +161,6 @@ $ luarocks install --server=http://luarocks.org/dev lua-lsp
120161
$ npm install vue-language-server -g
121162
```
122163

123-
## Install
124-
125-
To use this configuration layer, add it to your `~/.spacevim`.
126-
127-
Currently, [LanguageClient-neovim](https://github.com/autozimu/LanguageClient-neovim) is the default LS client. To use [coc.nvim](https://github.com/neoclide/coc.nvim) instead:
128-
129-
```vim
130-
let g:spacevim_lsp_engine = 'coc'
131-
```
132-
133164
## Key Bindings
134165

135166
Key Binding | Mode | Description

layers/+tools/nodejs/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# nodejs tool Layer
2+
3+
## Table of Contents
4+
5+
<!-- vim-markdown-toc GFM -->
6+
* [Description](#description)
7+
* [Install](#install)
8+
* [MS Windows](#ms-windows)
9+
10+
<!-- vim-markdown-toc -->
11+
12+
## Description
13+
14+
Folowing the zero-install philosophy, this layer adds the Node.js tool, built from source if needed.
15+
16+
If `node` is found on $PATH, or $NODE points to `node`, and either is of
17+
sufficient version, that will be used instead of building from source.
18+
19+
Node.js built with the repo requires 1.2 GiB space. Intermediate files are deleted after build.
20+
21+
The curated node will be added to `$PATH` for use by other plugins. Also includes yarn/npm, useful for other plugins (such as language servers) to install dependencies.
22+
23+
## Install
24+
25+
To use this layer, add it to your `~/.spacevim` as follows:
26+
27+
```
28+
let g:spacevim_layers = [
29+
\ 'programming', 'nodejs',
30+
\ ]
31+
```
32+
33+
Under appropriate conditions the plugin manager will clone and build the repository in a Vim tab.
34+
35+
To speed up the build and conserve disk space, the repository will be built in a temp directory which will be cleared on Vim exit. See `:help tempfile`. Set `g:spacevim.build_deps_in_tmp = 0` to disable this optimization.
36+
37+
### MS Windows
38+
39+
You have two ABI choices: MSVC (comes with Visual Studio Tools) and GNU. Your choice depends on on C/C++ libraries you want to interoperate with.
40+
41+
Automated building from source isn't tested. However it should use the environmental variables as defined by `msys2_shell.cmd` or `vcvars64.bat` should you run that prior to launching `vim`.
42+
43+
Alternatively, navigate to the plugin directory and build manually. Installing system-wide isn't required, it may be used in-place. Or install prebuilt binaries and set $PATH or $NODE accordingly.

layers/+tools/nodejs/config.vim

Whitespace-only changes.

layers/+tools/nodejs/packages.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if spacevim#plug#nodejs#IsBuildRequired(16, 10)
2+
MP 'nodejs/node', { 'do': function('spacevim#plug#nodejs#Build') }
3+
endif

layers/LAYERS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Topic | Layer | Plugins
5050
+tools | [file-manager](https://github.com/liuchengxu/space-vim/tree/master/layers/+tools/file-manager) | <ul><li>[Xuyuanp/nerdtree-git-plugin](https://github.com/Xuyuanp/nerdtree-git-plugin)</li><li>[danro/rename.vim](https://github.com/danro/rename.vim)</li><li>[liuchengxu/nerdtree-dash](https://github.com/liuchengxu/nerdtree-dash)</li><li>[ryanoasis/vim-devicons](https://github.com/ryanoasis/vim-devicons)</li><li>[scrooloose/nerdtree](https://github.com/scrooloose/nerdtree)</li></ul>
5151
+tools | [fzf](https://github.com/liuchengxu/space-vim/tree/master/layers/+tools/fzf) | <ul><li>[Yggdroot/LeaderF](https://github.com/Yggdroot/LeaderF)</li><li>[junegunn/fzf](https://github.com/junegunn/fzf)</li><li>[junegunn/fzf.vim](https://github.com/junegunn/fzf.vim)</li><li>[liuchengxu/vim-clap](https://github.com/liuchengxu/vim-clap)</li></ul>
5252
+tools | [lsp](https://github.com/liuchengxu/space-vim/tree/master/layers/+tools/lsp) | <ul><li>[autozimu/LanguageClient-neovim](https://github.com/autozimu/LanguageClient-neovim)</li><li>[mattn/vim-lsp-settings](https://github.com/mattn/vim-lsp-settings)</li><li>[neoclide/coc-neco](https://github.com/neoclide/coc-neco)</li><li>[neoclide/coc.nvim](https://github.com/neoclide/coc.nvim)</li><li>[prabirshrestha/async.vim](https://github.com/prabirshrestha/async.vim)</li><li>[prabirshrestha/vim-lsp](https://github.com/prabirshrestha/vim-lsp)</li><li>[rhysd/vim-lsp-ale](https://github.com/rhysd/vim-lsp-ale)</li></ul>
53+
+tools | [nodejs](https://github.com/liuchengxu/space-vim/tree/master/layers/+tools/nodejs) | <ul><li>[nodejs/node](https://github.com/nodejs/node)</li></ul>
5354
+tools | [tmux](https://github.com/liuchengxu/space-vim/tree/master/layers/+tools/tmux) | <ul><li>[christoomey/vim-tmux-navigator](https://github.com/christoomey/vim-tmux-navigator)</li><li>[jebaum/vim-tmuxify](https://github.com/jebaum/vim-tmuxify)</li><li>[lucidstack/ctrlp-tmux.vim](https://github.com/lucidstack/ctrlp-tmux.vim)</li></ul>
5455
+tools | [ycmd](https://github.com/liuchengxu/space-vim/tree/master/layers/+tools/ycmd) | <ul><li>[rdnetto/YCM-Generator](https://github.com/rdnetto/YCM-Generator)</li><li>[ycm-core/YouCompleteMe](https://github.com/ycm-core/YouCompleteMe)</li></ul>
5556
+version-control | [git](https://github.com/liuchengxu/space-vim/tree/master/layers/+version-control/git) | <ul><li>[junegunn/gv.vim](https://github.com/junegunn/gv.vim)</li><li>[mhinz/vim-signify](https://github.com/mhinz/vim-signify)</li><li>[tpope/vim-fugitive](https://github.com/tpope/vim-fugitive)</li></ul>

0 commit comments

Comments
 (0)