From 632e0342da6bf6f5568dfc882a49b86c4e764100 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 26 Feb 2023 18:41:10 -0600 Subject: feat(nvim): add keymap to close rust-tools runnables buffer --- .../nvim/lua/tobyvin/plugins/rust-tools.lua | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'nvim/.config') diff --git a/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua b/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua index c15190a..eaf0daa 100644 --- a/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua +++ b/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua @@ -59,6 +59,41 @@ local function set_target() end) end +local term_open = { + command_buf = nil, +} + +function term_open.execute_command(command, args, cwd) + local utils = require("rust-tools.utils.utils") + + local full_command = utils.chain_commands({ + utils.make_command_from_args("cd", { cwd }), + utils.make_command_from_args(command, args), + }) + + utils.delete_buf(term_open.buf) + term_open.buf = vim.api.nvim_create_buf(false, true) + utils.split(false, term_open.buf) + + -- make the new buffer smaller + utils.resize(false, "-5") + + -- close the buffer + vim.keymap.set("n", "q", function() + utils.delete_buf(term_open.buf) + end, { buffer = term_open.buf }) + + -- run the command + vim.fn.termopen(full_command) + + -- when the buffer is closed, set the latest buf id to nil else there are + -- some edge cases with the id being sit but a buffer not being open + local function onDetach(_, _) + term_open.buf = nil + end + vim.api.nvim_buf_attach(term_open.buf, false, { on_detach = onDetach }) +end + function M.init() require("tobyvin.lsp.configs").rust_analyzer = nil @@ -88,4 +123,9 @@ function M.init() }) end +function M.config(_, opts) + require("rust-tools").setup(opts) + require("rust-tools.executors.termopen").execute_command = term_open.execute_command +end + return M -- cgit v1.2.3-70-g09d2