diff --git a/lua/lsp/init.lua b/lua/lsp/init.lua index bb47cb11..5b93b77a 100644 --- a/lua/lsp/init.lua +++ b/lua/lsp/init.lua @@ -79,6 +79,31 @@ register("saecki/crates.nvim", { border = "single", }, }) + + -- Find buffer id for `Cargo.toml` file + local existing_buffer = vim.api.nvim_list_bufs() + local cargo_toml_buf_id = nil + for _, buf_id in ipairs(existing_buffer) do + local buf_name = vim.api.nvim_buf_get_name(buf_id) + local filename = vim.fn.fnamemodify(buf_name, ":t") + if filename == "Cargo.toml" then + cargo_toml_buf_id = buf_id + end + end + if cargo_toml_buf_id == nil then + return + end + + local crates = require("crates") + + -- this key mappings will only apply to the `Cargo.toml` file buffer + require("libs.keymap").buf_map(cargo_toml_buf_id, "n", { + { "cu", crates.upgrade_crate, desc = "Upgrade crate under current cursor" }, + { "cv", crates.show_versions_popup, desc = "Show current crate versions" }, + { "cf", crates.show_features_popup, desc = "Show current crate features" }, + { "cR", crates.open_repository, desc = "Open source code in browser" }, + { "cD", crates.open_documentation, desc = "Open docs.rs in browser" }, + }) end, })