Adds Lsp formatting and folding options

main
Luis Soto 4 months ago committed by zoomiti
parent 3fc2cc16d1
commit ffaf250f73

@ -306,29 +306,74 @@ end, {})
-- }}} -- }}}
-- {{{ LSP -- {{{ LSP
vim.keymap.set('n', 'grd', vim.lsp.buf.definition, { desc = "vim.lsp.buf.definition" }) vim.o.foldlevel = 1
vim.keymap.set({ 'n', 'v', 'x' }, 'grf', vim.lsp.buf.format, { desc = "LSP Format" }) vim.o.foldmethod = 'expr'
vim.lsp.inlay_hint.enable(true) vim.o.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
vim.g.c_syntax_for_h = true vim.g.c_syntax_for_h = true
vim.lsp.enable({ 'lua_ls', 'ccls' }) vim.lsp.enable({ 'lua_ls', 'ccls' })
vim.g.auto_format = true
vim.api.nvim_create_autocmd("LspAttach", { vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args) callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id) local client = vim.lsp.get_client_by_id(args.data.client_id)
if not client then return end if not client then return end
vim.lsp.inlay_hint.enable(true)
vim.keymap.set('n', 'grd', vim.lsp.buf.definition, { desc = "vim.lsp.buf.definition" })
if client:supports_method('textDocument/formatting', args.buf) then if client:supports_method('textDocument/formatting', args.buf) then
vim.keymap.set({ 'n', 'v', 'x' }, 'grf', vim.lsp.buf.format, { desc = "LSP Format" })
vim.b.auto_format = true
vim.api.nvim_create_autocmd("BufWritePre", { vim.api.nvim_create_autocmd("BufWritePre", {
group = vim.api.nvim_create_augroup("AutoFormat", { clear = true }),
buffer = args.buf, buffer = args.buf,
callback = function() callback = function()
vim.lsp.buf.format({ bufnr = args.buf, id = client.id }) if vim.g.auto_format and vim.b.auto_format then
vim.lsp.buf.format({ bufnr = args.buf, id = client.id })
end
end end
}) })
end end
if client:supports_method('textDocument/foldingRange') then
local win = vim.api.nvim_get_current_win()
vim.wo[win][0].foldexpr = 'v:lua.vim.lsp.foldexpr()'
end
if client:supports_method('textDocument/documentColor') then if client:supports_method('textDocument/documentColor') then
vim.lsp.document_color.enable(true, args.buf) vim.lsp.document_color.enable(true, args.buf)
end end
end end
}) })
-- {{{ Commands to toggle auto format
vim.api.nvim_create_user_command("FormatToggle", function()
vim.g.auto_format = not vim.g.auto_format
print("Auto format " .. (vim.g.auto_format and "enabled" or "disabled"))
end, {})
vim.api.nvim_create_user_command("FormatEnable", function()
vim.g.auto_format = true
print("Auto format enabled")
end, {})
vim.api.nvim_create_user_command("FormatDisable", function()
vim.g.auto_format = false
print("Auto format disabled")
end, {})
vim.api.nvim_create_user_command("FormatToggleBuffer", function()
vim.b.auto_format = not vim.b.auto_format
print("Auto format " .. (vim.b.auto_format and "enabled" or "disabled"))
end, {})
vim.api.nvim_create_user_command("FormatEnableBuffer", function()
vim.b.auto_format = true
print("Auto format enabled")
end, {})
vim.api.nvim_create_user_command("FormatDisableBuffer", function()
vim.b.auto_format = false
print("Auto format disabled")
end, {})
-- }}}
vim.diagnostic.config({ vim.diagnostic.config({
jump = { jump = {
virtual_lines = true virtual_lines = true

Loading…