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
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*",
callback = function()
require("lazygit.utils").project_root_dir()
end,
})
if pcall(require, "lazygit.utils") then
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*",
callback = function()
require("lazygit.utils").project_root_dir()
end,
})
end
-- TodoFzfLua command override to use Telescope
-- when fzf-lua is not installed
@@ -75,43 +77,59 @@ autocmd({ "BufEnter" }, {
})
-- ━━━━━━━━━━━━━━━━━━━━━━━ Set up Qalc keymappings ━━━━━━━━━━━━━━━━━━━━━━━
-- Create a group for filetype-specific mappings
vim.api.nvim_create_augroup("QalcFileTypeMappings", { clear = true })
if pcall(require, "qalc") then
-- Create a group for filetype-specific mappings
vim.api.nvim_create_augroup("QalcFileTypeMappings", { clear = true })
-- Create an autocommand for the qalc filetype
vim.api.nvim_create_autocmd("FileType", {
pattern = "qalc",
group = "QalcFileTypeMappings",
callback = function()
-- Set the key mapping: 'y' to run the :QalcYank command in normal mode
vim.keymap.set( -- Yank Result
"n",
"y",
":QalcYank +<CR>",
{ noremap = true, silent = true }
)
-- Set the key mapping: 'q' to run the :QalcClose command in normal mode
vim.keymap.set( -- Close Qalc
"n",
"q",
":QalcClose<CR>",
{ noremap = true, silent = true }
)
end,
})
-- Create an autocommand for the qalc filetype
vim.api.nvim_create_autocmd("FileType", {
pattern = "qalc",
group = "QalcFileTypeMappings",
callback = function()
-- Set the key mapping: 'y' to run the :QalcYank command in normal mode
vim.keymap.set( -- Yank Result
"n",
"y",
":QalcYank +<CR>",
{ noremap = true, silent = true }
)
-- Set the key mapping: 'q' to run the :QalcClose command in normal mode
vim.keymap.set( -- Close Qalc
"n",
"q",
":QalcClose<CR>",
{ noremap = true, silent = true }
)
end,
})
-- Define a custom command to close the Qalc buffer
vim.api.nvim_create_user_command("QalcClose", function()
local buf_name = vim.api.nvim_buf_get_name(0)
if buf_name ~= "" then
vim.cmd("bd!")
else
-- If the buffer has no name, just remove it without invoking :bd!
vim.api.nvim_buf_delete(0, { force = true })
end
end, { bang = true })
-- Define a custom command to close the Qalc buffer
vim.api.nvim_create_user_command("QalcClose", function()
local buf_name = vim.api.nvim_buf_get_name(0)
if buf_name ~= "" then
vim.cmd("bd!")
else
-- If the buffer has no name, just remove it without invoking :bd!
vim.api.nvim_buf_delete(0, { force = true })
end
end, { bang = true })
end
-- ━━━━━━━━━━━━━━━━━━━━━━━━━━ Set up highlights ━━━━━━━━━━━━━━━━━━━━━━━━━━
local load_highlight = require("utils.highlight")
load_highlight.setup_autocommands()
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
-- 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 = {
options = { -- General options
@@ -147,16 +156,17 @@ return { -- Lualine
},
{ -- MultiCursors
function()
return data.func.mc_statusline().icon
return data.func.mc_statusline().count
.. data.func.mc_statusline().icon
end,
cond = function()
return data.func.mc_statusline().enabled
return data.func.mc_statusline().cursors > 1
end,
color = function()
return data.func.mc_statusline().color
end,
padding = { left = 0, right = 0 },
separator = { left = "", right = "" },
padding = { left = 1, right = 0 },
separator = { left = "", right = "" },
},
},
lualine_b = {
@@ -181,6 +191,7 @@ return { -- Lualine
end,
cond = function()
return funcs.is_window_wide_enough(100)
and pcall(require, "arrow")
end,
padding = { left = 1, right = 0 },
on_click = function()
@@ -322,6 +333,9 @@ return { -- Lualine
end,
padding = { left = 1, right = 1 },
cond = function()
if not pcall(require, "nvim_updater") then
return false
end
return not string.find(vim.bo.filetype, "neovim_updater_term")
end,
},
@@ -398,6 +412,9 @@ return { -- Lualine
lualine_z = {
{ -- Recording
require("recorder").recordingStatus,
cond = function()
return pcall(require, "recorder")
end,
color = "CurSearch",
separator = { left = "", right = "" },
},
+85 -69
View File
@@ -64,80 +64,85 @@ function M.setup_indent_highlight()
-- Get the foreground color of Error
local miniiconsred_fg_hex = get_fg_color("Error")
-- Load the ibl plugin hooks
local hooks = require("ibl.hooks")
-- create the highlight groups in the highlight setup hook
if cursorline_bg_hex then
-- Define custom highlight groups for indent
hooks.register(hooks.type.HIGHLIGHT_SETUP, function()
set_hl(
0,
"IndentBlanklineIndent",
{ fg = cursorline_bg_hex, nocombine = true }
)
set_hl(
0,
"IndentsScopeHighlight",
{ fg = miniiconsred_fg_hex, nocombine = true }
)
end)
end
-- Load the ibl plugin
if pcall(require, "ibl") then
-- Load the ibl plugin hooks
local hooks = require("ibl.hooks")
-- create the highlight groups in the highlight setup hook
if cursorline_bg_hex then
-- Define custom highlight groups for indent
hooks.register(hooks.type.HIGHLIGHT_SETUP, function()
set_hl(
0,
"IndentBlanklineIndent",
{ fg = cursorline_bg_hex, nocombine = true }
)
set_hl(
0,
"IndentsScopeHighlight",
{ fg = miniiconsred_fg_hex, nocombine = true }
)
end)
end
-- Define characters for indent-blankline
local tab_char, indent_char
if vim.g.tab_char then
tab_char = vim.g.tab_char
elseif vim.g.tab_char_name then
tab_char = data.types.ibl.char[vim.g.tab_char_name]
else
tab_char = data.types.ibl.char.none
end
if vim.g.indent_char then
indent_char = vim.g.indent_char
elseif vim.g.indent_char_name then
indent_char = data.types.ibl.char[vim.g.indent_char_name]
else
indent_char = data.types.ibl.char.none
end
-- Define characters for indent-blankline
local tab_char, indent_char
if vim.g.tab_char then
tab_char = vim.g.tab_char
elseif vim.g.tab_char_name then
tab_char = data.types.ibl.char[vim.g.tab_char_name]
else
tab_char = data.types.ibl.char.none
end
if vim.g.indent_char then
indent_char = vim.g.indent_char
elseif vim.g.indent_char_name then
indent_char = data.types.ibl.char[vim.g.indent_char_name]
else
indent_char = data.types.ibl.char.none
end
-- Configure the indent-blankline plugin
require("ibl").setup({
indent = {
char = indent_char,
tab_char = tab_char,
highlight = {
"IndentBlanklineIndent",
-- Configure the indent-blankline plugin
require("ibl").setup({
indent = {
char = indent_char,
tab_char = tab_char,
highlight = {
"IndentBlanklineIndent",
},
},
},
scope = {
enabled = false,
},
exclude = {
filetypes = data.types.highlights.exclude,
},
})
scope = {
enabled = false,
},
exclude = {
filetypes = data.types.highlights.exclude,
},
})
end
-- Configure the deadcolumn plugin
require("deadcolumn").setup({
scope = "line", ---@type string|fun(): integer
modes = function(mode)
return mode:find("^[ictRss\x13]") ~= nil
end,
blending = {
threshold = 0.9,
colorcode = cursorline_bg_hex,
hlgroup = { "Normal", "bg" },
},
warning = {
alpha = 0.4,
offset = 0,
colorcode = miniiconsred_fg_hex,
hlgroup = { "Error", "bg" },
},
extra = {
follow_tw = "80",
},
})
if pcall(require, "deadcolumn") then
require("deadcolumn").setup({
scope = "line", ---@type string|fun(): integer
modes = function(mode)
return mode:find("^[ictRss\x13]") ~= nil
end,
blending = {
threshold = 0.9,
colorcode = cursorline_bg_hex,
hlgroup = { "Normal", "bg" },
},
warning = {
alpha = 0.4,
offset = 0,
colorcode = miniiconsred_fg_hex,
hlgroup = { "Error", "bg" },
},
extra = {
follow_tw = "80",
},
})
end
-- Set NeoCodeium suggestion highlight
vim.api.nvim_set_hl(0, "NeoCodeiumSuggestion", { link = "Comment" })
@@ -223,6 +228,16 @@ function M.setup_avante_highlights()
set_hl(0, "AvantePopupHint", { link = "DiagnosticVirtualTextHint" })
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
---@return nil
function M.setup_autocommands()
@@ -243,6 +258,7 @@ function M.setup_autocommands()
M.setup_indent_highlight()
M.setup_minimap_highlight()
M.setup_avante_highlights()
M.setup_multi_cursor_highlight()
else
if pcall(require, "auto-cursorline") then
require("auto-cursorline").disable({