-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rust): add bacon-ls and improve debugging #3212
base: main
Are you sure you want to change the base?
Conversation
|
i just discovered bacon, and it looks awesome, but i don't think lazyvim should swap the default rust LSP everyone is used to, and assume is used ? maybe as a extra extra ? like rust-bacon ? |
This MR does not disable rust-analyzer at all, it just disables the diagnostic if bacon and bacon-ls are used. To me this is pertinent to the Rust extra, but I'll defer to people more experienced with LazyVim. |
|
Thanks for the explanation. here is a repro.lua (include the rust.lua, your version not the merged one) -- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")
-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)
vim.g.lazyvim_rust_diagnostics = "bacon-ls"
if lazyvim_docs then
-- LSP Server to use for Rust.
-- Set to "bacon-ls" to use bacon-ls instead of rust-analyzer.
-- only for diagnostics. The rest of LSP support will still be
-- provided by rust-analyzer.
vim.g.lazyvim_rust_diagnostics = "rust-analyzer"
end
local diagnostics = vim.g.lazyvim_rust_diagnostics or "rust-analyzer"
-- install plugins
local plugins = {
"folke/tokyonight.nvim",
{ "LazyVim/LazyVim", import = "lazyvim.plugins" },
-- add any other plugins here
-- Extend auto completion
{
"hrsh7th/nvim-cmp",
dependencies = {
{
"Saecki/crates.nvim",
event = { "BufRead Cargo.toml" },
opts = {
completion = {
cmp = { enabled = true },
},
},
},
},
---@param opts cmp.ConfigSchema
opts = function(_, opts)
opts.sources = opts.sources or {}
table.insert(opts.sources, { name = "crates" })
end,
},
-- Add Rust & related to treesitter
{
"nvim-treesitter/nvim-treesitter",
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, { "ron", "rust", "toml" })
end,
},
-- Ensure Rust debugger is installed
{
"williamboman/mason.nvim",
optional = true,
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, { "codelldb" })
if diagnostics == "bacon-ls" then
vim.list_extend(opts.ensure_installed, { "bacon", "bacon-ls" })
end
end,
},
{
"mrcjkb/rustaceanvim",
version = "^4", -- Recommended
ft = { "rust" },
opts = {
server = {
on_attach = function(_, bufnr)
vim.keymap.set("n", "<leader>cR", function()
vim.cmd.RustLsp("codeAction")
end, { desc = "Code Action", buffer = bufnr })
vim.keymap.set("n", "<leader>dr", function()
vim.cmd.RustLsp("debuggables")
end, { desc = "Rust Debuggables", buffer = bufnr })
end,
default_settings = {
-- rust-analyzer language server configuration
["rust-analyzer"] = {
cargo = {
allFeatures = true,
loadOutDirsFromCheck = true,
buildScripts = {
enable = true,
},
},
-- Enable diagnostics if using rust-analyzer
diagnostics = {
enable = diagnostics == "rust-analyzer",
},
-- Add clippy lints for Rust.
checkOnSave = {
enable = diagnostics == "rust-analyzer",
allFeatures = true,
command = "clippy",
extraArgs = { "--no-deps" },
},
procMacro = {
enable = true,
ignored = {
["async-trait"] = { "async_trait" },
["napi-derive"] = { "napi" },
["async-recursion"] = { "async_recursion" },
},
},
},
},
},
},
config = function(_, opts)
-- Configure dap with codelldb
local package_path = require("mason-registry").get_package("codelldb"):get_install_path()
opts.dap = {
adapter = require("rustaceanvim.config").get_codelldb_adapter(
package_path .. "/codelldb",
package_path .. "/extension/lldb/lib/liblldb.dylib"
),
}
vim.g.rustaceanvim = vim.tbl_deep_extend("keep", vim.g.rustaceanvim or {}, opts or {})
end,
},
-- Correctly setup lspconfig for Rust 🚀
{
"neovim/nvim-lspconfig",
opts = {
servers = {
rust_analyzer = {
enabled = false,
},
bacon_ls = {
enabled = diagnostics == "bacon-ls",
},
taplo = {
keys = {
{
"K",
function()
if vim.fn.expand("%:t") == "Cargo.toml" and require("crates").popup_available() then
require("crates").show_popup()
else
vim.lsp.buf.hover()
end
end,
desc = "Show Crate Documentation",
},
},
},
},
setup = {
rust_analyzer = function()
return true
end,
},
},
},
{
"nvim-neotest/neotest",
optional = true,
opts = function(_, opts)
opts.adapters = opts.adapters or {}
vim.list_extend(opts.adapters, {
require("rustaceanvim.neotest"),
})
end,
},
}
require("lazy").setup(plugins, {
root = root .. "/plugins",
})
vim.cmd.colorscheme("tokyonight")
-- add anything else hereand i get some |
|
I also tried your PR and don't get any diagnostics from I left out the rust-analyzer stuff in if lazyvim_docs then
-- LSP Server to use for Rust.
-- Set to "bacon-ls" to use bacon-ls instead of rust-analyzer.
-- only for diagnostics. The rest of LSP support will still be
-- provided by rust-analyzer.
vim.g.lazyvim_rust_diagnostics = "rust-analyzer"
end
local diagnostics = vim.g.lazyvim_rust_diagnostics or "rust-analyzer"
return {
recommended = function()
return LazyVim.extras.wants({
ft = "rust",
root = { "Cargo.toml", "rust-project.json" },
})
end,
-- Extend auto completion
{
"hrsh7th/nvim-cmp",
dependencies = {
{
"Saecki/crates.nvim",
event = { "BufRead Cargo.toml" },
opts = {
completion = {
cmp = { enabled = true },
},
},
},
},
---@param opts cmp.ConfigSchema
opts = function(_, opts)
opts.sources = opts.sources or {}
table.insert(opts.sources, { name = "crates" })
end,
},
-- Add Rust & related to treesitter
{
"nvim-treesitter/nvim-treesitter",
opts = { ensure_installed = { "rust", "ron" } },
},
-- Ensure Rust debugger is installed
{
"williamboman/mason.nvim",
optional = true,
opts = function(_, opts)
opts.ensure_installed = opts.ensure_installed or {}
vim.list_extend(opts.ensure_installed, { "codelldb" })
if diagnostics == "bacon-ls" then
vim.list_extend(opts.ensure_installed, { "bacon", "bacon-ls" })
end
end,
},
{
"mrcjkb/rustaceanvim",
version = "^4", -- Recommended
ft = { "rust" },
opts = {
server = {
on_attach = function(_, bufnr)
vim.keymap.set("n", "<leader>cR", function()
vim.cmd.RustLsp("codeAction")
end, { desc = "Code Action", buffer = bufnr })
vim.keymap.set("n", "<leader>dr", function()
vim.cmd.RustLsp("debuggables")
end, { desc = "Rust Debuggables", buffer = bufnr })
end,
default_settings = {
-- rust-analyzer language server configuration
["rust-analyzer"] = {
cargo = {
allFeatures = true,
loadOutDirsFromCheck = true,
buildScripts = {
enable = true,
},
},
-- Enable diagnostics if using rust-analyzer
diagnostics = {
enable = diagnostics == "rust-analyzer",
},
-- Add clippy lints for Rust.
checkOnSave = {
enable = diagnostics == "rust-analyzer",
allFeatures = true,
command = "clippy",
extraArgs = { "--no-deps" },
},
procMacro = {
enable = true,
ignored = {
["async-trait"] = { "async_trait" },
["napi-derive"] = { "napi" },
["async-recursion"] = { "async_recursion" },
},
},
},
},
},
},
config = function(_, opts)
-- Configure dap with codelldb
local package_path = require("mason-registry").get_package("codelldb"):get_install_path()
opts.dap = {
adapter = require("rustaceanvim.config").get_codelldb_adapter(
package_path .. "/codelldb",
package_path .. "/extension/lldb/lib/liblldb.dylib"
),
}
vim.g.rustaceanvim = vim.tbl_deep_extend("keep", vim.g.rustaceanvim or {}, opts or {})
if vim.fn.executable("rust-analyzer") == 0 then
LazyVim.error(
"**rust-analyzer** not found in PATH, please install it.\nhttps://rust-analyzer.github.io/",
{ title = "rustaceanvim" }
)
end
end,
},
-- Correctly setup lspconfig for Rust 🚀
{
"neovim/nvim-lspconfig",
opts = {
servers = {
bacon_ls = {
enabled = diagnostics == "bacon-ls",
},
taplo = {
keys = {
{
"K",
function()
if vim.fn.expand("%:t") == "Cargo.toml" and require("crates").popup_available() then
require("crates").show_popup()
else
vim.lsp.buf.hover()
end
end,
desc = "Show Crate Documentation",
},
},
},
},
},
},
{
"nvim-neotest/neotest",
optional = true,
opts = {
adapters = {
["rustaceanvim.neotest"] = {},
},
},
},
} |
|
So, I started |
|
Bacon requires a specific configuration to work with the language server. Have you followed the installation instructions here by any chance here https://github.com/crisidev/bacon-ls/blob/main/README.md? |
|
Personally no I didn't check further, sorry, as usually lsp are plug and play, especially in lazyvim. i don't know where to put the [export]
enabled = true
path = ".bacon-locations"
line_format = "{kind}:{path}:{line}:{column}:{message}"but i guess it's not required as there is the same config in lazyvim spec. thanks ! |
|
I'll try to finish the PR early next week btw, the mason.nvim inclusion for bacon and bacon-ls was merged a couple of days ago. |
|
In the same directory where I left the |
|
Following @dpetka2001 instruction and my repro.lua i got it working edit: it's my personal opinion, just throwing some first impression
It would need a few things for it to be usable for me: |
|
@mirsella Seriously? How? Why is it not working for me? I'll try your |
|
@dpetka2001 i was using mason's bacon and bacon-ls btw don't know if it can change anything 🤷 |
|
Can you share the content of I'll try to use the repro this weekend to check what's going on with your config, because "it works on my computer" 😞 |
|
Funnily enough I just created a new Rust project with |
|
This is ready for review now! |
Co-authored-by: Iordanis Petkakis <12776461+dpetka2001@users.noreply.github.com>
| opts.ensure_installed = opts.ensure_installed or {} | ||
| vim.list_extend(opts.ensure_installed, { "codelldb" }) | ||
| if diagnostics == "bacon-ls" then | ||
| vim.list_extend(opts.ensure_installed, { "bacon", "bacon-ls" }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also remove bacon-ls from mason.ensure_installed, since LSP servers that are defined in nvim-lspconfig spec get installed automatically if they are enabled. This is just a suggestion and doesn't mean you absolutely have to do it. It's just how the rest of LazyVim LSP servers are defined as well throughout the codebase. Corresponding code snippet in LazyVim that takes care of it.
PS: Also keep in mind I'm just a regular user like you and everything I comment on is just a suggestion from my part. The maintainer has the final say.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I'll change this, it will make the code lighter..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bacon needs to be remain if I am not mistaken, since it's not an lsp server itself..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes correct.
|
Is there anything else to address in this PR or can it be merged? |
|
Only the approval of the maintainer himself and maybe if he has some suggestions of his own. As I don't do Rust and am just a simple user like you, I have no say. It would also be nice if some people who actually do Rust share their experience with this PR, as they are more knowledgeable about possible pitfalls (if any) I'm not aware of. |
|
Already shared by opinions 😃 #3212 (comment) edit: the original comment was better phrased and shorter lol Good work OP on this PR and bacon in general ! |
Thanks for sharing this! I'll answer below. |
This is something that needs to be done once, as
Again, this needs to be done only once :)
I am happy to add launching local function bacon_term()
LazyVim.terminal.open(
{ "bacon", "clippy", "--", "--all-features", "--target", vim.env.NEOVIM_RUST_TARGET },
{
ft = "bacon",
cwd = LazyVim.root.get(),
env = { LAZYTERM_TYPE = "bacon" },
}
)
end
if vim.g.lazyvim_rust_diagnostics == "bacon-ls" then
bacon_term()
vim.defer_fn(function()
bacon_term()
end, 2000)
vim.keymap.set({ "n", "i", "t" }, "<c-y>", bacon_term, { desc = "Bacon" })
end
|
| opts.dap = { | ||
| adapter = require("rustaceanvim.config").get_codelldb_adapter( | ||
| package_path .. "/codelldb", | ||
| package_path .. "/extension/lldb/lib/liblldb.dylib" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmm, now that I relook at this, it requires some changes to also work on Linux..
|
ohhhh it can be configured globally, nice ! well i have nothing more to say. might try to use this and bacon when it's merged |
|
This PR is stale because it has been open 30 days with no activity. |
|
Wanted to check in here -- should I add bacon from rustup so I can run the bacon --init command in my home config? At the moment it's not in my PATH, although I've installed it in nvim, or are we waiting on this merge for the mason install to complete correctly (& handle the bacon initialization accordingly?) |
|
Is something blocking this PR ? would be cool to have it merged |
|
I'm willing to look at this, but there's currently merge conflicts |
|
I will solve the merge conflict next week. I am currently on holidays without a laptop 😄 |
|
Enjoy! |
Resolve conflicts with folke's main
|
I fixed the conflicts and update the codelldb settings to support both .dylib and .so files. I'll do some more testing to make sure everything still works properly. @folke I think it's ready for review. |
|
Testing done, it seems to be working properly. |
Changes
NOTE: the PR will be in draft until mason-org/mason-registry#5774 is not merged
Some improvements in the Rust extra
codelldbadapter for rustacean.nvimbacon-lscan be used as an alternative torust-analyzerfor diagnostics, improvingrust-analyzerperformances. This is configured byvim.g.lazyvim_rust_diagnostics, which can be set torust-analyzerorbacon-lsp.