1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 11:20:06 +08:00

feat(format): support custom format

This commit is contained in:
Eric Wong 2024-04-16 12:24:21 +08:00
parent d393df8ada
commit 4db99ea058

View File

@ -3,6 +3,8 @@ local M = {}
local util = require('format.util')
local task = require('format.task')
local custom_formatters = {}
function M.format(bang, user_input, start_line, end_line)
if not vim.o.modifiable then
return util.msg('buffer is not modifiable!')
@ -13,11 +15,16 @@ function M.format(bang, user_input, start_line, end_line)
if filetype == '' then
return util.msg('format: skip empty filetype')
end
local ok, formatter
if custom_formatters[filetype] then
formatter = custom_formatters[filetype]
end
local ok, default_formatter = pcall(require, 'format.ft.' .. filetype)
if not ok then
return util.msg('no formatter for ' .. filetype)
if not formatter then
ok, formatter = pcall(require, 'format.ft.' .. filetype)
if not ok then
return util.msg('no formatter for ' .. filetype)
end
end
task.run({
@ -25,10 +32,15 @@ function M.format(bang, user_input, start_line, end_line)
stdin = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line - 1, false),
start_line = start_line - 1,
end_line = end_line - 1,
formatter = default_formatter
formatter = formatter,
})
end
function M.setup(opts) end
function M.setup(opt)
if opt.custom_formatters and type(opt.custom_formatters) == 'table' then
for k, v in pairs(opt.custom_formatters) do
custom_formatters[k] = v
end
end
end
return M