(nvim) tidy up nvim config

This commit is contained in:
jc 2024-08-20 14:45:43 -04:00
commit c6ea262073
2 changed files with 189 additions and 213 deletions

View file

@ -3,22 +3,22 @@ local M = {}
---@class ConfigOptions ---@class ConfigOptions
local defaultOpts = { local defaultOpts = {
---@type string | fun() ---@type string | fun()
colorscheme = "rose-pine", colorscheme = "rose-pine",
} }
vim.g.disable_autoformat = false vim.g.disable_autoformat = false
---@param name "options" | "keymaps" | "clipboard" ---@param name "options" | "keymaps" | "clipboard"
function M.load(name) function M.load(name)
local mod = "juancwu.config." .. name local mod = "juancwu.config." .. name
local error_handler = function(err) local error_handler = function(err)
local msg = "Failed loading " .. mod .. "\n\n" .. err local msg = "Failed loading " .. mod .. "\n\n" .. err
print(msg) print(msg)
end end
xpcall(function() xpcall(function()
require(mod) require(mod)
end, error_handler) end, error_handler)
end end
---@type ConfigOptions ---@type ConfigOptions
@ -26,55 +26,55 @@ local options
---@param opts? ConfigOptions ---@param opts? ConfigOptions
function M.setup(opts) function M.setup(opts)
options = vim.tbl_deep_extend("force", defaultOpts, opts or {}) or {} options = vim.tbl_deep_extend("force", defaultOpts, opts or {}) or {}
M.load("options") M.load("options")
M.load("keymaps") M.load("keymaps")
M.load("clipboard") M.load("clipboard")
require("lazy").setup("juancwu.plugins") require("lazy").setup("juancwu.plugins")
-- try to load colorscheme -- try to load colorscheme
xpcall(function() xpcall(function()
if type(M.colorscheme) == "function" then if type(M.colorscheme) == "function" then
M.colorscheme() M.colorscheme()
else else
vim.cmd.colorscheme(M.colorscheme) vim.cmd.colorscheme(M.colorscheme)
end end
end, function(err) end, function(err)
if type(M.colorscheme) == "string" then if type(M.colorscheme) == "string" then
local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err
print(msg) print(msg)
else else
print("Failed to load colorscheme\n\n" .. err) print("Failed to load colorscheme\n\n" .. err)
end end
vim.cmd.colorscheme("rose-pine") vim.cmd.colorscheme("rose-pine")
end) end)
-- create command to disable autoformat -- create command to disable autoformat
vim.api.nvim_create_user_command("FormatDisable", function(args) vim.api.nvim_create_user_command("FormatDisable", function(args)
vim.g.disable_autoformat = true vim.g.disable_autoformat = true
end, { desc = "Disable Autoformat" }) end, { desc = "Disable Autoformat" })
vim.api.nvim_create_user_command("FormatEnable", function(args) vim.api.nvim_create_user_command("FormatEnable", function(args)
vim.g.disable_autoformat = false vim.g.disable_autoformat = false
end, { desc = "Enable Autoformat" }) end, { desc = "Enable Autoformat" })
-- create command to toggle colorscheme -- create command to toggle colorscheme
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
"ToggleColors", "ToggleColors",
require("juancwu.utils.colors").toggle_colors, require("juancwu.utils.colors").toggle_colors,
{ desc = "Toggle colorscheme" } { desc = "Toggle colorscheme" }
) )
end end
setmetatable(M, { setmetatable(M, {
__index = function(_, k) __index = function(_, k)
if options == nil then if options == nil then
return vim.deepcopy(defaultOpts)[k] return vim.deepcopy(defaultOpts)[k]
end end
return options[k] return options[k]
end, end,
}) })
return M return M

View file

