dotfiles/nvim/.config/nvim/lua/core/util.lua
Marty Oehme 405af0f020
nvim: Move util to core.util module
Moved all utility functions from their own directory into
the core functionality direcotyr as a single file.
2024-06-06 16:23:41 +02:00

48 lines
1.5 KiB
Lua

local T = {}
-- from astronvim util function
--- Check if a plugin is defined in lazy. Useful with lazy loading when a plugin is not necessarily loaded yet
---@param plugin string The plugin to search for
---@return boolean available # Whether the plugin is available
function T.is_available(plugin)
return T.get_plugin(plugin) and true or false
end
-- Get the plugin file handle if it exists, return nil otherwise
function T.get_plugin(plugin)
local status, lib = pcall(require, plugin)
if status then
return lib
end
return nil
end
-- from https://github.com/ray-x/navigator.lua/issues/247#issue-1465308677
local function path_join(...)
return table.concat(vim.tbl_flatten({ ... }), "/")
end
-- return the current python environment path
function T.get_python_venv(workspace)
-- Use activated virtualenv.
if vim.env.VIRTUAL_ENV then
return path_join(vim.env.VIRTUAL_ENV, "bin", "python")
end
-- Find and use virtualenv in workspace directory.
for _, pattern in ipairs({ "*", ".*" }) do
local match = vim.fn.glob(path_join(workspace, pattern, "pyvenv.cfg"))
if match ~= "" then
local py = path_join("bin", "python")
match = string.gsub(match, "pyvenv.cfg", py)
return match
end
match = vim.fn.glob(path_join(workspace, pattern, "poetry.lock"))
if match ~= "" then
local venv_base_folder = vim.fn.trim(vim.fn.system("poetry env info -p"))
return path_join(venv_base_folder, "bin", "python")
end
end
-- Fallback to system Python.
return vim.fn.exepath("python3") or vim.fn.exepath("python") or "python"
end
return T