1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 10:40:03 +08:00

feat(format): add rustfmt

This commit is contained in:
Eric Wong 2024-04-18 21:28:35 +08:00
parent 732401aee9
commit d6bc345442
2 changed files with 84 additions and 69 deletions

View File

@ -1,69 +1,69 @@
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!')
end
local filetype = vim.o.filetype
local argvs = vim.split(user_input, '%s+')
if bang and #argvs > 0 then
filetype = argvs[1]
end
if filetype == '' then
return util.msg('format: skip empty filetype')
end
local ok, formatter
if custom_formatters[filetype] then
formatter = custom_formatters[filetype]
if formatter.exe and type(formatter.exe) == 'string' then
util.info('using custom formatter:' .. formatter.exe)
end
end
if not formatter then
ok = pcall(function()
local default = require('format.ft.' .. filetype)
for _, formatname in ipairs(default.enabled()) do
formatter = default[formatname]({
filepath = vim.fn.expand('%:p'),
start_line = start_line,
end_line = end_line,
})
if vim.fn.executable(formatter.exe) == 1 then
util.info('using default formatter:' .. formatname)
break
end
end
end)
if not ok then
return util.msg('no formatter for ' .. filetype)
end
end
task.run({
bufnr = vim.fn.bufnr(),
stdin = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false),
start_line = start_line - 1,
end_line = end_line,
formatter = formatter,
})
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
if type(v) == 'table' then
custom_formatters[k] = v
end
end
end
end
return M
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!')
end
local filetype = vim.o.filetype
local argvs = vim.split(user_input, '%s+')
if bang and #argvs > 0 then
filetype = argvs[1]
end
if filetype == '' then
return util.msg('format: skip empty filetype')
end
local ok, formatter
if custom_formatters[filetype] then
formatter = custom_formatters[filetype]
if formatter.exe and type(formatter.exe) == 'string' then
util.info('using custom formatter:' .. formatter.exe)
end
end
if not formatter then
ok = pcall(function()
local default = require('format.ft.' .. filetype)
for _, formatname in ipairs(default.enabled()) do
formatter = default[formatname]({
filepath = vim.fn.expand('%:p'),
start_line = start_line,
end_line = end_line,
})
if vim.fn.executable(formatter.exe) == 1 then
util.info('using default formatter:' .. formatname)
break
end
end
end)
if not ok then
return util.msg('no formatter for ' .. filetype)
end
end
task.run({
bufnr = vim.fn.bufnr(),
stdin = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false),
start_line = start_line - 1,
end_line = end_line,
formatter = formatter,
})
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
if type(v) == 'table' then
custom_formatters[k] = v
end
end
end
end
return M

View File

@ -0,0 +1,15 @@
local M = {}
function M.enabled()
return { 'rustfmt' }
end
function M.rustfmt(opt)
return {
exe = 'rustfmt',
args = {},
stdin = true,
}
end
return M