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
+28 -10
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
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*", pattern = "*",
callback = function() callback = function()
require("lazygit.utils").project_root_dir() require("lazygit.utils").project_root_dir()
end, 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,11 +77,12 @@ 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()
@@ -98,10 +101,10 @@ vim.api.nvim_create_autocmd("FileType", {
{ 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!")
@@ -109,9 +112,24 @@ vim.api.nvim_create_user_command("QalcClose", function()
-- 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 = "" },
}, },
+16
View File
@@ -64,6 +64,8 @@ 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
if pcall(require, "ibl") then
-- Load the ibl plugin hooks -- Load the ibl plugin hooks
local hooks = require("ibl.hooks") local hooks = require("ibl.hooks")
-- create the highlight groups in the highlight setup hook -- create the highlight groups in the highlight setup hook
@@ -116,8 +118,10 @@ function M.setup_indent_highlight()
filetypes = data.types.highlights.exclude, filetypes = data.types.highlights.exclude,
}, },
}) })
end
-- Configure the deadcolumn plugin -- Configure the deadcolumn plugin
if pcall(require, "deadcolumn") then
require("deadcolumn").setup({ require("deadcolumn").setup({
scope = "line", ---@type string|fun(): integer scope = "line", ---@type string|fun(): integer
modes = function(mode) modes = function(mode)
@@ -138,6 +142,7 @@ function M.setup_indent_highlight()
follow_tw = "80", 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({