Skip to content

Commit

Permalink
feat(plugin): rework vim-node-rpc part
Browse files Browse the repository at this point in the history
- Start service after VimEnter
- Support `g:vim_node_rpc_folder`
- Prefer installed by yarn
- Use background upgrade
  • Loading branch information
chemzqm committed Dec 16, 2018
1 parent 4443592 commit f80def6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 60 deletions.
5 changes: 3 additions & 2 deletions autoload/coc/client.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ function! s:start() dict
if self.running | return | endif
if s:is_vim
if empty($NVIM_LISTEN_ADDRESS)
echoerr 'vim-node-rpc service not started!'
return
let command = nvim#rpc#get_command()
if empty(command) | return | endif
call nvim#rpc#start_server()
endif
let self.running = 1
let options = {
Expand Down
66 changes: 27 additions & 39 deletions autoload/nvim/rpc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let s:is_win = has("win32") || has("win64")
let s:clientIds = []
let s:logfile = tempname()
let s:channel = v:null
let s:command = ''

" env used only for testing purpose
if !empty($NVIM_LISTEN_ADDRESS)
Expand Down Expand Up @@ -48,25 +49,25 @@ function! s:on_exit(channel)
endfunction

function! nvim#rpc#get_command() abort
" executable not works on windows
if executable('vim-node-rpc') && !s:is_win
return 'vim-node-rpc'
let folder = get(g:, 'vim_node_rpc_folder', '')
let file = empty(folder) ? '' : folder . '/lib/index.js'
if empty(file) && executable('yarn')
let dir = trim(system('yarn global dir'))
let p = dir . '/node_modules/vim-node-rpc/lib/index.js'
if filereadable(p)
let file = p
endif
endif
if executable('npm')
if empty(file) && executable('npm')
let root = trim(system('npm --loglevel silent root -g'))
let file = root . '/vim-node-rpc/lib/index.js'
if filereadable(file)
let command = ['node', file]
return s:is_win ? join(command, ' ') : command
let p = root . '/vim-node-rpc/lib/index.js'
if filereadable(p)
let file = p
endif
endif
if executable('yarn')
let dir = trim(system('yarn global dir'))
let file = dir . '/node_modules/vim-node-rpc/lib/index.js'
if filereadable(file)
let command = ['node', file]
return s:is_win ? join(command, ' ') : command
endif
if !empty(file)
let s:command = s:is_win ? join(['node', file], ' ') : ['node', file]
return s:command
endif
return ''
endfunction
Expand All @@ -86,7 +87,11 @@ function! nvim#rpc#start_server() abort
return
endif
let command = nvim#rpc#get_command()
if empty(command) | return | endif
if empty(command)
let installed = nvim#rpc#install_node_rpc()
if installed | call nvim#rpc#start_server() | endif
return
endif
let g:coc_node_rpc_command = command
let options = {
\ 'in_mode': 'json',
Expand Down Expand Up @@ -148,30 +153,13 @@ function! nvim#rpc#check_client(clientId)
endfunction

function! nvim#rpc#install_node_rpc(...) abort
let prompt = get(a:, 1, 1)
if prompt
let res = input('[coc.nvim] vim-node-rpc module not found, install? [y/n]')
if res !=? 'y' | return 0 | endif
endif
let cmd = ''
let idx = inputlist(['Select package manager:', '1. npm', '2. yarn'])
if idx <= 0 | return 0 | endif
if idx == 1
let isLinux = !s:is_win && substitute(system('uname'), '\n', '', '') ==# 'Linux'
if executable('npm')
let cmd = (isLinux ? 'sudo ' : ' ').'npm i -g vim-node-rpc'
else
echohl Error | echon '[coc.nvim] executable "npm" not find in $PATH' | echohl None
return 0
endif
else
if executable('yarn')
let cmd = 'yarn global add vim-node-rpc'
else
echohl Error | echon '[coc.nvim] executable "yarn" not find in $PATH' | echohl None
return 0
endif
let res = coc#util#prompt_confirm('vim-node-rpc module not found, install?')
if !res | return 0 | endif
if !executable('yarn')
echohl Error | echom 'yarn not found in $PATH checkout https://yarnpkg.com/en/docs/install.' | echohl None
return 0
endif
let cmd = 'yarn global add vim-node-rpc'
execute '!'.cmd
return v:shell_error == 0
endfunction
7 changes: 7 additions & 0 deletions doc/coc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ g:coc_node_rpc_debug *g:coc_node_rpc_debug*

Default: 0

g:vim_node_rpc_folder *g:vim_node_rpc_folder*

Folder contains `vim-node-rpc` module, used for speed
up vim startup.

Default: undefined

g:coc_denite_quickfix_action *g:coc_denite_quickfix_action*

Defaut action for open file in denite quickfix list.
Expand Down
15 changes: 4 additions & 11 deletions plugin/coc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ let g:did_coc_loaded = 1
let g:rooter_patterns = get(g:, 'rooter_patterns', ['.vim/', '.git/', '.hg/', '.projections.json'])
let s:is_vim = !has('nvim')

if s:is_vim
call nvim#rpc#start_server()
else
if $NODE_ENV !=# 'test' && $NVIM_LISTEN_ADDRESS !=# '/tmp/nvim'
call coc#rpc#start_server()
endif
if has('nvim') && $NODE_ENV !=# 'test' && $NVIM_LISTEN_ADDRESS !=# '/tmp/nvim'
call coc#rpc#start_server()
endif

function! CocAction(...) abort
Expand Down Expand Up @@ -178,11 +174,8 @@ endfunction
function! s:OnVimEnter()
" it's possible that client is not ready
call coc#rpc#notify('VimEnter', [])
if s:is_vim && empty(get(g:, 'coc_node_rpc_command', ''))
let installed = nvim#rpc#install_node_rpc()
if installed
call nvim#rpc#start_server()
endif
if s:is_vim
call nvim#rpc#start_server()
endif
endfunction

Expand Down
25 changes: 17 additions & 8 deletions src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,26 @@ export class Extensions {
let update = await this.checkUpdate(id, packageJSON.version)
if (update) await workspace.nvim.command(`CocInstall ${id}`)
}, null, this.disposables)
if (workspace.isVim) {
let filepath = await workspace.resolveModule('vim-node-rpc')
if (filepath) {
let jsonFile = path.join(filepath, 'package.json')
let obj = loadJson(jsonFile)
let update = await this.checkUpdate('vim-node-rpc', obj.version)
if (update) nvim.call('nvim#rpc#install_node_rpc', [0], true)
if (workspace.isVim) this.updateNodeRpc() // tslint:disable-line
}

public async updateNodeRpc(): Promise<void> {
let filepath = await workspace.resolveModule('vim-node-rpc')
if (filepath) {
let jsonFile = path.join(filepath, 'package.json')
let obj = loadJson(jsonFile)
let update = await this.checkUpdate('vim-node-rpc', obj.version, false)
if (update) {
let status = workspace.createStatusBarItem(99, { progress: true })
status.text = 'Upgrading vim-node-rpc'
status.show()
await workspace.runCommand('yarn global add vim-node-rpc', process.cwd(), 30000)
status.dispose()
}
}
}

public async checkUpdate(moduleName: string, oldVersion: string): Promise<boolean> {
public async checkUpdate(moduleName: string, oldVersion: string, prompt = true): Promise<boolean> {
let now = new Date()
let { interval, db } = this
let day = new Date(now.getFullYear(), now.getMonth(), now.getDate() - (interval == 'daily' ? 0 : 7))
Expand All @@ -93,6 +101,7 @@ export class Extensions {
let res = await workspace.runCommand(`yarn info ${moduleName} version --json`)
let version = JSON.parse(res).data
if (semver.gt(version, oldVersion)) {
if (!prompt) return true
let res = await workspace.showPrompt(`a new version: ${version} of ${moduleName} available, update?`)
if (res) return true
}
Expand Down

0 comments on commit f80def6

Please sign in to comment.