From 1a36e16bf6fdf5463bb0675761faa7b0985599a6 Mon Sep 17 00:00:00 2001 From: jaredgoldman Date: Mon, 8 Apr 2024 10:46:05 -0400 Subject: [PATCH] feat: formatting with conform --- init.lua | 3 +- lazy-lock.json | 3 +- lua/config/conform.lua | 29 +++++ lua/config/mason.lua | 1 - lua/keymaps.lua | 121 ++++++++++----------- lua/lsp.lua | 103 +++++++++--------- lua/options.lua | 34 +++--- lua/plugins.lua | 242 +++++++++++++++++++++-------------------- 8 files changed, 289 insertions(+), 247 deletions(-) create mode 100644 lua/config/conform.lua diff --git a/init.lua b/init.lua index 28af065..ff9b8f0 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,4 @@ require("plugins") -require("options") require("colorscheme") require("lsp") require("config.telescope") @@ -13,5 +12,7 @@ require("config.fterm") require("config.harpoon") require("config.autopairs") require("config.autotags") +require("config.conform") -- require("config.dashboard") +require("options") require("keymaps") diff --git a/lazy-lock.json b/lazy-lock.json index da6352f..90bf7dc 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -6,6 +6,7 @@ "cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" }, "cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" }, "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, + "conform.nvim": { "branch": "master", "commit": "9d5ba06d6ee7418c674f498634617416d15b6239" }, "copilot.vim": { "branch": "release", "commit": "7097b09e52621a97d11f254e04de5e5a0f26e5f5" }, "dashboard-nvim": { "branch": "master", "commit": "681300934baf36f6184ca41f0b26aed22056d4ee" }, "harpoon": { "branch": "harpoon2", "commit": "da326d0438ac68dee9b6b62a734be940a8bd8405" }, @@ -28,4 +29,4 @@ "telescope.nvim": { "branch": "master", "commit": "6312868392331c9c0f22725041f1ec2bef57c751" }, "themery.nvim": { "branch": "main", "commit": "1005a58801276d29c4b1e11244cf7631250f9143" }, "vim-maximizer": { "branch": "master", "commit": "2e54952fe91e140a2e69f35f22131219fcd9c5f1" } -} \ No newline at end of file +} diff --git a/lua/config/conform.lua b/lua/config/conform.lua new file mode 100644 index 0000000..b8bacfe --- /dev/null +++ b/lua/config/conform.lua @@ -0,0 +1,29 @@ +require("conform").setup({ + formatters_by_ft = { + lua = { "stylua" }, + python = { "isort", "black" }, + javascript = { { "prettierd", "prettier" } }, + json = { { "prettierd", "prettier" } }, + }, +}) + +local M = {} + +M.format = function(args) + local range = nil + if args and args.count ~= -1 then + -- Assuming args.line1 and args.line2 are provided and valid + local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1] + range = { + start = { args.line1, 0 }, + ["end"] = { args.line2, end_line:len() }, + } + end + require("conform").format({ async = true, lsp_fallback = true, range = range }) +end + +vim.api.nvim_create_user_command("Format", function(args) + M.format(args) +end, { range = true }) + +return M diff --git a/lua/config/mason.lua b/lua/config/mason.lua index 9bda00d..381dba1 100644 --- a/lua/config/mason.lua +++ b/lua/config/mason.lua @@ -20,6 +20,5 @@ require('mason-lspconfig').setup({ 'tsserver', 'jsonls', 'lua_ls', - 'prettier' }, }) diff --git a/lua/keymaps.lua b/lua/keymaps.lua index a12361e..b200517 100644 --- a/lua/keymaps.lua +++ b/lua/keymaps.lua @@ -1,71 +1,68 @@ -local fterm = require('FTerm') local keymap = vim.api.nvim_set_keymap local opts = { noremap = true, silent = true } -local api = vim.api --- Custom commands -api.nvim_create_user_command('FTermToggle', fterm.toggle, { bang = true }) -api.nvim_create_user_command('FTermClose', fterm.close, { bang = true }) local mappings = { - n = { - -- highlighting - ["nh"] = ":nohl", - -- move between buffers - [""] = "h", - [""] = "j", - [""] = "k", - [""] = "l", - -- open and close buggers - ["sv"] = "s", - ["sh"] = "v", - ["se"] = "=", - ["sx"] = ":close", - -- nvim-tree - ["e"] = ":NvimTreeToggle", - ["cf"] = ":NvimTreeCollapse", - -- LSP - ["ls"] = ":lspstop", - ["lo"] = ":lspstart", - -- Telescope - ["ff"] = "Telescope find_files", - ["fw"] = "Telescope live_grep", - ["fc"] = "Telescope grep_string", - ["th"] = "Themery", - ["sm"] = ":MaximizerToggle", - -- FTerm - ["h"] = "FTermToggle", - -- Harpoon - ["a"] = "HarpoonAdd", - [""] = "HarpoonUI" - }, - x = { - [""] = ":move '>+1gv-gv", - [""] = ":move '<-2gv-gv", - }, - i = { - [""] = { 'copilot#Accept("")', expr = true }, - [""] = "", - }, - t = { - ["A-l"] = "FTermClose" - } + n = { + -- highlighting + ["nh"] = ":nohl", + -- move between buffers + [""] = "h", + [""] = "j", + [""] = "k", + [""] = "l", + -- open and close buggers + ["sv"] = "s", + ["sh"] = "v", + ["se"] = "=", + ["sx"] = ":close", + -- nvim-tree + ["e"] = ":NvimTreeToggle", + ["cf"] = ":NvimTreeCollapse", + -- LSP + ["ls"] = ":lspstop", + ["lo"] = ":lspstart", + -- Telescope + ["ff"] = "Telescope find_files", + ["fw"] = "Telescope live_grep", + ["fc"] = "Telescope grep_string", + ["th"] = "Themery", + ["sm"] = ":MaximizerToggle", + -- FTerm + ["h"] = "FTermToggle", + -- Harpoon + ["a"] = "HarpoonAdd", + [""] = "HarpoonUI", + -- Formating + ["fm"] = "Format", + }, + x = { + [""] = ":move '>+1gv-gv", + [""] = ":move '<-2gv-gv", + }, + i = { + [""] = { 'copilot#Accept("")', expr = true }, + [""] = "", + }, + t = { + ["A-l"] = "FTermClose", + }, } for mode, mode_mappings in pairs(mappings) do - for key, mapping in pairs(mode_mappings) do - if type(mapping) == "function" then - -- For direct Lua function calls; ensure your function returns a string command - keymap(mode, key, "lua " .. mapping() .. "", opts) - elseif type(mapping) == "table" and mapping.expr then - -- For expression mappings, like for copilot - local expr_opts = vim.tbl_extend("force", opts, { expr = true }) - keymap(mode, key, mapping[1], expr_opts) - elseif type(mapping) == "table" then - -- For mappings that have their options specified directly - keymap(mode, key, mapping[1], mapping[2] or opts) - else - -- For simple string command mappings - keymap(mode, key, mapping, opts) - end + for key, mapping in pairs(mode_mappings) do + if type(mapping) == "function" then + -- For direct Lua function calls; ensure your function returns a string command + keymap(mode, key, "lua " .. mapping() .. "", opts) + elseif type(mapping) == "table" and mapping.expr then + -- For expression mappings, like for copilot + local expr_opts = vim.tbl_extend("force", opts, { expr = true }) + keymap(mode, key, mapping[1], expr_opts) + elseif type(mapping) == "table" then + -- For mappings that have their options specified directly + keymap(mode, key, mapping[1], mapping[2] or opts) + else + -- For simple string command mappings + keymap(mode, key, mapping, opts) end + end end diff --git a/lua/lsp.lua b/lua/lsp.lua index a133758..612b041 100644 --- a/lua/lsp.lua +++ b/lua/lsp.lua @@ -1,66 +1,67 @@ -local lspconfig = require('lspconfig') +local lspconfig = require("lspconfig") -local mappings = { - ['k'] = vim.diagnostic.open_float, - ['[d'] = vim.diagnostic.goto_prev, - [']d'] = vim.diagnostic.goto_next, - ['q'] = vim.diagnostic.setloclist, - ['gD'] = vim.lsp.buf.declaration, - ['gd'] = vim.lsp.buf.definition, - ['K'] = vim.lsp.buf.hover, - ['gi'] = vim.lsp.buf.implementation, - ['wa'] = vim.lsp.buf.add_workspace_folder, - ['wr'] = vim.lsp.buf.remove_workspace_folder, - ['wl'] = function() - print(vim.inspect(vim.lsp.buf.list_workleader_folders())) - end, - ['D'] = vim.lsp.buf.type_definition, - ['rn'] = vim.lsp.buf.rename, - ['ca'] = vim.lsp.buf.code_action, - ['gr'] = vim.lsp.buf.references, - ['fm'] = function() - vim.lsp.buf.format({ async = true }) - end, + local mappings = { + ["k"] = vim.diagnostic.open_float, + ["[d"] = vim.diagnostic.goto_prev, + ["]d"] = vim.diagnostic.goto_next, + ["q"] = vim.diagnostic.setloclist, + ["gD"] = vim.lsp.buf.declaration, + ["gd"] = vim.lsp.buf.definition, + ["K"] = vim.lsp.buf.hover, + ["gi"] = vim.lsp.buf.implementation, + ["wa"] = vim.lsp.buf.add_workspace_folder, + ["wr"] = vim.lsp.buf.remove_workspace_folder, + ["wl"] = function() + print(vim.inspect(vim.lsp.buf.list_workleader_folders())) + end, + ["D"] = vim.lsp.buf.type_definition, + ["rn"] = vim.lsp.buf.rename, + ["ca"] = vim.lsp.buf.code_action, + ["gr"] = vim.lsp.buf.references, + ["fm"] = function() + vim.lsp.buf.format({ async = true }) + end, } local on_attach = function(client, bufnr) - -- Enable completion triggered by - vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') - local bufopts = { noremap = true, silent = true, buffer = bufnr } - for key, mapping in pairs(mappings) do - vim.keymap.set('n', key, mapping, bufopts) - end + -- Enable completion triggered by + vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") + local bufopts = { noremap = true, silent = true, buffer = bufnr } + for key, mapping in pairs(mappings) do + vim.keymap.set("n", key, mapping, bufopts) + end end local servers = { - "tsserver", - "intelephense", - "tailwindcss", - "lua_ls", + "tsserver", + "intelephense", + "tailwindcss", + "lua_ls", } + local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem = { - documentationFormat = { "markdown", "plaintext" }, - snippetSupport = true, - preselectSupport = true, - insertReplaceSupport = true, - labelDetailsSupport = true, - deprecatedSupport = true, - commitCharactersSupport = true, - tagSupport = { valueSet = { 1 } }, - resolveSupport = { - properties = { - "documentation", - "detail", - "additionalTextEdits", - }, - }, + documentationFormat = { "markdown", "plaintext" }, + snippetSupport = true, + preselectSupport = true, + insertReplaceSupport = true, + labelDetailsSupport = true, + deprecatedSupport = true, + commitCharactersSupport = true, + tagSupport = { valueSet = { 1 } }, + resolveSupport = { + properties = { + "documentation", + "detail", + "additionalTextEdits", + }, + }, } for _, lsp in ipairs(servers) do - lspconfig[lsp].setup { - on_attach = on_attach, - capabilities = capabilities, - } + lspconfig[lsp].setup({ + on_attach = on_attach, + capabilities = capabilities, + }) end diff --git a/lua/options.lua b/lua/options.lua index 2b41f21..08d8cc8 100644 --- a/lua/options.lua +++ b/lua/options.lua @@ -1,11 +1,12 @@ local opt = vim.opt local api = vim.api local global = vim.g +local formatter = require("config.conform") -- Hint: use `:h