dotfiles/nvim/.config/nvim/lua/core/util.lua
Marty Oehme 4f5445cc0e
nvim: Add function to cleanly unmap keys
Removes them from being active in vim as well as removing them from
being displayed in which-key.

Unfortunately the which-key implementation still seems broken, sometimes
removing them sometimes leaving them as-is.
2024-06-16 16:40:10 +02:00

59 lines
1.9 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
-- Remove the key from the vim keymap and from being displayed in which-key
-- FIXME This does not consistently currently with which-key
-- Every once in a while the maps are correctly hidden but other times they stay?
function T.unmap_key(lhs, mode)
mode = mode or "n"
if T.is_available("which-key") then
vim.keymap.set(mode, lhs, "", { desc = "which_key_ignore", silent = true })
end
pcall(vim.keymap.del, mode, lhs)
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