Skip to content

Commit 178d6e2

Browse files
Add nodejs tool layer
1 parent 9ab50bb commit 178d6e2

File tree

6 files changed

+171
-13
lines changed

6 files changed

+171
-13
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
" If no valid $NODE or systemwide node.js: build node.js dependency
2+
function! spacevim#plug#nodejs#build(info)
3+
if g:spacevim.os.windows
4+
echom 'Building Node.js automatically is not supported on Windows, Build it manually or send PR'
5+
return
6+
endif
7+
if (!exists('$CC') && executable('gcc') != 1) || executable('make') != 1 || executable('python3') != 1
8+
echom 'Unable to build Node.js, requires a C++ compiler, make, and python3'
9+
return
10+
endif
11+
if !spacevim#load('programming')
12+
echom "nodejs layer requires the 'programming' layer to build. Add 'programming' layer to init.spacevim"
13+
return
14+
endif
15+
16+
echom 'Building Node.js, please wait... restart Vim on completion'
17+
let ninja_flag = ''
18+
let jobs_flag = ''
19+
if executable('ninja')
20+
let ninja_flag=' --ninja'
21+
else
22+
" Determine number of cores/threads for make -j, ninja autodetects
23+
let num_threads = 4
24+
if executable('lscpu')
25+
let result = system("lscpu | grep -E '?^(CPU\\(s\\):|Thread\\(s\\) per core:)' | tr -s ' ' | cut -f 2 -d:")
26+
if !v:shell_error
27+
let num_threads = join(split(result),'*')
28+
endif
29+
endif
30+
let jobs_flag = ' -j '.num_threads
31+
endif
32+
33+
" Build intermediate files to temp, vim will delete folder on exit
34+
let temp_dir = fnamemodify(tempname(), ':p:h')
35+
call mkdir(temp_dir.'/out', 'p')
36+
if getftype('out') =~# 'link'
37+
call system('rm out')
38+
endif
39+
call system('ln -s '.temp_dir.'/out out')
40+
call system('./configure --prefix=./build'.ninja_flag.' > '.temp_dir.'/nodejs_configure.log')
41+
execute('AsyncRun -mode=term -pos=tab -program=make @'.jobs_flag)
42+
43+
let pathsep = g:spacevim.os.windows ? ';' : ':'
44+
let $PATH = g:plug_home.'/node'.pathsep.$PATH
45+
46+
call rename('out/Release/node', 'node')
47+
call system('rm -r deps/icu-tmp')
48+
49+
" This sets up corepack/npm links the same as `make install` does
50+
" Could alternatively `make install` to prefix and set PATH to prefix/bin
51+
call system('ln -s deps/corepack/dist/corepack.js corepack')
52+
call system('ln -s deps/npm/bin/npm-cli.js npm')
53+
call system('ln -s deps/npm/bin/npx-cli.js npx')
54+
" Make Yarn available
55+
call system('node corepack enable')
56+
endfunction
57+
58+
" Check for Node.js >= v16.10
59+
function! s:isNodeJsVersionSufficient(node)
60+
if executable(a:node) == 1
61+
let node_version = system(a:node.' --version')
62+
let node_version_parts = map(filter(split(node_version, '\v\ze\.|v'), 'v:val =~# "\\v\\d+"'), 'matchstr(v:val, "\\v\\d+")')
63+
if len(node_version_parts) >= 2 && (node_version_parts[0] > 16 || (node_version_parts[0] == 16 && node_version_parts[1] >= 10))
64+
return 1
65+
endif
66+
endif
67+
return 0
68+
endfunction
69+
70+
" Will do the following checks in order of precedence:
71+
" -Use $NODE or $NODE/node if they exist and are executable
72+
" -check for node as vim-plug plugin (user-specific)
73+
" -check for system-wide install of node
74+
" -If all above fail will get node as vim-plug plugin and build it
75+
" Changes $PATH to include node path
76+
function! spacevim#plug#nodejs#isBuildRequired()
77+
let pathsep = g:spacevim.os.windows ? ';' : ':'
78+
" Prefer $NODE override
79+
if exists('$NODE') && s:isNodeJsVersionSufficient('$NODE')
80+
let $PATH = fnamemodify('$NODE', ':h').pathsep.$PATH
81+
return 0
82+
endif
83+
" Prefer user plugin Node.js if it exists
84+
if s:isNodeJsVersionSufficient(g:plug_home.'/node/node')
85+
let $PATH = g:plug_home.'/node'.pathsep.$PATH
86+
return 0
87+
endif
88+
" Last check for system-wide Node.js
89+
if s:isNodeJsVersionSufficient('node')
90+
return 0
91+
endif
92+
return 1
93+
endfunction

layers/+tools/lsp/README.md

Lines changed: 42 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,46 @@
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+
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`.
53+
54+
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.
55+
56+
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):
57+
58+
```vim
59+
let g:spacevim_layers = [
60+
\ 'programming', 'nodejs', 'lsp',
61+
\ ]
62+
```
63+
64+
## Manual Language Server Installation
65+
66+
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.
2867

2968
### Rust
3069

@@ -120,16 +159,6 @@ $ luarocks install --server=http://luarocks.org/dev lua-lsp
120159
$ npm install vue-language-server -g
121160
```
122161

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-
133162
## Key Bindings
134163

135164
Key Binding | Mode | Description

layers/+tools/nodejs/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# nodejs Layer
2+
3+
## Table of Contents
4+
5+
<!-- vim-markdown-toc GFM -->
6+
* [Description](#description)
7+
* [Install](#install)
8+
* [Key Bindings](#keybindings)
9+
10+
<!-- vim-markdown-toc -->
11+
12+
## Description
13+
14+
This layer adds support for the Node.js tool.
15+
16+
## Install
17+
18+
To use this layer, add it to your `~/.spacevim` as follows:
19+
20+
```
21+
let g:spacevim_layers = [
22+
\ 'programming', 'nodejs',
23+
\ ]
24+
```
25+
26+
If Node.js is installed system-wide and is version >= 16.10 then that will be used. Otherwise the nodejs layer requires the programming layer to build Node.js as a dependency within vim-plug's plugged directory.
27+
28+
Node.js built with the repo requires 1.2 GiB space. Intermediate files are deleted after build.
29+
30+
If you instead wish to use Node.js elsewhere (not on `$PATH`), this package will look at `$NODE` and use that if it is of sufficient version.
31+
32+
The curated node will be added to `$PATH` for use by other plugins. Node.js >= v16.10 also includes yarn/npm, useful for other plugins to install dependencies.

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()
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)