Skip to content

Commit e2fca30

Browse files
committed
feat: add range formatting to formatter
1 parent 6ba2408 commit e2fca30

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

init.lua

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -740,16 +740,6 @@ require('lazy').setup({
740740
'stevearc/conform.nvim',
741741
event = { 'BufWritePre' },
742742
cmd = { 'ConformInfo' },
743-
keys = {
744-
{
745-
'<leader>f',
746-
function()
747-
require('conform').format { async = true, lsp_format = 'fallback' }
748-
end,
749-
mode = '',
750-
desc = '[F]ormat buffer',
751-
},
752-
},
753743
opts = {
754744
notify_on_error = false,
755745
format_on_save = function(bufnr)
@@ -774,6 +764,22 @@ require('lazy').setup({
774764
-- You can use 'stop_after_first' to run the first available formatter from the list
775765
-- javascript = { "prettierd", "prettier", stop_after_first = true },
776766
},
767+
vim.api.nvim_create_user_command('Format', function(args)
768+
local range = nil
769+
if args.count ~= -1 then
770+
local end_line = vim.api.nvim_buf_gt_lines(0, args.line2 - 1, args.line2, true)[1]
771+
range = {
772+
start = { args.line1, 0 },
773+
['end'] = { args.line2, end_line:len() },
774+
}
775+
end
776+
require('conform').format {
777+
async = true,
778+
lsp_format = 'fallback',
779+
range = range,
780+
}
781+
end, { range = true }),
782+
vim.keymap.set({ 'n', 'v' }, '<leader>f', '<Cmd>Format<CR>'),
777783
},
778784
},
779785

0 commit comments

Comments
 (0)