Skip to content

Commit

Permalink
fix(rustfmt): use Cargo.toml settings and default to recent edition (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Mar 13, 2024
1 parent db2c697 commit 0ff1b7d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ require("conform").formatters.my_formatter = {

- [injected](doc/formatter_options.md#injected)
- [prettier](doc/formatter_options.md#prettier)
- [rustfmt](doc/formatter_options.md#rustfmt)

<!-- /FORMATTER_OPTIONS -->

Expand Down
10 changes: 10 additions & 0 deletions doc/formatter_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [injected](#injected)
- [prettier](#prettier)
- [rustfmt](#rustfmt)

<!-- /TOC -->

Expand Down Expand Up @@ -85,4 +86,13 @@ options = {
}
```

## rustfmt

```lua
options = {
-- The default edition of Rust to use when no Cargo.toml file is found
default_edition = "2021",
}
```

<!-- /OPTIONS -->
15 changes: 14 additions & 1 deletion lua/conform/formatters/rustfmt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,18 @@ return {
description = "A tool for formatting rust code according to style guidelines.",
},
command = "rustfmt",
args = { "--emit=stdout" },
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 manifest = vim.fs.find("Cargo.toml", { upward = true, path = ctx.dirname })[1]
if manifest then
table.insert(args, "--manifest-path=" .. manifest)
elseif self.options.default_edition then
table.insert(args, "--edition=" .. self.options.default_edition)
end
return args
end,
}

0 comments on commit 0ff1b7d

Please sign in to comment.