From 33e762d6a5aedfd62bc1d494ff0ed8bb708f74a7 Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 6 Oct 2024 21:50:07 -0400 Subject: [PATCH] add menu plugin --- nvim/lazy-lock.json | 2 + nvim/lua/helper/init.lua | 13 ++ nvim/lua/plugins/menu.lua | 371 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 386 insertions(+) create mode 100644 nvim/lua/plugins/menu.lua diff --git a/nvim/lazy-lock.json b/nvim/lazy-lock.json index 1016892..f4dbd22 100644 --- a/nvim/lazy-lock.json +++ b/nvim/lazy-lock.json @@ -48,4 +48,6 @@ "trouble.nvim": { "branch": "main", "commit": "6efc446226679fda0547c0fd6a7892fd5f5b15d8" }, "vim-sleuth": { "branch": "master", "commit": "cd9d382e33bb817abe7f10cdc3a606bf1d491d75" }, "which-key.nvim": { "branch": "main", "commit": "bfec3d6bc0a9b0b2cb11644642f78c2c3915eef0" } + "volt": { "branch": "main", "commit": "43f72b49037c191eb3cfe26ba7a5574b4bfce226" }, + "which-key.nvim": { "branch": "main", "commit": "fb070344402cfc662299d9914f5546d840a22126" } } diff --git a/nvim/lua/helper/init.lua b/nvim/lua/helper/init.lua index e667651..301c061 100644 --- a/nvim/lua/helper/init.lua +++ b/nvim/lua/helper/init.lua @@ -16,4 +16,17 @@ function M.in_cargo_project() return M.get_cargo_project_file() ~= '' end +function M.in_git_project() + return vim.fn.finddir('.git', '.;') ~= '' +end + +function M.contains(tbl, v) + for i, value in ipairs(tbl) do + if value == v then + return true + end + end + return false +end + return M diff --git a/nvim/lua/plugins/menu.lua b/nvim/lua/plugins/menu.lua new file mode 100644 index 0000000..f353276 --- /dev/null +++ b/nvim/lua/plugins/menu.lua @@ -0,0 +1,371 @@ +local helper = require 'helper' + +function get_default_menu() + local menu = { + { + name = ' LSP Actions', + hl = 'ExBlue', + items = get_lsp_menu(), + }, + { name = 'separator' }, + { + name = ' Toggle Terminal', + hl = 'ExRed', + cmd = function() + require('toggleterm').toggle() + end, + rtxt = 'tt', + }, + { + name = ' Toggle Outline', + cmd = function() + vim.cmd 'Outline' + end, + rtxt = '', + }, + { + name = '󱖫 Toggle Buffer Diagnostics', + cmd = function() + vim.cmd 'Trouble diagnostics toggle pinned=true filter.buf=0' + end, + rtxt = '', + }, + { + name = '󱖫 Toggle Inline Diagnostics', + cmd = function() + vim.diagnostic.enable(not vim.diagnostic.is_enabled()) + end, + rtxt = 'tn', + }, + { name = 'separator' }, + { + name = 'Format Buffer', + cmd = function() + require('conform').format { async = true, lsp_format = 'fallback' } + end, + rtxt = 'f', + }, + { name = 'separator' }, + { + name = 'Copy Content', + cmd = 'y+', + }, + { + name = 'Delete Content', + cmd = '%d', + }, + } + + if helper.in_cargo_project() then + table.insert(menu, 1, { + name = ' Rust Actions', + hl = 'ExBlue', + items = get_rust_menu(), + }) + end + + if helper.in_cmake_project() then + table.insert(menu, 1, { + name = ' CMake Actions', + hl = 'ExBlue', + items = get_cmake_menu(), + rtxt = 'c', + }) + end + + if helper.in_git_project() then + table.insert(menu, 1, { + name = ' Git Actions', + hl = 'ExGreen', + items = get_git_menu(), + rtxt = 'h', + }) + end + + return menu +end + +function get_lsp_menu() + return { + { + name = 'Code Actions', + cmd = function() + vim.lsp.buf.code_action() + end, + rtxt = 'la', + }, + { name = 'separator' }, + { + name = 'Goto Definition', + cmd = vim.lsp.buf.definition, + rtxt = 'gd', + }, + { + name = 'Goto Declaration', + cmd = vim.lsp.buf.declaration, + rtxt = 'gD', + }, + { + name = 'Goto References', + cmd = function() + require('telescope.builtin').lsp_references() + end, + rtxt = 'gr', + }, + { + name = 'Goto Implementation', + cmd = function() + require('telescope.builtin').lsp_implementations() + end, + rtxt = 'gI', + }, + { name = 'separator' }, + { + name = 'Show signature help', + cmd = vim.lsp.buf.signature_help, + }, + { + name = 'Show symbol help', + cmd = vim.lsp.buf.hover, + rtxt = 'K', + }, + { + name = ' Toggle inlay hints', + cmd = function() + vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) + end, + rtxt = 'th', + }, + } +end + +function get_rust_menu() + return { + { + name = 'Code Actions', + cmd = function() + vim.cmd 'RustLsp codeAction' + end, + rtxt = 'ra', + }, + { name = 'separator' }, + { + name = ' Run Last Target', + hl = 'ExGreen', + cmd = function() + vim.cmd 'RustLsp! runnables' + end, + rtxt = 'rr', + }, + { + name = ' Runnables', + hl = 'ExGreen', + cmd = function() + vim.cmd 'RustLsp runnables' + end, + rtxt = 'rR', + }, + { + name = '󰃤 Debug Last Target', + hl = 'ExBlue', + cmd = function() + vim.cmd 'RustLsp! debuggables' + end, + rtxt = 'rd', + }, + { + name = '󰃤 Debuggables', + hl = 'ExBlue', + cmd = function() + vim.cmd 'RustLsp debuggables' + end, + rtxt = 'rD', + }, + { + name = '󰸞 Testables', + hl = 'ExRed', + cmd = function() + vim.cmd 'RustLsp testables' + end, + rtxt = 'rt', + }, + { name = 'separator' }, + { + name = 'Open Cargo.toml', + cmd = function() + vim.cmd 'RustLsp openCargo' + end, + rtxt = 'ro', + }, + { + name = 'Reload Workspace', + cmd = function() + vim.cmd 'RustLsp reloadWorkspace' + end, + rtxt = 'rl', + }, + } +end + +function get_cmake_menu() + return { + { + name = 'Build', + hl = 'ExRed', + cmd = function() + vim.cmd 'CMakeBuild' + end, + rtxt = 'cb', + }, + { + name = 'Clean', + cmd = function() + vim.cmd 'CMakeClean' + end, + rtxt = 'cc', + }, + { + name = 'Select Build Target', + cmd = function() + vim.cmd 'CMakeSelectBuildTarget' + end, + rtxt = 'cB', + }, + { + name = 'Select Build Type', + cmd = function() + vim.cmd 'CMakeSelectBuildType' + end, + rtxt = 'cp', + }, + { name = 'separator' }, + { + name = ' Run', + hl = 'ExGreen', + cmd = function() + vim.cmd 'CMakeRun' + end, + rtxt = 'cr', + }, + { + name = '󰃤 Debug', + hl = 'ExBlue', + cmd = function() + vim.cmd 'CMakeDebug' + end, + rtxt = 'cd', + }, + { + name = 'Select Launch Target', + cmd = function() + vim.cmd 'CMakeSelectLaunchTarget' + end, + rtxt = 'cl', + }, + { + name = ' Target Settings', + cmd = function() + vim.cmd 'CMakeTargetSettings' + end, + rtxt = 'cs', + }, + { name = 'separator' }, + { + name = 'Generate', + cmd = function() + vim.cmd 'CMakeGenerate' + end, + rtxt = 'cg', + }, + { + name = ' Project Settings', + cmd = function() + vim.cmd 'CMakeSettings' + end, + rtxt = 'cS', + }, + } +end + +function get_git_menu() + local gitsigns = require 'gitsigns' + return { + { + name = 'Stage hunk', + cmd = gitsigns.stage_hunk, + rtxt = 'hs', + }, + { + name = 'Reset hunk', + cmd = gitsigns.reset_hunk, + rtxt = 'hr', + }, + { + name = 'Undo stage hunk', + cmd = gitsigns.undo_stage_hunk, + rtxt = 'hu', + }, + { name = 'separator' }, + { + name = 'Stage buffer', + cmd = gitsigns.stage_buffer, + rtxt = 'hS', + }, + { + name = 'Reset buffer', + cmd = gitsigns.reset_buffer, + rtxt = 'hR', + }, + { name = 'separator' }, + { + name = 'Blame line', + cmd = gitsigns.blame_line, + rtxt = 'hb', + }, + { + name = ' Toggle Current line blame', + cmd = gitsigns.toggle_current_line_blame, + rtxt = 'tb', + }, + { name = 'separator' }, + { + name = 'Preview hunk', + cmd = gitsigns.preview_hunk, + rtxt = 'hp', + }, + { + name = 'Diff against index', + cmd = gitsigns.diffthis, + rtxt = 'hd', + }, + { + name = 'Diff against last commit', + cmd = function() + gitsigns.diffthis '@' + end, + rtxt = 'hD', + }, + } +end + +return { + { + 'nvchad/volt', + { + 'nvchad/menu', + config = function() + local ignore_ft = { + 'help', + 'Outline', + 'trouble', + } + vim.keymap.set('n', '', function() + if not helper.contains(ignore_ft, vim.bo.ft) then + vim.cmd.exec '"normal! \\"' + require('menu').open(get_default_menu 'n', { mouse = true }) + end + end, {}) + end, + }, + }, +}