diff --git a/README.md b/README.md index 2ff572bf..ec29aa26 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,7 @@ You can view this list in vim with `:help conform-formatters` - [yamlfix](https://github.com/lyz-code/yamlfix) - A configurable YAML formatter that keeps comments. - [yamlfmt](https://github.com/google/yamlfmt) - yamlfmt is an extensible command line tool or library to format yaml files. - [yapf](https://github.com/google/yapf) - Yet Another Python Formatter. +- [yew-fmt](https://github.com/schvv31n/yew-fmt) - Code formatter for the Yew framework. - [yq](https://github.com/mikefarah/yq) - YAML/JSON processor - [zigfmt](https://github.com/ziglang/zig) - Reformat Zig source into canonical form. - [zprint](https://github.com/kkinnear/zprint) - Formatter for Clojure and EDN. diff --git a/doc/conform.txt b/doc/conform.txt index 977a0f06..cd543a09 100644 --- a/doc/conform.txt +++ b/doc/conform.txt @@ -368,6 +368,7 @@ FORMATTERS *conform-formatter `yamlfmt` - yamlfmt is an extensible command line tool or library to format yaml files. `yapf` - Yet Another Python Formatter. +`yew-fmt` - Code formatter for the Yew framework. `yq` - YAML/JSON processor `zigfmt` - Reformat Zig source into canonical form. `zprint` - Formatter for Clojure and EDN. diff --git a/lua/conform/formatters/rustfmt.lua b/lua/conform/formatters/rustfmt.lua index 62d2d781..e33fb862 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 00000000..f441fdff --- /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 2f334d07..fe2a306e 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