From d6bc345442cdee44ca4bb30847e6e43fc8f90b83 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 18 Apr 2024 21:28:35 +0800 Subject: [PATCH] feat(format): add rustfmt --- bundle/format.nvim/lua/format.lua | 138 +++++++++++----------- bundle/format.nvim/lua/format/ft/rust.lua | 15 +++ 2 files changed, 84 insertions(+), 69 deletions(-) create mode 100644 bundle/format.nvim/lua/format/ft/rust.lua diff --git a/bundle/format.nvim/lua/format.lua b/bundle/format.nvim/lua/format.lua index 499c6e7bd..9f10ea45d 100644 --- a/bundle/format.nvim/lua/format.lua +++ b/bundle/format.nvim/lua/format.lua @@ -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 diff --git a/bundle/format.nvim/lua/format/ft/rust.lua b/bundle/format.nvim/lua/format/ft/rust.lua new file mode 100644 index 000000000..98bd32849 --- /dev/null +++ b/bundle/format.nvim/lua/format/ft/rust.lua @@ -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