1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-09 09:50:06 +08:00

refactor(format): enable format function

This commit is contained in:
Eric Wong 2024-04-16 13:54:12 +08:00
parent 5b3b5886d4
commit a7fbe2415b
5 changed files with 62 additions and 10 deletions

View File

@ -24,11 +24,19 @@ function M.format(bang, user_input, start_line, end_line)
end end
if not formatter then if not formatter then
ok, formatter = pcall(require, 'format.ft.' .. filetype) ok = pcall(function()
local default = require('format.ft.' .. filetype)
local formatname = default.enabled()[1]
formatter = default[formatname]({
filepath = vim.fn.expand('%:p'),
start_line = start_line,
end_line = end_line,
})
util.info('using default formatter:' .. formatname)
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
util.info('using default formatter:' .. formatter.exe)
end end
task.run({ task.run({

View File

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

View File

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

View File

@ -1,8 +1,16 @@
if vim.fn.executable('prettier') == 1 then local M = {}
function M.enabled()
return { 'prettier' }
end
function M.prettier(opt)
return { return {
exe = vim.fn.exepath('prettier'), exe = vim.fn.exepath('prettier'),
name = 'prettier', name = 'prettier',
args = {'--stdin-filepath', 't.md'}, args = {'--stdin-filepath', opt.filepath},
stdin = true stdin = true
} }
end end
return M

View File

@ -4,5 +4,16 @@ end, {
nargs = '*', nargs = '*',
range = '%', range = '%',
bang = true, bang = true,
bar = true bar = true,
complete = function(_, line)
local ft = vim.o.filetype
local l = vim.split(line, '%s+')
local ok, default = pcall(require, 'format.ft.' .. ft)
if ok then
return vim.tbl_filter(function(val)
return vim.startswith(val, l[#l])
end, default.enabled())
else
end
end,
}) })