mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-23 17:49:57 +08:00
feat(format): add rustfmt
This commit is contained in:
parent
732401aee9
commit
d6bc345442
@ -1,69 +1,69 @@
|
|||||||
local M = {}
|
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 = {}
|
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!')
|
||||||
end
|
end
|
||||||
|
|
||||||
local filetype = vim.o.filetype
|
local filetype = vim.o.filetype
|
||||||
local argvs = vim.split(user_input, '%s+')
|
local argvs = vim.split(user_input, '%s+')
|
||||||
if bang and #argvs > 0 then
|
if bang and #argvs > 0 then
|
||||||
filetype = argvs[1]
|
filetype = argvs[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
local ok, formatter
|
||||||
if custom_formatters[filetype] then
|
if custom_formatters[filetype] then
|
||||||
formatter = custom_formatters[filetype]
|
formatter = custom_formatters[filetype]
|
||||||
if formatter.exe and type(formatter.exe) == 'string' then
|
if formatter.exe and type(formatter.exe) == 'string' then
|
||||||
util.info('using custom formatter:' .. formatter.exe)
|
util.info('using custom formatter:' .. formatter.exe)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not formatter then
|
if not formatter then
|
||||||
ok = pcall(function()
|
ok = pcall(function()
|
||||||
local default = require('format.ft.' .. filetype)
|
local default = require('format.ft.' .. filetype)
|
||||||
for _, formatname in ipairs(default.enabled()) do
|
for _, formatname in ipairs(default.enabled()) do
|
||||||
formatter = default[formatname]({
|
formatter = default[formatname]({
|
||||||
filepath = vim.fn.expand('%:p'),
|
filepath = vim.fn.expand('%:p'),
|
||||||
start_line = start_line,
|
start_line = start_line,
|
||||||
end_line = end_line,
|
end_line = end_line,
|
||||||
})
|
})
|
||||||
if vim.fn.executable(formatter.exe) == 1 then
|
if vim.fn.executable(formatter.exe) == 1 then
|
||||||
util.info('using default formatter:' .. formatname)
|
util.info('using default formatter:' .. formatname)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
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
|
end
|
||||||
|
|
||||||
task.run({
|
task.run({
|
||||||
bufnr = vim.fn.bufnr(),
|
bufnr = vim.fn.bufnr(),
|
||||||
stdin = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false),
|
stdin = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false),
|
||||||
start_line = start_line - 1,
|
start_line = start_line - 1,
|
||||||
end_line = end_line,
|
end_line = end_line,
|
||||||
formatter = formatter,
|
formatter = formatter,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(opt)
|
function M.setup(opt)
|
||||||
if opt.custom_formatters and type(opt.custom_formatters) == 'table' then
|
if opt.custom_formatters and type(opt.custom_formatters) == 'table' then
|
||||||
for k, v in pairs(opt.custom_formatters) do
|
for k, v in pairs(opt.custom_formatters) do
|
||||||
if type(v) == 'table' then
|
if type(v) == 'table' then
|
||||||
custom_formatters[k] = v
|
custom_formatters[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
15
bundle/format.nvim/lua/format/ft/rust.lua
Normal file
15
bundle/format.nvim/lua/format/ft/rust.lua
Normal 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
|
Loading…
x
Reference in New Issue
Block a user