mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-02 23:10:06 +08:00
feat(format): support custom format
This commit is contained in:
parent
d393df8ada
commit
4db99ea058
@ -3,6 +3,8 @@ local M = {}
|
|||||||
local util = require('format.util')
|
local util = require('format.util')
|
||||||
local task = require('format.task')
|
local task = require('format.task')
|
||||||
|
|
||||||
|
local custom_formatters = {}
|
||||||
|
|
||||||
function M.format(bang, user_input, start_line, end_line)
|
function M.format(bang, user_input, start_line, end_line)
|
||||||
if not vim.o.modifiable then
|
if not vim.o.modifiable then
|
||||||
return util.msg('buffer is not modifiable!')
|
return util.msg('buffer is not modifiable!')
|
||||||
@ -13,11 +15,16 @@ function M.format(bang, user_input, start_line, end_line)
|
|||||||
if filetype == '' then
|
if filetype == '' then
|
||||||
return util.msg('format: skip empty filetype')
|
return util.msg('format: skip empty filetype')
|
||||||
end
|
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 formatter then
|
||||||
|
ok, formatter = pcall(require, 'format.ft.' .. filetype)
|
||||||
if not ok then
|
if not ok then
|
||||||
return util.msg('no formatter for ' .. filetype)
|
return util.msg('no formatter for ' .. filetype)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
task.run({
|
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),
|
stdin = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line - 1, false),
|
||||||
start_line = start_line - 1,
|
start_line = start_line - 1,
|
||||||
end_line = end_line - 1,
|
end_line = end_line - 1,
|
||||||
formatter = default_formatter
|
formatter = formatter,
|
||||||
})
|
})
|
||||||
end
|
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
|
return M
|
||||||
|
Loading…
Reference in New Issue
Block a user