| local M = {} |
| local min_version = '3.9' |
| local s_err |
| local s_host |
|
|
| local python_candidates = { |
| 'python3', |
| 'python3.13', |
| 'python3.12', |
| 'python3.11', |
| 'python3.10', |
| 'python3.9', |
| 'python', |
| } |
|
|
| |
| |
| |
| local function import_module(prog, module) |
| local program = [[ |
| 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]));]] |
|
|
| program = program |
| .. string.format('sys.exit(2 * int(importlib.util.find_spec("%s") is None))', module) |
|
|
| local out = vim.system({ prog, '-W', 'ignore', '-c', program }):wait() |
| return out.code, assert(out.stdout) |
| end |
|
|
| |
| |
| |
| local function check_for_module(prog, module) |
| local prog_path = vim.fn.exepath(prog) |
| if prog_path == '' then |
| return prog .. ' not found in search path or not executable.' |
| end |
|
|
| |
| |
| |
| |
| |
| local prog_exitcode, prog_version = import_module(prog, module) |
| if prog_exitcode == 2 or prog_exitcode == 0 then |
| |
| if vim.version.lt(prog_version, min_version) then |
| return string.format( |
| '%s is Python %s and cannot provide Python >= %s.', |
| prog_path, |
| prog_version, |
| min_version |
| ) |
| end |
| end |
|
|
| if prog_exitcode == 2 then |
| return string.format('%s does not have the "%s" module.', prog_path, module) |
| elseif prog_exitcode == 127 then |
| |
| return string.format('%s does not exist: %s', prog_path, prog_version) |
| elseif prog_exitcode ~= 0 then |
| return string.format( |
| 'Checking %s caused an unknown error. (%s, output: %s) Report this at https://github.com/neovim/neovim', |
| prog_path, |
| prog_exitcode, |
| prog_version |
| ) |
| end |
|
|
| return nil |
| end |
|
|
| |
| |
| |
| function M.detect_by_module(module) |
| local python_exe = vim.fn.expand(vim.g.python3_host_prog or '', true) |
|
|
| if python_exe ~= '' then |
| return vim.fn.exepath(vim.fn.expand(python_exe, true)), nil |
| end |
|
|
| if vim.fn.executable('pynvim-python') == 1 then |
| return 'pynvim-python' |
| end |
|
|
| local errors = {} |
| for _, exe in ipairs(python_candidates) do |
| local error = check_for_module(exe, module) |
| if not error then |
| return exe, error |
| end |
| |
| table.insert(errors, error) |
| end |
|
|
| |
| return nil, 'Could not load Python :\n' .. table.concat(errors, '\n') |
| end |
|
|
| function M.require(host) |
| |
| local prog = M.detect_by_module('neovim') |
| local args = { |
| prog, |
| '-c', |
| 'import sys; sys.path = [p for p in sys.path if p != ""]; import neovim; neovim.start_host()', |
| } |
|
|
| |
| local python_plugins = vim.fn['remote#host#PluginsForHost'](host.name) |
| |
| for _, plugin in ipairs(python_plugins) do |
| table.insert(args, plugin.path) |
| end |
|
|
| return vim.fn['provider#Poll']( |
| args, |
| host.orig_name, |
| '$NVIM_PYTHON_LOG_FILE', |
| { ['overlapped'] = true } |
| ) |
| end |
|
|
| function M.call(method, args) |
| if s_err then |
| return |
| end |
|
|
| if not s_host then |
| |
| local ok, result = pcall(vim.fn['remote#host#Require'], 'legacy-python3-provider') |
| if not ok then |
| s_err = result |
| vim.api.nvim_echo({ { result, 'WarningMsg' } }, true, {}) |
| return |
| end |
| s_host = result |
| end |
|
|
| return vim.fn.rpcrequest(s_host, 'python_' .. method, unpack(args)) |
| end |
|
|
| function M.start() |
| |
| vim.fn['remote#host#RegisterClone']('legacy-python3-provider', 'python3') |
| vim.fn['remote#host#RegisterPlugin']('legacy-python3-provider', 'script_host.py', {}) |
| end |
|
|
| return M |
|
|