feat: add conform formatting keymaps

This commit is contained in:
Youwen Wu 2024-09-17 15:51:01 -07:00
parent 8a9c7c25a7
commit 859e91ada2
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3
2 changed files with 94 additions and 13 deletions

View file

@ -489,14 +489,60 @@
silent = true;
};
}
# {
# action = "<cmd>lua require'conform'.format({ bufnr = args.bf })<CR>";
# key = "<Leader>cf";
# options = {
# silent = true;
# noremap = true;
# desc = "Format buffer";
# };
# }
{
action = {
__raw = ''
function() require('conform').format() end
'';
};
key = "<Leader>cf";
options = {
silent = true;
noremap = true;
desc = "Format buffer";
};
}
{
action = {
__raw = ''
function()
-- we do this instead of `not vim.g.disable_autoformat` so that we
-- can handle when `vim.g.disable_autoformat` has not yet been defined
if (vim.g.disable_autoformat) then
vim.g.disable_autoformat = false
else
vim.g.disable_autoformat = true
end
end
'';
};
key = "<Leader>ctf";
options = {
silent = true;
noremap = true;
desc = "Toggle autoformat globally";
};
}
{
action = {
__raw = ''
-- we do this instead of `not vim.g.disable_autoformat` so that we
-- can handle when `vim.g.disable_autoformat` has not yet been defined
function()
if (vim.b[0].disable_autoformat) then
vim.b[0].disable_autoformat = false
else
vim.b[0].disable_autoformat = true
end
end
'';
};
key = "<Leader>cbf";
options = {
silent = true;
noremap = true;
desc = "Toggle autoformat in buffer";
};
}
];
}

View file

@ -39,10 +39,45 @@
});
# package = pkgs.vimPlugins.conform-nvim;
settings = {
format_on_save = {
timeoutMs = 500;
lspFallback = true;
};
# format_on_save = {
# timeoutMs = 500;
# lspFallback = true;
# };
format_on_save = # Lua
''
function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
-- if slow_format_filetypes[vim.bo[bufnr].filetype] then
-- return
-- end
--
-- local function on_format(err)
-- if err and err:match("timeout$") then
-- slow_format_filetypes[vim.bo[bufnr].filetype] = true
-- end
-- end
--
-- return { timeout_ms = 500, lsp_fallback = true }, on_format
return { timeout_ms = 500, lsp_fallback = true }
end
'';
format_after_save = # Lua
''
function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
-- if not slow_format_filetypes[vim.bo[bufnr].filetype] then
-- return
-- end
return { lsp_fallback = true }
end
'';
formatters_by_ft = {
lua = [ "stylua" ];
python = [ "black" ];