diff options
Diffstat (limited to 'lua/conform')
-rw-r--r-- | lua/conform/formatters/rustfmt.lua | 22 | ||||
-rw-r--r-- | lua/conform/formatters/yew-fmt.lua | 21 | ||||
-rw-r--r-- | lua/conform/util.lua | 17 |
3 files changed, 40 insertions, 20 deletions
diff --git a/lua/conform/formatters/rustfmt.lua b/lua/conform/formatters/rustfmt.lua index 62d2d78..e33fb86 100644 --- a/lua/conform/formatters/rustfmt.lua +++ b/lua/conform/formatters/rustfmt.lua @@ -1,15 +1,4 @@ ----@param manifest string ----@return nil|string -local function parse_edition(manifest) - for line in io.lines(manifest) do - if line:match("^edition *=") then - local edition = line:match("%d+") - if edition then - return edition - end - end - end -end +local util = require("conform.util") ---@type conform.FileFormatterConfig return { @@ -24,14 +13,7 @@ return { }, args = function(self, ctx) local args = { "--emit=stdout" } - local edition - local manifest = vim.fs.find("Cargo.toml", { upward = true, path = ctx.dirname })[1] - if manifest then - edition = parse_edition(manifest) - end - if not edition then - edition = self.options.default_edition - end + local edition = util.parse_rust_edition(ctx.dirname) or self.options.default_edition table.insert(args, "--edition=" .. edition) return args diff --git a/lua/conform/formatters/yew-fmt.lua b/lua/conform/formatters/yew-fmt.lua new file mode 100644 index 0000000..f441fdf --- /dev/null +++ b/lua/conform/formatters/yew-fmt.lua @@ -0,0 +1,21 @@ +local util = require("conform.util") + +---@type conform.FileFormatterConfig +return { + meta = { + url = "https://github.com/schvv31n/yew-fmt", + description = "Code formatter for the Yew framework.", + }, + command = "yew-fmt", + options = { + -- The default edition of Rust to use when no Cargo.toml file is found + default_edition = "2021", + }, + args = function(self, ctx) + local args = { "--emit=stdout" } + local edition = util.parse_rust_edition(ctx.dirname) or self.options.default_edition + table.insert(args, "--edition=" .. edition) + + return args + end, +} diff --git a/lua/conform/util.lua b/lua/conform/util.lua index 2f334d0..fe2a306 100644 --- a/lua/conform/util.lua +++ b/lua/conform/util.lua @@ -187,4 +187,21 @@ M.buf_get_changedtick = function(bufnr) end end +---Parse the rust edition from the Cargo.toml file +---@param dir string +---@return string? +M.parse_rust_edition = function(dir) + local manifest = vim.fs.find("Cargo.toml", { upward = true, path = dir })[1] + if manifest then + for line in io.lines(manifest) do + if line:match("^edition *=") then + local edition = line:match("%d+") + if edition then + return edition + end + end + end + end +end + return M |