diff options
author | Steven Arcangeli <506791+stevearc@users.noreply.github.com> | 2024-03-13 08:43:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-13 08:43:23 -0700 |
commit | 0ff1b7d32fd3e8df194ca5ebec1dab9c61fb9911 (patch) | |
tree | 8cb5057734a5700dee5e0dc920ed73d55524d4db | |
parent | db2c697fe8302f0328b50b480204be1b577a1e2f (diff) |
fix(rustfmt): use Cargo.toml settings and default to recent edition (#328)
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | doc/formatter_options.md | 10 | ||||
-rw-r--r-- | lua/conform/formatters/rustfmt.lua | 15 |
3 files changed, 25 insertions, 1 deletions
@@ -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, } |