fix: formatting autocmd queued only once
This commit is contained in:
parent
850c3835f8
commit
0cc8369ac1
3 changed files with 31 additions and 34 deletions
30
lua/cosmic/lsp/commands.lua
Normal file
30
lua/cosmic/lsp/commands.lua
Normal file
|
@ -0,0 +1,30 @@
|
|||
local augroup_name = 'CosmicNvimLspFormat'
|
||||
local group = vim.api.nvim_create_augroup(augroup_name, { clear = true })
|
||||
local config = require('cosmic.core.user')
|
||||
|
||||
if config.lsp.format_on_save then
|
||||
local format_filetypes = ''
|
||||
if vim.tbl_islist(config.lsp.format_on_save) then
|
||||
for _, ft in pairs(config.lsp.format_on_save) do
|
||||
format_filetypes = format_filetypes .. '*' .. ft
|
||||
end
|
||||
else
|
||||
format_filetypes = '*'
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd(string.format('BufWritePre %s', format_filetypes), {
|
||||
callback = function()
|
||||
vim.lsp.buf.format({
|
||||
timeout_ms = config.lsp.format_timeout,
|
||||
-- check user config to see if we can format on save
|
||||
filter = function(client)
|
||||
if config.lsp.can_client_format(client.name) then
|
||||
return client.name
|
||||
end
|
||||
end,
|
||||
})
|
||||
end,
|
||||
group = group,
|
||||
nested = true,
|
||||
})
|
||||
end
|
|
@ -3,6 +3,7 @@ local config = require('cosmic.core.user')
|
|||
-- set up lsp servers
|
||||
require('cosmic.lsp.providers')
|
||||
require('cosmic.lsp.diagnostics')
|
||||
require('cosmic.lsp.commands')
|
||||
|
||||
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, {
|
||||
border = config.border,
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
local config = require('cosmic.core.user')
|
||||
local augroup_name = 'CosmicNvimLspFormat'
|
||||
local group = vim.api.nvim_create_augroup(augroup_name, { clear = true })
|
||||
local M = {}
|
||||
|
||||
local auto_format_lock = false
|
||||
|
||||
function M.on_attach(client, bufnr)
|
||||
local function buf_set_option(...)
|
||||
vim.api.nvim_buf_set_option(bufnr, ...)
|
||||
|
@ -13,35 +8,6 @@ function M.on_attach(client, bufnr)
|
|||
-- Enable completion triggered by <c-x><c-o>
|
||||
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
|
||||
|
||||
if config.lsp.format_on_save then
|
||||
local format_filetypes = ''
|
||||
if vim.tbl_islist(config.lsp.format_on_save) then
|
||||
for _, ft in pairs(config.lsp.format_on_save) do
|
||||
format_filetypes = format_filetypes .. '*' .. ft
|
||||
end
|
||||
else
|
||||
format_filetypes = '*'
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd(string.format('BufWritePre %s', format_filetypes), {
|
||||
callback = function()
|
||||
vim.lsp.buf.format({
|
||||
timeout_ms = config.lsp.format_timeout,
|
||||
-- check user config to see if we can format on save
|
||||
filter = function(clients)
|
||||
return vim.tbl_filter(function(ct)
|
||||
if config.lsp.can_client_format(ct.name) then
|
||||
return ct.name
|
||||
end
|
||||
end, clients)
|
||||
end,
|
||||
})
|
||||
end,
|
||||
group = group,
|
||||
nested = true,
|
||||
})
|
||||
end
|
||||
|
||||
require('cosmic.lsp.mappings').init(client, bufnr)
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue