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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
local M = {}
--- @param cmd string[]
--- @param cwd string
--- @param on_exit fun(out: vim.SystemCompleted)
local function git_cmd(cmd, cwd, on_exit)
cmd = vim.list_extend({ 'git', '-c', 'gc.auto=0' }, cmd)
local env = vim.fn.environ() --- @type table<string,string>
env.GIT_DIR, env.GIT_WORK_TREE, env.GIT_INDEX_FILE = nil, nil, nil
local sys_opts = { cwd = cwd, text = true, env = env, clear_env = true }
vim.system(cmd, sys_opts, vim.schedule_wrap(on_exit))
end
local capabilities = {
codeActionProvider = true,
documentLinkProvider = { resolveProvider = false },
documentSymbolProvider = true,
executeCommandProvider = { commands = { 'delete_plugin', 'update_plugin', 'skip_update_plugin' } },
hoverProvider = true,
}
--- @type table<string,function>
local methods = {}
--- @param callback function
function methods.initialize(_, callback)
return callback(nil, { capabilities = capabilities })
end
--- @param callback function
function methods.shutdown(_, callback)
return callback(nil, nil)
end
--- @param uri string
local function get_confirm_bufnr(uri)
return vim._tointeger(uri:match('^nvim%-pack://confirm#(%d+)$'))
end
local group_header_pattern = '^# (%S+)'
local plugin_header_pattern = '^## (.+)$'
--- @param bufnr integer
--- @param lnum integer
--- @return { group: string?, name: string?, from: integer?, to: integer?, active: boolean? }
local function get_plug_data_at_lnum(bufnr, lnum)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
--- @type string, string, integer, integer
local group, name, from, to
for i = lnum, 1, -1 do
local line = lines[i]
if not line then
return {}
end
group = group or line:match(group_header_pattern) --[[@as string]]
-- If group is found earlier than name - `lnum` is for group header line
-- If later - proper group header line.
if group then
break
end
name = name or line:match(plugin_header_pattern) --[[@as string]]
from = (not from and name) and i or from --[[@as integer]]
end
if not (group and name and from) then
return {}
end
--- @cast group string
--- @cast from integer
for i = lnum + 1, #lines do
if lines[i]:match(group_header_pattern) or lines[i]:match(plugin_header_pattern) then
-- Do not include blank line before next section
to = i - 2
break
end
end
to = to or #lines
if not (from <= lnum and lnum <= to) then
return {}
end
return { group = group, name = name:gsub(' %(not active%)$', ''), from = from, to = to }
end
--- @alias vim.pack.lsp.Position { line: integer, character: integer }
--- @alias vim.pack.lsp.Range { start: vim.pack.lsp.Position, end: vim.pack.lsp.Position }
--- @alias vim.pack.lsp.DocumentLink { range: vim.pack.lsp.Range, target: string }
--- Finds a line range to be linked and computes the LSP style link
--- @param line string Buffer line to find a link in
--- @param pattern string Pattern with three captures: `()`, the link text, and `()`,
--- like `'^Path: +()(.+)()$'`. The empty captures return byte positions.
--- @param link_type "commit"|"path"|"src"|"tag"
--- @param lnum integer Line number in a buffer
--- @param src string Plugin source
--- @return vim.pack.lsp.DocumentLink? # A link structure according to the LSP specification
local function match_link(line, pattern, link_type, lnum, src)
local from, match, to = line:match(pattern)
if type(from) ~= 'number' or type(match) ~= 'string' or type(to) ~= 'number' then
return nil
end
--- @cast from integer
--- @cast to integer
-- Convert to UTF index used in LSP positions
local start_col = vim.str_utfindex(line, 'utf-16', from - 1, false)
local end_col = vim.str_utfindex(line, 'utf-16', to - 2, false)
--- @type string?
local target = match
if link_type == 'commit' or link_type == 'tag' then
---@diagnostic disable-next-line: param-type-mismatch
target = require('vim._core.util').get_forge_url(src, match, link_type)
elseif link_type == 'path' then
target = vim.uri_from_fname(match)
end
if target == nil then
return nil
end
return {
range = {
start = { line = lnum - 1, character = start_col },
['end'] = { line = lnum - 1, character = end_col },
},
target = target,
}
end
--- @param params { textDocument: { uri: string } }
--- @param callback function
methods['textDocument/documentLink'] = function(params, callback)
local bufnr = get_confirm_bufnr(params.textDocument.uri)
if bufnr == nil then
return callback(nil, {})
end
--- @type vim.pack.lsp.DocumentLink[]
local links = {}
local cur_src = ''
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
for i, l in ipairs(lines) do
cur_src = l:match('^Source: +(.+)$') or cur_src
links[#links + 1] = match_link(l, '^Path: +()(.+)()$', 'path', i, cur_src)
links[#links + 1] = match_link(l, '^Source: +()(.+)()$', 'src', i, cur_src)
links[#links + 1] = match_link(l, '^Revision[^:]*: +()(%S+)()', 'commit', i, cur_src)
-- NOTE: Assume that short revision works in the link
links[#links + 1] = match_link(l, '^[><] ()(%S+)()', 'commit', i, cur_src)
links[#links + 1] = match_link(l, '^• ()(.+)()$', 'tag', i, cur_src)
end
return callback(nil, links)
end
--- @param params { textDocument: { uri: string } }
--- @param callback function
methods['textDocument/documentSymbol'] = function(params, callback)
local bufnr = get_confirm_bufnr(params.textDocument.uri)
if bufnr == nil then
return callback(nil, {})
end
--- @alias vim.pack.lsp.Symbol {
--- name: string,
--- kind: number,
--- range: vim.pack.lsp.Range,
--- selectionRange: vim.pack.lsp.Range,
--- children: vim.pack.lsp.Symbol[]?,
--- }
--- @param name string?
--- @param start_line integer?
--- @param end_line integer
--- @param kind integer
--- @return vim.pack.lsp.Symbol?
local function new_symbol(name, start_line, end_line, kind)
if name == nil then
return nil
end
local range = {
start = {
line = start_line --[[@as integer]],
character = 0,
},
['end'] = { line = end_line, character = 0 },
}
return { name = name, kind = kind, range = range, selectionRange = range }
end
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
--- @param pattern string
--- @param start_line integer
--- @param end_line integer
--- @param kind integer
--- @return vim.pack.lsp.Symbol[]
local function parse_headers(pattern, start_line, end_line, kind)
local res, cur_match, cur_start = {}, nil, nil
for i = start_line, end_line do
local m = assert(lines[i + 1]):match(pattern)
if m ~= nil and m ~= cur_match then
table.insert(res, new_symbol(cur_match, cur_start, i, kind))
cur_match, cur_start = m, i
end
end
table.insert(res, new_symbol(cur_match, cur_start, end_line, kind))
return res
end
local group_kind = vim.lsp.protocol.SymbolKind.Namespace
local symbols = parse_headers(group_header_pattern, 0, #lines - 1, group_kind)
local plug_kind = vim.lsp.protocol.SymbolKind.Module
for _, group in ipairs(symbols) do
local start_line, end_line = group.range.start.line, group.range['end'].line
group.children = parse_headers(plugin_header_pattern, start_line, end_line, plug_kind)
end
return callback(nil, symbols)
end
--- @alias vim.pack.lsp.CodeActionContext { diagnostics: table, only: table?, triggerKind: integer? }
--- @param params { textDocument: { uri: string }, range: vim.pack.lsp.Range, context: vim.pack.lsp.CodeActionContext }
--- @param callback function
methods['textDocument/codeAction'] = function(params, callback)
local bufnr = get_confirm_bufnr(params.textDocument.uri)
local empty_kind = vim.lsp.protocol.CodeActionKind.Empty
local only = params.context.only or { empty_kind }
if not (bufnr and vim.tbl_contains(only, empty_kind)) then
return callback(nil, {})
end
local plug_data = get_plug_data_at_lnum(bufnr, params.range.start.line + 1)
if not plug_data.name then
return callback(nil, {})
end
--- @param title string
--- @param command string
local function new_action(title, command)
return {
title = ('%s `%s`'):format(title, plug_data.name),
command = { title = title, command = command, arguments = { bufnr, plug_data } },
}
end
local res = {}
if plug_data.group == 'Update' then
vim.list_extend(res, {
new_action('Update', 'update_plugin'),
new_action('Skip updating', 'skip_update_plugin'),
}, 0)
end
plug_data.active = assert(vim.pack.get({ plug_data.name })[1]).active
vim.list_extend(res, { new_action('Delete', 'delete_plugin') })
callback(nil, res)
end
local commands = {
update_plugin = function(plug_data)
vim.pack.update({ plug_data.name }, { force = true, offline = true })
end,
skip_update_plugin = function(_) end,
delete_plugin = function(plug_data)
if plug_data.active then
local confirm_msg = ('Plugin `%s` is active.'):format(plug_data.name)
.. ' Make sure its `vim.pack.add` call is removed from config.'
local choice = vim.fn.confirm(confirm_msg, 'Delete? &Yes\n&No', 1, 'Question')
if choice ~= 1 then
return 'skip_line_update'
end
end
vim.pack.del({ plug_data.name }, { force = true })
end,
}
-- NOTE: Use `vim.schedule_wrap` to avoid hit-enter after choosing code
-- action via built-in `vim.fn.inputlist()`
--- @param params { command: string, arguments: table }
--- @param callback function
methods['workspace/executeCommand'] = vim.schedule_wrap(function(params, callback)
--- @type integer, table
local bufnr, plug_data = unpack(params.arguments)
local ok, res = pcall(commands[params.command] --[[@as function]], plug_data)
if not ok then
return callback({ code = 1, message = res }, {})
end
-- Remove plugin lines (including blank line) to not later act on plugin
if res ~= 'skip_line_update' then
vim.bo[bufnr].modifiable = true
vim.api.nvim_buf_set_lines(bufnr, plug_data.from - 2, plug_data.to, false, {})
vim.bo[bufnr].modifiable, vim.bo[bufnr].modified = false, false
end
callback(nil, {})
end)
--- @param params { textDocument: { uri: string }, position: vim.pack.lsp.Position }
--- @param callback function
methods['textDocument/hover'] = function(params, callback)
local bufnr = get_confirm_bufnr(params.textDocument.uri)
if bufnr == nil then
return
end
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local lnum = params.position.line + 1
local line = lines[lnum]
if not line then
return
end
local commit = line:match('^[<>] (%x+) │') or line:match('^Revision.*:%s+(%x+)')
local tag = line:match('^• (.+)$')
if commit == nil and tag == nil then
return
end
local path --- @type string?
local path_lnum = lnum - 1
while path == nil and path_lnum >= 1 do
path = assert(lines[path_lnum]):match('^Path:%s+(.+)$')
path_lnum = path_lnum - 1
end
if path == nil then
return
end
--- @param sys_out vim.SystemCompleted
local on_exit = function(sys_out)
local markdown = '```diff\n' .. sys_out.stdout .. '\n```'
local res = { contents = { kind = vim.lsp.protocol.MarkupKind.Markdown, value = markdown } }
callback(nil, res)
end
git_cmd({ 'show', '--no-color', commit or tag }, path, on_exit)
end
local dispatchers = {}
-- TODO: Simplify after `vim.lsp.server` is a thing
-- https://github.com/neovim/neovim/pull/24338
--- @param disp vim.lsp.rpc.Dispatchers
local function cmd(disp)
-- Store dispatchers to use for showing progress notifications
dispatchers = disp
local res, closing, request_id = {}, false, 0
--- @param method vim.lsp.protocol.Method.ClientToServer.Request
--- @param params table?
--- @param callback fun(err: lsp.ResponseError?, result: any)
function res.request(method, params, callback)
local method_impl = methods[method]
if method_impl ~= nil then
method_impl(params, callback)
end
request_id = request_id + 1
return true, request_id
end
--- @param method vim.lsp.protocol.Method.ClientToServer.Notification
function res
|