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 --- README.md | 1 + doc/conform.txt | 1 + lua/conform/formatters/rustfmt.lua | 22 ++-------------------- lua/conform/formatters/yew-fmt.lua | 21 +++++++++++++++++++++ lua/conform/util.lua | 17 +++++++++++++++++ 5 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 lua/conform/formatters/yew-fmt.lua diff --git a/README.md b/README.md index d33421a..d9ce5f9 100644 --- a/README.md +++ b/README.md @@ -319,6 +319,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 a4981d0..834822e 100644 --- a/doc/conform.txt +++ b/doc/conform.txt @@ -373,6 +373,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 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 -- cgit v1.2.3-70-g09d2