From c6ea262073f3b184692c698c1e4aad090f5bd04e Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:45:43 -0400 Subject: [PATCH] (nvim) tidy up nvim config --- nvim/lua/juancwu/config/init.lua | 100 ++++----- nvim/lua/juancwu/plugins/lsp/init.lua | 302 ++++++++++++-------------- 2 files changed, 189 insertions(+), 213 deletions(-) diff --git a/nvim/lua/juancwu/config/init.lua b/nvim/lua/juancwu/config/init.lua index 418eb7c..203664d 100644 --- a/nvim/lua/juancwu/config/init.lua +++ b/nvim/lua/juancwu/config/init.lua @@ -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 diff --git a/nvim/lua/juancwu/plugins/lsp/init.lua b/nvim/lua/juancwu/plugins/lsp/init.lua index 7b74db4..0d82b26 100644 --- a/nvim/lua/juancwu/plugins/lsp/init.lua +++ b/nvim/lua/juancwu/plugins/lsp/init.lua @@ -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", "lua vim.diagnostic.goto_prev()", "Goto Prev Diagnostic") - nmap("]d", "lua vim.diagnostic.goto_next()", "Goto Next Diagnostic") + nmap("[d", "lua vim.diagnostic.goto_prev()", "Goto Prev Diagnostic") + nmap("]d", "lua vim.diagnostic.goto_next()", "Goto Next Diagnostic") - -- local current_dir = vim.fn.expand("%:p:h") + -- local current_dir = vim.fn.expand("%:p:h") - -- vim.keymap.set('n', 'gr', 'Telescope lsp_references', { buffer = true }) + -- vim.keymap.set('n', 'gr', 'Telescope lsp_references', { buffer = true }) - -- format with space + f - -- vim.keymap.set("n", "fb", "lua vim.lsp.buf.format()", - -- { desc = "[F]ormat [B]uffer" }) - nmap("fb", format, "[F]ormat [B]uffer") + -- format with space + f + -- vim.keymap.set("n", "fb", "lua vim.lsp.buf.format()", + -- { desc = "[F]ormat [B]uffer" }) + nmap("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 - [""] = cmp.mapping.confirm({ select = true }), - [""] = cmp.mapping.abort(), - }, - }) - end, - }, + cmp.setup({ + mapping = { + -- press 'enter' to confirm completion/suggestion + [""] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping.abort(), + }, + }) + end, + }, }