|
Hiya, quick question about a problem which is hopefully down to my own incompetence:
init.lua: -- Set up a global autocommand to restart LSP after direnv loads.
-- Maybe this should go elsewhere?
vim.api.nvim_create_autocmd("User", {
pattern = { "DirenvReady" },
callback = function()
-- Only restart if the current file is a Rust file.
if vim.bo.filetype == "rust" then
require("rustaceanvim.lsp").restart()
end
end,
})plugins.lua: require("lze").load({
{
"direnv.nvim",
for_cat = "general",
-- lazy = false, -- Tried this but it didn't seem to help
after = function()
require("direnv-nvim").setup({
-- type = "dir",
async = true, -- Seems to have no impact
})
end,
},
{
"rustaceanvim",
for_cat = "general",
-- lazy = false, -- Tried this and it didn't make much of a difference.
-- event = { "User DirenvReady" }, -- I tried this but it didn't seem to make much of a difference.
before = function()
vim.g.rustaceanvim = {
server = {
on_attach = function(client, bufnr)
if vim.bo.filetype == "rust" then
vim.keymap.set("n", "<leader>ca", function()
vim.cmd.RustLsp("codeAction") -- supports rust-analyzer's grouping
-- or vim.lsp.buf.codeAction() if you don't want grouping.
end, { noremap = true, silent = true, buffer = bufnr })
vim.keymap.set("n", "<leader>cz", function()
vim.cmd.RustLsp("codeAction") -- supports rust-analyzer's grouping
-- or vim.lsp.buf.codeAction() if you don't want grouping.
end, { noremap = true, silent = true, buffer = bufnr })
vim.keymap.set(
"n",
"K", -- Override Neovim's built-in hover keymap with rustaceanvim's hover actions
function()
vim.cmd.RustLsp({ "hover", "actions" })
end,
{ noremap = true, silent = true, buffer = bufnr }
)
end
end,
},
}
end,
},
)}It seems as if rustaceanvim loads before direnv is ready, and therefore because rust-analyzer is not available, vim.g.rustaceanvim isn't loaded properly and can't start, or something to that effect. what am I doing wrong here? edit: I should add that this specifically applies to if I switch to a session for which the focussed buffer is a .rs window. If it is a nix file (for which i use lspconfig and pre-installed stuff), the lsploads, and then if i switch to a .rs buffer, rust-analyzer loads. |
Replies: 1 comment
|
I got it working! In case someone stumbles across this same issue I'll post what I came up with here: {
"direnv.nvim",
for_cat = "general",
after = function()
require("direnv-nvim").setup({
async = true,
on_direnv_finished = function()
vim.cmd("LspStart")
if vim.bo.filetype == "rust" then
require("rustaceanvim.lsp").start()
end
end,
})
end,
},
{
"rustaceanvim",
for_cat = "general",
lazy = false,
before = function()
vim.g.rustaceanvim = {
server = {
on_attach = function(client, bufnr)
-- your config for what you want to do when you attach
end,
},
}
end,
},
no other config needed. |
I got it working! In case someone stumbles across this same issue I'll post what I came up with here:
{ "direnv.nvim", for_cat = "general", after = function() require("direnv-nvim").setup({ async = true, on_direnv_finished = function() vim.cmd("LspStart") if vim.bo.filetype == "rust" then require("rustaceanvim.lsp").start() end end, }) end, }, { "rustaceanvim", for_cat = "general", lazy = false, before = function() vim.g.rustaceanvim = { server = { on_attach = function(client, bufnr) -- your config for what you want to do when you attach end, }, } end, },no other config needed.
if someone sees any…