Skip to content

Commit 7b835e3

Browse files
committed
perf: async rustc unpretty command
1 parent cff0267 commit 7b835e3

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

lua/rustaceanvim/cargo.lua

+12-12
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,21 @@ function cargo.get_config_root_dir(config, file_name, callback)
127127
end
128128
end
129129

130-
---@param buf_name? string
131-
---@return string edition
132-
function cargo.get_rustc_edition(buf_name)
130+
---@param callback fun(edition: string)
131+
function cargo.get_rustc_edition(callback)
133132
local config = require('rustaceanvim.config.internal')
134-
buf_name = buf_name or vim.api.nvim_buf_get_name(0)
133+
local buf_name = vim.api.nvim_buf_get_name(0)
135134
local path = vim.fs.dirname(buf_name)
136-
local _, cargo_metadata = get_cargo_metadata(path)
137-
local default_edition = config.tools.rustc.default_edition
138-
if not cargo_metadata then
139-
return default_edition
140-
end
141-
local package = vim.iter(cargo_metadata.packages or {}):find(function(pkg)
142-
return type(pkg.edition) == 'string'
135+
get_cargo_metadata(path, function(_, cargo_metadata)
136+
local default_edition = config.tools.rustc.default_edition
137+
if not cargo_metadata then
138+
return callback(default_edition)
139+
end
140+
local pkg = vim.iter(cargo_metadata.packages or {}):find(function(p)
141+
return type(p.edition) == 'string'
142+
end)
143+
return callback(pkg and pkg.edition or default_edition)
143144
end)
144-
return package and package.edition or default_edition
145145
end
146146

147147
return cargo

lua/rustaceanvim/commands/rustc_unpretty.lua

+14-12
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,20 @@ function M.rustc_unpretty(level)
135135
end
136136
text = table.concat(b, '\n')
137137

138-
vim.system({
139-
rustc,
140-
'--crate-type',
141-
'lib',
142-
'--edition',
143-
cargo.get_rustc_edition(),
144-
'-Z',
145-
'unstable-options',
146-
'-Z',
147-
'unpretty=' .. level,
148-
'-',
149-
}, { stdin = text }, vim.schedule_wrap(handler))
138+
cargo.get_rustc_edition(function(edition)
139+
vim.system({
140+
rustc,
141+
'--crate-type',
142+
'lib',
143+
'--edition',
144+
edition,
145+
'-Z',
146+
'unstable-options',
147+
'-Z',
148+
'unpretty=' .. level,
149+
'-',
150+
}, { stdin = text }, vim.schedule_wrap(handler))
151+
end)
150152
end
151153

152154
return M

0 commit comments

Comments
 (0)