From b300acf4a18e4fdfa69fecac47a249d063b7469a Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sun, 24 Dec 2023 18:50:56 -0600 Subject: feat: formatting keybind --- lua/plugins/lspconfig.lua | 32 +++++++++++++++++++++++++------- lua/plugins/nonels.lua | 45 --------------------------------------------- 2 files changed, 25 insertions(+), 52 deletions(-) delete mode 100644 lua/plugins/nonels.lua (limited to 'lua/plugins') diff --git a/lua/plugins/lspconfig.lua b/lua/plugins/lspconfig.lua index 5523af6..b2c9c17 100644 --- a/lua/plugins/lspconfig.lua +++ b/lua/plugins/lspconfig.lua @@ -20,7 +20,7 @@ return { 'nvim-telescope/telescope.nvim', { 'williamboman/mason.nvim', config = true }, 'williamboman/mason-lspconfig.nvim', - { 'j-hui/fidget.nvim', tag = "legacy", opts = {} }, + { 'j-hui/fidget.nvim', tag = 'legacy', opts = {} }, { 'folke/neodev.nvim', opts = {} }, }, config = function() @@ -34,7 +34,29 @@ return { ensure_installed = vim.tbl_keys(servers) }) - local on_attach = function(_, bufnr) + local on_attach = function(_, bufnr) -- _ is client + -- Create a command `:Format` local to the LSP buffer + vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) + -- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead + -- on later neovim version, you should use vim.lsp.buf.format({ async = false }) instead + vim.lsp.buf.format() + end, { desc = 'Format current buffer with LSP' }) + + -- autoformatting on save from: https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Formatting-on-save + -- pretty sure this will autoformat any code + -- temporarily disabled as using the keymap is better so far, else a lot of unnecessary changes will be made to random files (such as thesenvim configs) + -- local augroup = vim.api.nvim_create_augroup('LspFormatting', {}) + -- if client.supports_method('textDocument/formatting') then + -- vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) + -- vim.api.nvim_create_autocmd('BufWritePre', { + -- group = augroup, + -- buffer = bufnr, + -- callback = function() + -- vim.cmd('Format') + -- end, + -- }) + -- end + local nmap = function(keys, func, desc) if desc then desc = 'LSP: ' .. desc @@ -42,6 +64,7 @@ return { vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) end + nmap('f', 'Format', '[F]ormat') nmap('rn', vim.lsp.buf.rename, '[R]e[n]ame') nmap('ca', vim.lsp.buf.code_action, '[C]ode [A]ction') @@ -63,11 +86,6 @@ return { nmap('wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, '[W]orkspace [L]ist Folders') - - -- Create a command `:Format` local to the LSP buffer - vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) - vim.lsp.buf.format() - end, { desc = 'Format current buffer with LSP' }) end mason_lspconfig.setup_handlers({ diff --git a/lua/plugins/nonels.lua b/lua/plugins/nonels.lua deleted file mode 100644 index c0c27df..0000000 --- a/lua/plugins/nonels.lua +++ /dev/null @@ -1,45 +0,0 @@ -return { - "nvimtools/none-ls.nvim", - enabled = false, - dependencies = { - { "williamboman/mason.nvim", config = true}, - { - "jay-babu/mason-null-ls.nvim", - event = { "BufReadPre", "BufNewFile" }, - opts = {}, - }, - }, - config = function () - local mason_nullls = require('mason-null-ls') - mason_nullls.setup({ - ensure_installed = { - 'gofumpt', -- or gofmt - 'golines', - }, - automatic_installation = true, - handlers = {} - }) - - -- autoformatting on save from: https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Formatting-on-save - -- pretty sure this will autoformat any code - local nonels = require('null-ls') - local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) - nonels.setup({ - -- you can reuse a shared lspconfig on_attach callback here - on_attach = function(client, bufnr) - if client.supports_method("textDocument/formatting") then - vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr }) - vim.api.nvim_create_autocmd("BufWritePre", { - group = augroup, - buffer = bufnr, - callback = function() - -- on 0.8, you should use vim.lsp.buf.format({ bufnr = bufnr }) instead - -- on later neovim version, you should use vim.lsp.buf.format({ async = false }) instead - vim.lsp.buf.format({async = false}) - end, - }) - end - end, - }) - end, -} -- cgit v1.2.3-54-g00ecf