-- Context-aware menu -- https://github.com/plex/vim-config local M = {} ---@param method string ---@param clients lsp.Client[] ---@return boolean local function supports_method(method, clients) for _, client in pairs(clients) do if client.supports_method(method) then return true end end return false end M.show = function() if vim.fn.has('nvim-0.8') ~= 1 then vim.notify( 'You must be running Neovim ≥8.0', vim.log.levels.WARN, { title = 'Contextmenu' } ) return end local cword = vim.fn.expand('') local bufnr = vim.api.nvim_get_current_buf() local clients if vim.lsp.get_clients ~= nil then clients = vim.lsp.get_clients({ bufnr = bufnr }) else ---@diagnostic disable-next-line: deprecated clients = vim.lsp.get_active_clients({ bufnr = bufnr }) end -- Remove all menu options pcall(vim.cmd.aunmenu, 'Context') if cword == '' then -- Cursor is on blank character. vim.cmd([[ nmenu Context.Select\ All ggVG nmenu Context.-1- ]]) else -- Add LSP methods, only if one of the servers support it. if supports_method('textDocument/declaration', clients) then vim.cmd( 'nmenu Context.Declaration lua vim.lsp.buf.declaration()' ) end if supports_method('textDocument/definition', clients) then vim.cmd('nmenu Context.&Definition lua vim.lsp.buf.definition()') end if supports_method('textDocument/references', clients) then vim.cmd( 'nmenu Context.&References… lua vim.lsp.buf.references()' ) end if supports_method('textDocument/implementation', clients) then vim.cmd( 'nmenu Context.Implementation lua vim.lsp.buf.implementation()' ) end if #clients > 0 then vim.cmd([[ nmenu Context.-1- nmenu Context.Find\ symbol… lua vim.schedule(function() require'telescope.builtin'.lsp_workspace_symbols({default_text = vim.fn.expand('')}) end) ]]) end vim.cmd([[ nmenu Context.Grep… lua vim.schedule(function() require'telescope.builtin'.live_grep({default_text = vim.fn.expand('')}) end) nmenu Context.-2- ]]) end vim.cmd([[ nmenu Context.Diagnostics Trouble nmenu Context.Bookmark m; nmenu Context.TODOs TodoTrouble nmenu Context.Git\ diff Gdiffsplit nmenu Context.Unsaved\ diff DiffOrig nmenu Context.Open\ in\ browser lua require('gitlinker').get_buf_range_url('n') ]]) pcall(vim.cmd.popup, 'Context') end return M