forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontextmenu.lua
93 lines (81 loc) · 2.51 KB
/
contextmenu.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-- Context-aware menu
-- https://github.com/rafi/vim-config
---@class rafi.util.contextmenu
local M = {}
---@param method string
---@param clients vim.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.9') ~= 1 then
vim.notify(
'You must be running Neovim ≥9.0',
vim.log.levels.WARN,
{ title = 'Contextmenu' }
)
return
end
local cword = vim.fn.expand('<cword>')
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, 'PopUp')
if cword == '' then
-- Cursor is on blank character.
vim.cmd([[
nmenu PopUp.Select\ All ggVG
nmenu PopUp.-1- <Nop>
]])
else
-- Add LSP methods, only if one of the servers support it.
if supports_method('textDocument/declaration', clients) then
vim.cmd('nmenu PopUp.Declaration <cmd>lua vim.lsp.buf.declaration()<CR>')
end
if supports_method('textDocument/definition', clients) then
vim.cmd('nmenu PopUp.&Definition <cmd>lua vim.lsp.buf.definition()<CR>')
end
if supports_method('textDocument/references', clients) then
vim.cmd(
'nmenu PopUp.&References… <cmd>lua vim.lsp.buf.references()<CR>'
)
end
if supports_method('textDocument/implementation', clients) then
vim.cmd(
'nmenu PopUp.Implementation <cmd>lua vim.lsp.buf.implementation()<CR>'
)
end
if #clients > 0 then
vim.cmd([[
nmenu PopUp.-1- <Nop>
nmenu PopUp.Find\ symbol… <cmd>lua vim.schedule(function() require'telescope.builtin'.lsp_workspace_symbols({default_text = vim.fn.expand('<cword>')}) end)<CR>
]])
end
vim.cmd([[
nmenu PopUp.Grep… <cmd>lua vim.schedule(function() require'telescope.builtin'.live_grep({default_text = vim.fn.expand('<cword>')}) end)<CR>
nmenu PopUp.-2- <Nop>
]])
end
vim.cmd([[
nmenu PopUp.Diagnostics <cmd>Trouble<CR>
nmenu PopUp.Bookmark m;
nmenu PopUp.TODOs <cmd>TodoTrouble<CR>
nmenu PopUp.Git\ diff <cmd>Gdiffsplit<CR>
nmenu PopUp.Unsaved\ diff <cmd>DiffOrig<CR>
nmenu PopUp.Open\ in\ browser <cmd>lua require('gitlinker').get_buf_range_url('n')<CR>
]])
pcall(vim.cmd.popup, 'PopUp')
end
return M