fix: harden against missing plugins

- Avoid errors when plugins are disabled by checking for them before
calling them whenever possible
This commit is contained in:
2024-10-04 14:01:46 -04:00
parent 69524bf589
commit 96c71019db
3 changed files with 165 additions and 114 deletions
+57 -39
View File
@@ -55,12 +55,14 @@ autocmd({ "BufEnter", "FocusGained" }, {
}) })
-- LazyGit root detection -- LazyGit root detection
vim.api.nvim_create_autocmd("BufEnter", { if pcall(require, "lazygit.utils") then
pattern = "*", vim.api.nvim_create_autocmd("BufEnter", {
callback = function() pattern = "*",
require("lazygit.utils").project_root_dir() callback = function()
end, require("lazygit.utils").project_root_dir()
}) end,
})
end
-- TodoFzfLua command override to use Telescope -- TodoFzfLua command override to use Telescope
-- when fzf-lua is not installed -- when fzf-lua is not installed
@@ -75,43 +77,59 @@ autocmd({ "BufEnter" }, {
}) })
-- ━━━━━━━━━━━━━━━━━━━━━━━ Set up Qalc keymappings ━━━━━━━━━━━━━━━━━━━━━━━ -- ━━━━━━━━━━━━━━━━━━━━━━━ Set up Qalc keymappings ━━━━━━━━━━━━━━━━━━━━━━━
-- Create a group for filetype-specific mappings if pcall(require, "qalc") then
vim.api.nvim_create_augroup("QalcFileTypeMappings", { clear = true }) -- Create a group for filetype-specific mappings
vim.api.nvim_create_augroup("QalcFileTypeMappings", { clear = true })
-- Create an autocommand for the qalc filetype -- Create an autocommand for the qalc filetype
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
pattern = "qalc", pattern = "qalc",
group = "QalcFileTypeMappings", group = "QalcFileTypeMappings",
callback = function() callback = function()
-- Set the key mapping: 'y' to run the :QalcYank command in normal mode -- Set the key mapping: 'y' to run the :QalcYank command in normal mode
vim.keymap.set( -- Yank Result vim.keymap.set( -- Yank Result
"n", "n",
"y", "y",
":QalcYank +<CR>", ":QalcYank +<CR>",
{ noremap = true, silent = true } { noremap = true, silent = true }
) )
-- Set the key mapping: 'q' to run the :QalcClose command in normal mode -- Set the key mapping: 'q' to run the :QalcClose command in normal mode
vim.keymap.set( -- Close Qalc vim.keymap.set( -- Close Qalc
"n", "n",
"q", "q",
":QalcClose<CR>", ":QalcClose<CR>",
{ noremap = true, silent = true } { noremap = true, silent = true }
) )
end, end,
}) })
-- Define a custom command to close the Qalc buffer -- Define a custom command to close the Qalc buffer
vim.api.nvim_create_user_command("QalcClose", function() vim.api.nvim_create_user_command("QalcClose", function()
local buf_name = vim.api.nvim_buf_get_name(0) local buf_name = vim.api.nvim_buf_get_name(0)
if buf_name ~= "" then if buf_name ~= "" then
vim.cmd("bd!") vim.cmd("bd!")
else else
-- If the buffer has no name, just remove it without invoking :bd! -- If the buffer has no name, just remove it without invoking :bd!
vim.api.nvim_buf_delete(0, { force = true }) vim.api.nvim_buf_delete(0, { force = true })
end end
end, { bang = true }) end, { bang = true })
end
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━ Set up highlights ━━━━━━━━━━━━━━━━━━━━━━━━━━ -- ━━━━━━━━━━━━━━━━━━━━━━━━━━ Set up highlights ━━━━━━━━━━━━━━━━━━━━━━━━━━
local load_highlight = require("utils.highlight") local load_highlight = require("utils.highlight")
load_highlight.setup_autocommands() load_highlight.setup_autocommands()
load_highlight.setup_dashboard_highlight() load_highlight.setup_dashboard_highlight()
vim.api.nvim_create_autocmd("TextYankPost", {
callback = function()
local ev = vim.v.event
local content = {}
if ev.regtype:sub(1, 1) ~= "" or not ev.visual then
return
end
for _, line in ipairs(ev.regcontents) do
table.insert(content, vim.fn.trim(line, "", 2))
end
vim.fn.setreg("+", content)
end,
})
+23 -6
View File
@@ -123,8 +123,17 @@ return { -- Lualine
vim.o.laststatus = vim.g.lualine_laststatus vim.o.laststatus = vim.g.lualine_laststatus
-- Define todo-comments component -- Define todo-comments component
local todos_component =
require("todos-lualine").component(data.types.todo.lualine()) -- Attempt to require the plugin and handle the case where it's not available
local status, todos = pcall(require, "todos-lualine")
local todos_component
if not status then
todos_component = nil -- Set to nil if the plugin is not loaded
else
todos_component = todos.component(data.types.todo.lualine())
end
local opts = { local opts = {
options = { -- General options options = { -- General options
@@ -147,16 +156,17 @@ return { -- Lualine
}, },
{ -- MultiCursors { -- MultiCursors
function() function()
return data.func.mc_statusline().icon return data.func.mc_statusline().count
.. data.func.mc_statusline().icon
end, end,
cond = function() cond = function()
return data.func.mc_statusline().enabled return data.func.mc_statusline().cursors > 1
end, end,
color = function() color = function()
return data.func.mc_statusline().color return data.func.mc_statusline().color
end, end,
padding = { left = 0, right = 0 }, padding = { left = 1, right = 0 },
separator = { left = "", right = "" }, separator = { left = "", right = "" },
}, },
}, },
lualine_b = { lualine_b = {
@@ -181,6 +191,7 @@ return { -- Lualine
end, end,
cond = function() cond = function()
return funcs.is_window_wide_enough(100) return funcs.is_window_wide_enough(100)
and pcall(require, "arrow")
end, end,
padding = { left = 1, right = 0 }, padding = { left = 1, right = 0 },
on_click = function() on_click = function()
@@ -322,6 +333,9 @@ return { -- Lualine
end, end,
padding = { left = 1, right = 1 }, padding = { left = 1, right = 1 },
cond = function() cond = function()
if not pcall(require, "nvim_updater") then
return false
end
return not string.find(vim.bo.filetype, "neovim_updater_term") return not string.find(vim.bo.filetype, "neovim_updater_term")
end, end,
}, },
@@ -398,6 +412,9 @@ return { -- Lualine
lualine_z = { lualine_z = {
{ -- Recording { -- Recording
require("recorder").recordingStatus, require("recorder").recordingStatus,
cond = function()
return pcall(require, "recorder")
end,
color = "CurSearch", color = "CurSearch",
separator = { left = "", right = "" }, separator = { left = "", right = "" },
}, },
+85 -69
View File
@@ -64,80 +64,85 @@ function M.setup_indent_highlight()
-- Get the foreground color of Error -- Get the foreground color of Error
local miniiconsred_fg_hex = get_fg_color("Error") local miniiconsred_fg_hex = get_fg_color("Error")
-- Load the ibl plugin hooks -- Load the ibl plugin
local hooks = require("ibl.hooks") if pcall(require, "ibl") then
-- create the highlight groups in the highlight setup hook -- Load the ibl plugin hooks
if cursorline_bg_hex then local hooks = require("ibl.hooks")
-- Define custom highlight groups for indent -- create the highlight groups in the highlight setup hook
hooks.register(hooks.type.HIGHLIGHT_SETUP, function() if cursorline_bg_hex then
set_hl( -- Define custom highlight groups for indent
0, hooks.register(hooks.type.HIGHLIGHT_SETUP, function()
"IndentBlanklineIndent", set_hl(
{ fg = cursorline_bg_hex, nocombine = true } 0,
) "IndentBlanklineIndent",
set_hl( { fg = cursorline_bg_hex, nocombine = true }
0, )
"IndentsScopeHighlight", set_hl(
{ fg = miniiconsred_fg_hex, nocombine = true } 0,
) "IndentsScopeHighlight",
end) { fg = miniiconsred_fg_hex, nocombine = true }
end )
end)
end
-- Define characters for indent-blankline -- Define characters for indent-blankline
local tab_char, indent_char local tab_char, indent_char
if vim.g.tab_char then if vim.g.tab_char then
tab_char = vim.g.tab_char tab_char = vim.g.tab_char
elseif vim.g.tab_char_name then elseif vim.g.tab_char_name then
tab_char = data.types.ibl.char[vim.g.tab_char_name] tab_char = data.types.ibl.char[vim.g.tab_char_name]
else else
tab_char = data.types.ibl.char.none tab_char = data.types.ibl.char.none
end end
if vim.g.indent_char then if vim.g.indent_char then
indent_char = vim.g.indent_char indent_char = vim.g.indent_char
elseif vim.g.indent_char_name then elseif vim.g.indent_char_name then
indent_char = data.types.ibl.char[vim.g.indent_char_name] indent_char = data.types.ibl.char[vim.g.indent_char_name]
else else
indent_char = data.types.ibl.char.none indent_char = data.types.ibl.char.none
end end
-- Configure the indent-blankline plugin -- Configure the indent-blankline plugin
require("ibl").setup({ require("ibl").setup({
indent = { indent = {
char = indent_char, char = indent_char,
tab_char = tab_char, tab_char = tab_char,
highlight = { highlight = {
"IndentBlanklineIndent", "IndentBlanklineIndent",
},
}, },
}, scope = {
scope = { enabled = false,
enabled = false, },
}, exclude = {
exclude = { filetypes = data.types.highlights.exclude,
filetypes = data.types.highlights.exclude, },
}, })
}) end
-- Configure the deadcolumn plugin -- Configure the deadcolumn plugin
require("deadcolumn").setup({ if pcall(require, "deadcolumn") then
scope = "line", ---@type string|fun(): integer require("deadcolumn").setup({
modes = function(mode) scope = "line", ---@type string|fun(): integer
return mode:find("^[ictRss\x13]") ~= nil modes = function(mode)
end, return mode:find("^[ictRss\x13]") ~= nil
blending = { end,
threshold = 0.9, blending = {
colorcode = cursorline_bg_hex, threshold = 0.9,
hlgroup = { "Normal", "bg" }, colorcode = cursorline_bg_hex,
}, hlgroup = { "Normal", "bg" },
warning = { },
alpha = 0.4, warning = {
offset = 0, alpha = 0.4,
colorcode = miniiconsred_fg_hex, offset = 0,
hlgroup = { "Error", "bg" }, colorcode = miniiconsred_fg_hex,
}, hlgroup = { "Error", "bg" },
extra = { },
follow_tw = "80", extra = {
}, follow_tw = "80",
}) },
})
end
-- Set NeoCodeium suggestion highlight -- Set NeoCodeium suggestion highlight
vim.api.nvim_set_hl(0, "NeoCodeiumSuggestion", { link = "Comment" }) vim.api.nvim_set_hl(0, "NeoCodeiumSuggestion", { link = "Comment" })
@@ -223,6 +228,16 @@ function M.setup_avante_highlights()
set_hl(0, "AvantePopupHint", { link = "DiagnosticVirtualTextHint" }) set_hl(0, "AvantePopupHint", { link = "DiagnosticVirtualTextHint" })
end end
function M.setup_multi_cursor_highlight()
-- Customize how cursors look.
vim.api.nvim_set_hl(0, "MultiCursorCursor", { link = "Cursor" })
vim.api.nvim_set_hl(0, "MultiCursorVisual", { link = "Visual" })
vim.api.nvim_set_hl(0, "MultiCursorSign", { link = "GitSignsAdd" })
vim.api.nvim_set_hl(0, "MultiCursorDisabledCursor", { link = "Visual" })
vim.api.nvim_set_hl(0, "MultiCursorDisabledVisual", { link = "Visual" })
vim.api.nvim_set_hl(0, "MultiCursorDisabledSign", { link = "SignColumn" })
end
--- Function to set up autocommands --- Function to set up autocommands
---@return nil ---@return nil
function M.setup_autocommands() function M.setup_autocommands()
@@ -243,6 +258,7 @@ function M.setup_autocommands()
M.setup_indent_highlight() M.setup_indent_highlight()
M.setup_minimap_highlight() M.setup_minimap_highlight()
M.setup_avante_highlights() M.setup_avante_highlights()
M.setup_multi_cursor_highlight()
else else
if pcall(require, "auto-cursorline") then if pcall(require, "auto-cursorline") then
require("auto-cursorline").disable({ require("auto-cursorline").disable({