Skip to content

Commit

Permalink
feat: auto-detect the rustc edition
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jul 29, 2024
1 parent 029ae8e commit d69d260
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
when using cargo-nextest 0.9.7 [[#460](https://github.com/mrcjkb/rustaceanvim/issues/460)].
- Neotest: Disable ansi colour coding in output to ensure output can be parsed.
- `Rustc unpretty`: Support Windows.
- Auto-detect the `rustc` edition.
This deprecates the `vim.g.rustaceanvim.tools.rustc.edition` option
in favour of `vim.g.rustaceanvim.tools.rustc.default_edition`.

## [5.1.0] - 2024-07-27

Expand Down
7 changes: 4 additions & 3 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,10 @@ rustaceanvim.crate-graph.Opts *rustaceanvim.crate-graph.Opts*
rustaceanvim.rustc.Opts *rustaceanvim.rustc.Opts*

Fields: ~
{edition} (string)
The edition to use. See https://rustc-dev-guide.rust-lang.org/guides/editions.html.
Default '2021'.
{default_edition?} (string)
The default edition to use if it cannot be auto-detected.
See https://rustc-dev-guide.rust-lang.org/guides/editions.html.
Default '2021'.


rustaceanvim.lsp.ClientOpts *rustaceanvim.lsp.ClientOpts*
Expand Down
26 changes: 24 additions & 2 deletions lua/rustaceanvim/cargo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ local function get_cargo_metadata(path)
return cargo_crate_dir
end

---@param buf_name? string
---@return string edition
function cargo.get_rustc_edition(buf_name)
local config = require('rustaceanvim.config.internal')
---@diagnostic disable-next-line: undefined-field
if config.tools.rustc.edition then
vim.deprecate('vim.g.rustaceanvim.config.tools.edition', 'default_edition', '6.0.0', 'rustaceanvim')
---@diagnostic disable-next-line: undefined-field
return config.tools.rustc.edition
end
buf_name = buf_name or vim.api.nvim_buf_get_name(0)
local path = vim.fs.dirname(buf_name)
local _, cargo_metadata = get_cargo_metadata(path)
local default_edition = config.tools.rustc.default_edition
if not cargo_metadata then
return default_edition
end
local package = vim.iter(cargo_metadata.packages or {}):find(function(pkg)
return type(pkg.edition) == 'string'
end)
return package and package.edition or default_edition
end

---The default implementation used for `vim.g.rustaceanvim.server.root_dir`
---@param file_name string
---@return string | nil root_dir
Expand All @@ -85,8 +108,7 @@ function cargo.get_root_dir(file_name)
return nil
end
local cargo_crate_dir, cargo_metadata = get_cargo_metadata(path)
local cargo_workspace_dir = cargo_metadata and vim.print(cargo_metadata['workspace_root'])
return cargo_workspace_dir
return cargo_metadata and cargo_metadata.workspace_root
or cargo_crate_dir
---@diagnostic disable-next-line: missing-fields
or vim.fs.dirname(vim.fs.find({ 'rust-project.json' }, {
Expand Down
4 changes: 2 additions & 2 deletions lua/rustaceanvim/commands/rustc_unpretty.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local M = {}

local config = require('rustaceanvim.config.internal')
local cargo = require('rustaceanvim.cargo')
local ui = require('rustaceanvim.ui')
local api = vim.api
local ts = vim.treesitter
Expand Down Expand Up @@ -139,7 +139,7 @@ function M.rustc_unpretty(level)
'--crate-type',
'lib',
'--edition',
config.tools.rustc.edition,
cargo.get_rustc_edition(),
'-Z',
'unstable-options',
'-Z',
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function M.validate(cfg)
end
local rustc = tools.rustc
ok, err = validate('tools.rustc', {
edition = { rustc.edition, 'string' },
default_edition = { rustc.default_edition, 'string' },
})
if not ok then
return false, err
Expand Down
5 changes: 3 additions & 2 deletions lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ vim.g.rustaceanvim = vim.g.rustaceanvim

---@class rustaceanvim.rustc.Opts
---
---The edition to use. See https://rustc-dev-guide.rust-lang.org/guides/editions.html.
---The default edition to use if it cannot be auto-detected.
---See https://rustc-dev-guide.rust-lang.org/guides/editions.html.
---Default '2021'.
---@field edition string
---@field default_edition? string

---@class rustaceanvim.lsp.ClientOpts
---
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ local RustaceanDefaultConfig = {
---@class rustaceanvim.rustc.Config
rustc = {
---@type string
edition = '2021',
default_edition = '2021',
},
},

Expand Down

0 comments on commit d69d260

Please sign in to comment.