feat(lua):introduce yank highlight autocmd and improve secure_env + LSP configs

This commit is contained in:
sankalp committed 2026-05-21 15:03:49 +05:45
1 parent d826fec47e
commit 36ab130e5a
2 files changed
+309 -286

No files matched your search

+172 -149
View File
@@ -1,7 +1,16 @@
local function augroup(name) local function augroup(name)
return vim.api.nvim_create_augroup("nvim_" .. name, { clear = true }) return vim.api.nvim_create_augroup('nvim_' .. name, { clear = true })
end end
-- Yank highlight autocmd (built-in)
vim.api.nvim_create_autocmd('TextYankPost', {
group = vim.api.nvim_create_augroup('YankHighlight', { clear = true }),
pattern = '*',
callback = function()
vim.highlight.on_yank({ higroup = 'IncSearch', timeout = 150 })
end,
})
-- smart column only when needed -- smart column only when needed
local smart_column_group = vim.api.nvim_create_augroup('SmartColumn', { clear = true }) local smart_column_group = vim.api.nvim_create_augroup('SmartColumn', { clear = true })
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI', 'BufEnter' }, { vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI', 'BufEnter' }, {
@@ -25,197 +34,211 @@ vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI', 'BufEnter' }, {
}) })
-- Secure .env Masking -- Secure .env Masking
local env_ns = vim.api.nvim_create_namespace("secure_env") local env_ns = vim.api.nvim_create_namespace('secure_env')
local function toggle_env(buf) local function toggle_env(buf)
local is_mod = vim.bo[buf].modified local is_mod = vim.bo[buf].modified
vim.bo[buf].modifiable = true vim.bo[buf].modifiable = true
if vim.b[buf].env_cache then if vim.b[buf].env_cache then
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.b[buf].env_cache) vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.b[buf].env_cache)
vim.api.nvim_buf_clear_namespace(buf, env_ns, 0, -1) vim.api.nvim_buf_clear_namespace(buf, env_ns, 0, -1)
vim.b[buf].env_cache = nil vim.b[buf].env_cache = nil
else else
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
vim.b[buf].env_cache = vim.deepcopy(lines) vim.b[buf].env_cache = vim.deepcopy(lines)
for i, l in ipairs(lines) do for i, l in ipairs(lines) do
if not l:match("^%s*#") then if not l:match('^%s*#') then
lines[i] = l:gsub("(=%s*[\"']?)(.-)([\"']?%s*)$", function(p, v, s) lines[i] = l:gsub('(=%s*["\']?)(.-)(["\']?%s*)$', function(p, v, s)
return #v > 0 and (p .. string.rep("*", math.max(#v, 8)) .. s) or l return #v > 0 and (p .. string.rep('*', math.max(#v, 8)) .. s) or l
end) end)
end end
end end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.api.nvim_buf_set_extmark(buf, env_ns, 0, 0, { virt_text = {{" [Values Hidden]", "WarningMsg"}}, virt_text_pos = "eol" }) vim.api.nvim_buf_set_extmark(
vim.bo[buf].modifiable = false buf,
end env_ns,
vim.bo[buf].modified = is_mod 0,
0,
{ virt_text = { { ' [Values Hidden]', 'WarningMsg' } }, virt_text_pos = 'eol' }
)
vim.bo[buf].modifiable = false
end
vim.bo[buf].modified = is_mod
end end
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile", "BufWriteCmd" }, { vim.api.nvim_create_autocmd({ 'BufReadPost', 'BufNewFile', 'BufWriteCmd' }, {
group = augroup("secure_env"), group = augroup('secure_env'),
pattern = { ".env", ".env.*", "*.env" }, pattern = { '.env', '.env.*', '*.env' },
callback = function(args) callback = function(args)
local ev, buf = args.event, args.buf local ev, buf = args.event, args.buf
if ev == "BufReadPost" or ev == "BufNewFile" then if ev == 'BufReadPost' or ev == 'BufNewFile' then
vim.keymap.set("n", "<leader>et", function() toggle_env(buf) end, { buffer = buf, desc = "Toggle env" }) vim.keymap.set('n', '<leader>et', function()
if not vim.b[buf].env_cache then toggle_env(buf) end toggle_env(buf)
elseif ev == "BufWriteCmd" then end, { buffer = buf, desc = 'Toggle env' })
local file = vim.api.nvim_buf_get_name(buf) if not vim.b[buf].env_cache then
if file ~= "" then toggle_env(buf)
local lines = vim.b[buf].env_cache or vim.api.nvim_buf_get_lines(buf, 0, -1, false) end
if pcall(vim.fn.writefile, lines, file) then elseif ev == 'BufWriteCmd' then
vim.bo[buf].modified = false local file = vim.api.nvim_buf_get_name(buf)
else if file ~= '' then
vim.notify("Write failed!", vim.log.levels.ERROR) local lines = vim.b[buf].env_cache or vim.api.nvim_buf_get_lines(buf, 0, -1, false)
end if pcall(vim.fn.writefile, lines, file) then
end vim.bo[buf].modified = false
end else
end, vim.notify('Write failed!', vim.log.levels.ERROR)
end
end
end
end,
}) })
-- Strip trailing spaces before write -- Strip trailing spaces before write
vim.api.nvim_create_autocmd("BufWritePre", { vim.api.nvim_create_autocmd('BufWritePre', {
group = augroup("strip_space"), group = augroup('strip_space'),
pattern = "*", pattern = '*',
callback = function() callback = function()
local save = vim.fn.winsaveview() local save = vim.fn.winsaveview()
vim.cmd([[keeppatterns %s/\s\+$//e]]) vim.cmd([[keeppatterns %s/\s\+$//e]])
vim.fn.winrestview(save) vim.fn.winrestview(save)
end, end,
}) })
-- Resize splits if window got resized -- Resize splits if window got resized
vim.api.nvim_create_autocmd("VimResized", { vim.api.nvim_create_autocmd('VimResized', {
group = augroup("resize_splits"), group = augroup('resize_splits'),
callback = function() callback = function()
vim.cmd("tabdo wincmd =") vim.cmd('tabdo wincmd =')
end, end,
}) })
-- Close specific filetypes with <q> -- Close specific filetypes with <q>
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd('FileType', {
group = augroup("close_with_q"), group = augroup('close_with_q'),
pattern = { pattern = {
"help", "lspinfo", "man", "notify", "qf", "query", "spectre_panel", 'help',
"startuptime", "tsplayground", "neotest-output", "checkhealth", 'lspinfo',
}, 'man',
callback = function(event) 'notify',
vim.bo[event.buf].buflisted = false 'qf',
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true }) 'query',
end, 'spectre_panel',
'startuptime',
'tsplayground',
'neotest-output',
'checkhealth',
},
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set('n', 'q', '<cmd>close<cr>', { buffer = event.buf, silent = true })
end,
}) })
-- Auto-create directories on save -- Auto-create directories on save
vim.api.nvim_create_autocmd("BufWritePre", { vim.api.nvim_create_autocmd('BufWritePre', {
group = augroup("auto_create_dir"), group = augroup('auto_create_dir'),
callback = function(event) callback = function(event)
if event.match:match("^%w%w+://") then if event.match:match('^%w%w+://') then
return return
end end
local file = vim.loop.fs_realpath(event.match) or event.match local file = vim.loop.fs_realpath(event.match) or event.match
vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p") vim.fn.mkdir(vim.fn.fnamemodify(file, ':p:h'), 'p')
end, end,
}) })
-- LSP Attach Features -- LSP Attach Features
vim.api.nvim_create_autocmd("LspAttach", { vim.api.nvim_create_autocmd('LspAttach', {
group = augroup("lsp_attach_autocmds"), group = augroup('lsp_attach_autocmds'),
callback = function(args) callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id) local client = vim.lsp.get_client_by_id(args.data.client_id)
-- Document Highlight
if client and client:supports_method("textDocument/documentHighlight") then
vim.api.nvim_create_autocmd({ "CursorHold" }, {
buffer = args.buf,
callback = vim.lsp.buf.document_highlight,
})
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
buffer = args.buf,
callback = vim.lsp.buf.clear_references,
})
end
-- Inlay Hints -- Document Highlight
if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then if client and client:supports_method('textDocument/documentHighlight') then
vim.lsp.inlay_hint.enable(true, { bufnr = args.buf }) vim.api.nvim_create_autocmd({ 'CursorHold' }, {
end buffer = args.buf,
end, callback = vim.lsp.buf.document_highlight,
})
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
buffer = args.buf,
callback = vim.lsp.buf.clear_references,
})
end
-- Inlay Hints
if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
vim.lsp.inlay_hint.enable(true, { bufnr = args.buf })
end
end,
}) })
-- Wrap and spellcheck in text filetypes -- Wrap and spellcheck in text filetypes
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd('FileType', {
group = augroup("wrap_spell"), group = augroup('wrap_spell'),
pattern = { "gitcommit", "markdown", "text" }, pattern = { 'gitcommit', 'markdown', 'text' },
callback = function() callback = function()
vim.opt_local.wrap = true vim.opt_local.wrap = true
vim.opt_local.spell = true vim.opt_local.spell = true
end, end,
}) })
-- Disable auto-commenting on new lines -- Disable auto-commenting on new lines
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd('FileType', {
group = augroup("no_auto_comment"), group = augroup('no_auto_comment'),
callback = function() callback = function()
vim.opt_local.formatoptions:remove({ "c", "r", "o" }) vim.opt_local.formatoptions:remove({ 'c', 'r', 'o' })
end, end,
}) })
-- Manual Treesitter Installation command -- Manual Treesitter Installation command
vim.api.nvim_create_user_command("TSInstallAll", function() vim.api.nvim_create_user_command('TSInstallAll', function()
local parsers = require("extensions").ts_parsers local parsers = require('extensions').ts_parsers
vim.notify("Installing all configured Tree-sitter parsers...", vim.log.levels.INFO, { title = "Tree-sitter" }) vim.notify('Installing all configured Tree-sitter parsers...', vim.log.levels.INFO, { title = 'Tree-sitter' })
require("nvim-treesitter.install").install(parsers) require('nvim-treesitter.install').install(parsers)
end, { desc = "Install all configured Tree-sitter parsers" }) end, { desc = 'Install all configured Tree-sitter parsers' })
-- Macro Notifications -- Macro Notifications
vim.api.nvim_create_autocmd("RecordingEnter", { vim.api.nvim_create_autocmd('RecordingEnter', {
callback = function() callback = function()
vim.notify("Recording macro...", vim.log.levels.INFO, { title = "Macro", timeout = 1000 }) vim.notify('Recording macro...', vim.log.levels.INFO, { title = 'Macro', timeout = 1000 })
end, end,
}) })
vim.api.nvim_create_autocmd("RecordingLeave", { vim.api.nvim_create_autocmd('RecordingLeave', {
callback = function() callback = function()
local reg = vim.v.recording local reg = vim.v.recording
vim.notify("Stopped recording @" .. reg, vim.log.levels.INFO, { title = "Macro", timeout = 1000 }) vim.notify('Stopped recording @' .. reg, vim.log.levels.INFO, { title = 'Macro', timeout = 1000 })
end, end,
}) })
-- Auto-Save on InsertLeave with 3s Debounce -- Auto-Save on InsertLeave with 3s Debounce
local save_timer = nil local save_timer = nil
local function clear_timer() local function clear_timer()
if save_timer then if save_timer then
save_timer:stop() save_timer:stop()
save_timer = nil save_timer = nil
end end
end end
vim.api.nvim_create_autocmd({ "InsertEnter" }, { vim.api.nvim_create_autocmd({ 'InsertEnter' }, {
group = augroup("autosave_cancel"), group = augroup('autosave_cancel'),
callback = clear_timer, callback = clear_timer,
}) })
vim.api.nvim_create_autocmd({ "InsertLeave" }, { vim.api.nvim_create_autocmd({ 'InsertLeave' }, {
group = augroup("autosave"), group = augroup('autosave'),
callback = function(args) callback = function(args)
clear_timer() clear_timer()
if vim.api.nvim_buf_is_valid(args.buf) and vim.bo[args.buf].modified and vim.bo[args.buf].buftype == "" then if vim.api.nvim_buf_is_valid(args.buf) and vim.bo[args.buf].modified and vim.bo[args.buf].buftype == '' then
save_timer = vim.defer_fn(function() save_timer = vim.defer_fn(function()
if vim.api.nvim_buf_is_valid(args.buf) and vim.bo[args.buf].modified then if vim.api.nvim_buf_is_valid(args.buf) and vim.bo[args.buf].modified then
vim.api.nvim_buf_call(args.buf, function() vim.api.nvim_buf_call(args.buf, function()
vim.cmd("silent! write") vim.cmd('silent! write')
vim.notify("Autosaved " .. vim.fn.expand("%:t"), vim.log.levels.INFO, { title = "Auto-Save" }) vim.notify('Autosaved ' .. vim.fn.expand('%:t'), vim.log.levels.INFO, { title = 'Auto-Save' })
end) end)
end end
end, 3000) end, 3000)
end end
end, end,
}) })
-- Autoinstall missing plugins on startup -- Autoinstall missing plugins on startup
+137 -137
View File
@@ -2,152 +2,152 @@
-- LSP -- LSP
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
vim.api.nvim_create_autocmd("User", { vim.api.nvim_create_autocmd('User', {
pattern = "PackerComplete", -- Fallback or custom event if needed, but let's use a more standard one pattern = 'PackerComplete', -- Fallback or custom event if needed, but let's use a more standard one
callback = function() callback = function()
-- This is where we would put it if we had a specific event. -- This is where we would put it if we had a specific event.
-- Since we're using VimEnter, let's use that instead. -- Since we're using VimEnter, let's use that instead.
end, end,
}) })
-- Let's use a simple function and schedule it to run after plugins -- Let's use a simple function and schedule it to run after plugins
vim.schedule(function() vim.schedule(function()
vim.lsp.config("*", { vim.lsp.config('*', {
capabilities = require("blink.cmp").get_lsp_capabilities(), capabilities = require('blink.cmp').get_lsp_capabilities(),
}) })
-- ts_ls -- ts_ls
vim.lsp.config("ts_ls", { vim.lsp.config('ts_ls', {
settings = { settings = {
typescript = { typescript = {
inlayHints = { inlayHints = {
includeInlayParameterNameHints = "all", includeInlayParameterNameHints = 'all',
includeInlayParameterNameHintsWhenArgumentMatchesName = false, includeInlayParameterNameHintsWhenArgumentMatchesName = false,
includeInlayFunctionParameterTypeHints = true, includeInlayFunctionParameterTypeHints = true,
includeInlayVariableTypeHints = true, includeInlayVariableTypeHints = true,
includeInlayPropertyDeclarationTypeHints = true, includeInlayPropertyDeclarationTypeHints = true,
includeInlayFunctionLikeReturnTypeHints = true, includeInlayFunctionLikeReturnTypeHints = true,
includeInlayEnumMemberValueHints = true, includeInlayEnumMemberValueHints = true,
}, },
}, },
}, },
}) })
vim.lsp.enable("ts_ls") vim.lsp.enable('ts_ls')
vim.lsp.enable("html") vim.lsp.enable('html')
vim.lsp.enable("cssls") vim.lsp.enable('cssls')
vim.lsp.config("tailwindcss", { vim.lsp.config('tailwindcss', {
filetypes = { filetypes = {
"astro", 'astro',
"blade", 'blade',
"edge", 'edge',
"ejs", 'ejs',
"erb", 'erb',
"gohtml", 'gohtml',
"gohtmltmpl", 'gohtmltmpl',
"handlebars", 'handlebars',
"hbs", 'hbs',
"html", 'html',
"jade", 'jade',
"leaf", 'leaf',
"liquid", 'liquid',
"markdown", 'markdown',
"mdx", 'mdx',
"mustache", 'mustache',
"nunjucks", 'nunjucks',
"php", 'php',
"razor", 'razor',
"slim", 'slim',
"twig", 'twig',
"css", 'css',
"less", 'less',
"postcss", 'postcss',
"sass", 'sass',
"scss", 'scss',
"stylus", 'stylus',
"sugarss", 'sugarss',
"javascript", 'javascript',
"javascriptreact", 'javascriptreact',
"rescript", 'rescript',
"typescript", 'typescript',
"typescriptreact", 'typescriptreact',
"vue", 'vue',
"svelte", 'svelte',
"templ", 'templ',
}, },
}) })
vim.lsp.enable("tailwindcss") vim.lsp.enable('tailwindcss')
-- rust -- rust
vim.lsp.config("rust_analyzer", { vim.lsp.config('rust_analyzer', {
settings = { settings = {
["rust-analyzer"] = { ['rust-analyzer'] = {
diagnostics = { diagnostics = {
enable = true, enable = true,
experimental = { enable = true }, experimental = { enable = true },
}, },
}, },
}, },
}) })
vim.lsp.enable("rust_analyzer") vim.lsp.enable('rust_analyzer')
-- lua -- lua
vim.lsp.config("lua_ls", { vim.lsp.config('lua_ls', {
settings = { settings = {
Lua = { Lua = {
diagnostics = { diagnostics = {
disable = { disable = {
"missing-fields", 'missing-fields',
"undefined-global", 'undefined-global',
}, },
}, },
}, },
}, },
}) })
vim.lsp.enable("lua_ls") vim.lsp.enable('lua_ls')
-- python -- python
vim.lsp.config("basedpyright", { vim.lsp.config('basedpyright', {
settings = { settings = {
basedpyright = { basedpyright = {
inlayHints = true, inlayHints = true,
analysis = { analysis = {
diagnosticSeverityOverrides = { diagnosticSeverityOverrides = {
reportUnusedImport = "none", reportUnusedImport = 'none',
reportUnusedVariable = "none", reportUnusedVariable = 'none',
reportAttributeAccessIssue = "none", reportAttributeAccessIssue = 'none',
reportUnusedClass = "none", reportUnusedClass = 'none',
reportUnusedFunction = "none", reportUnusedFunction = 'none',
reportDuplicateImport = "none", reportDuplicateImport = 'none',
reportArgumentType = "error", reportArgumentType = 'error',
}, },
typeCheckingMode = "standard", typeCheckingMode = 'standard',
}, },
}, },
}, },
}) })
vim.lsp.enable("basedpyright") vim.lsp.enable('basedpyright')
-- bash -- bash
vim.lsp.config("bashls", { vim.lsp.config('bashls', {
settings = { settings = {
bashIde = { bashIde = {
shellcheckArguments = { shellcheckArguments = {
"--exclude=SC1090,SC1091,SC2076,SC2164", '--exclude=SC1090,SC1091,SC2076,SC2164',
}, },
}, },
}, },
}) })
vim.lsp.enable("bashls") vim.lsp.enable('bashls')
-- cpp -- cpp
vim.lsp.config("clangd", { vim.lsp.config('clangd', {
cmd = { cmd = {
"clangd", 'clangd',
"--fallback-style=llvm", '--fallback-style=llvm',
"--header-insertion=iwyu", '--header-insertion=iwyu',
"-j=4", '-j=4',
}, },
}) })
vim.lsp.enable("clangd") vim.lsp.enable('clangd')
end) end)