From 2cfe4748e57bd510b98ca81bd915f801f5a50bb5 Mon Sep 17 00:00:00 2001 From: Rui Abreu Ferreira Date: Sun, 9 Jun 2019 18:22:10 +0100 Subject: provider: let providers decide their status Instead of deciding provider status in eval_has_provider, move the decision to the provider Vim scripts. Previously, provider loading worked as follows: 1. eval_has_provider() verified provider availability by searching for the provider#providername#Call function and cached this verificaion as a static variable for some providers 2. providers short-circuited on loading to prevent the definition of the Call function (with the exception of the node provider that did not) This commit changes the expected interface between nvim and its providers to facilitate provider reloading, by splitting the verification of the provider from the availability of the Call function. eval_has_provider() now checks for a provider#providername#enabled variable. It is up to the provider script to set this to 0 or 1 accordingly. eval_call_provider() remains unchanged. All providers hosting a Call function were updated to respect this. The clipboard provider now has a Reload function to reload the provider. --- runtime/autoload/provider/clipboard.vim | 16 ++++++++++------ runtime/autoload/provider/node.vim | 4 ++++ runtime/autoload/provider/python.vim | 6 +----- runtime/autoload/provider/python3.vim | 6 +----- runtime/autoload/provider/ruby.vim | 3 +++ runtime/doc/develop.txt | 6 +++--- 6 files changed, 22 insertions(+), 19 deletions(-) (limited to 'runtime') diff --git a/runtime/autoload/provider/clipboard.vim b/runtime/autoload/provider/clipboard.vim index 2b06ee8c48..e43f8fbb7a 100644 --- a/runtime/autoload/provider/clipboard.vim +++ b/runtime/autoload/provider/clipboard.vim @@ -48,6 +48,9 @@ endfunction let s:cache_enabled = 1 let s:err = '' +" eval_has_provider checks the variable to verify provider status +let g:provider#clipboard#enabled = 0 + function! provider#clipboard#Error() abort return s:err endfunction @@ -120,12 +123,11 @@ function! provider#clipboard#Executable() abort return '' endfunction -if empty(provider#clipboard#Executable()) - " provider#clipboard#Call() *must not* be defined if the provider is broken. - " Otherwise eval_has_provider() thinks the clipboard provider is - " functioning, and eval_call_provider() will happily call it. - finish -endif +" Call this to setup/reload the provider +function! provider#clipboard#Reload() + " #enabled is used by eval_has_provider() + let g:provider#clipboard#enabled = !empty(provider#clipboard#Executable()) +endfunction function! s:clipboard.get(reg) abort if type(s:paste[a:reg]) == v:t_func @@ -192,3 +194,5 @@ function! provider#clipboard#Call(method, args) abort let s:here = v:false endtry endfunction + +call provider#clipboard#Reload() diff --git a/runtime/autoload/provider/node.vim b/runtime/autoload/provider/node.vim index 35882849bd..6413720605 100644 --- a/runtime/autoload/provider/node.vim +++ b/runtime/autoload/provider/node.vim @@ -2,6 +2,7 @@ if exists('g:loaded_node_provider') finish endif let g:loaded_node_provider = 1 +let g:provider#node#enabled = 0 function! s:is_minimum_version(version, min_major, min_minor) abort if empty(a:version) @@ -143,6 +144,9 @@ let s:prog = provider#node#Detect() if empty(s:prog) let s:err = 'Cannot find the "neovim" node package. Try :checkhealth' +else + let g:provider#node#enabled = 1 endif call remote#host#RegisterPlugin('node-provider', 'node', []) + diff --git a/runtime/autoload/provider/python.vim b/runtime/autoload/provider/python.vim index a06cbe4814..d65506a55a 100644 --- a/runtime/autoload/provider/python.vim +++ b/runtime/autoload/provider/python.vim @@ -10,6 +10,7 @@ endif let g:loaded_python_provider = 1 let [s:prog, s:err] = provider#pythonx#Detect(2) +let g:provider#python#enabled = !empty(s:prog) function! provider#python#Prog() abort return s:prog @@ -19,11 +20,6 @@ function! provider#python#Error() abort return s:err endfunction -if s:prog == '' - " Detection failed - finish -endif - " The Python provider plugin will run in a separate instance of the Python " host. call remote#host#RegisterClone('legacy-python-provider', 'python') diff --git a/runtime/autoload/provider/python3.vim b/runtime/autoload/provider/python3.vim index 242a224cb3..469611c7ce 100644 --- a/runtime/autoload/provider/python3.vim +++ b/runtime/autoload/provider/python3.vim @@ -10,6 +10,7 @@ endif let g:loaded_python3_provider = 1 let [s:prog, s:err] = provider#pythonx#Detect(3) +let g:provider#python3#enabled = !empty(s:prog) function! provider#python3#Prog() abort return s:prog @@ -19,11 +20,6 @@ function! provider#python3#Error() abort return s:err endfunction -if s:prog == '' - " Detection failed - finish -endif - " The Python3 provider plugin will run in a separate instance of the Python3 " host. call remote#host#RegisterClone('legacy-python3-provider', 'python3') diff --git a/runtime/autoload/provider/ruby.vim b/runtime/autoload/provider/ruby.vim index 3b4c6c4839..df43dffa40 100644 --- a/runtime/autoload/provider/ruby.vim +++ b/runtime/autoload/provider/ruby.vim @@ -7,6 +7,7 @@ let g:loaded_ruby_provider = 1 function! provider#ruby#Detect() abort return s:prog endfunction +let g:provider#ruby#enabled = 0 function! provider#ruby#Prog() abort return s:prog @@ -65,6 +66,8 @@ let s:plugin_path = expand(':p:h') . '/script_host.rb' if empty(s:prog) let s:err = 'Cannot find the neovim RubyGem. Try :checkhealth' +else + let g:provider#ruby#enabled = 1 endif call remote#host#RegisterClone('legacy-ruby-provider', 'ruby') diff --git a/runtime/doc/develop.txt b/runtime/doc/develop.txt index 843e23ee54..0f9e17e697 100644 --- a/runtime/doc/develop.txt +++ b/runtime/doc/develop.txt @@ -111,7 +111,7 @@ functions in eval.c: - eval_call_provider(name, method, arguments): calls provider#(name)#Call with the method and arguments. - eval_has_provider(name): Checks if a provider is implemented. Returns true - if the provider#(name)#Call function is implemented. Called by |has()| + if the provider#(name)#enabled variable is not 0. Called by |has()| (vimscript) to check if features are available. The provider#(name)#Call function implements integration with an external @@ -119,8 +119,8 @@ system, because shell commands and |RPC| clients are easier to work with in vimscript. For example, the Python provider is implemented by the -autoload/provider/python.vim script; the provider#python#Call function is only -defined if a valid external Python host is found. That works well with the +autoload/provider/python.vim script; the variable provider#python#enabled is only +1 if a valid external Python host is found. That works well with the `has('python')` expression (normally used by Python plugins) because if the Python host isn't installed then the plugin will "think" it is running in a Vim compiled without the "+python" feature. -- cgit From 66938b928c05b913f3a11e520d13ca854621799d Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 4 Aug 2019 03:54:06 +0200 Subject: provider: decide status by g:loaded_xx_provider --- runtime/autoload/provider/clipboard.vim | 22 +++++++++------- runtime/autoload/provider/node.vim | 9 +++---- runtime/autoload/provider/python.vim | 4 +-- runtime/autoload/provider/python3.vim | 4 +-- runtime/autoload/provider/ruby.vim | 8 +++--- runtime/doc/develop.txt | 46 ++++++++++++++++----------------- 6 files changed, 42 insertions(+), 51 deletions(-) (limited to 'runtime') diff --git a/runtime/autoload/provider/clipboard.vim b/runtime/autoload/provider/clipboard.vim index e43f8fbb7a..9b79e5abec 100644 --- a/runtime/autoload/provider/clipboard.vim +++ b/runtime/autoload/provider/clipboard.vim @@ -1,6 +1,16 @@ " The clipboard provider uses shell commands to communicate with the clipboard. " The provider function will only be registered if a supported command is " available. + +if exists('g:loaded_clipboard_provider') + finish +endif +" Default to FALSE. Set by provider#clipboard#Executable() later. +" To force a reload: +" :unlet g:loaded_clipboard_provider +" :runtime autoload/provider/clipboard.vim +let g:loaded_clipboard_provider = 0 + let s:copy = {} let s:paste = {} let s:clipboard = {} @@ -48,9 +58,6 @@ endfunction let s:cache_enabled = 1 let s:err = '' -" eval_has_provider checks the variable to verify provider status -let g:provider#clipboard#enabled = 0 - function! provider#clipboard#Error() abort return s:err endfunction @@ -123,12 +130,6 @@ function! provider#clipboard#Executable() abort return '' endfunction -" Call this to setup/reload the provider -function! provider#clipboard#Reload() - " #enabled is used by eval_has_provider() - let g:provider#clipboard#enabled = !empty(provider#clipboard#Executable()) -endfunction - function! s:clipboard.get(reg) abort if type(s:paste[a:reg]) == v:t_func return s:paste[a:reg]() @@ -195,4 +196,5 @@ function! provider#clipboard#Call(method, args) abort endtry endfunction -call provider#clipboard#Reload() +" eval_has_provider() decides based on this variable. +let g:loaded_clipboard_provider = !empty(provider#clipboard#Executable()) diff --git a/runtime/autoload/provider/node.vim b/runtime/autoload/provider/node.vim index 6413720605..dd2423fe2a 100644 --- a/runtime/autoload/provider/node.vim +++ b/runtime/autoload/provider/node.vim @@ -1,8 +1,7 @@ if exists('g:loaded_node_provider') finish endif -let g:loaded_node_provider = 1 -let g:provider#node#enabled = 0 +let g:loaded_node_provider = 0 function! s:is_minimum_version(version, min_major, min_minor) abort if empty(a:version) @@ -141,12 +140,10 @@ endfunction let s:err = '' let s:prog = provider#node#Detect() +let g:loaded_node_provider = !empty(s:prog) -if empty(s:prog) +if !g:loaded_node_provider let s:err = 'Cannot find the "neovim" node package. Try :checkhealth' -else - let g:provider#node#enabled = 1 endif call remote#host#RegisterPlugin('node-provider', 'node', []) - diff --git a/runtime/autoload/provider/python.vim b/runtime/autoload/provider/python.vim index d65506a55a..7921701141 100644 --- a/runtime/autoload/provider/python.vim +++ b/runtime/autoload/provider/python.vim @@ -7,10 +7,8 @@ if exists('g:loaded_python_provider') finish endif -let g:loaded_python_provider = 1 - let [s:prog, s:err] = provider#pythonx#Detect(2) -let g:provider#python#enabled = !empty(s:prog) +let g:loaded_python_provider = !empty(s:prog) function! provider#python#Prog() abort return s:prog diff --git a/runtime/autoload/provider/python3.vim b/runtime/autoload/provider/python3.vim index 469611c7ce..67350e3753 100644 --- a/runtime/autoload/provider/python3.vim +++ b/runtime/autoload/provider/python3.vim @@ -7,10 +7,8 @@ if exists('g:loaded_python3_provider') finish endif -let g:loaded_python3_provider = 1 - let [s:prog, s:err] = provider#pythonx#Detect(3) -let g:provider#python3#enabled = !empty(s:prog) +let g:loaded_python3_provider = !empty(s:prog) function! provider#python3#Prog() abort return s:prog diff --git a/runtime/autoload/provider/ruby.vim b/runtime/autoload/provider/ruby.vim index df43dffa40..f9d4f2b885 100644 --- a/runtime/autoload/provider/ruby.vim +++ b/runtime/autoload/provider/ruby.vim @@ -2,12 +2,11 @@ if exists('g:loaded_ruby_provider') finish endif -let g:loaded_ruby_provider = 1 +let g:loaded_ruby_provider = 0 function! provider#ruby#Detect() abort return s:prog endfunction -let g:provider#ruby#enabled = 0 function! provider#ruby#Prog() abort return s:prog @@ -63,11 +62,10 @@ endfunction let s:err = '' let s:prog = s:detect() let s:plugin_path = expand(':p:h') . '/script_host.rb' +let g:loaded_ruby_provider = !empty(s:prog) -if empty(s:prog) +if !g:loaded_ruby_provider let s:err = 'Cannot find the neovim RubyGem. Try :checkhealth' -else - let g:provider#ruby#enabled = 1 endif call remote#host#RegisterClone('legacy-ruby-provider', 'ruby') diff --git a/runtime/doc/develop.txt b/runtime/doc/develop.txt index 0f9e17e697..180612cf20 100644 --- a/runtime/doc/develop.txt +++ b/runtime/doc/develop.txt @@ -84,12 +84,11 @@ Developer guidelines *dev-guidelines* PROVIDERS *dev-provider* -A goal of Nvim is to allow extension of the editor without special knowledge -in the core. But some Vim components are too tightly coupled; in those cases -a "provider" hook is exposed. +A primary goal of Nvim is to allow extension of the editor without special +knowledge in the core. Some core functions are delegated to "providers" +implemented as external scripts. -Consider two examples of integration with external systems that are -implemented in Vim and are now decoupled from Nvim core as providers: +Examples: 1. In the Vim source code, clipboard logic accounts for more than 1k lines of C source code (ui.c), to perform two tasks that are now accomplished with @@ -101,29 +100,28 @@ implemented in Vim and are now decoupled from Nvim core as providers: scripting is performed by an external host process implemented in ~2k lines of Python. -Ideally we could implement Python and clipboard integration in pure vimscript -and without touching the C code. But this is infeasible without compromising -backwards compatibility with Vim; that's where providers help. +The provider framework invokes VimL from C. It is composed of two functions +in eval.c: -The provider framework helps call vimscript from C. It is composed of two -functions in eval.c: - -- eval_call_provider(name, method, arguments): calls provider#(name)#Call +- eval_call_provider(name, method, arguments): calls provider#{name}#Call with the method and arguments. -- eval_has_provider(name): Checks if a provider is implemented. Returns true - if the provider#(name)#enabled variable is not 0. Called by |has()| - (vimscript) to check if features are available. - -The provider#(name)#Call function implements integration with an external -system, because shell commands and |RPC| clients are easier to work with in -vimscript. +- eval_has_provider(name): Checks the `g:loaded_{name}_provider` variable + which must be set by the provider script to indicate whether it is enabled + and working. Called by |has()| to check if features are available. For example, the Python provider is implemented by the -autoload/provider/python.vim script; the variable provider#python#enabled is only -1 if a valid external Python host is found. That works well with the -`has('python')` expression (normally used by Python plugins) because if the -Python host isn't installed then the plugin will "think" it is running in -a Vim compiled without the "+python" feature. +"autoload/provider/python.vim" script, which sets `g:loaded_python_provider` +to TRUE only if a valid external Python host is found. Then `has("python")` +reflects whether Python support is working. + + *provider-reload* +Sometimes a GUI or other application may want to force a provider to +"reload". To reload a provider, undefine its "loaded" flag, then use +|:runtime| to reload it: > + + :unlet g:loaded_clipboard_provider + :runtime autoload/provider/clipboard.vim + DOCUMENTATION *dev-doc* -- cgit From 241956720d02d933b0b27097a3b0a1966f138d0b Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 4 Aug 2019 12:06:24 +0200 Subject: provider: g:loaded_xx_provider=2 means "enabled and working" Value of 1 cannot be used, because users might set that in their vimrc to _disable_ a provider, which would confuse :checkhealth and has(). --- runtime/autoload/provider/clipboard.vim | 6 +++--- runtime/autoload/provider/node.vim | 6 +++--- runtime/autoload/provider/python.vim | 2 +- runtime/autoload/provider/python3.vim | 2 +- runtime/autoload/provider/ruby.vim | 6 +++--- runtime/doc/develop.txt | 6 +++--- runtime/doc/provider.txt | 8 ++++---- 7 files changed, 18 insertions(+), 18 deletions(-) (limited to 'runtime') diff --git a/runtime/autoload/provider/clipboard.vim b/runtime/autoload/provider/clipboard.vim index 9b79e5abec..ce140b0948 100644 --- a/runtime/autoload/provider/clipboard.vim +++ b/runtime/autoload/provider/clipboard.vim @@ -5,11 +5,11 @@ if exists('g:loaded_clipboard_provider') finish endif -" Default to FALSE. Set by provider#clipboard#Executable() later. +" Default to 1. provider#clipboard#Executable() may set 2. " To force a reload: " :unlet g:loaded_clipboard_provider " :runtime autoload/provider/clipboard.vim -let g:loaded_clipboard_provider = 0 +let g:loaded_clipboard_provider = 1 let s:copy = {} let s:paste = {} @@ -197,4 +197,4 @@ function! provider#clipboard#Call(method, args) abort endfunction " eval_has_provider() decides based on this variable. -let g:loaded_clipboard_provider = !empty(provider#clipboard#Executable()) +let g:loaded_clipboard_provider = empty(provider#clipboard#Executable()) ? 1 : 2 diff --git a/runtime/autoload/provider/node.vim b/runtime/autoload/provider/node.vim index dd2423fe2a..b2a3b3ee08 100644 --- a/runtime/autoload/provider/node.vim +++ b/runtime/autoload/provider/node.vim @@ -1,7 +1,7 @@ if exists('g:loaded_node_provider') finish endif -let g:loaded_node_provider = 0 +let g:loaded_node_provider = 1 function! s:is_minimum_version(version, min_major, min_minor) abort if empty(a:version) @@ -140,9 +140,9 @@ endfunction let s:err = '' let s:prog = provider#node#Detect() -let g:loaded_node_provider = !empty(s:prog) +let g:loaded_node_provider = empty(s:prog) ? 1 : 2 -if !g:loaded_node_provider +if g:loaded_node_provider != 2 let s:err = 'Cannot find the "neovim" node package. Try :checkhealth' endif diff --git a/runtime/autoload/provider/python.vim b/runtime/autoload/provider/python.vim index 7921701141..8a1d162784 100644 --- a/runtime/autoload/provider/python.vim +++ b/runtime/autoload/provider/python.vim @@ -8,7 +8,7 @@ if exists('g:loaded_python_provider') finish endif let [s:prog, s:err] = provider#pythonx#Detect(2) -let g:loaded_python_provider = !empty(s:prog) +let g:loaded_python_provider = empty(s:prog) ? 1 : 2 function! provider#python#Prog() abort return s:prog diff --git a/runtime/autoload/provider/python3.vim b/runtime/autoload/provider/python3.vim index 67350e3753..38ef0cccfc 100644 --- a/runtime/autoload/provider/python3.vim +++ b/runtime/autoload/provider/python3.vim @@ -8,7 +8,7 @@ if exists('g:loaded_python3_provider') finish endif let [s:prog, s:err] = provider#pythonx#Detect(3) -let g:loaded_python3_provider = !empty(s:prog) +let g:loaded_python3_provider = empty(s:prog) ? 1 : 2 function! provider#python3#Prog() abort return s:prog diff --git a/runtime/autoload/provider/ruby.vim b/runtime/autoload/provider/ruby.vim index f9d4f2b885..f843050df9 100644 --- a/runtime/autoload/provider/ruby.vim +++ b/runtime/autoload/provider/ruby.vim @@ -2,7 +2,7 @@ if exists('g:loaded_ruby_provider') finish endif -let g:loaded_ruby_provider = 0 +let g:loaded_ruby_provider = 1 function! provider#ruby#Detect() abort return s:prog @@ -62,9 +62,9 @@ endfunction let s:err = '' let s:prog = s:detect() let s:plugin_path = expand(':p:h') . '/script_host.rb' -let g:loaded_ruby_provider = !empty(s:prog) +let g:loaded_ruby_provider = empty(s:prog) ? 1 : 2 -if !g:loaded_ruby_provider +if g:loaded_ruby_provider != 2 let s:err = 'Cannot find the neovim RubyGem. Try :checkhealth' endif diff --git a/runtime/doc/develop.txt b/runtime/doc/develop.txt index 180612cf20..3262d9dec7 100644 --- a/runtime/doc/develop.txt +++ b/runtime/doc/develop.txt @@ -106,12 +106,12 @@ in eval.c: - eval_call_provider(name, method, arguments): calls provider#{name}#Call with the method and arguments. - eval_has_provider(name): Checks the `g:loaded_{name}_provider` variable - which must be set by the provider script to indicate whether it is enabled - and working. Called by |has()| to check if features are available. + which must be set to 2 by the provider script to indicate that it is + "enabled and working". Called by |has()| to check if features are available. For example, the Python provider is implemented by the "autoload/provider/python.vim" script, which sets `g:loaded_python_provider` -to TRUE only if a valid external Python host is found. Then `has("python")` +to 2 only if a valid external Python host is found. Then `has("python")` reflects whether Python support is working. *provider-reload* diff --git a/runtime/doc/provider.txt b/runtime/doc/provider.txt index 594c9602f4..dc045c360a 100644 --- a/runtime/doc/provider.txt +++ b/runtime/doc/provider.txt @@ -68,11 +68,11 @@ startup faster. Useful for working with virtualenvs. > < *g:loaded_python_provider* To disable Python 2 support: > - let g:loaded_python_provider = 1 + let g:loaded_python_provider = 0 < *g:loaded_python3_provider* To disable Python 3 support: > - let g:loaded_python3_provider = 1 + let g:loaded_python3_provider = 0 PYTHON VIRTUALENVS ~ @@ -111,7 +111,7 @@ Run |:checkhealth| to see if your system is up-to-date. RUBY PROVIDER CONFIGURATION ~ *g:loaded_ruby_provider* To disable Ruby support: > - let g:loaded_ruby_provider = 1 + let g:loaded_ruby_provider = 0 < *g:ruby_host_prog* Command to start the Ruby host. By default this is "neovim-ruby-host". With @@ -142,7 +142,7 @@ Run |:checkhealth| to see if your system is up-to-date. NODEJS PROVIDER CONFIGURATION~ *g:loaded_node_provider* To disable Node.js support: > - :let g:loaded_node_provider = 1 + :let g:loaded_node_provider = 0 < *g:node_host_prog* Command to start the Node.js host. Setting this makes startup faster. -- cgit From e952b7fc2f9838441308d1f71b98f108ae5faa8a Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 4 Aug 2019 12:27:44 +0200 Subject: health.vim: check has("debug") --- runtime/autoload/health/nvim.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime') diff --git a/runtime/autoload/health/nvim.vim b/runtime/autoload/health/nvim.vim index a2227e3b29..c25f5ee64f 100644 --- a/runtime/autoload/health/nvim.vim +++ b/runtime/autoload/health/nvim.vim @@ -123,7 +123,7 @@ function! s:check_performance() abort else call health#report_info(buildtype) call health#report_warn( - \ 'Non-optimized build-type. Nvim will be slower.', + \ 'Non-optimized '.(has('debug')?'(DEBUG) ':'').'build. Nvim will be slower.', \ ['Install a different Nvim package, or rebuild with `CMAKE_BUILD_TYPE=RelWithDebInfo`.', \ s:suggest_faq]) endif -- cgit