@ -1,187 +1,163 @@
return { return {
-- lspconfig -- lspconfig
{ {
"VonHeikemen/lsp-zero.nvim", "VonHeikemen/lsp-zero.nvim",
branch = "v2.x", branch = "v2.x",
dependencies = { dependencies = {
-- LSP Support -- LSP Support
{ "neovim/nvim-lspconfig" }, -- Required { "neovim/nvim-lspconfig" }, -- Required
{ {
-- Optional -- Optional
"williamboman/mason.nvim", "williamboman/mason.nvim",
build = function() build = function()
pcall(vim.cmd, "MasonUpdate") pcall(vim.cmd, "MasonUpdate")
end, end,
}, },
{ "williamboman/mason-lspconfig.nvim" }, -- Optional { "williamboman/mason-lspconfig.nvim" }, -- Optional
-- Autocompletion -- Autocompletion
{ "hrsh7th/nvim-cmp" }, -- Required { "hrsh7th/nvim-cmp" }, -- Required
{ "hrsh7th/cmp-nvim-lsp" }, -- Required { "hrsh7th/cmp-nvim-lsp" }, -- Required
{ "L3MON4D3/LuaSnip" }, -- Required { "L3MON4D3/LuaSnip" }, -- Required
-- Neovim Plugin Development Completions -- Neovim Plugin Development Completions
{ {
"folke/neodev.nvim", "folke/neodev.nvim",
opts = {}, opts = {},
}, },
-- formatting -- formatting
{ {
"stevearc/conform.nvim", "stevearc/conform.nvim",
config = function() config = function()
require("conform").setup({ require("conform").setup({
formatters_by_ft = { formatters_by_ft = {
lua = { "stylua" }, lua = { "stylua" },
javascript = { "prettier" }, javascript = { "prettier" },
typescript = { "prettier" }, typescript = { "prettier" },
javascriptreact = { "prettier" }, javascriptreact = { "prettier" },
typescriptreact = { "prettier" }, typescriptreact = { "prettier" },
markdown = { "prettier" }, markdown = { "prettier" },
go = { "gofumpt" }, go = { "gofumpt" },
python = { "autopep8" }, python = { "autopep8" },
yaml = { "yamlfmt" }, yaml = { "yamlfmt" },
}, },
format_on_save = function(bufnr) format_on_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return return
end end
return { timeout_ms = 500, lsp_fallback = true } return { timeout_ms = 500, lsp_fallback = true }
end, end,
}) })
vim.api.nvim_create_user_command("FormatDisable", function() vim.api.nvim_create_user_command("FormatDisable", function()
vim.g.disable_autoformat = true vim.g.disable_autoformat = true
end, { end, {
desc = "Disable autoformat on save", desc = "Disable autoformat on save",
}) })
vim.api.nvim_create_user_command("FormatEnable", function() vim.api.nvim_create_user_command("FormatEnable", function()
vim.g.disable_autoformat = false vim.g.disable_autoformat = false
end, { end, {
desc = "Enable autoformat on save", desc = "Enable autoformat on save",
}) })
end, end,
}, },
}, },
config = function() config = function()
-- required to setup neodev before lspconfig -- required to setup neodev before lspconfig
require("neodev").setup({}) require("neodev").setup({})
local lspzero = require("lsp-zero") local lspzero = require("lsp-zero")
lspzero.preset({}) lspzero.preset({})
lspzero.on_attach(function(_, bufnr) lspzero.on_attach(function(_, bufnr)
lspzero.default_keymaps({ lspzero.default_keymaps({
buffer = bufnr, buffer = bufnr,
omit = { omit = {
"gr", "gr",
}, },
}) })
local function nmap(key, action, desc) local function nmap(key, action, desc)
vim.keymap.set("n", key, action, { vim.keymap.set("n", key, action, {
desc = "LSP: " .. desc, desc = "LSP: " .. desc,
}) })
end end
local function format() local function format()
require("conform").format({ bufnr = bufnr }) require("conform").format({ bufnr = bufnr })
end end
nmap("[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", "Goto Prev Diagnostic") nmap("[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", "Goto Prev Diagnostic")
nmap("]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", "Goto Next Diagnostic") nmap("]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", "Goto Next Diagnostic")
-- local current_dir = vim.fn.expand("%:p:h") -- local current_dir = vim.fn.expand("%:p:h")
-- vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references<cr>', { buffer = true }) -- vim.keymap.set('n', 'gr', '<cmd>Telescope lsp_references<cr>', { buffer = true })
-- format with space + f -- format with space + f
-- vim.keymap.set("n", "<leader>fb", "<cmd>lua vim.lsp.buf.format()<CR>", -- vim.keymap.set("n", "<leader>fb", "<cmd>lua vim.lsp.buf.format()<CR>",
-- { desc = "[F]ormat [B]uffer" }) -- { desc = "[F]ormat [B]uffer" })
nmap("<leader>fb", format, "[F]ormat [B]uffer") nmap("<leader>fb", format, "[F]ormat [B]uffer")
vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_) vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
format() format()
end, { desc = "Format current buffer with LSP" }) end, { desc = "Format current buffer with LSP" })
end) end)
lspzero.ensure_installed({ local status, lspconfig = pcall(require, "lspconfig")
"tsserver",
"eslint",
"tailwindcss",
})
-- lspzero.format_on_save({ if status then
-- format_ops = { lspconfig.tsserver.setup({})
-- async = true, lspconfig.tailwindcss.setup({
-- timeout_ms = 10000, filetypes = {
-- }, "templ",
-- servers = { "html",
-- ["lua_ls"] = { "lua" }, "javascript",
-- ["null-ls"] = { "typescript",
-- "javascript", "javascriptreact",
-- "typescript", "typescriptreact",
-- "javascriptreact", },
-- "typescriptreact", init_options = {
-- "html", userLanguages = {
-- "css", templ = "html",
-- }, },
-- }, },
-- }) })
lspconfig.zls.setup({})
lspconfig.rust_analyzer.setup({})
lspconfig.gopls.setup({})
lspconfig.html.setup({})
vim.filetype.add({
extension = {
templ = "templ",
},
})
lspconfig.templ.setup({
filetypes = {
"templ",
},
})
end
local status, lspconfig = pcall(require, "lspconfig") lspzero.setup()
if status then local cmp
lspconfig.tsserver.setup({}) status, cmp = pcall(require, "cmp")
lspconfig.tailwindcss.setup({
filetypes = {
"templ",
"html",
"javascript",
"typescript",
"javascriptreact",
"typescriptreact",
},
init_options = {
userLanguages = {
templ = "html",
},
},
})
lspconfig.zls.setup({})
lspconfig.rust_analyzer.setup({})
lspconfig.gopls.setup({})
lspconfig.html.setup({})
vim.filetype.add({
extension = {
templ = "templ",
},
})
lspconfig.templ.setup({
filetypes = {
"templ",
},
})
end
lspzero.setup() if not status then
return
end
local cmp cmp.setup({
status, cmp = pcall(require, "cmp") mapping = {
-- press 'enter' to confirm completion/suggestion
if not status then ["<CR>"] = cmp.mapping.confirm({ select = true }),
return ["<C-e>"] = cmp.mapping.abort(),
end },
})
cmp.setup({ end,
mapping = { },
-- press 'enter' to confirm completion/suggestion
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<C-e>"] = cmp.mapping.abort(),
},
})
end,
},
} }