forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeymaps.lua
165 lines (147 loc) · 5.03 KB
/
keymaps.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
-- LSP: Key-maps
-- https://github.com/rafi/vim-config
local M = {}
---@type PluginLspKeys
M._keys = nil
---@return (LazyKeys|{has?:string})[]
function M.get()
if M._keys then
return M._keys
end
local format = function()
require('rafi.plugins.lsp.format').format({ force = true })
end
---@class PluginLspKeys
-- stylua: ignore
M._keys = {
{ 'gD', vim.lsp.buf.declaration, desc = 'Goto Declaration', has = 'declaration' },
{ 'gd', vim.lsp.buf.definition, desc = 'Goto Definition', has = 'definition' },
{ 'gr', vim.lsp.buf.references, desc = 'References', has = 'references' },
{ 'gy', vim.lsp.buf.type_definition, desc = 'Goto Type Definition', has = 'typeDefinition' },
{ 'gi', vim.lsp.buf.implementation, desc = 'Goto Implementation', has = 'implementation' },
{ 'gK', vim.lsp.buf.signature_help, desc = 'Signature Help', has = 'signatureHelp' },
{ '<C-g>h', vim.lsp.buf.signature_help, mode = 'i', desc = 'Signature Help', has = 'signatureHelp' },
{ ']d', M.diagnostic_goto(true), desc = 'Next Diagnostic' },
{ '[d', M.diagnostic_goto(false), desc = 'Prev Diagnostic' },
{ ']e', M.diagnostic_goto(true, 'ERROR'), desc = 'Next Error' },
{ '[e', M.diagnostic_goto(false, 'ERROR'), desc = 'Prev Error' },
{ ',wa', vim.lsp.buf.add_workspace_folder, desc = 'Show Workspace Folders' },
{ ',wr', vim.lsp.buf.remove_workspace_folder, desc = 'Remove Workspace Folder' },
{ ',wl', '<cmd>lua =vim.lsp.buf.list_workspace_folders()<CR>', desc = 'List Workspace Folders' },
{ 'K', function()
-- Show hover documentation or folded lines.
local winid = require('rafi.lib.utils').has('nvim-ufo')
and require('ufo').peekFoldedLinesUnderCursor() or nil
if not winid then
vim.lsp.buf.hover()
end
end },
{ '<Leader>ud', function() M.diagnostic_toggle(false) end, desc = 'Disable Diagnostics' },
{ '<Leader>uD', function() M.diagnostic_toggle(true) end, desc = 'Disable All Diagnostics' },
{ '<leader>cl', '<cmd>LspInfo<cr>' },
{ '<leader>cf', format, desc = 'Format Document', has = 'formatting' },
{ '<leader>cf', format, mode = 'x', desc = 'Format Range', has = 'rangeFormatting' },
{ '<Leader>cr', vim.lsp.buf.rename, desc = 'Rename', has = 'rename' },
{ '<Leader>ce', vim.diagnostic.open_float, desc = 'Open diagnostics' },
{ '<Leader>ca', vim.lsp.buf.code_action, mode = { 'n', 'x' }, has = 'codeAction', desc = 'Code Action' },
{ '<Leader>cA', function()
vim.lsp.buf.code_action({
context = {
only = { 'source' },
diagnostics = {},
},
})
end, desc = 'Source Action', has = 'codeAction' },
}
return M._keys
end
---@param method string
function M.has(buffer, method)
method = method:find('/') and method or 'textDocument/' .. method
local clients = vim.lsp.get_active_clients({ bufnr = buffer })
for _, client in ipairs(clients) do
if client.supports_method(method) then
return true
end
end
return false
end
---@param buffer integer
function M.resolve(buffer)
local Keys = require('lazy.core.handler.keys')
local keymaps = {} ---@type table<string,LazyKeys|{has?:string}>
local function add(keymap)
local keys = Keys.parse(keymap)
if keys[2] == false then
keymaps[keys.id] = nil
else
keymaps[keys.id] = keys
end
end
for _, keymap in ipairs(M.get()) do
add(keymap)
end
local opts = require('rafi.lib.utils').opts('nvim-lspconfig')
local clients = vim.lsp.get_active_clients({ bufnr = buffer })
for _, client in ipairs(clients) do
local maps = opts.servers[client.name] and opts.servers[client.name].keys or {}
for _, keymap in ipairs(maps) do
add(keymap)
end
end
return keymaps
end
---@param client lsp.Client
---@param buffer integer
function M.on_attach(client, buffer)
local Keys = require('lazy.core.handler.keys')
local keymaps = M.resolve(buffer)
for _, keys in pairs(keymaps) do
if not keys.has or M.has(buffer, keys.has) then
local opts = Keys.opts(keys)
---@diagnostic disable-next-line: no-unknown
opts.has = nil
opts.silent = opts.silent ~= false
opts.buffer = buffer
vim.keymap.set(keys.mode or 'n', keys[1], keys[2], opts)
end
end
end
-- Toggle diagnostics locally (false) or globally (true).
---@param global boolean
function M.diagnostic_toggle(global)
local bufnr, cmd, msg, state
if global then
bufnr = nil
state = vim.g.diagnostics_disabled
vim.g.diagnostics_disabled = not state
else
bufnr = 0
if vim.fn.has('nvim-0.9') == 1 then
state = vim.diagnostic.is_disabled(bufnr)
else
state = vim.b.diagnostics_disabled
vim.b.diagnostics_disabled = not state
end
end
cmd = state and 'enable' or 'disable'
msg = cmd:gsub('^%l', string.upper) .. 'd diagnostics'
if global then
msg = msg .. ' globally'
end
vim.notify(msg)
vim.schedule(function()
vim.diagnostic[cmd](bufnr)
end)
end
---@param next boolean
---@param severity string|nil
---@return fun()
function M.diagnostic_goto(next, severity)
local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev
local severity_int = severity and vim.diagnostic.severity[severity] or nil
return function()
go({ severity = severity_int })
end
end
return M