From 4b24a1326387c80e7b969aea565f56413e03b1c2 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Tue, 19 Dec 2023 15:33:19 -0500 Subject: [PATCH] add conform for formatting --- nvim/init.lua | 22 +-- nvim/lazy-lock.json | 2 + nvim/lua/juancwu/plugins/lsp/init.lua | 263 ++++++++++++++------------ 3 files changed, 152 insertions(+), 135 deletions(-) diff --git a/nvim/init.lua b/nvim/init.lua index 0c38c0e..e568265 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -1,18 +1,18 @@ local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" if not vim.loop.fs_stat(lazypath) then - vim.fn.system({ - "git", - "clone", - "--filter=blob:none", - "https://github.com/folke/lazy.nvim.git", - "--branch=stable", -- latest stable release - lazypath, - }) + vim.fn.system({ + "git", + "clone", + "--filter=blob:none", + "https://github.com/folke/lazy.nvim.git", + "--branch=stable", -- latest stable release + lazypath, + }) end vim.opt.rtp:prepend(lazypath) require("juancwu.config").setup({ - colorscheme = function() - require("tokyonight").load() - end, + colorscheme = function() + require("tokyonight").load() + end, }) diff --git a/nvim/lazy-lock.json b/nvim/lazy-lock.json index 987fe88..f05c3d2 100644 --- a/nvim/lazy-lock.json +++ b/nvim/lazy-lock.json @@ -1,6 +1,7 @@ { "Comment.nvim": { "branch": "master", "commit": "0236521ea582747b58869cb72f70ccfa967d2e89" }, "LuaSnip": { "branch": "master", "commit": "57c9f5c31b3d712376c704673eac8e948c82e9c1" }, + "catppuccin": { "branch": "main", "commit": "079500a625f3ae5aa6efb758f1a17fe4c7a57e52" }, "cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" }, "fzf": { "branch": "master", "commit": "d21d5c9510170d74a7f959309da720b6df72ca01" }, "fzf-lua": { "branch": "main", "commit": "a1a2d0f42eaec400cc6918a8e898fc1f9c4dbc5f" }, @@ -35,6 +36,7 @@ "telescope-fzf-native.nvim": { "branch": "main", "commit": "6c921ca12321edaa773e324ef64ea301a1d0da62" }, "telescope.nvim": { "branch": "master", "commit": "6213322ab56eb27356fdc09a5078e41e3ea7f3bc" }, "todo-comments.nvim": { "branch": "main", "commit": "4a6737a8d70fe1ac55c64dfa47fcb189ca431872" }, + "tokyonight.nvim": { "branch": "main", "commit": "f247ee700b569ed43f39320413a13ba9b0aef0db" }, "undotree": { "branch": "master", "commit": "36ff7abb6b60980338344982ad4cdf03f7961ecd" }, "vim-closetag": { "branch": "master", "commit": "d0a562f8bdb107a50595aefe53b1a690460c3822" }, "vim-floaterm": { "branch": "master", "commit": "3f01a623376957437f9376327637491b74719e38" }, diff --git a/nvim/lua/juancwu/plugins/lsp/init.lua b/nvim/lua/juancwu/plugins/lsp/init.lua index 1ed8ce8..e85bded 100644 --- a/nvim/lua/juancwu/plugins/lsp/init.lua +++ b/nvim/lua/juancwu/plugins/lsp/init.lua @@ -1,144 +1,159 @@ 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 = {}, - }, - }, - config = function() - -- required to setup neodev before lspconfig - require('neodev').setup({}) + -- Neovim Plugin Development Completions + { + "folke/neodev.nvim", + opts = {}, + }, - local lspzero = require("lsp-zero") + -- formatting + { + "stevearc/conform.nvim", + opts = { + formatters_by_ft = { + lua = { "stylua" }, + }, + format_on_save = { + timeout_ms = 500, + lsp_fallback = true, + }, + }, + }, + }, + config = function() + -- required to setup neodev before lspconfig + require("neodev").setup({}) - lspzero.preset({}) + local lspzero = require("lsp-zero") - lspzero.on_attach(function(client, bufnr) - lspzero.default_keymaps({ - buffer = bufnr, - omit = { - 'gr' - }, - }) + lspzero.preset({}) - function nmap(key, action, desc) - vim.keymap.set( - "n", - key, - action, - { - desc = "LSP: " .. desc, - } - ) - end + lspzero.on_attach(function(client, bufnr) + lspzero.default_keymaps({ + buffer = bufnr, + omit = { + "gr", + }, + }) - nmap("[d", "lua vim.diagnostic.goto_prev()", "Goto Prev Diagnostic"); - nmap("]d", "lua vim.diagnostic.goto_next()", "Goto Next Diagnostic") + function nmap(key, action, desc) + vim.keymap.set("n", key, action, { + desc = "LSP: " .. desc, + }) + end - local current_dir = vim.fn.expand("%:p:h") + nmap("[d", "lua vim.diagnostic.goto_prev()", "Goto Prev Diagnostic") + nmap("]d", "lua vim.diagnostic.goto_next()", "Goto Next Diagnostic") - -- vim.keymap.set('n', 'gr', 'Telescope lsp_references', { buffer = true }) + local current_dir = vim.fn.expand("%:p:h") - -- format with space + f - -- vim.keymap.set("n", "fb", "lua vim.lsp.buf.format()", - -- { desc = "[F]ormat [B]uffer" }) - nmap("fb", "lua vim.lsp.buf.format()", "[F]ormat [B]uffer") - end) + -- vim.keymap.set('n', 'gr', 'Telescope lsp_references', { buffer = true }) - lspzero.ensure_installed({ - "tsserver", - "eslint", - "tailwindcss", - }) + -- format with space + f + -- vim.keymap.set("n", "fb", "lua vim.lsp.buf.format()", + -- { desc = "[F]ormat [B]uffer" }) + nmap("fb", "lua vim.lsp.buf.format()", "[F]ormat [B]uffer") - lspzero.format_on_save({ - format_ops = { - async = true, - timeout_ms = 10000, - }, - servers = { - ["lua_ls"] = { "lua" }, - ["null-ls"] = { - "javascript", - "typescript", - "javascriptreact", - "typescriptreact", - "html", - "css", - }, - }, - }) + vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_) + require("conform").format({ bufnr = bufnr }) + end, { desc = "Format current buffer with LSP" }) + end) - local status, lspconfig = pcall(require, "lspconfig") + lspzero.ensure_installed({ + "tsserver", + "eslint", + "tailwindcss", + }) - 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 + -- lspzero.format_on_save({ + -- format_ops = { + -- async = true, + -- timeout_ms = 10000, + -- }, + -- servers = { + -- ["lua_ls"] = { "lua" }, + -- ["null-ls"] = { + -- "javascript", + -- "typescript", + -- "javascriptreact", + -- "typescriptreact", + -- "html", + -- "css", + -- }, + -- }, + -- }) - lspzero.setup() + local status, lspconfig = pcall(require, "lspconfig") - local cmp - status, cmp = pcall(require, "cmp") + 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 - if not status then return end + lspzero.setup() - cmp.setup({ - mapping = { - -- press 'enter' to confirm completion/suggestion - [''] = cmp.mapping.confirm({ select = true }), - [''] = cmp.mapping.abort(), - }, - }) - 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, + }, }