dotfiles/.config/nvim/lua/my/functions.lua
2024-08-19 11:59:14 +02:00

99 lines
2.3 KiB
Lua

local M = {}
function M.has_api_level(desired)
return vim.version().api_level >= desired
end
function M.nerdfont_installed()
if vim.fn.executable('fc-list') == 1 and
os.execute([[fc-list -q 'Symbols Nerd Font']]) == 0 then
return true
end
return false
end
function M.map(mode, shortcut, command, description, buffer)
local opts = { noremap = true, silent = true }
opts.desc = description or nil
opts.buffer = buffer or nil
vim.keymap.set(mode, shortcut, command, opts)
end
-- Files that could indicate the project's root directory
M.project_root_markers = {
'.git', '.hg', '.svn', '.bzr', '_darcs',
'.projectile', '.luarc.json', '.editorconfig', 'Cargo.toml'
}
function M.get_project_root()
local lsp_root = vim.lsp.buf.list_workspace_folders()[1]
if lsp_root ~= nil then
return lsp_root
end
local path = vim.api.nvim_buf_get_name(0)
local sep = '/'
repeat
path = path:gsub(string.format('%s[^%s]*$', sep, sep), '')
for _, marker in ipairs(M.project_root_markers) do
if path ~= os.getenv('HOME') and io.open(path .. sep .. marker) then
return path
end
end
until path == ''
return nil
end
function M.not_firenvim()
return vim.g.started_by_firenvim == nil
end
function M.lsp_status()
local status = ''
for _, msg in ipairs(require('lsp-status/messaging').messages()) do
if msg.progress then
status = ''
elseif msg.status then
-- clangd parsing includes and whatnot
if msg.content ~= 'idle' then
status = ''
end
end
end
return status
end
function M.lsp_sig_status()
if not vim.lsp.buf.server_ready() then
return ""
end
local width = vim.o.columns / 3
if width < 40 then
return ""
end
if width > 50 then
width = width * 1.5
end
local label = require('lsp_signature').status_line().label
if label:len() > width then
label = label:sub(1, math.floor(width)) .. ''
end
return label
end
function M.current_function()
local curfun = vim.b.lsp_current_function
if curfun and curfun ~= '' then
return '(' .. curfun .. ')'
end
return ''
end
return M