From b52d462cb7bea5e81174ece43eb349357add2f11 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Mon, 13 May 2024 19:23:40 +0200 Subject: feat: add support for yew-fmt (#398) * feat: add support for yew-fmt Should close #314 :) * refactor: copy rustfmt to yew-fmt * fix: use correct default_edition * refactor: extract Cargo.toml parsing into utility function * refactor: yew-fmt doesn't rely directly on rustfmt definition --------- Co-authored-by: Steven Arcangeli --- lua/conform/formatters/rustfmt.lua | 22 ++-------------------- lua/conform/formatters/yew-fmt.lua | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 lua/conform/formatters/yew-fmt.lua (limited to 'lua/conform/formatters') 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, +} -- cgit v1.2.3-70-g09d2