Files
darcula_nvim/nvim/lua/darcula/utils/init.lua
2026-03-31 12:07:49 -05:00

235 lines
5.9 KiB
Lua

-- The MIT License (MIT)
--
-- Copyright © 2026 Scott E. Graves <scott.e.graves@protonmail.com>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software
-- and associated documentation files (the “Software”), to deal in the Software without restriction,
-- including without limitation the rights to use, copy, modify, merge, publish, distribute,
-- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or
-- substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
-- BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
local cword = function()
---@diagnostic disable-next-line: missing-parameter
return vim.fn.expand("<cword>")
end
local coc_command = function(command)
vim.cmd("CocCommand " .. command)
end
local autocmd = function(group, event, pattern, callback)
vim.api.nvim_create_autocmd(
event,
{
callback = callback,
group = group,
pattern = pattern
}
)
end
local autocmd_buffer = function(group, event, buffer, callback)
vim.api.nvim_create_autocmd(
event,
{
buffer = buffer,
callback = callback,
group = group
}
)
end
local augroup_with_autocmd = function(name, event, pattern, callback)
local group = vim.api.nvim_create_augroup(name, {clear = true})
autocmd(group, event, pattern, callback)
return group
end
local augroup_with_autocmd_buffer = function(name, event, buffer, callback)
local group = vim.api.nvim_create_augroup(name, {clear = true})
autocmd_buffer(group, event, buffer, callback)
return group
end
local file_exists = function(name)
local f = io.open(name, "r")
return f ~= nil and io.close(f)
end
local get_visual_text = function()
vim.cmd('normal! "ry')
return vim.fn.getreg("r")
end
local is_c_family_file = function()
local name = vim.api.nvim_buf_get_name(0)
if name == nil or name == "" then
return false
end
local ext = name:match("%.([^.]+)$")
if not ext then
return false
end
ext = ext:lower()
return ext == "h" or ext == "hh" or ext == "hxx" or ext == "hpp" or ext == "c" or
ext == "cc" or
ext == "cpp" or
ext == "cxx"
end
local function get_files_sorted_without_extension(dir)
local files = {}
local scanner = vim.loop.fs_scandir(dir)
if not scanner then
return files
end
while true do
local name, typ = vim.loop.fs_scandir_next(scanner)
if not name then
break
end
if typ == "file" then
local base = name:match("(.+)%.[^%.]+$") or name
table.insert(files, base)
end
end
table.sort(files)
return files
end
local function create_vert_display(name, command)
vim.api.nvim_create_user_command(
name,
function()
vim.cmd("vert new")
local buf = vim.api.nvim_get_current_buf()
vim.bo[buf].buftype = "nofile"
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].buflisted = false
vim.bo[buf].swapfile = false
vim.bo[buf].modifiable = true
vim.opt_local.wrap = true
local lines = vim.fn.execute(command, "silent")
vim.api.nvim_buf_set_lines(
buf,
0,
-1,
false,
vim.split(lines, "\n", {plain = true, trimempty = true})
)
vim.bo[buf].modifiable = false
vim.api.nvim_win_set_cursor(0, {vim.api.nvim_buf_line_count(buf), 0})
end,
{}
)
end
local function copy_file(src, dst)
local uv = vim.uv
local ok, err = uv.fs_copyfile(src, dst)
if not ok then
error(("copy failed: %s"):format(err))
end
end
local function iff(b, l, r)
if b then
return l
end
return r
end
local parse_env = function(var, def)
if vim.env[var] ~= nil then
if type(def) == "boolean" then
local result =
({
["0"] = false,
["1"] = true,
["false"] = false,
["n"] = false,
["no"] = false,
["off"] = false,
["on"] = true,
["true"] = true,
["y"] = true,
["yes"] = true
})[tostring(vim.env[var]):lower()]
if result == nil then
return def
end
return result
end
return vim.env[var]
end
return def
end
local parse_env_list = function(var, def)
if vim.env[var] ~= nil then
local items = vim.split(var, " ", {plain = true, trimempty = true})
for i, v in ipairs(items) do
items[i] = vim.fn.trim(v)
end
return items
end
return def
end
local function get_current_filename(buffer)
buffer = buffer or 0
local name = vim.api.nvim_buf_get_name(buffer)
if name == "" then
return ""
end
return vim.fn.fnamemodify(name, ":t")
end
local notify_setting_enabled = function(text, enabled)
vim.notify(
text .. ": " .. iff(enabled, "ENABLED", "DISABLED"),
vim.log.levels.INFO
)
end
return {
augroup_with_autocmd = augroup_with_autocmd,
augroup_with_autocmd_buffer = augroup_with_autocmd_buffer,
autocmd = autocmd,
autocmd_buffer = autocmd_buffer,
coc_command = coc_command,
copy_file = copy_file,
create_vert_display = create_vert_display,
cword = cword,
file_exists = file_exists,
get_current_filename = get_current_filename,
get_files_sorted_without_extension = get_files_sorted_without_extension,
get_visual_text = get_visual_text,
iff = iff,
is_c_family_file = is_c_family_file,
notify_setting_enabled = notify_setting_enabled,
parse_env = parse_env,
parse_env_list = parse_env_list
}