From be558ed299f393dd68d0c19378948b683863887f Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 9 May 2024 18:44:02 +0200 Subject: [PATCH 1/5] feat: add support for yew-fmt Should close #314 :) --- README.md | 1 + doc/conform.txt | 1 + lua/conform/formatters/yew-fmt.lua | 11 +++++++++++ 3 files changed, 13 insertions(+) create mode 100644 lua/conform/formatters/yew-fmt.lua 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/yew-fmt.lua b/lua/conform/formatters/yew-fmt.lua new file mode 100644 index 00000000..15fa8173 --- /dev/null +++ b/lua/conform/formatters/yew-fmt.lua @@ -0,0 +1,11 @@ +-- yew-fmt is a fork of rustfmt +local conf = vim.deepcopy(require("conform.formatters.rustfmt")) + +conf.meta = { + url = "https://github.com/schvv31n/yew-fmt", + description = "Code formatter for the Yew framework.", +} + +conf.command = "yew-fmt" + +return conf From 7a284b6a2879a289de77ed5f6fed2b3f10ded26d Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 9 May 2024 18:55:25 +0200 Subject: [PATCH 2/5] refactor: copy rustfmt to yew-fmt --- lua/conform/formatters/yew-fmt.lua | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lua/conform/formatters/yew-fmt.lua b/lua/conform/formatters/yew-fmt.lua index 15fa8173..f2fcdbef 100644 --- a/lua/conform/formatters/yew-fmt.lua +++ b/lua/conform/formatters/yew-fmt.lua @@ -1,11 +1,16 @@ -- yew-fmt is a fork of rustfmt -local conf = vim.deepcopy(require("conform.formatters.rustfmt")) +local rustfmt = require("conform.formatters.rustfmt") -conf.meta = { - url = "https://github.com/schvv31n/yew-fmt", - description = "Code formatter for the Yew framework.", +---@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 = rustfmt.default_edition, + }, + args = rustfmt.args, } - -conf.command = "yew-fmt" - -return conf From 140d71941f8db492b8da190ab322dd002e918ba9 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 9 May 2024 18:58:08 +0200 Subject: [PATCH 3/5] fix: use correct default_edition --- lua/conform/formatters/yew-fmt.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/conform/formatters/yew-fmt.lua b/lua/conform/formatters/yew-fmt.lua index f2fcdbef..71199f0d 100644 --- a/lua/conform/formatters/yew-fmt.lua +++ b/lua/conform/formatters/yew-fmt.lua @@ -10,7 +10,7 @@ return { command = "yew-fmt", options = { -- The default edition of Rust to use when no Cargo.toml file is found - default_edition = rustfmt.default_edition, + default_edition = rustfmt.options.default_edition, }, args = rustfmt.args, } From 6bbd4d268ea6ea4bc8b3ed1b701a4affcdcefcaa Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 13 May 2024 11:21:21 -0600 Subject: [PATCH 4/5] refactor: extract Cargo.toml parsing into utility function --- lua/conform/formatters/rustfmt.lua | 22 ++-------------------- lua/conform/util.lua | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 20 deletions(-) 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/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 From 97b063ad418103fceee9fbc23ffc28f0a7ef7266 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 13 May 2024 11:22:33 -0600 Subject: [PATCH 5/5] refactor: yew-fmt doesn't rely directly on rustfmt definition --- lua/conform/formatters/yew-fmt.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lua/conform/formatters/yew-fmt.lua b/lua/conform/formatters/yew-fmt.lua index 71199f0d..f441fdff 100644 --- a/lua/conform/formatters/yew-fmt.lua +++ b/lua/conform/formatters/yew-fmt.lua @@ -1,5 +1,4 @@ --- yew-fmt is a fork of rustfmt -local rustfmt = require("conform.formatters.rustfmt") +local util = require("conform.util") ---@type conform.FileFormatterConfig return { @@ -10,7 +9,13 @@ return { command = "yew-fmt", options = { -- The default edition of Rust to use when no Cargo.toml file is found - default_edition = rustfmt.options.default_edition, + default_edition = "2021", }, - args = rustfmt.args, + 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, }