diff options
Diffstat (limited to 'runtime/autoload')
-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 | ||||
-rw-r--r-- | runtime/autoload/remote/host.vim | 76 |
5 files changed, 185 insertions, 80 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): diff --git a/runtime/autoload/remote/host.vim b/runtime/autoload/remote/host.vim index ebbd85b6e6..c4b7ebf91f 100644 --- a/runtime/autoload/remote/host.vim +++ b/runtime/autoload/remote/host.vim @@ -1,6 +1,7 @@ let s:hosts = {} let s:plugin_patterns = { - \ 'python': '*.py' + \ 'python': '*.py', + \ 'python3': '*.py', \ } let s:remote_plugins_manifest = fnamemodify($MYVIMRC, ':p:h') \.'/.'.fnamemodify($MYVIMRC, ':t').'-rplugin~' @@ -25,7 +26,12 @@ function! remote#host#RegisterClone(name, 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} + let s:hosts[a:name] = { + \ 'factory': Factory, + \ 'channel': 0, + \ 'initialized': 0, + \ 'orig_name': a:orig_name + \ } endfunction @@ -51,8 +57,8 @@ function! remote#host#IsRunning(name) endfunction -" Example of registering a python plugin with two commands(one async), one -" autocmd(async) and one function(sync): +" Example of registering a Python plugin with two commands (one async), one +" autocmd (async) and one function (sync): " " let s:plugin_path = expand('<sfile>:p:h').'/nvim_plugin.py' " call remote#host#RegisterPlugin('python', s:plugin_path, [ @@ -182,72 +188,29 @@ endfunction " Registration of standard hosts -" Python {{{ +" Python/Python3 {{{ function! s:RequirePythonHost(name) + let ver_name = has_key(s:hosts[a:name], 'orig_name') ? + \ s:hosts[a:name].orig_name : a:name + let ver = (ver_name ==# 'python') ? 2 : 3 + " Python host arguments let args = ['-c', 'import neovim; neovim.start_host()'] - " Collect registered python plugins into args + " Collect registered Python plugins into args let python_plugins = s:PluginsForHost(a:name) for plugin in python_plugins call add(args, plugin.path) endfor - " Try loading a python host using `python_host_prog` or `python` - let python_host_prog = get(g:, 'python_host_prog', 'python') - try - let channel_id = rpcstart(python_host_prog, args) - if rpcrequest(channel_id, 'poll') == 'ok' - return channel_id - endif - catch - endtry - - " Failed, try a little harder to find the correct interpreter or - " report a friendly error to user - let get_version = - \ ' -c "import sys; sys.stdout.write(str(sys.version_info[0]) + '. - \ '\".\" + str(sys.version_info[1]))"' - - let supported = ['2.6', '2.7'] - - " To load the python host a python executable must be available - if exists('g:python_host_prog') - \ && executable(g:python_host_prog) - \ && index(supported, system(g:python_host_prog.get_version)) >= 0 - let python_host_prog = g:python_host_prog - elseif executable('python') - \ && index(supported, system('python'.get_version)) >= 0 - let python_host_prog = 'python' - elseif executable('python2') - \ && index(supported, system('python2'.get_version)) >= 0 - " In some distros, python3 is the default python - let python_host_prog = 'python2' - else - throw 'No python interpreter found.' . - \ " Try setting 'let g:python_host_prog=/path/to/python' in your '.nvimrc'" . - \ " or see ':help nvim-python'." - endif - - " Make sure we pick correct python version on path. - let python_host_prog = exepath(python_host_prog) - let python_version = systemlist(python_host_prog . ' --version')[0] - - " Execute python, import neovim and print a string. If import_result doesn't - " matches the printed string, the user is missing the neovim module - let import_result = system(python_host_prog . - \ ' -c "import neovim, sys; sys.stdout.write(\"ok\")"') - if import_result != 'ok' - throw 'No neovim module found for ' . python_version . '.' . - \ " Try installing it with 'pip install neovim' or see ':help nvim-python'." - endif - try - let channel_id = rpcstart(python_host_prog, args) + let channel_id = rpcstart((ver == '2' ? + \ provider#python#Prog() : provider#python3#Prog()), args) if rpcrequest(channel_id, 'poll') == 'ok' return channel_id endif catch + echomsg v:exception endtry throw 'Failed to load python host. You can try to see what happened ' . \ 'by starting Neovim with $NVIM_PYTHON_PYTHON_LOG and opening '. @@ -256,4 +219,5 @@ function! s:RequirePythonHost(name) endfunction call remote#host#Register('python', function('s:RequirePythonHost')) +call remote#host#Register('python3', function('s:RequirePythonHost')) " }}} |