Skip to content

Commit

Permalink
feat: add support for yew-fmt (#398)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hougesen and stevearc authored May 13, 2024
1 parent 40faaa8 commit b52d462
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions doc/conform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 2 additions & 20 deletions lua/conform/formatters/rustfmt.lua
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lua/conform/formatters/yew-fmt.lua
Original file line number Diff line number Diff line change
@@ -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,
}
17 changes: 17 additions & 0 deletions lua/conform/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b52d462

Please sign in to comment.