robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
eb5d15e383
master
1let s: hosts = {} 2let s: plugin_patterns = {} 3let s: plugins_for_host = {} 4 5" Register a host by associating it with a factory(funcref) 6function ! remote#host#Register ( name , pattern , factory ) abort 7let s: hosts [ a: name ] = { 'factory' : a: factory , 'channel' : 0 , 'initialized' : 0 } 8let s: plugin_patterns [ a: name ] = a: pattern 9if type ( a: factory ) == type ( 1 ) && a: factory 10" Passed a channel directly 11let s: hosts [ a: name ]. channel = a: factory 12endif 13endfunction 14 15" Register a clone to an existing host. The new host will use the same factory 16" as `source`, but it will run as a different process. This can be used by 17" plugins that should run isolated from other plugins created for the same host 18" type 19function ! remote#host#RegisterClone ( name , orig_name ) abort 20if ! has_key ( s: hosts , a: orig_name ) 21throw 'No host named "' . a: orig_name . '" is registered' 22endif 23let Factory = s: hosts [ a: orig_name ]. factory 24let s: hosts [ a: name ] = { 25 \'factory' : Factory , 26 \'channel' : 0 , 27 \'initialized' : 0 , 28 \'orig_name' : a: orig_name 29 \} 30endfunction 31 32" Get a host channel, bootstrapping it if necessary 33function ! remote#host#Require ( name ) abort 34if ! has_key ( s: hosts , a: name ) 35throw 'No host named "' . a: name . '" is registered' 36endif 37let host = s: hosts [ a: name ] 38if ! host . channel && ! host . initialized 39let host_info = { 40 \'name' : a: name , 41 \'orig_name' : get ( host , 'orig_name' , a: name ) 42 \} 43let host . channel = call ( host . factory , [ host_info ]) 44let host . initialized = 1 45endif 46return host . channel 47endfunction 48 49function ! remote#host#IsRunning ( name ) abort 50if ! has_key ( s: hosts , a: name ) 51throw 'No host named "' . a: name . '" is registered' 52endif 53return s: hosts [ a: name ]. channel != 0 54endfunction 55 56" Example of registering a Python plugin with two commands (one async), one 57" autocmd (async) and one function (sync): 58" 59" let s:plugin_path = expand('<sfile>:p:h').'/nvim_plugin.py' 60" call remote#host#RegisterPlugin('python', s:plugin_path, [ 61" \ {'type': 'command', 'name': 'PyCmd', 'sync': 1, 'opts': {}}, 62" \ {'type': 'command', 'name': 'PyAsyncCmd', 'sync': 0, 'opts': {'eval': 'cursor()'}}, 63" \ {'type': 'autocmd', 'name': 'BufEnter', 'sync': 0, 'opts': {'eval': 'expand("<afile>")'}}, 64" \ {'type': 'function', 'name': 'PyFunc', 'sync': 1, 'opts': {}} 65" \ ]) 66" 67" The third item in a declaration is a boolean: non zero means the command, 68" autocommand or function will be executed synchronously with rpcrequest. 69function ! remote#host#RegisterPlugin ( host , path , specs ) abort 70let plugins = remote#host#PluginsForHost ( a: host ) 71 72for plugin in plugins 73if plugin . path == a: path 74throw 'Plugin "' . a: path . '" is already registered' 75endif 76endfor 77 78if has_key ( s: hosts , a: host ) && remote#host#IsRunning ( a: host ) 79" For now we won't allow registration of plugins when the host is already 80" running. 81throw 'Host "' . a: host . '" is already running' 82endif 83 84for spec in a: specs 85let type = spec . type 86let name = spec . name 87let sync = spec . sync 88let opts = spec . opts 89let rpc_method = a: path 90if type == 'command' 91let rpc_method .= ':command:' . name 92call remote#define#CommandOnHost ( a: host , rpc_method , sync , name , opts ) 93elseif type == 'autocmd' 94" Since multiple handlers can be attached to the same autocmd event by a 95" single plugin, we need a way to uniquely identify the rpc method to 96" call. The solution is to append the autocmd pattern to the method 97" name(This still has a limit: one handler per event/pattern combo, but 98" there's no need to allow plugins define multiple handlers in that case) 99let rpc_method .= ':autocmd:' . name . ':' . get ( opts , 'pattern' , '*' ) 100call remote#define#AutocmdOnHost ( a: host , rpc_method , sync , name , opts ) 101elseif type == 'function' 102let rpc_method .= ':function:' . name 103call remote#define#FunctionOnHost ( a: host , rpc_method , sync , name , opts ) 104else 105echoerr 'Invalid declaration type: ' . type 106endif 107endfor 108 109call add ( plugins , { 'path' : a: path , 'specs' : a: specs }) 110endfunction 111 112function ! s: RegistrationCommands ( host ) abort 113" Register a temporary host clone for discovering specs 114let host_id = a: host . '-registration-clone' 115call remote#host#RegisterClone ( host_id , a: host ) 116let pattern = s: plugin_patterns [ a: host ] 117let paths = nvim_get_runtime_file ( 'rplugin/' . a: host . '/' . pattern , 1 ) 118let paths = map ( paths , 'tr(resolve(v:val),"\\","/")' ) " Normalize slashes #4795 119let paths = uniq ( sort ( paths )) 120if empty ( paths ) 121return [] 122endif 123 124for path in paths 125call remote#host#RegisterPlugin ( host_id , path , []) 126endfor 127let channel = remote#host#Require ( host_id ) 128let lines = [] 129let registered = [] 130for path in paths 131unlet ! specs 132let specs = rpcrequest ( channel , 'specs' , path ) 133if type ( specs ) != type ([]) 134" host didn't return a spec list, indicates a failure while loading a 135" plugin 136continue 137endif 138call add ( lines , "call remote#host#RegisterPlugin('" . a: host 139 \. "', '" . path . "', [" ) 140for spec in specs 141call add ( lines , " \\ " . string ( spec ) . "," ) 142endfor 143call add ( lines , " \\ ])" ) 144call add ( registered , path ) 145endfor 146echomsg printf ( "remote/host: %s host registered plugins %s" , 147 \a: host , string ( map ( registered , "fnamemodify(v:val, ':t')" ))) 148 149" Delete the temporary host clone 150call jobstop ( s: hosts [ host_id ]. channel ) 151call remove ( s: hosts , host_id ) 152call remove ( s: plugins_for_host , host_id ) 153return lines 154endfunction 155 156function ! remote#host#UpdateRemotePlugins () abort 157let commands = [] 158let hosts = keys ( s: hosts ) 159for host in hosts 160if has_key ( s: plugin_patterns , host ) 161try 162let commands += 163 \[ '" ' . host . ' plugins' ] 164 \+ s: RegistrationCommands ( host ) 165 \+ [ '' , '' ] 166catch 167echomsg v: throwpoint 168echomsg v: exception 169endtry 170endif 171endfor 172call writefile ( commands , g: loaded_remote_plugins ) 173echomsg printf ( 'remote/host: generated rplugin manifest: %s' , 174 \g: loaded_remote_plugins ) 175endfunction 176 177function ! remote#host#PluginsForHost ( host ) abort 178if ! has_key ( s: plugins_for_host , a: host ) 179let s: plugins_for_host [ a: host ] = [] 180end 181return s: plugins_for_host [ a: host ] 182endfunction 183 184function ! remote#host#LoadErrorForHost ( host , log ) abort 185return 'Failed to load ' . a: host . ' host. ' . 186 \'You can try to see what happened by starting nvim with ' . 187 \a: log . ' set and opening the generated log file.' . 188 \' Also, the host stderr is available in messages.' 189endfunction 190 191" Registration of standard hosts 192 193" Python3 194call remote#host#Register ( 'python3' , '*' , 195 \function ( 'provider#python3#Require' )) 196 197" Ruby 198call remote#host#Register ( 'ruby' , '*.rb' , 199 \function ( 'provider#ruby#Require' )) 200 201" nodejs 202call remote#host#Register ( 'node' , '*' , 203 \function ( 'provider#node#Require' )) 204 205" perl 206call remote#host#Register ( 'perl' , '*' , 207 \function ( 'provider#perl#Require' ))