Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for yew-fmt #398

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions doc/conform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
Loading