1
0
Fork 0
mirror of https://github.com/pnx/dotfiles synced 2026-06-16 03:14:55 +02:00

nvim: conform: support range formatting

This commit is contained in:
Henrik Hautakoski 2024-11-03 13:41:53 +01:00
parent d82420ffeb
commit e9de80a97e
2 changed files with 14 additions and 4 deletions

View file

@ -10,13 +10,13 @@ if user.formatter == nil then
user.formatter = [[mfgggqG`f=zz]] user.formatter = [[mfgggqG`f=zz]]
end end
vim.api.nvim_create_user_command('Format', function() vim.api.nvim_create_user_command('Format', function(args)
if type(user.formatter) == "string" then if type(user.formatter) == "string" then
local cmd = vim.api.nvim_replace_termcodes(user.formatter, true, false, true) local cmd = vim.api.nvim_replace_termcodes(user.formatter, true, false, true)
vim.api.nvim_feedkeys(cmd, 'n', false) vim.api.nvim_feedkeys(cmd, 'n', false)
elseif type(user.formatter) == "function" then elseif type(user.formatter) == "function" then
user.formatter() user.formatter(args)
else else
vim.api.nvim_err_writeln("No formatter found") vim.api.nvim_err_writeln("No formatter found")
end end
end, {}) end, { range = true })

View file

@ -105,7 +105,17 @@ return {
}, },
init = function() init = function()
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
user.formatter = ":lua require'conform'.format({ async = true })<CR>:<DEL>" user.formatter = function (args)
local range = nil
if args.count ~= -1 then
local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
range = {
start = { args.line1, 0 },
["end"] = { args.line2, end_line:len() },
}
end
require("conform").format({ async = true, range = range })
end
end end
}, },
{ import = "user.plugins.editor.fuzzyfinder" }, { import = "user.plugins.editor.fuzzyfinder" },