feat(lsp): use autogroup for autocmd
This commit is contained in:
parent
ae6363c0d6
commit
d4f1e232c4
1 changed files with 11 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
||||||
local config = require('cosmic.config')
|
local config = require('cosmic.config')
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local auto_format_lock = false;
|
local auto_format_lock = false
|
||||||
|
|
||||||
function M.on_attach(client, bufnr)
|
function M.on_attach(client, bufnr)
|
||||||
local function buf_set_option(...)
|
local function buf_set_option(...)
|
||||||
|
@ -18,14 +18,22 @@ function M.on_attach(client, bufnr)
|
||||||
if config.lsp.format_on_save and not auto_format_lock then
|
if config.lsp.format_on_save and not auto_format_lock then
|
||||||
auto_format_lock = true -- just run autocommand once
|
auto_format_lock = true -- just run autocommand once
|
||||||
local format_filetypes = ''
|
local format_filetypes = ''
|
||||||
if (vim.tbl_islist(config.lsp.format_on_save)) then
|
if vim.tbl_islist(config.lsp.format_on_save) then
|
||||||
for _, ft in pairs(config.lsp.format_on_save) do
|
for _, ft in pairs(config.lsp.format_on_save) do
|
||||||
format_filetypes = format_filetypes .. '*' .. ft
|
format_filetypes = format_filetypes .. '*' .. ft
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
format_filetypes = '*'
|
format_filetypes = '*'
|
||||||
end
|
end
|
||||||
vim.cmd(string.format('autocmd BufWritePre %s lua vim.lsp.buf.formatting_sync(nil, 200)', format_filetypes))
|
vim.cmd(string.format(
|
||||||
|
[[
|
||||||
|
augroup CosmicFormat
|
||||||
|
autocmd!
|
||||||
|
autocmd BufWritePre %s lua vim.lsp.buf.formatting_sync(nil, 200)
|
||||||
|
augroup end
|
||||||
|
]],
|
||||||
|
format_filetypes
|
||||||
|
))
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
client.resolved_capabilities.document_formatting = false
|
client.resolved_capabilities.document_formatting = false
|
||||||
|
|
Loading…
Add table
Reference in a new issue