refactor: restructure core modules into config directory for better organization

This commit is contained in:
sankalp committed 2025-10-17 15:33:11 +05:45
1 parent 16e50cdc3a
commit d3592bd80a
29 files changed
+1751 -1924

No files matched your search

+187
View File
@@ -0,0 +1,187 @@
local function augroup(name)
return vim.api.nvim_create_augroup("nvim" .. name, { clear = true })
end
-- Strip trailing spaces before write
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
group = augroup("strip_space"),
pattern = { "*" },
callback = function()
vim.cmd([[ %s/\s\+$//e ]])
end,
})
-- Check if we need to reload the file when it changed
vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
group = augroup("checktime"),
command = "checktime",
})
-- Highlight on yank
vim.api.nvim_create_autocmd("TextYankPost", {
group = augroup("highlight_yank"),
callback = function()
vim.highlight.on_yank()
end,
})
-- resize splits if window got resized
vim.api.nvim_create_autocmd({ "VimResized" }, {
group = augroup("resize_splits"),
callback = function()
vim.cmd("tabdo wincmd =")
end,
})
-- go to last loc when opening a buffer
vim.api.nvim_create_autocmd("BufReadPost", {
group = augroup("last_loc"),
callback = function()
local mark = vim.api.nvim_buf_get_mark(0, '"')
local lcount = vim.api.nvim_buf_line_count(0)
if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark)
end
end,
})
-- close some filetypes with <q>
vim.api.nvim_create_autocmd("FileType", {
group = augroup("close_with_q"),
pattern = {
"Jaq",
"PlenaryTestPopup",
"Avante",
"AvanteInput",
"AvanteSelectedFiles",
"fugitive",
"git",
"help",
"lir",
"lspinfo",
"man",
"netrw",
"notify",
"qf",
"spectre_panel",
"startuptime",
"tsplayground",
},
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
end,
})
-- wrap and check for spell in text filetypes
vim.api.nvim_create_autocmd("FileType", {
group = augroup("wrap_spell"),
pattern = { "gitcommit", "markdown" },
callback = function()
vim.opt_local.wrap = true
vim.opt_local.spell = true
end,
})
-- Auto create dir when saving a file, in case some intermediate directory does not exist
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
group = augroup("auto_create_dir"),
callback = function(event)
local file = vim.loop.fs_realpath(event.match) or event.match
vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
end,
})
-- Set arb filetype
vim.api.nvim_create_autocmd({ "BufEnter" }, {
group = augroup("set_file_type"),
pattern = { "*.arb" },
command = "setfiletype arb",
})
-- Disable format options
vim.api.nvim_create_autocmd("FileType", {
group = augroup("disable_formatoptions"),
pattern = "*",
callback = function()
vim.opt_local.formatoptions:remove({ "c", "r", "o" })
end,
})
local function enable_autoformat()
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
group = augroup("autoformat"),
pattern = { "*" },
callback = function()
vim.lsp.buf.format()
end,
})
end
enable_autoformat()
vim.api.nvim_create_user_command("WriteNoFormat", function()
-- Temporarily disable the autoformat autocmd
vim.api.nvim_del_augroup_by_name("keycodex_autoformat")
vim.cmd("write")
-- Re-enable the autoformat autocmd
enable_autoformat()
end, {})
-- Macro recording notifications
vim.api.nvim_create_autocmd("RecordingEnter", {
callback = function()
vim.notify("Macro recording started", vim.log.levels.INFO, {
title = "Macro",
timeout = 2000,
})
-- Print to command line for debugging
vim.cmd("echo 'Recording macro...'")
end,
})
vim.api.nvim_create_autocmd("RecordingLeave", {
callback = function()
vim.notify("Macro recording stopped", vim.log.levels.INFO, {
title = "Macro",
timeout = 2000,
})
-- Print to command line for debugging
vim.cmd("echo 'Macro recording stopped'")
end,
})
-- Function to show macro replay notification
local function show_macro_replay(count)
local message = count > 1 and string.format("Replaying macro %d times", count) or "Replaying macro"
vim.notify(message, vim.log.levels.INFO, {
title = "Macro",
timeout = 2000,
})
end
-- Function to clear all macros
local function clear_all_macros()
-- Clear all registers from a to z
for i = 97, 122 do -- ASCII values for 'a' to 'z'
vim.fn.setreg(string.char(i), "")
end
-- Clear all registers from 0 to 9
for i = 48, 57 do -- ASCII values for '0' to '9'
vim.fn.setreg(string.char(i), "")
end
vim.notify("All macros cleared", vim.log.levels.INFO, {
title = "Macro",
timeout = 2000,
})
end
-- Create keymaps for macro replay with notifications
vim.keymap.set("n", "@", function()
local count = vim.v.count1
show_macro_replay(count)
return "@"
end, { expr = true })
-- Add keybinding to clear all macros
vim.keymap.set("n", "<leader>mc", clear_all_macros, { desc = "Clear all macros" })
+66
View File
@@ -0,0 +1,66 @@
local treesitter_parsers = {
"c",
"markdown",
"python",
"java",
"cpp",
"bash",
"lua",
"make",
"json",
"jsonc",
"sql",
"yaml",
"toml",
"javascript",
"typescript",
"gitignore",
"go",
"rust",
"html",
"css",
"scss",
"markdown_inline",
}
local lsp_server = {
"ts_ls",
"html",
"cssls",
"tailwindcss",
"lua_ls",
"basedpyright",
"bashls",
"clangd",
"rust_analyzer",
}
local formatter = {
"clang_format",
"ruff",
"stylua",
"prettier",
"markdown-toc",
"markdownlint-cli2",
"biome",
}
local linter = {
"eslint_d",
"hadolint",
"write_good",
"markdownlint",
"shellcheck",
"actionlint",
"djlint",
"ruff",
"codespell",
"cspell",
}
return {
ts_parsers = treesitter_parsers,
lsp_server = lsp_server,
formatter = formatter,
linter = linter,
}
+140
View File
@@ -0,0 +1,140 @@
local function map(mode, lhs, rhs, opts)
opts = opts or {}
opts.silent = opts.silent ~= false
vim.keymap.set(mode, lhs, rhs, opts)
end
local opts = { noremap = true, silent = true }
-- Space as leader
map("n", "<Space>", "", opts)
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- Modes
-- normal_mode = "n", insert_mode = "i", visual_mode = "v",
-- visual_block_mode = "x", term_mode = "t", command_mode = "c",
-- Terminal Mappings
map("t", "jk", "<C-\\><C-n>")
map("t", "kj", "<C-\\><C-n>")
map("t", "<ESC>", "<C-\\><C-n>")
-- Clear search, diff update and redraw
map({ "i", "n" }, "<esc>", "<cmd>noh<cr><esc>", { desc = "Escape and clear hlsearch" })
map({ "i", "c" }, "<C-b>", "<Left>")
map({ "i", "c" }, "<C-f>", "<Right>")
map("n", "<leader>rL", vim.lsp.buf.rename, { desc = "Rename Lsp Symbol" })
map("n", "`", "'", { noremap = true }) -- better marks
map({ "n", "v" }, "H", "^", { desc = "Move to the beginning of the line" })
map({ "n", "v" }, "L", "$", { desc = "Move to the end of the line" })
-- Visual overwrite paste
map({ "v", "x" }, "p", '"_dP', opts)
-- Do not copy on x
map({ "v", "x" }, "x", '"_x', opts)
map("n", "X", '"_D', opts)
map("n", "dd", '"_dd', opts)
map({ "v", "x" }, "X", '"_d', opts)
-- Select all
map("n", "<C-a>", "ggVG", { desc = "Select all" })
map("i", "<C-a>", "<Esc>ggVG", { desc = "Select all" })
-- Move text up and down
map({ "v", "x" }, "K", ":move '<-2<cr>gv-gv", opts)
map({ "v", "x" }, "J", ":move '>+1<cr>gv-gv", opts)
-- Line operations
map("n", "<A-k>", ":m .-2<CR>==", { desc = "Move line up" })
map("n", "<A-j>", ":m .+1<CR>==", { desc = "Move line down" })
map("i", "<A-k>", "<Esc>:m .-2<CR>==gi", { desc = "Move line up" })
map("i", "<A-j>", "<Esc>:m .+1<CR>==gi", { desc = "Move line down" })
map("v", "<A-k>", ":m '<-2<CR>gv=gv", { desc = "Move selection up" })
map("v", "<A-j>", ":m '>+1<CR>gv=gv", { desc = "Move selection down" })
-- Line manipulation
map("n", "<C-]>", ">>", { desc = "Indent line" })
map("n", "<C-[>", "<<", { desc = "Unindent line" })
map("i", "<C-]>", "<C-o>>>", { desc = "Indent line" })
map("i", "<C-[>", "<C-o><<", { desc = "Unindent line" })
-- Quick word operations
map("n", "<C-BS>", "db", { desc = "Delete word backward" })
map("i", "<C-BS>", "<C-w>", { desc = "Delete word backward" })
map("n", "<C-Del>", "dw", { desc = "Delete word forward" })
map("i", "<C-Del>", "<C-o>dw", { desc = "Delete word forward" })
map("n", "d", '"_d', { desc = "Delete without replacing clipboard" })
map("n", "x", '"_x', { desc = "Cut without replacing clipboard" })
map("n", "c", '"_c', { desc = "Change without replacing clipboard" })
map("v", "p", '"_dP', { desc = "Paste without replacing clipboard" })
--insert the following characters around the visual selection
map("v", "<leader>(", "s()<esc>Pll", { desc = "wrap selection with parens" })
map("v", "<leader>[", "s[]<esc>Pll", { desc = "wrap selection with square braces" })
map("v", "<leader>{", "s{}<esc>Pll", { desc = "wrap selection with curly braces" })
map("v", "<leader>'", "s''<esc>Pll", { desc = "wrap selection with single quotes" })
map("v", '<leader>"', 's""<esc>Pll', { desc = "wrap selection with double quotes" })
-- Insert mode cursor movement with Ctrl+hjkl
map({ "i", "c" }, "<C-h>", "<Left>", { noremap = true, silent = true })
map({ "i", "c" }, "<C-j>", "<Down>", { noremap = true, silent = true })
map({ "i", "c" }, "<C-k>", "<Up>", { noremap = true, silent = true })
map({ "i", "c" }, "<C-l>", "<Right>", { noremap = true, silent = true })
map("n", "<leader>re", ":%s/<C-R><C-W>/", { desc = "Shortcut to replace current word under cursor" })
-- toggle inlay hints
vim.keymap.set("n", "<leader>th", function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
end)
-- No arrow navigation
local modes = { "n", "v", "i" }
local arrows = { "<Up>", "<Down>", "<Left>", "<Right>" }
for _, mode in ipairs(modes) do
for _, key in ipairs(arrows) do
vim.api.nvim_set_keymap(mode, key, "<Nop>", opts)
end
end
-- Go to Beginning or End of line
map({ "n", "v" }, "H", "_^", { desc = "Move to the beginning of the line" })
map({ "n", "v" }, "L", "$", { desc = "Move to the end of the line" })
-- Search inside visually highlighted text.
map("x", "g/", "<esc>/\\%V", { silent = false, desc = "Search inside visual selection" })
-- Search & replace in current file/line
map("v", "<leader>rw", '"9y:%s@<c-r>9@<c-r>9@g<left><left>', { desc = "Search & replace selection", silent = false })
map("n", "<leader>rw", 'viw"9y:%s@<c-r>9@<c-r>9@g<left><left>', { desc = "Search & replace word", silent = false })
map(
"v",
"<leader>rl",
'"9y:s@<c-r>9@<c-r>9@g<left><left>',
{ desc = "Search & replace in current line", silent = false }
)
map(
"n",
"<leader>rl",
'viw"9y:s@<c-r>9@<c-r>9@g<left><left>',
{ desc = "Search & replace word in current line", silent = false }
)
-- Folds
map("n", "<leader><", function()
if not vim.opt.foldmethod then
vim.opt.foldmethod = "indent"
end
vim.cmd("normal! zM")
end, { desc = "Fold all" })
map("n", "<leader>>", function()
vim.opt.foldenable = false
vim.cmd("normal! zR")
end, { desc = "Open all folds" })
+37
View File
@@ -0,0 +1,37 @@
--------------------------------------------------------------------------------
-- Bootstrap lazy plugin manager
--------------------------------------------------------------------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)
-- lazy config
require("lazy").setup({
spec = {
{ import = "plugins" },
},
install = {
missing = true,
colorscheme = { "gruvbox" },
},
change_detection = {
enable = true,
notify = false,
},
ui = {
border = "rounded",
backdrop = 100,
},
})
+100
View File
@@ -0,0 +1,100 @@
--------------------------------------------------------------------------------
-- LSP
--------------------------------------------------------------------------------
-- ts_ls
vim.lsp.config("ts_ls", {
settings = {
typescript = {
inlayHints = {
includeInlayParameterNameHints = "all",
includeInlayParameterNameHintsWhenArgumentMatchesName = false,
includeInlayFunctionParameterTypeHints = true,
includeInlayVariableTypeHints = true,
includeInlayPropertyDeclarationTypeHints = true,
includeInlayFunctionLikeReturnTypeHints = true,
includeInlayEnumMemberValueHints = true,
},
},
},
})
vim.lsp.enable("ts_ls")
vim.lsp.enable("html")
vim.lsp.enable("cssls")
vim.lsp.enable("tailwindcss")
-- rust
vim.lsp.config("rust_analyzer", {
settings = {
["rust-analyzer"] = {
diagnostics = {
enable = true,
experimental = { enable = true },
},
},
},
})
vim.lsp.enable("rust_analyzer")
-- lua
vim.lsp.config("lua_ls", {
settings = {
Lua = {
diagnostics = {
disable = {
"missing-fields",
"undefined-global",
},
},
},
},
})
vim.lsp.enable("lua_ls")
-- python
vim.lsp.config("basedpyright", {
settings = {
basedpyright = {
inlayHints = true,
analysis = {
diagnosticSeverityOverrides = {
reportUnusedImport = "none",
reportUnusedVariable = "none",
reportAttributeAccessIssue = "none",
reportUnusedClass = "none",
reportUnusedFunction = "none",
reportDuplicateImport = "none",
reportArgumentType = "error",
},
typeCheckingMode = "standard",
},
},
},
})
vim.lsp.enable("basedpyright")
-- bash
vim.lsp.config("bashls", {
settings = {
bashIde = {
shellcheckArguments = {
"--exclude=SC1090,SC1091,SC2076,SC2164",
},
},
},
})
vim.lsp.enable("bashls")
-- cpp
vim.lsp.config("clangd", {
cmd = {
"clangd",
"--fallback-style=llvm",
"--header-insertion=iwyu",
"-j=4",
},
})
vim.lsp.enable("clangd")
+67
View File
@@ -0,0 +1,67 @@
local options = {
ai = true,
clipboard = "unnamedplus", -- use system clipboard
number = true,
relativenumber = true,
undofile = true, -- save state of file on write
undodir = vim.fn.stdpath("cache") .. "/undo",
undolevels = 10000,
autoread = true, -- autoread changes from other sources
signcolumn = "yes",
cursorline = true,
cursorlineopt = "number",
wrap = false,
scrolloff = 3,
ruler = false, -- removes cursor position from lastline
pumheight = 10, -- size of completion window
showmode = false, -- do not show mode under statusline
swapfile = false, -- creates a swapfile
backup = false, -- creates a backup file
writebackup = false, -- do not edit backups
winminwidth = 5, -- Minimum window width
termguicolors = true, -- set term gui colors (most terminals support this)
showtabline = 0, -- always show tabs
showcmd = false,
mouse = "a", -- allow the mouse to be used in neovim
confirm = true, -- Confirm to save changes before exiting modified buffer
-- tabs
smarttab = true,
tabstop = 4, -- 1 tab represented as 4 spaces
expandtab = true, -- <tab> key will create " " instead of "\t"
shiftwidth = 4, -- indent change after backspace and >> <<
softtabstop = 4, -- number of spaces instead of tab
autoindent = true, -- auto indent
smartindent = true, -- make indenting smarter again
list = true,
-- listchars = { trail = "", tab = "", nbsp = "_", extends = ">", precedes = "<" }, -- highlight
showbreak = string.rep(" ", 3),
-- Search
ignorecase = true,
smartcase = true,
hlsearch = true,
wildmenu = true,
wildmode = "longest:full,full",
wildoptions = "pum",
-- Window
splitright = true,
splitbelow = true,
title = true,
}
for k, v in pairs(options) do
vim.opt[k] = v
end
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.opt.path:append({ "**" })
vim.opt.shortmess:append({ W = true, I = true, c = true }) -- disable greeting
-- Fix markdown indentation settings
vim.g.markdown_recommended_style = 0
vim.cmd([[set fillchars+=eob:\ ]])