From f833c18b5a64e70efe08420f09b8678189a519c0 Mon Sep 17 00:00:00 2001 From: Matthew Leong Date: Mon, 9 Jan 2023 12:12:44 -0800 Subject: [PATCH] fix(lsp): don't register autocmd for servers not allowed to format by user config --- lazy-lock.json | 6 +++--- lua/cosmic/lsp/providers/defaults.lua | 10 ++++++---- lua/cosmic/utils/lsp.lua | 4 ---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lazy-lock.json b/lazy-lock.json index 6d8356b..85e92c4 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,6 +1,6 @@ { "Comment.nvim": { "branch": "master", "commit": "e89df176e8b38e931b7e71a470f923a317976d86" }, - "LuaSnip": { "branch": "master", "commit": "508b41fb7b2a7f06522f96eb7742d21f025a8512" }, + "LuaSnip": { "branch": "master", "commit": "5d57303efde86fcb0959c52b1a6d40f923940f34" }, "SchemaStore.nvim": { "branch": "main", "commit": "c55c35929dd4e670af7ab6e2e197a4687baa2076" }, "auto-session": { "branch": "main", "commit": "c8b2f4048f846387361bd04cc185bf1aa7d2e3d1" }, "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" }, @@ -12,7 +12,7 @@ "cosmic-ui": { "branch": "main", "commit": "c0b14531999f2bfef3d927c4dcd57a1a8fed5ee9" }, "friendly-snippets": { "branch": "main", "commit": "484fb38b8f493ceeebf4e6fc499ebe41e10aae25" }, "gitsigns.nvim": { "branch": "main", "commit": "114362a85e51918ab2965181ffa31932c181f32f" }, - "lazy.nvim": { "branch": "main", "commit": "4304035ef4eae2d9dfac4fc082a1b391e6cd928e" }, + "lazy.nvim": { "branch": "main", "commit": "e6ee0fa6103e9514e85a96fc16902ad7f777b53f" }, "lsp_lines.nvim": { "branch": "main", "commit": "ec98b45c8280e5ef8c84028d4f38aa447276c002" }, "lualine.nvim": { "branch": "master", "commit": "d8c392dd75778d6258da4e7c55522e94ac389732" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "3751eb5c56c67b51e68a1f4a0da28ae74ab771c1" }, @@ -27,7 +27,7 @@ "nvim-lspconfig": { "branch": "master", "commit": "7b98aadc6e85db4fc3af6c1ec22c4774d965506e" }, "nvim-notify": { "branch": "master", "commit": "b005821516f1f37801a73067afd1cef2dbc4dfe8" }, "nvim-tree.lua": { "branch": "master", "commit": "87961d38a7b6cd799ebe193946e86a1037ba66e8" }, - "nvim-treesitter": { "branch": "master", "commit": "d8c84521dc9f407f88dfca35b9572c6532207a4a" }, + "nvim-treesitter": { "branch": "master", "commit": "ed021ac3affdfd04a26325896d221f5f519e5971" }, "nvim-treesitter-refactor": { "branch": "master", "commit": "75f5895cc662d61eb919da8050b7a0124400d589" }, "nvim-ts-autotag": { "branch": "main", "commit": "fdefe46c6807441460f11f11a167a2baf8e4534b" }, "nvim-ts-context-commentstring": { "branch": "main", "commit": "4a42b30376c1bd625ab5016c2079631d531d797a" }, diff --git a/lua/cosmic/lsp/providers/defaults.lua b/lua/cosmic/lsp/providers/defaults.lua index ccfe07f..80435c3 100644 --- a/lua/cosmic/lsp/providers/defaults.lua +++ b/lua/cosmic/lsp/providers/defaults.lua @@ -17,7 +17,8 @@ function M.on_attach(client, bufnr) vim.cmd(string.format("command! LspFormat lua require('cosmic.utils.lsp').format(%s)", bufnr)) -- set up auto format on save - if user_config.lsp.format_on_save then + if user_config.lsp.format_on_save and user_config.lsp.can_client_format(client) then + -- check user config to see if we can format on save -- collect filetype(s) from user config local format_filetypes = '' if vim.tbl_islist(user_config.lsp.format_on_save) then @@ -29,10 +30,11 @@ function M.on_attach(client, bufnr) end -- autocommand for format on save with specified filetype(s) - vim.api.nvim_create_autocmd(string.format('BufWritePre %s', format_filetypes), { - callback = function() - require('cosmic.utils.lsp').format(bufnr) + vim.api.nvim_create_autocmd('BufWritePre', { + callback = function(evt) + require('cosmic.utils.lsp').format(evt.buf) end, + pattern = format_filetypes, group = group, nested = true, }) diff --git a/lua/cosmic/utils/lsp.lua b/lua/cosmic/utils/lsp.lua index 3ecdac4..d5ad4ec 100644 --- a/lua/cosmic/utils/lsp.lua +++ b/lua/cosmic/utils/lsp.lua @@ -5,10 +5,6 @@ local M = {} function M.format(bufnr) vim.lsp.buf.format({ timeout_ms = user_config.lsp.format_timeout, - -- check user config to see if we can format on save - filter = function(client) - return user_config.lsp.can_client_format(client) - end, bufnr = bufnr or 0, }) end