aboutsummaryrefslogtreecommitdiff
path: root/runtime/autoload
diff options
context:
space:
mode:
authordundargoc <gocdundar@gmail.com>2024-01-07 13:05:03 +0100
committerdundargoc <33953936+dundargoc@users.noreply.github.com>2024-01-22 22:36:40 +0100
commiteb5d15e3838f53e2fcd25989c88db87458e9f984 (patch)
treede13432021adb8632ed61cd9bbe4270eaec0674c /runtime/autoload
parent13d50c3b13fb6f2bba948e50fefee451e0a90487 (diff)
downloadrneovim-eb5d15e3838f53e2fcd25989c88db87458e9f984.tar.gz
rneovim-eb5d15e3838f53e2fcd25989c88db87458e9f984.tar.bz2
rneovim-eb5d15e3838f53e2fcd25989c88db87458e9f984.zip
refactor: rewrite python provider in lua
Diffstat (limited to 'runtime/autoload')
-rw-r--r--runtime/autoload/provider/python3.vim44
-rw-r--r--runtime/autoload/provider/pythonx.vim112
-rw-r--r--runtime/autoload/remote/host.vim6
3 files changed, 9 insertions, 153 deletions
diff --git a/runtime/autoload/provider/python3.vim b/runtime/autoload/provider/python3.vim
index 38ef0cccfc..d4ffeab155 100644
--- a/runtime/autoload/provider/python3.vim
+++ b/runtime/autoload/provider/python3.vim
@@ -1,45 +1,15 @@
-" The Python3 provider uses a Python3 host to emulate an environment for running
-" python3 plugins. :help provider
-"
-" 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 [s:prog, s:err] = provider#pythonx#Detect(3)
-let g:loaded_python3_provider = empty(s:prog) ? 1 : 2
-function! provider#python3#Prog() abort
- return s:prog
+function! provider#python3#Call(method, args) abort
+ return v:lua.require'vim.provider.python'.call(a:method, a:args)
endfunction
-function! provider#python3#Error() abort
- return s:err
+function! provider#python3#Require(host) abort
+ return v:lua.require'vim.provider.python'.require(a:host)
endfunction
-" 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', 'script_host.py', [])
-
-function! provider#python3#Call(method, args) abort
- if s:err != ''
- return
- endif
- 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
- let s:err = v:exception
- echohl WarningMsg
- echomsg v:exception
- echohl None
- return
- endtry
- endif
- return call(s:rpcrequest, insert(insert(a:args, 'python_'.a:method), s:host))
-endfunction
+let s:prog = v:lua.require'vim.provider.python'.detect_by_module('neovim')
+let g:loaded_python3_provider = empty(s:prog) ? 1 : 2
+call v:lua.require'vim.provider.python'.start()
diff --git a/runtime/autoload/provider/pythonx.vim b/runtime/autoload/provider/pythonx.vim
deleted file mode 100644
index 48b96c699a..0000000000
--- a/runtime/autoload/provider/pythonx.vim
+++ /dev/null
@@ -1,112 +0,0 @@
-" The Python provider helper
-if exists('s:loaded_pythonx_provider')
- finish
-endif
-
-let s:loaded_pythonx_provider = 1
-
-function! provider#pythonx#Require(host) abort
- " Python host arguments
- let prog = provider#python3#Prog()
- let args = [prog, '-c', 'import sys; sys.path = [p for p in sys.path if p != ""]; import neovim; neovim.start_host()']
-
-
- " Collect registered Python plugins into args
- let python_plugins = remote#host#PluginsForHost(a:host.name)
- for plugin in python_plugins
- call add(args, plugin.path)
- endfor
-
- return provider#Poll(args, a:host.orig_name, '$NVIM_PYTHON_LOG_FILE', {'overlapped': v:true})
-endfunction
-
-function! s:get_python_executable_from_host_var(major_version) abort
- return expand(get(g:, 'python'.(a:major_version == 3 ? '3' : execute("throw 'unsupported'")).'_host_prog', ''), v:true)
-endfunction
-
-function! s:get_python_candidates(major_version) abort
- return {
- \ 3: ['python3', 'python3.12', 'python3.11', 'python3.10', 'python3.9', 'python3.8', 'python3.7', 'python']
- \ }[a:major_version]
-endfunction
-
-" Returns [path_to_python_executable, error_message]
-function! provider#pythonx#Detect(major_version) abort
- return provider#pythonx#DetectByModule('neovim', a:major_version)
-endfunction
-
-" Returns [path_to_python_executable, error_message]
-function! provider#pythonx#DetectByModule(module, major_version) abort
- let python_exe = s:get_python_executable_from_host_var(a:major_version)
-
- if !empty(python_exe)
- return [exepath(expand(python_exe, v:true)), '']
- endif
-
- let candidates = s:get_python_candidates(a:major_version)
- let errors = []
-
- for exe in candidates
- let [result, error] = provider#pythonx#CheckForModule(exe, a:module, a:major_version)
- if result
- return [exe, error]
- endif
- " Accumulate errors in case we don't find any suitable Python executable.
- call add(errors, error)
- endfor
-
- " No suitable Python executable found.
- return ['', 'Could not load Python '.a:major_version.":\n".join(errors, "\n")]
-endfunction
-
-" Returns array: [prog_exitcode, prog_version]
-function! s:import_module(prog, module) abort
- let prog_version = system([a:prog, '-W', 'ignore', '-c', printf(
- \ 'import sys, importlib.util; ' .
- \ 'sys.path = [p for p in sys.path if p != ""]; ' .
- \ 'sys.stdout.write(str(sys.version_info[0]) + "." + str(sys.version_info[1])); ' .
- \ 'sys.exit(2 * int(importlib.util.find_spec("%s") is None))',
- \ a:module)])
- return [v:shell_error, prog_version]
-endfunction
-
-" Returns array: [was_success, error_message]
-function! provider#pythonx#CheckForModule(prog, module, major_version) abort
- let prog_path = exepath(a:prog)
- if prog_path ==# ''
- return [0, a:prog . ' not found in search path or not executable.']
- endif
-
- let min_version = '3.7'
-
- " Try to load module, and output Python version.
- " Exit codes:
- " 0 module can be loaded.
- " 2 module cannot be loaded.
- " Otherwise something else went wrong (e.g. 1 or 127).
- let [prog_exitcode, prog_version] = s:import_module(a:prog, a:module)
-
- if prog_exitcode == 2 || prog_exitcode == 0
- " Check version only for expected return codes.
- if prog_version !~ '^' . a:major_version
- return [0, prog_path . ' is Python ' . prog_version . ' and cannot provide Python '
- \ . a:major_version . '.']
- elseif prog_version =~ '^' . a:major_version && str2nr(prog_version[2:]) < str2nr(min_version[2:])
- return [0, prog_path . ' is Python ' . prog_version . ' and cannot provide Python >= '
- \ . min_version . '.']
- endif
- endif
-
- if prog_exitcode == 2
- return [0, prog_path.' does not have the "' . a:module . '" module.']
- elseif prog_exitcode == 127
- " This can happen with pyenv's shims.
- return [0, prog_path . ' does not exist: ' . prog_version]
- elseif prog_exitcode
- return [0, 'Checking ' . prog_path . ' caused an unknown error. '
- \ . '(' . prog_exitcode . ', output: ' . prog_version . ')'
- \ . ' Report this at https://github.com/neovim/neovim']
- endif
-
- return [1, '']
-endfunction
diff --git a/runtime/autoload/remote/host.vim b/runtime/autoload/remote/host.vim
index 884b478f19..0032a4b71e 100644
--- a/runtime/autoload/remote/host.vim
+++ b/runtime/autoload/remote/host.vim
@@ -190,11 +190,9 @@ endfunction
" Registration of standard hosts
-" Python/Python3
-call remote#host#Register('python', '*',
- \ function('provider#pythonx#Require'))
+" Python3
call remote#host#Register('python3', '*',
- \ function('provider#pythonx#Require'))
+ \ function('provider#python3#Require'))
" Ruby
call remote#host#Register('ruby', '*.rb',