aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--doc/formatter_options.md10
-rw-r--r--lua/conform/formatters/rustfmt.lua15
3 files changed, 25 insertions, 1 deletions
diff --git a/README.md b/README.md
index d6fc078..4afc81a 100644
--- a/README.md
+++ b/README.md
@@ -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 -->
diff --git a/doc/formatter_options.md b/doc/formatter_options.md
index e5c8493..6dab826 100644
--- a/doc/formatter_options.md
+++ b/doc/formatter_options.md
@@ -4,6 +4,7 @@
- [injected](#injected)
- [prettier](#prettier)
+- [rustfmt](#rustfmt)
<!-- /TOC -->
@@ -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 -->
diff --git a/lua/conform/formatters/rustfmt.lua b/lua/conform/formatters/rustfmt.lua
index d8f0f19..34fb09b 100644
--- a/lua/conform/formatters/rustfmt.lua
+++ b/lua/conform/formatters/rustfmt.lua
@@ -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,
}