(nvim) tidy up nvim config
This commit is contained in:
parent
2901420626
commit
c6ea262073
2 changed files with 189 additions and 213 deletions
|
|
@ -3,22 +3,22 @@ local M = {}
|
|||
|
||||
---@class ConfigOptions
|
||||
local defaultOpts = {
|
||||
---@type string | fun()
|
||||
colorscheme = "rose-pine",
|
||||
---@type string | fun()
|
||||
colorscheme = "rose-pine",
|
||||
}
|
||||
|
||||
vim.g.disable_autoformat = false
|
||||
|
||||
---@param name "options" | "keymaps" | "clipboard"
|
||||
function M.load(name)
|
||||
local mod = "juancwu.config." .. name
|
||||
local error_handler = function(err)
|
||||
local msg = "Failed loading " .. mod .. "\n\n" .. err
|
||||
print(msg)
|
||||
end
|
||||
xpcall(function()
|
||||
require(mod)
|
||||
end, error_handler)
|
||||
local mod = "juancwu.config." .. name
|
||||
local error_handler = function(err)
|
||||
local msg = "Failed loading " .. mod .. "\n\n" .. err
|
||||
print(msg)
|
||||
end
|
||||
xpcall(function()
|
||||
require(mod)
|
||||
end, error_handler)
|
||||
end
|
||||
|
||||
---@type ConfigOptions
|
||||
|
|
@ -26,55 +26,55 @@ local options
|
|||
|
||||
---@param opts? ConfigOptions
|
||||
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("keymaps")
|
||||
M.load("clipboard")
|
||||
M.load("options")
|
||||
M.load("keymaps")
|
||||
M.load("clipboard")
|
||||
|
||||
require("lazy").setup("juancwu.plugins")
|
||||
require("lazy").setup("juancwu.plugins")
|
||||
|
||||
-- try to load colorscheme
|
||||
xpcall(function()
|
||||
if type(M.colorscheme) == "function" then
|
||||
M.colorscheme()
|
||||
else
|
||||
vim.cmd.colorscheme(M.colorscheme)
|
||||
end
|
||||
end, function(err)
|
||||
if type(M.colorscheme) == "string" then
|
||||
local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err
|
||||
print(msg)
|
||||
else
|
||||
print("Failed to load colorscheme\n\n" .. err)
|
||||
end
|
||||
vim.cmd.colorscheme("rose-pine")
|
||||
end)
|
||||
-- try to load colorscheme
|
||||
xpcall(function()
|
||||
if type(M.colorscheme) == "function" then
|
||||
M.colorscheme()
|
||||
else
|
||||
vim.cmd.colorscheme(M.colorscheme)
|
||||
end
|
||||
end, function(err)
|
||||
if type(M.colorscheme) == "string" then
|
||||
local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err
|
||||
print(msg)
|
||||
else
|
||||
print("Failed to load colorscheme\n\n" .. err)
|
||||
end
|
||||
vim.cmd.colorscheme("rose-pine")
|
||||
end)
|
||||
|
||||
-- create command to disable autoformat
|
||||
vim.api.nvim_create_user_command("FormatDisable", function(args)
|
||||
vim.g.disable_autoformat = true
|
||||
end, { desc = "Disable Autoformat" })
|
||||
vim.api.nvim_create_user_command("FormatEnable", function(args)
|
||||
vim.g.disable_autoformat = false
|
||||
end, { desc = "Enable Autoformat" })
|
||||
-- create command to disable autoformat
|
||||
vim.api.nvim_create_user_command("FormatDisable", function(args)
|
||||
vim.g.disable_autoformat = true
|
||||
end, { desc = "Disable Autoformat" })
|
||||
vim.api.nvim_create_user_command("FormatEnable", function(args)
|
||||
vim.g.disable_autoformat = false
|
||||
end, { desc = "Enable Autoformat" })
|
||||
|
||||
-- create command to toggle colorscheme
|
||||
vim.api.nvim_create_user_command(
|
||||
"ToggleColors",
|
||||
require("juancwu.utils.colors").toggle_colors,
|
||||
{ desc = "Toggle colorscheme" }
|
||||
)
|
||||
-- create command to toggle colorscheme
|
||||
vim.api.nvim_create_user_command(
|
||||
"ToggleColors",
|
||||
require("juancwu.utils.colors").toggle_colors,
|
||||
{ desc = "Toggle colorscheme" }
|
||||
)
|
||||
end
|
||||
|
||||
setmetatable(M, {
|
||||
__index = function(_, k)
|
||||
if options == nil then
|
||||
return vim.deepcopy(defaultOpts)[k]
|
||||
end
|
||||
__index = function(_, k)
|
||||
if options == nil then
|
||||
return vim.deepcopy(defaultOpts)[k]
|
||||
end
|
||||
|
||||
return options[k]
|
||||
end,
|
||||
return options[k]
|
||||
end,
|
||||
})
|
||||
|
||||
return M
|
||||
|
|
|
|||
|
|
@ -1,187 +1,163 @@
|
|||
return {
|
||||
-- lspconfig
|
||||
{
|
||||
"VonHeikemen/lsp-zero.nvim",
|
||||
branch = "v2.x",
|
||||
dependencies = {
|
||||
-- LSP Support
|
||||
{ "neovim/nvim-lspconfig" }, -- Required
|
||||
{
|
||||
-- Optional
|
||||
"williamboman/mason.nvim",
|
||||
build = function()
|
||||
pcall(vim.cmd, "MasonUpdate")
|
||||
end,
|
||||
},
|
||||
{ "williamboman/mason-lspconfig.nvim" }, -- Optional
|
||||
-- lspconfig
|
||||
{
|
||||
"VonHeikemen/lsp-zero.nvim",
|
||||
branch = "v2.x",
|
||||
dependencies = {
|
||||
-- LSP Support
|
||||
{ "neovim/nvim-lspconfig" }, -- Required
|
||||
{
|
||||
-- Optional
|
||||
"williamboman/mason.nvim",
|
||||
build = function()
|
||||
pcall(vim.cmd, "MasonUpdate")
|
||||
end,
|
||||
},
|
||||
{ "williamboman/mason-lspconfig.nvim" }, -- Optional
|
||||
|
||||
-- Autocompletion
|
||||
{ "hrsh7th/nvim-cmp" }, -- Required
|
||||
{ "hrsh7th/cmp-nvim-lsp" }, -- Required
|
||||
{ "L3MON4D3/LuaSnip" }, -- Required
|
||||
-- Autocompletion
|
||||
{ "hrsh7th/nvim-cmp" }, -- Required
|
||||
{ "hrsh7th/cmp-nvim-lsp" }, -- Required
|
||||
{ "L3MON4D3/LuaSnip" }, -- Required
|
||||
|
||||
-- Neovim Plugin Development Completions
|
||||
{
|
||||
"folke/neodev.nvim",
|
||||
opts = {},
|
||||
},
|
||||
-- Neovim Plugin Development Completions
|
||||
{
|
||||
"folke/neodev.nvim",
|
||||
opts = {},
|
||||
},
|
||||
|
||||
-- formatting
|
||||
{
|
||||
"stevearc/conform.nvim",
|
||||
config = function()
|
||||
require("conform").setup({
|
||||
formatters_by_ft = {
|
||||
lua = { "stylua" },
|
||||
javascript = { "prettier" },
|
||||
typescript = { "prettier" },
|
||||
javascriptreact = { "prettier" },
|
||||
typescriptreact = { "prettier" },
|
||||
-- formatting
|
||||
{
|
||||
"stevearc/conform.nvim",
|
||||
config = function()
|
||||
require("conform").setup({
|
||||
formatters_by_ft = {
|
||||
lua = { "stylua" },
|
||||
javascript = { "prettier" },
|
||||
typescript = { "prettier" },
|
||||
javascriptreact = { "prettier" },
|
||||
typescriptreact = { "prettier" },
|
||||
markdown = { "prettier" },
|
||||
go = { "gofumpt" },
|
||||
python = { "autopep8" },
|
||||
yaml = { "yamlfmt" },
|
||||
},
|
||||
format_on_save = function(bufnr)
|
||||
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
|
||||
return
|
||||
end
|
||||
return { timeout_ms = 500, lsp_fallback = true }
|
||||
end,
|
||||
})
|
||||
go = { "gofumpt" },
|
||||
python = { "autopep8" },
|
||||
yaml = { "yamlfmt" },
|
||||
},
|
||||
format_on_save = function(bufnr)
|
||||
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
|
||||
return
|
||||
end
|
||||
return { timeout_ms = 500, lsp_fallback = true }
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_user_command("FormatDisable", function()
|
||||
vim.g.disable_autoformat = true
|
||||
end, {
|
||||
desc = "Disable autoformat on save",
|
||||
})
|
||||
vim.api.nvim_create_user_command("FormatDisable", function()
|
||||
vim.g.disable_autoformat = true
|
||||
end, {
|
||||
desc = "Disable autoformat on save",
|
||||
})
|
||||
|
||||
vim.api.nvim_create_user_command("FormatEnable", function()
|
||||
vim.g.disable_autoformat = false
|
||||
end, {
|
||||
desc = "Enable autoformat on save",
|
||||
})
|
||||
end,
|
||||
},
|
||||
},
|
||||
config = function()
|
||||
-- required to setup neodev before lspconfig
|
||||
require("neodev").setup({})
|
||||
vim.api.nvim_create_user_command("FormatEnable", function()
|
||||
vim.g.disable_autoformat = false
|
||||
end, {
|
||||
desc = "Enable autoformat on save",
|
||||
})
|
||||
end,
|
||||
},
|
||||
},
|
||||
config = function()
|
||||
-- required to setup neodev before lspconfig
|
||||
require("neodev").setup({})
|
||||
|
||||
local lspzero = require("lsp-zero")
|
||||
local lspzero = require("lsp-zero")
|
||||
|
||||
lspzero.preset({})
|
||||
lspzero.preset({})
|
||||
|
||||
lspzero.on_attach(function(_, bufnr)
|
||||
lspzero.default_keymaps({
|
||||
buffer = bufnr,
|
||||
omit = {
|
||||
"gr",
|
||||
},
|
||||
})
|
||||
lspzero.on_attach(function(_, bufnr)
|
||||
lspzero.default_keymaps({
|
||||
buffer = bufnr,
|
||||
omit = {
|
||||
"gr",
|
||||
},
|
||||
})
|
||||
|
||||
local function nmap(key, action, desc)
|
||||
vim.keymap.set("n", key, action, {
|
||||
desc = "LSP: " .. desc,
|
||||
})
|
||||
end
|
||||
local function nmap(key, action, desc)
|
||||
vim.keymap.set("n", key, action, {
|
||||
desc = "LSP: " .. desc,
|
||||
})
|
||||
end
|
||||
|
||||
local function format()
|
||||
require("conform").format({ bufnr = bufnr })
|
||||
end
|
||||
local function format()
|
||||
require("conform").format({ bufnr = bufnr })
|
||||
end
|
||||
|
||||
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_prev()<CR>", "Goto Prev 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
|
||||
-- vim.keymap.set("n", "<leader>fb", "<cmd>lua vim.lsp.buf.format()<CR>",
|
||||
-- { desc = "[F]ormat [B]uffer" })
|
||||
nmap("<leader>fb", format, "[F]ormat [B]uffer")
|
||||
-- format with space + f
|
||||
-- vim.keymap.set("n", "<leader>fb", "<cmd>lua vim.lsp.buf.format()<CR>",
|
||||
-- { desc = "[F]ormat [B]uffer" })
|
||||
nmap("<leader>fb", format, "[F]ormat [B]uffer")
|
||||
|
||||
vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
|
||||
format()
|
||||
end, { desc = "Format current buffer with LSP" })
|
||||
end)
|
||||
vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
|
||||
format()
|
||||
end, { desc = "Format current buffer with LSP" })
|
||||
end)
|
||||
|
||||
lspzero.ensure_installed({
|
||||
"tsserver",
|
||||
"eslint",
|
||||
"tailwindcss",
|
||||
})
|
||||
local status, lspconfig = pcall(require, "lspconfig")
|
||||
|
||||
-- lspzero.format_on_save({
|
||||
-- format_ops = {
|
||||
-- async = true,
|
||||
-- timeout_ms = 10000,
|
||||
-- },
|
||||
-- servers = {
|
||||
-- ["lua_ls"] = { "lua" },
|
||||
-- ["null-ls"] = {
|
||||
-- "javascript",
|
||||
-- "typescript",
|
||||
-- "javascriptreact",
|
||||
-- "typescriptreact",
|
||||
-- "html",
|
||||
-- "css",
|
||||
-- },
|
||||
-- },
|
||||
-- })
|
||||
if status then
|
||||
lspconfig.tsserver.setup({})
|
||||
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
|
||||
|
||||
local status, lspconfig = pcall(require, "lspconfig")
|
||||
lspzero.setup()
|
||||
|
||||
if status then
|
||||
lspconfig.tsserver.setup({})
|
||||
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
|
||||
local cmp
|
||||
status, cmp = pcall(require, "cmp")
|
||||
|
||||
lspzero.setup()
|
||||
if not status then
|
||||
return
|
||||
end
|
||||
|
||||
local cmp
|
||||
status, cmp = pcall(require, "cmp")
|
||||
|
||||
if not status then
|
||||
return
|
||||
end
|
||||
|
||||
cmp.setup({
|
||||
mapping = {
|
||||
-- press 'enter' to confirm completion/suggestion
|
||||
["<CR>"] = cmp.mapping.confirm({ select = true }),
|
||||
["<C-e>"] = cmp.mapping.abort(),
|
||||
},
|
||||
})
|
||||
end,
|
||||
},
|
||||
cmp.setup({
|
||||
mapping = {
|
||||
-- press 'enter' to confirm completion/suggestion
|
||||
["<CR>"] = cmp.mapping.confirm({ select = true }),
|
||||
["<C-e>"] = cmp.mapping.abort(),
|
||||
},
|
||||
})
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue