diff options
Diffstat (limited to 'runtime/autoload/provider')
-rw-r--r-- | runtime/autoload/provider/python.vim | 50 | ||||
-rw-r--r-- | runtime/autoload/provider/python3.vim | 47 | ||||
-rw-r--r-- | runtime/autoload/provider/pythonx.vim | 69 | ||||
-rw-r--r-- | runtime/autoload/provider/script_host.py | 23 |
4 files changed, 165 insertions, 24 deletions
diff --git a/runtime/autoload/provider/python.vim b/runtime/autoload/provider/python.vim index 53b984dfe2..4c43c8a613 100644 --- a/runtime/autoload/provider/python.vim +++ b/runtime/autoload/provider/python.vim @@ -1,28 +1,46 @@ -" The python provider uses a python host to emulate an environment for running -" python-vim plugins(:h python-vim). See :h nvim-providers for more -" information. +" The Python provider uses a Python host to emulate an environment for running +" python-vim plugins. See ":help nvim-provider" for more information. " -" Associating the plugin with the python host is the first step because plugins +" Associating the plugin with the Python host is the first step because plugins " will be passed as command-line arguments -if exists('s:loaded_python_provider') || &cp + +if exists('g:loaded_python_provider') + finish +endif +let g:loaded_python_provider = 1 + +let [s:prog, s:err] = provider#pythonx#Detect(2) +if s:prog == '' + " Detection failed finish endif -let s:loaded_python_provider = 1 + +function! provider#python#Prog() + return s:prog +endfunction + +function! provider#python#Error() + return s:err +endfunction + let s:plugin_path = expand('<sfile>:p:h').'/script_host.py' -" The python provider plugin will run in a separate instance of the python + +" The Python provider plugin will run in a separate instance of the Python " host. call remote#host#RegisterClone('legacy-python-provider', 'python') call remote#host#RegisterPlugin('legacy-python-provider', s:plugin_path, []) -" Ensure that we can load the python host before bootstrapping -try - let s:host = remote#host#Require('legacy-python-provider') -catch - echomsg v:exception - finish -endtry - -let s:rpcrequest = function('rpcrequest') function! provider#python#Call(method, args) + if !exists('s:host') + let s:rpcrequest = function('rpcrequest') + + " Ensure that we can load the Python host before bootstrapping + try + let s:host = remote#host#Require('legacy-python-provider') + catch + echomsg v:exception + finish + endtry + endif return call(s:rpcrequest, insert(insert(a:args, 'python_'.a:method), s:host)) endfunction diff --git a/runtime/autoload/provider/python3.vim b/runtime/autoload/provider/python3.vim new file mode 100644 index 0000000000..1a52ade0ef --- /dev/null +++ b/runtime/autoload/provider/python3.vim @@ -0,0 +1,47 @@ +" The Python3 provider uses a Python3 host to emulate an environment for running +" python3 plugins. See ":help nvim-provider" for more information. +" +" Associating the plugin with the Python3 host is the first step because +" plugins will be passed as command-line arguments + +if exists('g:loaded_python3_provider') + finish +endif +let g:loaded_python3_provider = 1 + +let [s:prog, s:err] = provider#pythonx#Detect(3) +if s:prog == '' + " Detection failed + finish +endif + +function! provider#python3#Prog() + return s:prog +endfunction + +function! provider#python3#Error() + return s:err +endfunction + +let s:plugin_path = expand('<sfile>:p:h').'/script_host.py' + +" The Python3 provider plugin will run in a separate instance of the Python3 +" host. +call remote#host#RegisterClone('legacy-python3-provider', 'python3') +call remote#host#RegisterPlugin('legacy-python3-provider', s:plugin_path, []) + +function! provider#python3#Call(method, args) + if !exists('s:host') + let s:rpcrequest = function('rpcrequest') + + " Ensure that we can load the Python3 host before bootstrapping + try + let s:host = remote#host#Require('legacy-python3-provider') + catch + echomsg v:exception + finish + endtry + endif + + return call(s:rpcrequest, insert(insert(a:args, 'python_'.a:method), s:host)) +endfunction diff --git a/runtime/autoload/provider/pythonx.vim b/runtime/autoload/provider/pythonx.vim new file mode 100644 index 0000000000..6137d16fcb --- /dev/null +++ b/runtime/autoload/provider/pythonx.vim @@ -0,0 +1,69 @@ +" The Python provider helper +if exists('s:loaded_pythonx_provider') + finish +endif + +let s:loaded_pythonx_provider = 1 + +function! provider#pythonx#Detect(ver) abort + let host_var = (a:ver == 2) ? + \ 'g:python_host_prog' : 'g:python3_host_prog' + let skip_var = (a:ver == 2) ? + \ 'g:python_host_skip_check' : 'g:python3_host_skip_check' + let skip = exists(skip_var) ? {skip_var} : 0 + if exists(host_var) + " Disable auto detection + let [check, err] = s:check_interpreter({host_var}, a:ver, skip) + return check ? [{host_var}, err] : ['', err] + endif + + let detect_versions = (a:ver == 2) ? + \ ['2.7', '2.6', '2', ''] + \ : ['3.5', '3.4', '3.3', '3.2', '3', ''] + + for prog in map(detect_versions, "'python' . v:val") + let [check, err] = s:check_interpreter(prog, a:ver, skip) + if check + let [check, err] = s:check_version(prog, a:ver, skip) + return [prog, err] + endif + endfor + + " No Python interpreter + return ['', 'Neovim module installed Python' + \ .a:ver.' interpreter is not found.'] +endfunction + +function! s:check_version(prog, ver, skip) abort + if a:skip + return [1, ''] + endif + + let get_version = + \ ' -c "import sys; sys.stdout.write(str(sys.version_info[0]) + '. + \ '\".\" + str(sys.version_info[1]))"' + let min_version = (a:ver == 2) ? '2.6' : '3.3' + if system(a:prog . get_version) >= min_version + return [1, ''] + endif + return [0, 'Python ' . get_version . ' interpreter is not supported.'] +endfunction + +function! s:check_interpreter(prog, ver, skip) abort + if !executable(a:prog) + return [0, 'Python'.a:ver.' interpreter is not executable.'] + endif + + if a:skip + return [1, ''] + endif + + " Load neovim module check + call system(a:prog . ' -c ' . + \ (a:ver == 2 ? + \ '''import pkgutil; exit(pkgutil.get_loader("neovim") is None)''': + \ '''import importlib; exit(importlib.find_loader("neovim") is None)''') + \ ) + return [!v:shell_error, 'Python'.a:ver.' interpreter have not neovim module.'] +endfunction + diff --git a/runtime/autoload/provider/script_host.py b/runtime/autoload/provider/script_host.py index 14208310aa..e0b9ee6012 100644 --- a/runtime/autoload/provider/script_host.py +++ b/runtime/autoload/provider/script_host.py @@ -1,4 +1,4 @@ -"""Legacy python-vim emulation.""" +"""Legacy python/python3-vim emulation.""" import imp import logging import os @@ -35,7 +35,7 @@ class ScriptHost(object): if IS_PYTHON3: self.legacy_vim = self.legacy_vim.with_hook( neovim.DecodeHook( - encoding=nvim.options['encoding'].decode('ascii'))) + encoding=nvim.options['encoding'])) sys.modules['vim'] = self.legacy_vim def setup(self, nvim): @@ -96,7 +96,7 @@ class ScriptHost(object): # Python3 code (exec) must be a string, mixing bytes with # function_def would use bytes.__repr__ instead if isinstance and isinstance(code, bytes): - code = code.decode(nvim.options['encoding'].decode('ascii')) + code = code.decode(nvim.options['encoding']) # define the function function_def = 'def %s(line, linenr):\n %s' % (fname, code,) exec(function_def, self.module.__dict__) @@ -166,6 +166,9 @@ class RedirectStream(object): def writelines(self, seq): self.redirect_handler('\n'.join(seq)) + def flush(self): + pass + class LegacyEvalHook(neovim.SessionHook): @@ -175,8 +178,12 @@ class LegacyEvalHook(neovim.SessionHook): super(LegacyEvalHook, self).__init__(from_nvim=self._string_eval) def _string_eval(self, obj, session, method, kind): - if method == 'vim_eval' and isinstance(obj, (int, long, float)): - return str(obj) + if method == 'vim_eval': + if IS_PYTHON3: + if isinstance(obj, (int, float)): + return str(obj) + elif isinstance(obj, (int, long, float)): + return str(obj) return obj @@ -231,11 +238,11 @@ def discover_runtime_directories(nvim): for path in nvim.list_runtime_paths(): if not os.path.exists(path): continue - path1 = os.path.join(path, b'pythonx') + path1 = os.path.join(path, 'pythonx') if IS_PYTHON3: - path2 = os.path.join(path, b'python3') + path2 = os.path.join(path, 'python3') else: - path2 = os.path.join(path, b'python2') + path2 = os.path.join(path, 'python2') if os.path.exists(path1): rv.append(path1) if os.path.exists(path2): |