From cecf188adc023e1b296af418e8059c8421dbb42c Mon Sep 17 00:00:00 2001 From: Daniil Tsivinsky Date: Tue, 1 Feb 2022 22:51:02 +0300 Subject: [PATCH] Add dy mapping to yank diagnostic message (wip) --- config/nvim/lua/user/cmds.lua | 19 +++++++++++++++++++ config/nvim/lua/user/remaps.lua | 3 ++- config/nvim/lua/user/utils.lua | 9 +++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/config/nvim/lua/user/cmds.lua b/config/nvim/lua/user/cmds.lua index 75aa8fc..34bfbc6 100644 --- a/config/nvim/lua/user/cmds.lua +++ b/config/nvim/lua/user/cmds.lua @@ -1,3 +1,5 @@ +local yank = require("user.utils").yank + function open_terminal(vertically) vertically = vertically or false @@ -7,3 +9,20 @@ function open_terminal(vertically) vim.cmd(":split term://bash") end end + +function copy_diagnostic_message() + local diagnostics = vim.lsp.diagnostic.get_line_diagnostics() + + if #diagnostics == 0 then + print("No diagnostics to yank") + return + end + + -- TODO: print all messages available and prompt which one to yank + + local message = diagnostics[1].message + + yank(message) + + print("Diagnostic message was yanked") +end diff --git a/config/nvim/lua/user/remaps.lua b/config/nvim/lua/user/remaps.lua index 4b1031e..d91b186 100644 --- a/config/nvim/lua/user/remaps.lua +++ b/config/nvim/lua/user/remaps.lua @@ -52,9 +52,10 @@ nmap("gd", ":lua vim.lsp.buf.definition()") -- g+d - Go to definition nmap("gr", ":lua vim.lsp.buf.references()") -- g+r - Show references nmap("K", ":lua vim.lsp.buf.hover()") -- Shift+k - Show documentation in hover window nmap("", ":lua vim.lsp.buf.rename()") -- F2 - Rename thing under the cursor -nmap("d", ':lua vim.diagnostic.open_float(nil, {focus = false, scope = "cursor"})') -- Leader+d - show diagnostics in float window nmap(".", ":lua vim.lsp.buf.code_action()") -- Leader+. - show code actions to run vmap(".", ":lua vim.lsp.buf.range_code_action()") -- same as above but for visual mode +nmap("dd", ':lua vim.diagnostic.open_float(nil, {focus = false, scope = "cursor"})') -- Leader+d - show diagnostics in float window +nmap("dy", ":lua copy_diagnostic_message()") -- Leader+d+c - copy diagnostic message -- Git nmap("gg", ":G") -- Leader+g+g - open vim-fugitive window diff --git a/config/nvim/lua/user/utils.lua b/config/nvim/lua/user/utils.lua index 0edf583..393bb9a 100644 --- a/config/nvim/lua/user/utils.lua +++ b/config/nvim/lua/user/utils.lua @@ -38,4 +38,13 @@ M.list_includes_item = function(list, item) return false end +M.yank = function(message) + if vim.fn.has("win32") == 1 then + os.execute("echo '" .. message .. "' | win32yank -i") + else + -- i use wayland, so there is no xclip for X11 + os.execute("echo -n '" .. message .. "' | wl-copy") + end +end + return M