|
|
let s:hosts = {} |
|
|
let s:plugin_patterns = {} |
|
|
let s:plugins_for_host = {} |
|
|
|
|
|
|
|
|
function! remote#host#Register(name, pattern, factory) abort |
|
|
let s:hosts[a:name] = {'factory': a:factory, 'channel': 0, 'initialized': 0} |
|
|
let s:plugin_patterns[a:name] = a:pattern |
|
|
if type(a:factory) == type(1) && a:factory |
|
|
|
|
|
let s:hosts[a:name].channel = a:factory |
|
|
endif |
|
|
endfunction |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function! remote#host#RegisterClone(name, orig_name) abort |
|
|
if !has_key(s:hosts, a:orig_name) |
|
|
throw 'No host named "'.a:orig_name.'" is registered' |
|
|
endif |
|
|
let Factory = s:hosts[a:orig_name].factory |
|
|
let s:hosts[a:name] = { |
|
|
\ 'factory': Factory, |
|
|
\ 'channel': 0, |
|
|
\ 'initialized': 0, |
|
|
\ 'orig_name': a:orig_name |
|
|
\ } |
|
|
endfunction |
|
|
|
|
|
|
|
|
function! remote#host#Require(name) abort |
|
|
if !has_key(s:hosts, a:name) |
|
|
throw 'No host named "'.a:name.'" is registered' |
|
|
endif |
|
|
let host = s:hosts[a:name] |
|
|
if !host.channel && !host.initialized |
|
|
let host_info = { |
|
|
\ 'name': a:name, |
|
|
\ 'orig_name': get(host, 'orig_name', a:name) |
|
|
\ } |
|
|
let host.channel = call(host.factory, [host_info]) |
|
|
let host.initialized = 1 |
|
|
endif |
|
|
return host.channel |
|
|
endfunction |
|
|
|
|
|
function! remote#host#IsRunning(name) abort |
|
|
if !has_key(s:hosts, a:name) |
|
|
throw 'No host named "'.a:name.'" is registered' |
|
|
endif |
|
|
return s:hosts[a:name].channel != 0 |
|
|
endfunction |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
" \ {'type': 'autocmd', 'name': 'BufEnter', 'sync': 0, 'opts': {'eval': 'expand("<afile> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function! remote#host#RegisterPlugin(host, path, specs) abort |
|
|
let plugins = remote#host#PluginsForHost(a:host) |
|
|
|
|
|
for plugin in plugins |
|
|
if plugin.path == a:path |
|
|
throw 'Plugin "'.a:path.'" is already registered' |
|
|
endif |
|
|
endfor |
|
|
|
|
|
if has_key(s:hosts, a:host) && remote#host#IsRunning(a:host) |
|
|
|
|
|
|
|
|
throw 'Host "'.a:host.'" is already running' |
|
|
endif |
|
|
|
|
|
for spec in a:specs |
|
|
let type = spec.type |
|
|
let name = spec.name |
|
|
let sync = spec.sync |
|
|
let opts = spec.opts |
|
|
let rpc_method = a:path |
|
|
if type == 'command' |
|
|
let rpc_method .= ':command:'.name |
|
|
call remote#define#CommandOnHost(a:host, rpc_method, sync, name, opts) |
|
|
elseif type == 'autocmd' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let rpc_method .= ':autocmd:'.name.':'.get(opts, 'pattern', '*') |
|
|
call remote#define#AutocmdOnHost(a:host, rpc_method, sync, name, opts) |
|
|
elseif type == 'function' |
|
|
let rpc_method .= ':function:'.name |
|
|
call remote#define#FunctionOnHost(a:host, rpc_method, sync, name, opts) |
|
|
else |
|
|
echoerr 'Invalid declaration type: '.type |
|
|
endif |
|
|
endfor |
|
|
|
|
|
call add(plugins, {'path': a:path, 'specs': a:specs}) |
|
|
endfunction |
|
|
|
|
|
function! s:RegistrationCommands(host) abort |
|
|
|
|
|
let host_id = a:host.'-registration-clone' |
|
|
call remote#host#RegisterClone(host_id, a:host) |
|
|
let pattern = s:plugin_patterns[a:host] |
|
|
let paths = nvim_get_runtime_file('rplugin/'.a:host.'/'.pattern, 1) |
|
|
let paths = map(paths, 'tr(resolve(v:val),"\\","/")') |
|
|
let paths = uniq(sort(paths)) |
|
|
if empty(paths) |
|
|
return [] |
|
|
endif |
|
|
|
|
|
for path in paths |
|
|
call remote#host#RegisterPlugin(host_id, path, []) |
|
|
endfor |
|
|
let channel = remote#host#Require(host_id) |
|
|
let lines = [] |
|
|
let registered = [] |
|
|
for path in paths |
|
|
unlet! specs |
|
|
let specs = rpcrequest(channel, 'specs', path) |
|
|
if type(specs) != type([]) |
|
|
|
|
|
|
|
|
continue |
|
|
endif |
|
|
call add(lines, "call remote#host#RegisterPlugin('".a:host |
|
|
\ ."', '".path."', [") |
|
|
for spec in specs |
|
|
call add(lines, " \\ ".string(spec).",") |
|
|
endfor |
|
|
call add(lines, " \\ ])") |
|
|
call add(registered, path) |
|
|
endfor |
|
|
echomsg printf("remote/host: %s host registered plugins %s", |
|
|
\ a:host, string(map(registered, "fnamemodify(v:val, ':t')"))) |
|
|
|
|
|
|
|
|
call jobstop(s:hosts[host_id].channel) |
|
|
call remove(s:hosts, host_id) |
|
|
call remove(s:plugins_for_host, host_id) |
|
|
return lines |
|
|
endfunction |
|
|
|
|
|
function! remote#host#UpdateRemotePlugins() abort |
|
|
let commands = [] |
|
|
let hosts = keys(s:hosts) |
|
|
for host in hosts |
|
|
if has_key(s:plugin_patterns, host) |
|
|
try |
|
|
let commands += |
|
|
\ ['" '.host.' plugins'] |
|
|
\ + s:RegistrationCommands(host) |
|
|
\ + ['', ''] |
|
|
catch |
|
|
echomsg v:throwpoint |
|
|
echomsg v:exception |
|
|
endtry |
|
|
endif |
|
|
endfor |
|
|
call writefile(commands, g:loaded_remote_plugins) |
|
|
echomsg printf('remote/host: generated rplugin manifest: %s', |
|
|
\ g:loaded_remote_plugins) |
|
|
endfunction |
|
|
|
|
|
function! remote#host#PluginsForHost(host) abort |
|
|
if !has_key(s:plugins_for_host, a:host) |
|
|
let s:plugins_for_host[a:host] = [] |
|
|
end |
|
|
return s:plugins_for_host[a:host] |
|
|
endfunction |
|
|
|
|
|
function! remote#host#LoadErrorForHost(host, log) abort |
|
|
return 'Failed to load '. a:host . ' host. '. |
|
|
\ 'You can try to see what happened by starting nvim with '. |
|
|
\ a:log . ' set and opening the generated log file.'. |
|
|
\ ' Also, the host stderr is available in messages.' |
|
|
endfunction |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
call remote#host#Register('python3', '*', |
|
|
\ function('provider#python3#Require')) |
|
|
|
|
|
|
|
|
call remote#host#Register('ruby', '*.rb', |
|
|
\ function('provider#ruby#Require')) |
|
|
|
|
|
|
|
|
call remote#host#Register('node', '*', |
|
|
\ function('provider#node#Require')) |
|
|
|
|
|
|
|
|
call remote#host#Register('perl', '*', |
|
|
\ function('provider#perl#Require')) |
|
|
|