nvim: Move util to core.util module

Moved all utility functions from their own directory into
the core functionality direcotyr as a single file.
This commit is contained in:
Marty Oehme 2024-06-06 10:39:41 +02:00
parent e939305df3
commit 405af0f020
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
11 changed files with 32 additions and 59 deletions

View file

@ -13,7 +13,7 @@ end
-- Start quarto session -- Start quarto session
local startsession = function(file, args) local startsession = function(file, args)
file = file or default_buffer_session() file = file or default_buffer_session()
local path = require("util").get_python_venv() local path = require("core.util").get_python_venv()
vim.g["python3_host_prog"] = path vim.g["python3_host_prog"] = path
if vim.fn.executable("jupyter-console") ~= 1 then if vim.fn.executable("jupyter-console") ~= 1 then

View file

@ -1,5 +1,5 @@
local map = vim.keymap.set local map = vim.keymap.set
local is_available = require("util").is_available local is_available = require("core.util").is_available
if is_available("which-key") then if is_available("which-key") then
local prefix = require("which-key").register local prefix = require("which-key").register

View file

@ -1,22 +1,37 @@
local T = {} local T = {}
local exepath = vim.fn.exepath
local function path_join(...) -- from astronvim util function
return table.concat(vim.tbl_flatten { ... }, '/') --- 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 end
-- from https://github.com/ray-x/navigator.lua/issues/247#issue-1465308677 -- from https://github.com/ray-x/navigator.lua/issues/247#issue-1465308677
T.get_path = function(workspace) 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. -- Use activated virtualenv.
if vim.env.VIRTUAL_ENV then if vim.env.VIRTUAL_ENV then
return path_join(vim.env.VIRTUAL_ENV, "bin", "python") return path_join(vim.env.VIRTUAL_ENV, "bin", "python")
end end
-- Find and use virtualenv in workspace directory. -- Find and use virtualenv in workspace directory.
for _, pattern in ipairs({ "*", ".*" }) do for _, pattern in ipairs({ "*", ".*" }) do
local match = vim.fn.glob(path_join(workspace, pattern, "pyvenv.cfg")) local match = vim.fn.glob(path_join(workspace, pattern, "pyvenv.cfg"))
if match ~= "" then if match ~= "" then
local py = path_join("bin", "python") local py = path_join("bin", "python")
match = string.gsub(match, "pyvenv.cfg", py) match = string.gsub(match, "pyvenv.cfg", py)
return match return match
end end
@ -26,9 +41,8 @@ T.get_path = function(workspace)
return path_join(venv_base_folder, "bin", "python") return path_join(venv_base_folder, "bin", "python")
end end
end end
-- Fallback to system Python. -- Fallback to system Python.
return exepath("python3") or exepath("python") or "python" return vim.fn.exepath("python3") or vim.fn.exepath("python") or "python"
end end
return T return T

View file

@ -165,7 +165,7 @@ local python_path
lspconfig.pyright.setup({ lspconfig.pyright.setup({
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
if python_path == nil then if python_path == nil then
python_path, _ = require("util").get_python_venv(client.config.root_dir) python_path, _ = require("core.util").get_python_venv(client.config.root_dir)
end end
-- print(string.format("[PYTHON VENV]: %s", vim.inspect(python_path))) -- print(string.format("[PYTHON VENV]: %s", vim.inspect(python_path)))
client.config.settings.python.pythonPath = python_path client.config.settings.python.pythonPath = python_path
@ -176,14 +176,14 @@ lspconfig.ruff_lsp.setup({
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
on_attach(client, bufnr) on_attach(client, bufnr)
if python_path == nil then if python_path == nil then
python_path, _ = require("util").get_python_venv(client.config.root_dir) python_path, _ = require("core.util").get_python_venv(client.config.root_dir)
end end
client.config.settings.python.pythonPath = python_path client.config.settings.python.pythonPath = python_path
end, end,
}) })
-- set up arduino with the help of arduino.nvim plugin -- set up arduino with the help of arduino.nvim plugin
if require("util").is_available("arduino") then if require("core.util").is_available("arduino") then
lspconfig.arduino_language_server.setup({ lspconfig.arduino_language_server.setup({
on_new_config = require("arduino").on_new_config, on_new_config = require("arduino").on_new_config,
}) })

View file

@ -111,7 +111,7 @@ return {
hooks = { hooks = {
pre = function() pre = function()
-- use treesitter commentstring functionality if it's installed -- use treesitter commentstring functionality if it's installed
if require("util").is_available("ts_context_commentstring") then if require("core.util").is_available("ts_context_commentstring") then
require("ts_context_commentstring.internal").update_commentstring() require("ts_context_commentstring.internal").update_commentstring()
end end
end, end,

View file

@ -265,7 +265,7 @@ return {
}), }),
}, },
}) })
if require("util").is_available("which-key") then if require("core.util").is_available("which-key") then
require("which-key").register({ ["<localleader>t"] = { name = "+test" } }) require("which-key").register({ ["<localleader>t"] = { name = "+test" } })
end end
end, end,

View file

@ -60,7 +60,7 @@ local prose_plugs = {
{ {
"mickael-menu/zk-nvim", "mickael-menu/zk-nvim",
config = function() config = function()
if require("util").is_available("which-key") then if require("core.util").is_available("which-key") then
local prefix = require("which-key").register local prefix = require("which-key").register
prefix({ ["<leader>n"] = { name = "+notes" } }) prefix({ ["<leader>n"] = { name = "+notes" } })
prefix({ ["<localleader>n"] = { name = "+note" } }) prefix({ ["<localleader>n"] = { name = "+note" } })

View file

@ -9,7 +9,7 @@ return {
}, },
cmd = "Telescope", cmd = "Telescope",
config = function() config = function()
if require("util").is_available("which-key") then if require("core.util").is_available("which-key") then
require("which-key").register({ ["<leader>f"] = { name = "+find" } }) require("which-key").register({ ["<leader>f"] = { name = "+find" } })
end end
-- Setup up telescope fuzzy finding settings -- Setup up telescope fuzzy finding settings

View file

@ -136,7 +136,7 @@ return {
local Terminal = require("toggleterm.terminal").Terminal local Terminal = require("toggleterm.terminal").Terminal
-- need to disable indentlines since they obscure first line of terminal -- need to disable indentlines since they obscure first line of terminal
if require("util").is_available("mini.nvim") then if require("core.util").is_available("mini.nvim") then
vim.api.nvim_create_autocmd({ "TermOpen" }, { vim.api.nvim_create_autocmd({ "TermOpen" }, {
pattern = "*", pattern = "*",
callback = function() callback = function()

View file

@ -1,17 +0,0 @@
-- helper for easily defining highlight groups
--
-- usage example - italicize comments:
-- set_hl("Comment", { gui = "italic" })
return function(group, options)
local bg = options.bg == nil and "" or "guibg=" .. options.bg
local fg = options.fg == nil and "" or "guifg=" .. options.fg
local gui = options.gui == nil and "" or "gui=" .. options.gui
local link = options.link or false
local target = options.target
if not link then
vim.cmd(string.format("hi %s %s %s %s", group, bg, fg, gui))
else
vim.cmd(string.format("hi! link", group, target))
end
end

View file

@ -1,24 +0,0 @@
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
-- get the current python environment
-- return its path
function T.get_python_venv(workspace)
return require("util.pyenv").get_path(workspace)
end
return T