Dot_Files/.config/nvim/lua/plugins/lsp-capabilities.lua

72 lines
2.2 KiB
Lua
Raw Normal View History

local async = require('plenary.async')
local M = {}
local augroup = vim.api.nvim_create_augroup('LspFormatting', {})
M.async_formatting = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
vim.lsp.buf_request(
bufnr,
'textDocument/formatting',
{ textDocument = { uri = vim.uri_from_bufnr(bufnr) } },
function(err, res, ctx)
if err then
local err_msg = type(err) == 'string' and err or err.message
vim.notify('formatting: ' .. err_msg, vim.log.levels.WARN)
return
end
if not vim.api.nvim_buf_is_loaded(bufnr) or vim.api.nvim_buf_get_option(bufnr, 'modified') then
return
end
if res then
local client = vim.lsp.get_client_by_id(ctx.client_id)
-- need to specify offset encoding
vim.lsp.util.apply_text_edits(res, bufnr, client and client.offset_encoding or 'utf-16')
vim.api.nvim_buf_call(bufnr, function()
vim.cmd('silent noautocmd update')
end)
end
end
)
end
M.on_attach = function(client, bufnr)
async.run(function()
vim.notify.async('Attached server ' .. client.name, 'info', {
title = 'Lsp Attach',
}).events.close()
end)
if client.supports_method('testDocment/formatting') then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd('BufWritePre', {
pattern = '*',
callback = function()
local filetype = vim.bo.filetype
local neoformat_types = { 'sh' }
local ignore_types = { 'sql' }
for _, ig_type in ipairs(ignore_types) do
if filetype == ig_type then
return
end
end
for _, nf_type in ipairs(neoformat_types) do
if filetype == nf_type then
vim.cmd('Neoformat')
return
end
end
M.async_formatting(bufnr)
end,
})
end
end
return M